From 1420d9d3a39e22520d28e815229ceac05898769f Mon Sep 17 00:00:00 2001 From: suchothendav Date: Mon, 12 Sep 2016 17:19:41 -0700 Subject: [PATCH] Fix mock gen issue for type number with minimum =0 #28 --- lib/generators/index.js | 23 +++++++++++++---------- tests/fixture/petstore.json | 19 +++++++++++++++++++ tests/param_mockgen.js | 16 ++++++++++++++-- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/lib/generators/index.js b/lib/generators/index.js index 52c0808..1cf7fb8 100644 --- a/lib/generators/index.js +++ b/lib/generators/index.js @@ -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); @@ -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); diff --git a/tests/fixture/petstore.json b/tests/fixture/petstore.json index 8e67f5c..cf8c6a3 100644 --- a/tests/fixture/petstore.json +++ b/tests/fixture/petstore.json @@ -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": { diff --git a/tests/param_mockgen.js b/tests/param_mockgen.js index dc66d70..2d45b1a 100644 --- a/tests/param_mockgen.js +++ b/tests/param_mockgen.js @@ -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(); }); });