Skip to content

Commit

Permalink
Merge pull request #34 from subii/v0.0.5
Browse files Browse the repository at this point in the history
V0.0.5
  • Loading branch information
subeeshcbabu authored Oct 11, 2016
2 parents 8e70d85 + 67063e4 commit cb45bd1
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v0.0.5

- exclusive limit issue #27 and multipleOf max value enhancement #26
- mock gen issue for type number with minimum =0 #28

# v0.0.4
## Features
- #11 support additional format options as per json schema (uri, hostname, ipv4, ipv6)
Expand Down
33 changes: 19 additions & 14 deletions lib/generators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,19 @@ 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)) ? (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;
} else {
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
Original file line number Diff line number Diff line change
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);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
28 changes: 28 additions & 0 deletions tests/fixture/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
"description": "ID of pet to return",
"required": true,
"type": "integer",
"minimum": 1000,
"maximum": 2000,
"format": "int64"
},
{
Expand All @@ -193,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 Expand Up @@ -294,6 +315,10 @@
"description": "ID of pet to update",
"required": true,
"type": "integer",
"exclusiveMinimum": true,
"exclusiveMaximum": true,
"minimum": 1000,
"maximum": 1010,
"format": "int64"
}, {
"name": "additionalMetadata",
Expand Down Expand Up @@ -758,6 +783,9 @@
},
"userStatus": {
"type": "integer",
"multipleOf": 100,
"minimum": 1000,
"exclusiveMinimum": true,
"format": "int32",
"description": "User Status"
}
Expand Down
26 changes: 20 additions & 6 deletions tests/param_mockgen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Assert = require('assert');
var Swagmock = require('../lib');
var Path = require('path')
var Path = require('path');
var Util = require('../lib/util');
//isInteger pollyfil for pre es6
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
Expand All @@ -22,7 +23,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();
});
});
Expand Down Expand Up @@ -56,10 +57,21 @@ 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');
params.query.forEach(function (param) {
if (param.name === 'petName') {
Assert.ok(/awesome+ (pet|cat|bird)/.test(param.value), 'OK value for petName');
}
if (param.name === 'petWeight') {
Assert.ok(Util.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(Util.isFinite(param.value), 'OK value for bmi');
Assert.ok(param.value <= 1 && param.value >= 0, 'OK value for bmi');
}
});
done();
});
});
Expand All @@ -76,7 +88,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');
Expand Down Expand Up @@ -138,6 +150,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();
Expand Down
2 changes: 1 addition & 1 deletion tests/params_mockgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down

0 comments on commit cb45bd1

Please sign in to comment.