Add test working from index

add pipeline from schemapack to related files
This commit is contained in:
Luis Tejeda
2022-04-21 15:35:12 -05:00
committed by Erik Mendoza
parent 270fb5ce43
commit 2312dccd74
6 changed files with 136 additions and 28 deletions

View File

@@ -58,7 +58,7 @@ function getReferences (currentNode) {
*/
function findNodeFromPath(referencePath, allData) {
return allData.find((node) => {
return node.path === referencePath; // todo change this comparision
return node.fileName === referencePath; // todo change this comparision
});
}
@@ -68,7 +68,7 @@ function findNodeFromPath(referencePath, allData) {
* @param {Array} allData - array of { path, content} objects
* @returns {object} - Detect root files result object
*/
function getReferencesNodes (currentNode, allData) {
function getAdjacentAndMissing (currentNode, allData) {
let currentNodeReferences = getReferences(currentNode),
graphAdj = [],
missingNodes = [];
@@ -80,7 +80,7 @@ function getReferencesNodes (currentNode, allData) {
graphAdj.push(adjacentNode);
}
else {
missingNodes.push({ relativeToRootPath: path });
missingNodes.push({ relativeToRootPath: referencePath });
}
});
return { graphAdj, missingNodes };
@@ -88,12 +88,18 @@ function getReferencesNodes (currentNode, allData) {
module.exports = {
/**
* Maps the output from get root files to detect root files
* @param {object} specRoot - root file information
* @param {Array} allData - array of { path, content} objects
* @returns {object} - Detect root files result object
*/
getRelatedFiles: function (specRoot, allData) {
let algorithm = new DFS(),
{ traverseOrder, missing } =
algorithm.traverse(specRoot, (currentNode) => { return getReferencesNodes(currentNode, allData); });
return { traverseOrder, missing };
algorithm.traverse(specRoot, (currentNode) => { return getAdjacentAndMissing(currentNode, allData); });
return { relatedFiles: traverseOrder.slice(1), missingRelatedFiles: missing };
},
getReferences,
getReferencesNodes
getAdjacentAndMissing
};

View File

@@ -4845,12 +4845,14 @@ module.exports = {
};
},
mapRootFiles(rootFiles) {
mapRootFiles(rootFiles, inputData) {
let data = rootFiles.map((root) => {
let relatedFiles = getRelatedFiles();
return { rootFile: { path: root.path }, relatedFiles: relatedFiles };
let { relatedFiles, missingRelatedFiles } = getRelatedFiles(root, inputData);
return {
rootFile: { path: root.path },
relatedFiles,
missingRelatedFiles };
});
return data;
},
@@ -4869,7 +4871,7 @@ module.exports = {
}
};
if (inputRelatedFiles.rootFiles && inputRelatedFiles.rootFiles.length > 0) {
res.output.data = this.mapRootFiles(inputRelatedFiles.rootFiles, version);
res.output.data = this.mapRootFiles(inputRelatedFiles.rootFiles, inputRelatedFiles.data);
}
return res;
}

View File

@@ -127,7 +127,7 @@
"path-browserify": "1.0.1",
"postman-collection": "4.0.0",
"swagger2openapi": "7.0.8",
"traverse": "^0.6.6",
"traverse": "0.6.6",
"yaml": "1.10.2"
},
"author": "Postman Labs <help@getpostman.com>",

View File

@@ -0,0 +1,38 @@
openapi: "3.0"
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification
termsOfService: http://swagger.io/terms/
contact:
name: Swagger API Team
email: apiteam@swagger.io
url: http://swagger.io
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
host: petstore.swagger.io
basePath: /api
schemes:
- http
consumes:
- application/json
produces:
- application/json
paths:
/pets:
get:
description: |
Returns all pets from the system that the user has access to
operationId: findPets
responses:
"200":
description: pet response
schema:
type: array
items:
$ref: 'Pet.yaml'
default:
description: unexpected error
schema:
$ref: '../common/Error.yaml'

View File

@@ -5,11 +5,13 @@ var expect = require('chai').expect,
VALID_OPENAPI_PATH = '../data/valid_openapi',
// VALID_OPENAPI_31_PATH = '../data/valid_openapi31X',
PET_STORE_SEPARATED = '../data/petstore separate yaml/spec',
RELATED_FILES = '../data/relatedFiles',
// PET_STORE_SEPARATED_JSON = '../data/petstore-separate/spec',
validPetstore = path.join(__dirname, VALID_OPENAPI_PATH + '/petstore.yaml'),
// noauth = path.join(__dirname, VALID_OPENAPI_PATH + '/noauth.yaml'),
// petstoreSeparated = path.join(__dirname, PET_STORE_SEPARATED + '/swagger.yaml'),
petstoreSeparatedPet = path.join(__dirname, PET_STORE_SEPARATED + '/Pet.yaml');
petstoreSeparatedPet = path.join(__dirname, PET_STORE_SEPARATED + '/Pet.yaml'),
missedRef = path.join(__dirname, RELATED_FILES + '/missedRef.yaml');
// petstoreSeparatedJson = path.join(__dirname, PET_STORE_SEPARATED_JSON + '/swagger.json'),
// petstoreSeparatedPetJson = path.join(__dirname, PET_STORE_SEPARATED_JSON + '/Pet.json'),
// validHopService31x = path.join(__dirname, VALID_OPENAPI_31_PATH + '/yaml/hopService.yaml');
@@ -56,4 +58,29 @@ describe('detectRoot method', function() {
expect(res.result).to.be.true;
expect(res.output.data[0].rootFile.path).to.equal('/petstore.yaml');
});
it('should return adjacent and missing nodes', async function () {
const contentFileMissedRef = fs.readFileSync(missedRef, 'utf8'),
contentFilePet = fs.readFileSync(petstoreSeparatedPet, 'utf8'),
input = {
type: 'folder',
specificationVersion: '3.0',
rootFiles: [
{
path: '/missedRef.yaml',
content: contentFileMissedRef
}
],
data: [
{
path: 'Pet.yaml',
content: contentFilePet
}
]
},
res = await Converter.detectRelatedFiles(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
});
});

