Skip to content

Commit

Permalink
Verify translator used for import test (zotero#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere authored and dstillman committed Mar 11, 2019
1 parent d7e575b commit eea4a6b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 49 deletions.
20 changes: 7 additions & 13 deletions src/importEndpoint.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

/*
***** BEGIN LICENSE BLOCK *****
Expand All @@ -23,17 +25,10 @@
***** END LICENSE BLOCK *****
*/

const config = require('config');
const Translate = require('./translation/translate');

function tagValue(tag) {
if (typeof tag.tag === 'string') return tag.tag;
if (typeof tag.tag.value === 'string') return tag.tag.value;
throw new Error(`Unexpected tag format ${JSON.stringify(tag)}`);
}

class ImportEndpoint {
async handle(ctx, _next) {
module.exports = {
handle: async function (ctx, _next) {
const translate = new Translate.Import();
translate.setString(ctx.request.body || '');

Expand All @@ -46,15 +41,14 @@ class ImportEndpoint {
var items = await translate.translate({ libraryID: 1 });

ctx.set('Content-Type', 'application/json');
ctx.set('Zotero-Translator-ID', translators[0].translatorID);

// Convert translator JSON to API JSON
var newItems = [];
items.forEach(item => {
items.forEach((item) => {
newItems.push(...Zotero.Utilities.itemToAPIJSON(item));
});

ctx.response.body = JSON.stringify(newItems, null, 2);
}
}

module.exports = new ImportEndpoint;
};
72 changes: 36 additions & 36 deletions test/import_test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
'use strict';

const fs = require('fs');
const path = require('path');

const endpoint = require('../src/importEndpoint');

function addTest(name, input, expected) {
for (const item of expected) {
endpoint.normalizeItem(item);
}

function addTest(name, translatorID, input, expected) {
it(`should import ${name}`, async function () {
const response = await request()
.post('/import')
.send(input)
.set('Content-Type', 'text/plain')
.expect(200);

for (const item of response.body) {
endpoint.normalizeItem(item);
}
assert.equal(response.headers['zotero-translator-id'], translatorID);

assert.deepEqual(response.body, expected);
});
}

function parseJSON(name, json, source) {
try {
return JSON.parse(json);
}
catch (err) {
it(`should import ${name}`, function () {
const m = err.message.match(/(.*) at position ([0-9]+)$/);
if (m) {
const pos = parseInt(m[2]);
const line = source.substring(0, source.indexOf(json) + pos).split('\n').length;
const column = pos - json.lastIndexOf('\n', pos);
throw new Error(`${m[1]} around line ${line}, column ${column}`);
}
throw err;
});
}
return null;
}

describe.skip("/import", function () {
const translators = path.join(__dirname, '../modules/translators');
for (const translator of fs.readdirSync(translators)) {
Expand All @@ -31,35 +44,22 @@ describe.skip("/import", function () {
const name = translator.replace(/\.js$/, '');
const code = fs.readFileSync(path.join(translators, translator), 'utf-8');

let start = code.indexOf('/** BEGIN TEST CASES **/');
if (start === -1) continue;
let cases = null;

try {
start = code.indexOf('[', start);
if (start === -1) throw new Error('Could not find start of test cases');
const marker = code.indexOf('/** BEGIN TEST CASES **/');
if (marker === -1) continue;

const end = code.lastIndexOf(']') + 1;
if (end === -1) throw new Error('Could not find end of test cases');
const header = parseJSON(name, code.replace(/\n}\n[\s\S]*/, '}'), code);
if (!header) continue;
const start = code.indexOf('[', marker);
const end = code.lastIndexOf(']') + 1;

cases = code.substring(start, end);
let caseNo = 0;
for (const test of JSON.parse(cases)) {
caseNo += 1;
if (test.type === 'import') addTest(`${name} # ${caseNo}`, test.input, test.items);
}
const cases = parseJSON(name, code.substring(Math.max(start, marker), Math.max(end, start, marker)), code);
if (!cases) continue;

} catch (err) {
it(`should import ${name}`, function () {
const m = err.message.match(/(.*) at position ([0-9]+)$/);
if (m && typeof cases === 'string') {
const pos = parseInt(m[2]);
const line = code.substring(0, start + pos).split('\n').length;
const column = pos - cases.lastIndexOf('\n', pos);
throw new Error(`${m[1]} around line ${line}, column ${column}`);
}
throw err;
});
let caseNo = 0;
for (const test of cases) {
caseNo += 1;
if (test.type !== 'import') continue;
addTest(`${name} # ${caseNo}`, header.translatorID, test.input, test.items);
}
}
});

0 comments on commit eea4a6b

Please sign in to comment.