Skip to content

Commit

Permalink
Fix mock gen issue for type number with minimum =0 #28
Browse files Browse the repository at this point in the history
  • Loading branch information
subeeshcbabu committed Sep 13, 2016
1 parent 0770eca commit 1420d9d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
23 changes: 13 additions & 10 deletions lib/generators/index.js
Expand Up @@ -95,7 +95,7 @@ const integerMock = schema => {
if (Number.isInteger(schema.multipleOf) && schema.multipleOf > 0) {
//Use the min/muplilier as the min number
//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.min = (Number.isInteger(opts.min)) ? (Math.ceil(opts.min / schema.multipleOf)) : 1;
//Use the max/muplilier as the new max value
//Use a default - min + 10 - if max value is not properly set.
opts.max = (Number.isInteger(opts.max)) ? (Math.floor(opts.max / schema.multipleOf)) : (opts.min + 10);
Expand Down Expand Up @@ -123,19 +123,22 @@ const numberMock = schema => {
return enumMock(schema);
}

if (schema.minimum) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum : schema.minimum + 0.1;
if (Number.isFinite(schema.minimum)) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum + 0.1 : schema.minimum;
}
if (schema.maximum) {
opts.max = (schema.exclusiveMaximum) ? schema.maximum : schema.maximum - 0.1;
if (Number.isFinite(schema.maximum)) {
opts.max = (schema.exclusiveMaximum) ? schema.maximum - 0.1 : schema.maximum ;
}
//Generate a number that is multiple of schema.multipleOf
if (schema.multipleOf > 0) {
//Use the muplilier as the min number
opts.min = schema.multipleOf;
if (Number.isFinite(schema.multipleOf) && schema.multipleOf > 0) {
//Use the min/muplilier as the min number
//Use default min as 1 if min is not properly set
opts.min = (Number.isFinite(opts.min)) ? (Math.ceil(opts.min / schema.multipleOf)) : 1;
//Use the max/muplilier as the new max value
opts.max = (opts.max) ? opts.max / schema.multipleOf : opts.max;
nummock = Chance.floating(opts);
//Use a default - min + 10 - if max value is not properly set.
opts.max = (Number.isFinite(opts.max)) ? (Math.floor(opts.max / schema.multipleOf)) : (opts.min + 10);

nummock = Chance.integer(opts);
nummock = nummock * schema.multipleOf;
} else {
nummock = Chance.floating(opts);
Expand Down
19 changes: 19 additions & 0 deletions tests/fixture/petstore.json
Expand Up @@ -203,6 +203,25 @@
"required": true,
"type": "string",
"pattern": "awesome+ (pet|cat|bird)"
},
{
"name": "petWeight",
"in": "query",
"description": "Weight of pet to return",
"required": false,
"type": "number",
"minimum": 10,
"maximum": 500
},
{
"name": "bmi",
"in": "query",
"description": "bmi of the pet",
"required": false,
"type": "number",
"minimum": 0,
"maximum": 1,
"multipleOf": 0.2
}],
"responses": {
"200": {
Expand Down
16 changes: 14 additions & 2 deletions tests/param_mockgen.js
Expand Up @@ -57,8 +57,20 @@ describe('Parameter Mock generator', () => {
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');
params.query.forEach(param => {
if (param.name === 'petName') {
Assert.ok(/awesome+ (pet|cat|bird)/.test(param.value), 'OK value for petName');
}
if (param.name === 'petWeight') {
Assert.ok(Number.isFinite(param.value), 'OK value for petWeight');
Assert.ok(param.value <= 500 && param.value >= 10, 'OK value for petWeight');
}
if (param.name === 'bmi') {
Assert.ok(Number.isFinite(param.value), 'OK value for bmi');
Assert.ok(param.value <= 1 && param.value >= 0, 'OK value for bmi');
}
});

done();
});
});
Expand Down

0 comments on commit 1420d9d

Please sign in to comment.