Skip to content

Commit

Permalink
Import tests: normalize some fields, compare order (zotero#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere authored and dstillman committed Apr 1, 2019
1 parent 6045c70 commit 083f239
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 38 deletions.
71 changes: 36 additions & 35 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
{
"name": "translation-server",
"version": "2.0.0",
"description": "Zotero translation server",
"license": "AGPL-3.0-only",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/zotero/translation-server-v2"
},
"dependencies": {
"aws-sdk": "^2.326.0",
"config": "^1.30.0",
"jsdom": "^13.1.0",
"koa": "^2.5.1",
"koa-bodyparser": "^4.2.1",
"koa-route": "^3.2.0",
"md5": "^2.2.1",
"request": "^2.87.0",
"request-promise-native": "^1.0.5",
"serverless-http": "^1.6.0",
"wicked-good-xpath": "git+https://github.com/zotero/wicked-good-xpath.git#1b88459",
"xregexp": "^4.2.0",
"yargs": "^12.0.2"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.2.0",
"sinon": "^6.1.4",
"supertest": "^3.1.0"
},
"scripts": {
"start": "node src/server.js",
"start:debug": "node --inspect src/server.js",
"test": "NODE_ENV=test ALLOW_CONFIG_MUTATIONS=1 node_modules/.bin/mocha test/*_test.js"
}
"name": "translation-server",
"version": "2.0.0",
"description": "Zotero translation server",
"license": "AGPL-3.0-only",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/zotero/translation-server-v2"
},
"dependencies": {
"aws-sdk": "^2.326.0",
"config": "^1.30.0",
"jsdom": "^13.1.0",
"koa": "^2.5.1",
"koa-bodyparser": "^4.2.1",
"koa-route": "^3.2.0",
"md5": "^2.2.1",
"request": "^2.87.0",
"request-promise-native": "^1.0.5",
"serverless-http": "^1.6.0",
"wicked-good-xpath": "git+https://github.com/zotero/wicked-good-xpath.git#1b88459",
"xregexp": "^4.2.0",
"yargs": "^12.0.2"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.2.0",
"sinon": "^6.1.4",
"supertest": "^3.1.0"
},
"scripts": {
"start": "node src/server.js",
"start:debug": "node --inspect src/server.js",
"test": "NODE_ENV=test ALLOW_CONFIG_MUTATIONS=1 node_modules/.bin/mocha test/*_test.js",
"test:import": "DEBUG_LEVEL=0 NODE_ENV=test ALLOW_CONFIG_MUTATIONS=1 node_modules/.bin/mocha test/import_test.js"
}
}
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ app.use(_.post('/search', SearchEndpoint.handle.bind(SearchEndpoint)));
app.use(_.post('/export', ExportEndpoint.handle.bind(ExportEndpoint)));
app.use(_.post('/import', ImportEndpoint.handle.bind(ImportEndpoint)));

Debug.init(1);
Debug.init(process.env.DEBUG_LEVEL ? parseInt(process.env.DEBUG_LEVEL) : 1);
Translators.init()
.then(function () {
// Don't start server in test mode, since it's handled by supertest
Expand Down
43 changes: 41 additions & 2 deletions test/import_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
const fs = require('fs');
const path = require('path');

function flatten(array) {
return [].concat.apply([], array);
}

function addTest(name, translatorID, input, expected) {
it(`should import ${name}`, async function () {
const response = await request()
Expand All @@ -13,7 +17,42 @@ function addTest(name, translatorID, input, expected) {

assert.equal(response.headers['zotero-translator-id'], translatorID);

assert.deepEqual(response.body, expected);
const items = {
expected: flatten(expected.map(item => Zotero.Utilities.itemToAPIJSON(item))), // normalize and flatten results
found: response.body,
};

for (const ef of ['expected', 'found']) {
for (const item of items[ef]) {
// inline attached notes -- notes are exported as separate items with a parentItem, but the parentItem is going to be different every time the test is ran
if (!item.notes) {
item.notes = items[ef]
.filter(note => (note.itemType === 'note' && note.parentItem === item.key))
.map(note => ({ note: note.note, itemType: note.itemType}));
}
}
// remove notes now inlined
items[ef] = items[ef].filter(item => (item.itemType !== 'note' || !item.parentItem));

for (const item of items[ef]) {
// remove purely administrative fields -- must be done in 2nd run because the key/parentItem are required in the 1st run for inlining notes
delete item.key;
delete item.version;
delete item.parentItem;

// remove empty array to simplify comparison
if (Array.isArray(item.tags) && !item.tags.length) {
delete item.tags;
}

// sort tags for stable comparison
if (item.tags) {
item.tags.sort((a, b) => `${a.type}::${a.tag}`.localeCompare(`${b.type}::${b.tag}`));
}
}
}

assert.deepEqual(items.expected, items.found);
});
}

Expand All @@ -36,7 +75,7 @@ function parseJSON(name, json, source) {
return null;
}

describe.skip("/import", function () {
(process.env.IMPORT_TESTS ? describe : describe.skip)("/import", function () {
const translators = path.join(__dirname, '../modules/translators');
for (const translator of fs.readdirSync(translators)) {
if (!translator.endsWith('.js')) continue;
Expand Down

0 comments on commit 083f239

Please sign in to comment.