Skip to content

Commit

Permalink
fix exclusive limit issue subeeshcbabu-zz#27 and multipleOf max value…
Browse files Browse the repository at this point in the history
… enhancement subeeshcbabu-zz#26
  • Loading branch information
subeeshcbabu committed Oct 11, 2016
1 parent 8e70d85 commit 54a2607
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.0.5

- exclusive limit issue #27 and multipleOf max value enhancement #26

# v0.0.4
## Features
- #11 support additional format options as per json schema (uri, hostname, ipv4, ipv6)
Expand Down
10 changes: 5 additions & 5 deletions lib/generators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ function integerMock(schema) {
}

if (Util.isInteger(schema.minimum)) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum : schema.minimum + 1;
opts.min = (schema.exclusiveMinimum) ? schema.minimum + 1 : schema.minimum;
}
if (Util.isInteger(schema.maximum)) {
opts.max = (schema.exclusiveMaximum) ? schema.maximum : schema.maximum - 1;
opts.max = (schema.exclusiveMaximum) ? schema.maximum - 1 : schema.maximum;
}
//Generate a number that is multiple of schema.multipleOf
if (Util.isInteger(schema.multipleOf) && schema.multipleOf > 0) {
//Use the muplilier as the min number
opts.min = schema.multipleOf;
//Use the max/muplilier as the new max value
opts.max = (Util.isInteger(opts.max)) ? (Math.floor(opts.max/schema.multipleOf)) : opts.max;
//Use default min as 1 if min is not properly set.
opts.min = (Number.isInteger(opts.min) && opts.min > 0) ? (Math.ceil(opts.min / schema.multipleOf)) : 1;
opts.max = (Number.isInteger(opts.max)) ? (Math.floor(opts.max / schema.multipleOf)) : (opts.min + 10);
intmock = Chance.integer(opts);
intmock = intmock * schema.multipleOf;
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagmock",
"version": "0.0.4",
"version": "0.0.5",
"description": "Mock data generator for swagger api",
"main": "lib/index.js",
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions tests/fixture/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
"description": "ID of pet to return",
"required": true,
"type": "integer",
"minimum": 1000,
"maximum": 2000,
"format": "int64"
},
{
Expand Down Expand Up @@ -294,6 +296,10 @@
"description": "ID of pet to update",
"required": true,
"type": "integer",
"exclusiveMinimum": true,
"exclusiveMaximum": true,
"minimum": 1000,
"maximum": 1010,
"format": "int64"
}, {
"name": "additionalMetadata",
Expand Down Expand Up @@ -758,6 +764,9 @@
},
"userStatus": {
"type": "integer",
"multipleOf": 100,
"minimum": 1000,
"exclusiveMinimum": true,
"format": "int32",
"description": "User Status"
}
Expand Down
8 changes: 5 additions & 3 deletions tests/param_mockgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Parameter Mock generator', function () {
Assert.ok(params, 'Generated parameters');
Assert.ok(params.path, 'Generated path parameter');
Assert.ok(params.path[0].name === 'orderId', 'generated mock parameter for orderId');
Assert.ok(params.path[0].value > 0 && params.path[0].value < 10, 'OK value for orderId');
Assert.ok(params.path[0].value >= 1 && params.path[0].value <= 10, 'OK value for orderId');
done();
});
});
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('Parameter Mock generator', function () {
Assert.ok(params.path, 'Generated path parameter');
Assert.ok(params.path[0].name === 'petId', 'generated mock parameter for petId');
Assert.ok(Number.isInteger(params.path[0].value), 'OK value for petId');

Assert.ok(params.path[0].value >= 1000 && params.path[0].value <= 2000, 'OK value for petId');
Assert.ok(params.query, 'Generated query parameter');
Assert.ok(params.query[0].name === 'petName', 'generated mock parameter for petName');
Assert.ok(/awesome+ (pet|cat|bird)/.test(params.query[0].value), 'OK value for petName');
Expand All @@ -76,7 +76,7 @@ describe('Parameter Mock generator', function () {
Assert.ok(params.path, 'Generated path parameter');
Assert.ok(params.path[0].name === 'petId', 'generated mock parameter for petId');
Assert.ok(Number.isInteger(params.path[0].value), 'OK value for petId');

Assert.ok(params.path[0].value > 1000 && params.path[0].value < 1010, 'OK value for petId');
Assert.ok(params.formData, 'Generated formData parameter');
Assert.ok(params.formData[0].name === 'additionalMetadata', 'generated mock parameter for additionalMetadata');
Assert.ok(typeof params.formData[0].value === 'string', 'OK value for additionalMetadata');
Expand Down Expand Up @@ -138,6 +138,8 @@ describe('Parameter Mock generator', function () {
Assert.ok(typeof user === 'object', 'OK value for user parameter');
Assert.ok(Number.isInteger(user.id), 'user.id is integer');
Assert.ok(Number.isInteger(user.userStatus), 'user.userStatus is integer');
Assert.ok(user.userStatus > 1000, 'user.userStatus is greater than 1000');
Assert.ok(user.userStatus % 100 === 0, 'user.userStatus is multipleOf 100');
Assert.ok(typeof user.username === 'string', 'user.username is string');

done();
Expand Down
2 changes: 1 addition & 1 deletion tests/params_mockgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Parameters Mock generator', function () {
Assert.ok(params, 'Generated parameters');
Assert.ok(params.path, 'Generated path parameter');
Assert.ok(params.path[0].name === 'orderId', 'generated mock parameter for orderId');
Assert.ok(params.path[0].value > 0 && params.path[0].value < 10, 'OK value for orderId');
Assert.ok(params.path[0].value >= 1 && params.path[0].value <= 10, 'OK value for orderId');
done();
});
});
Expand Down

0 comments on commit 54a2607

Please sign in to comment.