diff --git a/CHANGELOG.md b/CHANGELOG.md index 3de32b5..ad5b76e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/generators/index.js b/lib/generators/index.js index 6b6cc7a..c61b443 100644 --- a/lib/generators/index.js +++ b/lib/generators/index.js @@ -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 { diff --git a/package.json b/package.json index 4a5c282..3be244b 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/tests/fixture/petstore.json b/tests/fixture/petstore.json index a251f5c..006b613 100644 --- a/tests/fixture/petstore.json +++ b/tests/fixture/petstore.json @@ -184,6 +184,8 @@ "description": "ID of pet to return", "required": true, "type": "integer", + "minimum": 1000, + "maximum": 2000, "format": "int64" }, { @@ -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", @@ -758,6 +764,9 @@ }, "userStatus": { "type": "integer", + "multipleOf": 100, + "minimum": 1000, + "exclusiveMinimum": true, "format": "int32", "description": "User Status" } diff --git a/tests/param_mockgen.js b/tests/param_mockgen.js index a45a5dc..9555cdd 100644 --- a/tests/param_mockgen.js +++ b/tests/param_mockgen.js @@ -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(); }); }); @@ -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'); @@ -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'); @@ -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(); diff --git a/tests/params_mockgen.js b/tests/params_mockgen.js index 0c3520e..c690a56 100644 --- a/tests/params_mockgen.js +++ b/tests/params_mockgen.js @@ -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(); }); });