Skip to content

Commit

Permalink
_globals-test.js fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NickOvt committed Sep 14, 2023
1 parent 26f9c14 commit ee0c3c0
Showing 1 changed file with 68 additions and 74 deletions.
142 changes: 68 additions & 74 deletions test/_globals-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,89 +22,83 @@ beforeEach('Get test data before each test', async function () {

// eslint-disable-next-line no-undef
after('Generate test overview table after all tests', async () => {
try {
const data = await server.get('/api-methods');

const routes = data.body;

const mapApiMethodToSpec = {};
let content = '| API path | API method | Test count | Has positive test? | Has Negative test? |\n';
content += '| --- | :---: | --- | --- | --- | \n';

for (const routeName in routes) {
const route = routes[routeName];
const method = route.spec.method;
const path = route.spec.path;

mapApiMethodToSpec[`${method.toLowerCase()}_${path}`] = {
method,
path,
name: route.spec.name || route.name,
testCount: 0,
positiveTestCount: 0,
negativeTestCount: 0
};
}

for (const title of titles) {
const titleSplit = title.split(' ');
const method = titleSplit[1].toLowerCase();
const path = titleSplit[2];
const expectedResult = titleSplit[4];
const data = await server.get('/api-methods');

const routes = data.body;

const mapApiMethodToSpec = {};
let content = '| API path | API method | Test count | Has positive test? | Has Negative test? |\n';
content += '| --- | :---: | --- | --- | --- | \n';

for (const routeName in routes) {
const route = routes[routeName];
const method = route.spec.method;
const path = route.spec.path;

mapApiMethodToSpec[`${method.toLowerCase()}_${path}`] = {
method,
path,
name: route.spec.name || route.name,
testCount: 0,
positiveTestCount: 0,
negativeTestCount: 0
};
}

// missing method or path (string is too short to be accepted as valid test title)
if (!method || !path) {
continue;
}
for (const title of titles) {
const titleSplit = title.split(/\s+/);
const method = titleSplit[1].toLowerCase();
const path = titleSplit[2];
const expectedResult = titleSplit[4];

const data = mapApiMethodToSpec[`${method.toLowerCase()}_${path.replace(/{/g, ':').replace(/}/g, '')}`];
// missing method or path (string is too short to be accepted as valid test title)
if (!method || !path) {
continue;
}

// wront path or wrong data etc. (no such route, can't construct route from test title)
if (!data) {
continue;
}
const data = mapApiMethodToSpec[`${method}_${path.replace(/{/g, ':').replace(/}/g, '')}`];

data.testCount++;
if (expectedResult) {
if (expectedResult === 'success') {
data.positiveTestCount++;
} else if (expectedResult === 'failure') {
data.negativeTestCount++;
}
}
// wrong path or wrong data etc. (no such route, can't construct route from test title)
if (!data) {
unsupportedTitles.push(title);
continue;
}

const sortedData = Object.values(mapApiMethodToSpec).sort((a, b) => {
// 1) sort by test count
// 2) sort by path

if (a.testCount < b.testCount) {
return 1;
} else if (a.testCount > b.testCount) {
return -1;
} else if (a.path > b.path) {
return 1;
} else if (a.path < b.path) {
return -1;
} else {
return 0;
data.testCount++;
if (expectedResult) {
if (expectedResult === 'success') {
data.positiveTestCount++;
} else if (expectedResult === 'failure') {
data.negativeTestCount++;
}
});

for (const data of sortedData) {
content += `| \`${data.path}\` | \`${data.method}\` | ${data.testCount} | ${data.positiveTestCount > 0 ? '✅' : '❌'} (${
data.positiveTestCount
}) | ${data.negativeTestCount > 0 ? '✅' : '❌'} (${data.negativeTestCount}) |\n`;
}
}

fs.writeFile('./api-tests-overview.md', content, err => {
if (err) {
console.log(err);
}
});
const sortedData = Object.values(mapApiMethodToSpec).sort((a, b) => {
// 1) sort by test count
// 2) sort by path

if (a.testCount < b.testCount) {
return 1;
} else if (a.testCount > b.testCount) {
return -1;
} else if (a.path > b.path) {
return 1;
} else if (a.path < b.path) {
return -1;
} else {
return 0;
}
});

console.log('These titles were not included in the overview as they are wrong format:', unsupportedTitles);
} catch (error) {
console.log(error);
for (const data of sortedData) {
content += `| \`${data.path}\` | \`${data.method}\` | ${data.testCount} | ${data.positiveTestCount > 0 ? '✅' : '❌'} (${data.positiveTestCount}) | ${
data.negativeTestCount > 0 ? '✅' : '❌'
} (${data.negativeTestCount}) |\n`;
}

console.log(__dirname);
await fs.promises.writeFile(__dirname + '/../api-tests-overview.md', content);

console.log('These titles were not included in the overview as they are wrong format:', unsupportedTitles);
});

0 comments on commit ee0c3c0

Please sign in to comment.