Skip to content

Commit

Permalink
Unit test for option to set validated = true to generate mocks for al…
Browse files Browse the repository at this point in the history
…ready parsed api subeeshcbabu-zz#24
  • Loading branch information
subeeshcbabu committed Sep 9, 2016
1 parent 293351b commit 279027c
Showing 1 changed file with 40 additions and 43 deletions.
83 changes: 40 additions & 43 deletions tests/param_mockgen.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
var Assert = require('assert');
var Swagmock = require('../lib');
var Path = require('path')
//isInteger pollyfil for pre es6
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
const Assert = require('assert');
const Swagmock = require('../lib');
const Path = require('path');
const Parser = require('swagger-parser');

describe('Parameter Mock generator', function () {
var apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
var swagmock = Swagmock(apiPath);
it('should generate parameter mock for path /store/order/{orderId}', function(done) {
describe('Parameter Mock generator', () => {
let apiPath = Path.resolve(__dirname, 'fixture/petstore.json');
let apiResolver = Parser.validate(apiPath);
//Test case of valiadted api use case.
let swagmock = Swagmock(apiResolver, { validated: true });
it('should generate parameter mock for path /store/order/{orderId}', done => {
swagmock.parameters({
path: '/store/order/{orderId}',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.path, 'Generated path parameter');
Assert.ok(params.path[0].name === 'orderId', 'generated mock parameter for orderId');
Expand All @@ -27,14 +24,14 @@ describe('Parameter Mock generator', function () {
});
});

it('should generate parameter mock for path /pet/findByStatus', function(done) {
it('should generate parameter mock for path /pet/findByStatus', done => {
swagmock.parameters({
path: '/pet/findByStatus',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.query, 'Generated query parameter');
Assert.ok(params.query[0].name === 'status', 'generated mock parameter for status');
Expand All @@ -44,36 +41,36 @@ describe('Parameter Mock generator', function () {
});
});

it('should generate parameter mock for path /pet/{petId}', function(done) {
it('should generate parameter mock for path /pet/{petId}', done => {
swagmock.parameters({
path: '/pet/{petId}',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
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');
//Test the operation level overrides
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');
done();
});
});

it('should generate parameter mock for path /pet/{petId} post - common parameter', function(done) {
it('should generate parameter mock for path /pet/{petId} post - common parameter', (done) => {
swagmock.parameters({
path: '/pet/{petId}',
operation: 'post'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.path, 'Generated path parameter');
Assert.ok(params.path[0].name === 'petId', 'generated mock parameter for petId');
Expand All @@ -83,14 +80,14 @@ describe('Parameter Mock generator', function () {
});
});

it('should generate parameter mock for path /pet/{petId}/uploadImage', function(done) {
it('should generate parameter mock for path /pet/{petId}/uploadImage', (done) => {
swagmock.parameters({
path: '/pet/{petId}/uploadImage',
operation: 'post'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.path, 'Generated path parameter');
Assert.ok(params.path[0].name === 'petId', 'generated mock parameter for petId');
Expand All @@ -103,31 +100,31 @@ describe('Parameter Mock generator', function () {
});
});

it('should generate parameter mock for path /store/inventory', function(done) {
it('should generate parameter mock for path /store/inventory', (done) => {
swagmock.parameters({
path: '/store/inventory',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
done();
});
});

it('should generate parameter mock for path /store/order', function(done) {
it('should generate parameter mock for path /store/order', (done) => {
swagmock.parameters({
path: '/store/order',
operation: 'post'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.body, 'Generated body parameter');
Assert.ok(params.body[0].name === 'body', 'generated mock parameter for body');
var order = params.body[0].value;
let order = params.body[0].value;
Assert.ok(typeof order === 'object', 'OK value for body');
Assert.ok(Number.isInteger(order.id), 'order.id is integer');
Assert.ok(Number.isInteger(order.petId), 'order.petId is integer');
Expand All @@ -139,21 +136,21 @@ describe('Parameter Mock generator', function () {
});
});

it('should generate parameter mock for path /user/createWithArray', function(done) {
it('should generate parameter mock for path /user/createWithArray', (done) => {
swagmock.parameters({
path: '/user/createWithArray',
operation: 'post'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');

Assert.ok(params.body, 'Generated body parameter');
Assert.ok(params.body[0].name === 'body', 'generated mock parameter for body');
var users = params.body[0].value;
let users = params.body[0].value;
Assert.ok(users.length === 1, 'Created a parameter array of users');
var user = users[0];
let user = users[0];
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');
Expand All @@ -163,14 +160,14 @@ describe('Parameter Mock generator', function () {
});
});

it('should generate parameter mock for path /user/logout', function(done) {
it('should generate parameter mock for path /user/logout', (done) => {
swagmock.parameters({
path: '/user/logout',
operation: 'get'
}, function(err, mock) {
}, (err, mock) => {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
let params = mock.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.query, 'Generated path parameter');
Assert.ok(params.query[0].name === 'common', 'generated mock parameter for common parameter');
Expand Down

0 comments on commit 279027c

Please sign in to comment.