View File

@@ -1,39 +1,59 @@
// let expect = require('chai').expect,
const { getRelatedFiles, getReferences, getReferencesNodes } = require('./../../lib/relatedFiles');
var expect = require('chai').expect,
const { getRelatedFiles, getReferences, getAdjacentAndMissing } = require('./../../lib/relatedFiles'),
expect = require('chai').expect,
fs = require('fs'),
path = require('path'),
PET_STORE_SEPARATED = '../data/petstore separate yaml/spec',
RELATED_FILES = '../data/relatedFiles',
newPet = path.join(__dirname, PET_STORE_SEPARATED + '/NewPet.yaml'),
swaggerRoot = path.join(__dirname, PET_STORE_SEPARATED + '/swagger.yaml'),
petstoreSeparatedPet = path.join(__dirname, PET_STORE_SEPARATED + '/Pet.yaml');
petstoreSeparatedPet = path.join(__dirname, PET_STORE_SEPARATED + '/Pet.yaml'),
missedRef = path.join(__dirname, RELATED_FILES + '/missedRef.yaml');
describe('getReferencesNodes method', function () {
it('should return adjacent and missing nodes', function () {
describe('getAdjacentAndMissing function', function () {
it('should return adjacent and no missing nodes', function () {
const contentFileNewPet = fs.readFileSync(newPet, 'utf8'),
contentFilePet = fs.readFileSync(petstoreSeparatedPet, 'utf8'),
inputNode = {
path: '/NewPet.yaml',
fileName: '/NewPet.yaml',
content: contentFileNewPet
},
inputData = [{
path: 'Pet.yaml',
fileName: 'Pet.yaml',
content: contentFilePet
}],
{ graphAdj, missingNodes } = getReferencesNodes(inputNode, inputData);
{ graphAdj, missingNodes } = getAdjacentAndMissing(inputNode, inputData);
expect(graphAdj.length).to.equal(1);
expect(graphAdj[0].fileName).to.equal('Pet.yaml');
expect(missingNodes.length).to.equal(0);
});
it('should return adjacent and missing nodes', function () {
const contentFileMissedRef = fs.readFileSync(missedRef, 'utf8'),
contentFilePet = fs.readFileSync(petstoreSeparatedPet, 'utf8'),
inputNode = {
fileName: '/missedRef.yaml',
content: contentFileMissedRef
},
inputData = [{
fileName: 'Pet.yaml',
content: contentFilePet
}],
{ graphAdj, missingNodes } = getAdjacentAndMissing(inputNode, inputData);
expect(graphAdj.length).to.equal(1);
expect(graphAdj[0].fileName).to.equal('Pet.yaml');
expect(missingNodes.length).to.equal(1);
expect(missingNodes[0].relativeToRootPath).to.equal('../common/Error.yaml');
});
});
describe('getReferences method', function () {
describe('getReferences function', function () {
it('should return 1 reference from input', function () {
const contentFile = fs.readFileSync(newPet, 'utf8'),
inputNode = {
path: '/NewPet.yaml',
fileName: '/NewPet.yaml',
content: contentFile
},
result = getReferences(inputNode);
@@ -60,9 +80,24 @@ describe('getReferences method', function () {
});
});
describe('Get header family function ', function () {
it('should check for custom type JSON header', function () {
getRelatedFiles();
describe('getRelatedFiles function ', function () {
it('should return adjacent and missing nodes', function () {
const contentFileMissedRef = fs.readFileSync(missedRef, 'utf8'),
contentFilePet = fs.readFileSync(petstoreSeparatedPet, 'utf8'),
rootNode = {
fileName: '/missedRef.yaml',
content: contentFileMissedRef
},
inputData = [{
fileName: 'Pet.yaml',
content: contentFilePet
}],
{ relatedFiles, missingRelatedFiles } = getRelatedFiles(rootNode, inputData);
expect(relatedFiles.length).to.equal(1);
expect(relatedFiles[0].fileName).to.equal('Pet.yaml');
expect(missingRelatedFiles.length).to.equal(1);
expect(missingRelatedFiles[0].relativeToRootPath).to.equal('../common/Error.yaml');
});