Skip to content

Commit

Permalink
Fix text search test
Browse files Browse the repository at this point in the history
Fixes zotero#60
  • Loading branch information
dstillman committed Dec 13, 2018
1 parent 802d3d8 commit 6160d86
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
"scripts": {
"start": "node src/server.js",
"start:debug": "node --inspect src/server.js",
"test": "NODE_ENV=test node_modules/.bin/mocha test/*_test.js"
"test": "NODE_ENV=test ALLOW_CONFIG_MUTATIONS=1 node_modules/.bin/mocha test/*_test.js"
}
}
24 changes: 15 additions & 9 deletions src/textSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,10 @@ module.exports = {
}

ctx.response.body = [];
}
};


async function search(query, start) {
const numResults = 3;
let identifiers;
let moreResults = false;
try {
},

// Expose for stubbing in tests
queryLambda: async function (query) {
let params = {
FunctionName: config.get('identifierSearchLambda'),
InvocationType: 'RequestResponse',
Expand All @@ -130,6 +125,17 @@ async function search(query, start) {
}

identifiers = JSON.parse(result.Payload);
return identifiers;
}
};


async function search(query, start) {
const numResults = 3;
let identifiers;
let moreResults = false;
try {
identifiers = await module.exports.queryLambda(query);

// If passed a start= parameter, skip ahead
let startPos = 0;
Expand Down
28 changes: 15 additions & 13 deletions test/search_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const config = require('config');
const HTTP = require('../src/http');
const TextSearch = require('../src/textSearch');
const path = require('path');
const fs = require('fs');
const urlParse = require('url');
Expand All @@ -8,26 +10,14 @@ describe("/search", function () {
var bookISBN1 = '9781421402833';

beforeEach(() => {
var origHTTPRequest = HTTP.request;
var origHTTPRequest = HTTP.request.bind(HTTP);
sinon.stub(HTTP, 'request').callsFake(async function (method, url, options) {
if (url.startsWith('http://127.0.0.1')) {
return origHTTPRequest(method, url, options);
}

Zotero.debug("Mocking request");

// Mock identifier-search request
if (url.startsWith('http://identifier-search')) {
Zotero.debug('=-=-=');
let parts = urlParse.parse(url, true);
if (parts.query.q == bookTitle1.toLowerCase()) {
return {
status: 200,
responseText: `[{"ISBN":"${bookISBN1}"}]`
};
}
}

// Mock Library of Congress ISBN lookup
if (url.startsWith('http://lx2.loc.gov')) {
var xml = fs.readFileSync(
Expand All @@ -44,14 +34,26 @@ describe("/search", function () {

throw new Error("Unhandled request");
});

// Mock identifier-search Lambda call
var origQueryLambda = TextSearch.queryLambda.bind(TextSearch);
sinon.stub(TextSearch, 'queryLambda').callsFake(function (query) {
if (query == bookTitle1.toLowerCase()) {
return Promise.resolve([{"ISBN":"${bookISBN1}"}]);
}
return origQueryLambda(query);
});
});

afterEach(() => {
HTTP.request.restore();
TextSearch.queryLambda.restore();
});


it("should perform a text search", async function () {
config.identifierSearchLambda = 'IdentifierSearch';

var response = await request()
.post('/search')
.set('Content-Type', 'text/plain')
Expand Down

0 comments on commit 6160d86

Please sign in to comment.