Skip to content

Commit

Permalink
Fix mock gen issue for type number with minimum =0 subeeshcbabu-zz#28
Browse files Browse the repository at this point in the history
  • Loading branch information
subeeshcbabu committed Oct 11, 2016
1 parent 54a2607 commit b4aa44e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
25 changes: 15 additions & 10 deletions lib/generators/index.js
Expand Up @@ -109,7 +109,9 @@ function integerMock(schema) {
if (Util.isInteger(schema.multipleOf) && schema.multipleOf > 0) {
//Use the 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);
intmock = Chance.integer(opts);
intmock = intmock * schema.multipleOf;
Expand All @@ -135,19 +137,22 @@ function numberMock(schema) {
return enumMock(schema);
}

if (schema.minimum) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum : schema.minimum + 0.1;
if (Util.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 (Util.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 (Util.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
4 changes: 4 additions & 0 deletions lib/util/index.js
Expand Up @@ -10,3 +10,7 @@ module.exports.isInteger = function(value) {
isFinite(value) &&
Math.floor(value) === value;
};

module.exports.isFinite = function(value) {
return typeof value === "number" && isFinite(value);
};
19 changes: 19 additions & 0 deletions tests/fixture/petstore.json
Expand Up @@ -195,6 +195,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
15 changes: 13 additions & 2 deletions tests/param_mockgen.js
Expand Up @@ -58,8 +58,19 @@ describe('Parameter Mock generator', function () {
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');
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 b4aa44e

Please sign in to comment.