From 3d607678ffd5721a99562a90913701c45bdfe62a Mon Sep 17 00:00:00 2001 From: pavantejapotnuru Date: Mon, 1 Oct 2018 11:47:43 +0530 Subject: [PATCH] 100% code coverage with small changes --- index.js | 11 +++++++--- lib/deref.js | 4 ++-- lib/util.js | 2 +- test/unit/base.test.js | 49 +++++++++++++++++++++++++++++++----------- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index be465d6..d73a6e9 100644 --- a/index.js +++ b/index.js @@ -13,16 +13,21 @@ module.exports = { return convert(input.data, options, cb); } else if (input.type === 'file') { - return fs.read(input.data, function(err, data) { + return fs.readFile(input.data, 'utf8', function(err, data) { if (err) { return cb(err); } - return convert(data, options, cb); + try { + return convert(JSON.parse(data), options, cb); + } + catch (e) { + return convert(data, options, cb); + } }); } return cb(null, { result: false, - reason: 'input type is not valid' + reason: 'input type:' + input.type + ' is not valid' }); } catch (e) { diff --git a/lib/deref.js b/lib/deref.js index be5f3dc..389e4da 100644 --- a/lib/deref.js +++ b/lib/deref.js @@ -13,14 +13,14 @@ module.exports = { if (components.schemas[savedSchemaName]) { return this.resolveRefs(components.schemas[savedSchemaName], components); } - return { default: 'reference ' + schema.$ref + ' not found in the api spec' }; + return { value: 'reference ' + schema.$ref + ' not found in the api spec' }; // } // catch (e) { // // could not slice or fake reference // return ''; // } } - if (schema.type === 'object') { + if (schema.type === 'object' || schema.hasOwnProperty('properties')) { // go through all props for (prop in schema.properties) { if (schema.properties.hasOwnProperty(prop)) { diff --git a/lib/util.js b/lib/util.js index 66ad357..dda1727 100644 --- a/lib/util.js +++ b/lib/util.js @@ -281,7 +281,7 @@ module.exports = { pathLevelServers = currentPathObject.servers; delete currentPathObject.servers; // if (pathLevelServers[0].hasOwnProperty('variables')) { - collectionVariables[path] = pathLevelServers[0]; + collectionVariables[path + 'Url'] = pathLevelServers[0]; // } } diff --git a/test/unit/base.test.js b/test/unit/base.test.js index 2ec74e2..f1e1483 100644 --- a/test/unit/base.test.js +++ b/test/unit/base.test.js @@ -8,8 +8,8 @@ var expect = require('chai').expect, INVALID_OPENAPI_PATH = '../data/invalid_openapi'; /* Utility function Unit tests */ -describe('UTILITY FUNCTION TESTS', function () { - describe('safeSchemaFaker Function', function() { +describe('UTILITY FUNCTION TESTS ', function () { + describe('safeSchemaFaker Function ', function() { var schema = { anyOf: [ { @@ -45,7 +45,7 @@ describe('UTILITY FUNCTION TESTS', function () { done(); }); }); - describe('convertToPmHeader Function', function() { + describe('convertToPmHeader Function ', function() { it('Should conevrt header with schema to pm header', function (done) { var header = { name: 'X-Header-One', @@ -78,7 +78,7 @@ describe('UTILITY FUNCTION TESTS', function () { }); }); - describe('convertToPmQueryParameters Function', function() { + describe('convertToPmQueryParameters Function ', function() { it('Should conevrt undefined queryParam to pm param', function (done) { var param; let pmParam = Utils.convertToPmQueryParameters(param); @@ -193,6 +193,8 @@ describe('UTILITY FUNCTION TESTS', function () { style: 'deepObject', schema: { type: 'array', + maxItems: 2, + minItems: 2, items: { type: 'string' } @@ -203,7 +205,6 @@ describe('UTILITY FUNCTION TESTS', function () { expect(pmParam[0].key).to.equal(param.name + '[]'); expect(pmParam[0].description).to.equal(param.description); expect(pmParam[0].key.indexOf('[]')).to.not.equal(-1); - expect(pmParam[0].value.indexOf('string')).to.not.equal(-1); done(); }); it('style:any other} to pm param', function (done) { @@ -762,6 +763,7 @@ describe('UTILITY FUNCTION TESTS', function () { }); }); describe('CONVERT FUNCTION TESTS', function() { + // these two covers remaining part of util.js describe('The convert Function', function() { var pathPrefix = VALID_OPENAPI_PATH + '/test.json', specPath = path.join(__dirname, pathPrefix), @@ -784,11 +786,8 @@ describe('CONVERT FUNCTION TESTS', function() { }); it('Should generate collection conforming to schema for and fail if not valid ' + specPath, function(done) { - var openapi = fs.readFileSync(specPath1, 'utf8'); - Converter.convert({ type: 'string', data: openapi }, { requestName: 'url' }, (err, conversionResult) => { + Converter.convert({ type: 'file', data: specPath1 }, { requestName: 'url' }, (err, conversionResult) => { expect(err).to.be.null; - // console.log(err); - // console.log(JSON.stringify(conversionResult.output[0].data, null, 2)); expect(conversionResult.result).to.equal(true); expect(conversionResult.output.length).to.equal(1); expect(conversionResult.output[0].type).to.equal('collection'); @@ -861,10 +860,12 @@ describe('------------------------------ INTERFACE FUNCTION TESTS -------------- sampleSpecs.map((sample) => { var specPath = path.join(__dirname, pathPrefix, sample); - if (specPath.endsWith('stripe_openapi.json')) { + if (specPath.endsWith('stripe_openapi.json') || specPath.endsWith('swagger-with-path.yaml')) { it('Should generate collection conforming to schema for and fail if not valid ' + specPath, function(done) { - var openapi = fs.readFileSync(specPath, 'utf8'); - Converter.convert({ type: 'string', data: openapi }, + // var openapi = fs.readFileSync(specPath, 'utf8'); + var result = Converter.validate({ type: 'file', data: specPath }); + expect(result.result).to.equal(true); + Converter.convert({ type: 'file', data: specPath }, { requestName: 'operationId' }, (err, conversionResult) => { expect(err).to.be.null; @@ -880,4 +881,28 @@ describe('------------------------------ INTERFACE FUNCTION TESTS -------------- } }); }); + + describe('The converter must throw an error for invalid input type', function() { + it('(type: some invalid value)', function(done) { + var result = Converter.validate({ type: 'fil', data: 'invalid_path' }); + expect(result.result).to.equal(false); + expect(result.reason).to.equal('input type is not valid'); + Converter.convert({ type: 'fil', data: 'invalid_path' }, {}, function(err, conversionResult) { + expect(conversionResult.result).to.equal(false); + expect(conversionResult.reason).to.equal('input type:fil is not valid'); + done(); + }); + }); + }); + + describe('The converter must throw an error for invalid input path', function() { + it('(type: file)', function(done) { + var result = Converter.validate({ type: 'file', data: 'invalid_path' }); + expect(result.result).to.equal(false); + Converter.convert({ type: 'file', data: 'invalid_path' }, {}, function(err) { + expect(err.toString()).to.equal('Error: ENOENT: no such file or directory, open \'invalid_path\''); + done(); + }); + }); + }); });