From b4aa44e8eb0c52dca1f7b3797651e650b43ad771 Mon Sep 17 00:00:00 2001 From: suchothendav Date: Tue, 11 Oct 2016 16:20:03 -0700 Subject: [PATCH] Fix mock gen issue for type number with minimum =0 #28 --- lib/generators/index.js | 25 +++++++++++++++---------- lib/util/index.js | 4 ++++ tests/fixture/petstore.json | 19 +++++++++++++++++++ tests/param_mockgen.js | 15 +++++++++++++-- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/lib/generators/index.js b/lib/generators/index.js index c61b443..5cadd56 100644 --- a/lib/generators/index.js +++ b/lib/generators/index.js @@ -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; @@ -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); diff --git a/lib/util/index.js b/lib/util/index.js index 91e7c79..47b57a7 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -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); +}; diff --git a/tests/fixture/petstore.json b/tests/fixture/petstore.json index 006b613..32fb77e 100644 --- a/tests/fixture/petstore.json +++ b/tests/fixture/petstore.json @@ -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": { diff --git a/tests/param_mockgen.js b/tests/param_mockgen.js index 9555cdd..8c6a714 100644 --- a/tests/param_mockgen.js +++ b/tests/param_mockgen.js @@ -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(); }); });