From 3888ae03851a411e2183705423dd2ee253765fc7 Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Wed, 21 Sep 2016 14:02:14 -0400 Subject: [PATCH] change query module interfaces to allow for additional meta data --- controller/search.js | 11 +++++++++-- query/autocomplete.js | 5 ++++- query/reverse.js | 5 ++++- query/search.js | 6 +++++- query/search_original.js | 6 +++++- test/unit/mock/search_query.js | 10 ++++------ test/unit/query/autocomplete.js | 19 ++++++++++++++++--- test/unit/query/reverse.js | 18 ++++++++++++++++-- test/unit/query/search.js | 18 ++++++++++++++++-- test/unit/query/search_original.js | 18 ++++++++++++++++-- 10 files changed, 95 insertions(+), 21 deletions(-) diff --git a/controller/search.js b/controller/search.js index 30a6cd1c..5294eac9 100644 --- a/controller/search.js +++ b/controller/search.js @@ -10,6 +10,11 @@ function setup( config, backend, query ){ backend = backend || require('../src/backend'); query = query || require('../query/search'); + // verify that we don't let an old style query object slip through the cracks here + if (typeof query !== 'object' || !query.hasOwnProperty('query_type')) { + throw new Error('Search queries must return an object with query and query_type'); + } + function controller( req, res, next ){ // do not run controller when a request // validation error has occurred. @@ -31,7 +36,7 @@ function setup( config, backend, query ){ // log clean parameters for stats logger.info('[req]', 'endpoint=' + req.path, cleanOutput); - var query_body = query(req.clean); + var query_body = query.query(req.clean); // if there's no query to call ES with, skip the service if (_.isUndefined(query_body)) { @@ -61,7 +66,9 @@ function setup( config, backend, query ){ // set response data else { res.data = docs; - res.meta = meta; + res.meta = meta || {}; + // store the query_type for subsequent middleware + res.meta.query_type = query.query_type; } logger.debug('[ES response]', docs); next(); diff --git a/query/autocomplete.js b/query/autocomplete.js index 5f0826c0..44d3ca21 100644 --- a/query/autocomplete.js +++ b/query/autocomplete.js @@ -117,4 +117,7 @@ function generateQuery( clean ){ return query.render( vs ); } -module.exports = generateQuery; +module.exports = { + query: generateQuery, + query_type: 'autocomplete' +}; diff --git a/query/reverse.js b/query/reverse.js index 930cba49..e8f8612e 100644 --- a/query/reverse.js +++ b/query/reverse.js @@ -74,4 +74,7 @@ function generateQuery( clean ){ return query.render( vs ); } -module.exports = generateQuery; +module.exports = { + query: generateQuery, + query_type: 'reverse' +}; diff --git a/query/search.js b/query/search.js index 33847da5..300a26dc 100644 --- a/query/search.js +++ b/query/search.js @@ -134,4 +134,8 @@ function hasStreet(vs) { return vs.isset('input:street'); } -module.exports = generateQuery; +module.exports = { + query: generateQuery, + // this could later be set to disambiguation when appropriate + query_type: 'fallback' +}; diff --git a/query/search_original.js b/query/search_original.js index 4c923246..cc9e5180 100644 --- a/query/search_original.js +++ b/query/search_original.js @@ -128,4 +128,8 @@ function generateQuery( clean ){ return query.render( vs ); } -module.exports = generateQuery; + +module.exports = { + query: generateQuery, + query_type: 'original' +}; diff --git a/test/unit/mock/search_query.js b/test/unit/mock/search_query.js index 2a5f21fd..9bd43cba 100644 --- a/test/unit/mock/search_query.js +++ b/test/unit/mock/search_query.js @@ -1,10 +1,8 @@ - -function setup(){ - return query; -} - function query( clean ){ return clean; } -module.exports = setup; \ No newline at end of file +module.exports = { + query: query, + query_type: 'mock' +}; \ No newline at end of file diff --git a/test/unit/query/autocomplete.js b/test/unit/query/autocomplete.js index 437142f3..09ce34ad 100644 --- a/test/unit/query/autocomplete.js +++ b/test/unit/query/autocomplete.js @@ -1,11 +1,24 @@ - -var generate = require('../../../query/autocomplete'); +var query = require('../../../query/autocomplete'); +var generate = query.query; module.exports.tests = {}; module.exports.tests.interface = function(test, common) { test('valid interface', function(t) { - t.equal(typeof generate, 'function', 'valid function'); + t.equal(typeof query, 'object', 'valid query object'); + t.end(); + }); + test('valid interface', function(t) { + t.true(query.hasOwnProperty('query'), 'query is valid function'); + t.true(query.hasOwnProperty('query_type'), 'query is valid function'); + t.end(); + }); + test('valid interface', function(t) { + t.equal(typeof query.query, 'function', 'valid function'); + t.end(); + }); + test('valid interface', function(t) { + t.equal(typeof query.query_type, 'string', 'valid query_type'); t.end(); }); }; diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index 03985fbd..398eb731 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -1,10 +1,24 @@ -var generate = require('../../../query/reverse'); +var query = require('../../../query/reverse'); +var generate = query.query; module.exports.tests = {}; module.exports.tests.interface = function(test, common) { test('valid interface', function(t) { - t.equal(typeof generate, 'function', 'valid function'); + t.equal(typeof query, 'object', 'valid query object'); + t.end(); + }); + test('valid interface', function(t) { + t.true(query.hasOwnProperty('query'), 'query is valid function'); + t.true(query.hasOwnProperty('query_type'), 'query is valid function'); + t.end(); + }); + test('valid interface', function(t) { + t.equal(typeof query.query, 'function', 'valid function'); + t.end(); + }); + test('valid interface', function(t) { + t.equal(typeof query.query_type, 'string', 'valid query_type'); t.end(); }); }; diff --git a/test/unit/query/search.js b/test/unit/query/search.js index c758a23e..b4a11972 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -1,11 +1,25 @@ -var generate = require('../../../query/search'); +var query = require('../../../query/search'); +var generate = query.query; var fs = require('fs'); module.exports.tests = {}; module.exports.tests.interface = function(test, common) { test('valid interface', function(t) { - t.equal(typeof generate, 'function', 'valid function'); + t.equal(typeof query, 'object', 'valid query object'); + t.end(); + }); + test('valid interface', function(t) { + t.true(query.hasOwnProperty('query'), 'query is valid function'); + t.true(query.hasOwnProperty('query_type'), 'query is valid function'); + t.end(); + }); + test('valid interface', function(t) { + t.equal(typeof query.query, 'function', 'valid function'); + t.end(); + }); + test('valid interface', function(t) { + t.equal(typeof query.query_type, 'string', 'valid query_type'); t.end(); }); }; diff --git a/test/unit/query/search_original.js b/test/unit/query/search_original.js index b66bbca9..159200b1 100644 --- a/test/unit/query/search_original.js +++ b/test/unit/query/search_original.js @@ -1,10 +1,24 @@ -var generate = require('../../../query/search_original'); +var query = require('../../../query/search_original'); +var generate = query.query; module.exports.tests = {}; module.exports.tests.interface = function(test, common) { test('valid interface', function(t) { - t.equal(typeof generate, 'function', 'valid function'); + t.equal(typeof query, 'object', 'valid query object'); + t.end(); + }); + test('valid interface', function(t) { + t.true(query.hasOwnProperty('query'), 'query is valid function'); + t.true(query.hasOwnProperty('query_type'), 'query is valid function'); + t.end(); + }); + test('valid interface', function(t) { + t.equal(typeof query.query, 'function', 'valid function'); + t.end(); + }); + test('valid interface', function(t) { + t.equal(typeof query.query_type, 'string', 'valid query_type'); t.end(); }); };