From 6558cc9289400586d214335218c2627bcbd75f42 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 10 Oct 2014 16:40:01 +0100 Subject: [PATCH 01/21] progress commit --- app.js | 4 +++ controller/suggest.js | 52 +++++++++++++++++++++------- controller/suggest_admin.js | 50 ++++++++++++++++++++++++++ controller/suggest_poi.js | 46 ++++++++++++++++++++++++ query/suggest_admin.js | 29 ++++++++++++++++ query/{suggest.js => suggest_poi.js} | 0 6 files changed, 169 insertions(+), 12 deletions(-) create mode 100644 controller/suggest_admin.js create mode 100644 controller/suggest_poi.js create mode 100644 query/suggest_admin.js rename query/{suggest.js => suggest_poi.js} (100%) diff --git a/app.js b/app.js index 987b5bcc..e563723b 100644 --- a/app.js +++ b/app.js @@ -20,6 +20,8 @@ sanitisers.reverse = require('./sanitiser/reverse'); var controllers = {}; controllers.index = require('./controller/index'); controllers.suggest = require('./controller/suggest'); +controllers.suggest_poi = require('./controller/suggest_poi'); +controllers.suggest_admin = require('./controller/suggest_admin'); controllers.search = require('./controller/search'); /** ----------------------- routes ----------------------- **/ @@ -29,6 +31,8 @@ app.get( '/', controllers.index() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); +app.get( '/suggest/poi', sanitisers.suggest.middleware, controllers.suggest_poi() ); +app.get( '/suggest/admin', sanitisers.suggest.middleware, controllers.suggest_admin() ); // search API app.get( '/search', sanitisers.search.middleware, controllers.search() ); diff --git a/controller/suggest.js b/controller/suggest.js index 36c3cc98..27da5946 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -3,16 +3,19 @@ var geojsonify = require('../helper/geojsonify').suggest; function setup( backend, query ){ - // allow overriding of dependencies - backend = backend || require('../src/backend'); - query = query || require('../query/suggest'); - function controller( req, res, next ){ - // backend command + // combine the 2 queries + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + var query_admin = require('../query/suggest_admin'); + var query_poi = require('../query/suggest_admin'); + + // **query_poi** command var cmd = { index: 'pelias', - body: query( req.clean ) + body: query_poi( req.clean ) }; // query backend @@ -28,14 +31,39 @@ function setup( backend, query ){ docs = data['pelias'][0].options || []; } - // convert docs to geojson - var geojson = geojsonify( docs ); + // **query_admin** command + var cmd = { + index: 'pelias', + body: query_admin( req.clean ) + }; + + // query backend + backend().client.suggest( cmd, function( err, data ){ + + var docs2 = []; + + // handle backend errors + if( err ){ return next( err ); } + + // map response to a valid FeatureCollection + if( data && Array.isArray( data.pelias ) && data.pelias.length ){ + docs2 = data['pelias'][0].options || []; + } + + /** --- combine 2 doc sets --- **/ + var combined = docs2.slice(0, 3).concat(docs); + + // convert docs to geojson + var geojson = geojsonify( combined ); + + // response envelope + geojson.date = new Date().getTime(); + + // respond + return res.status(200).json( geojson ); - // response envelope - geojson.date = new Date().getTime(); + }); - // respond - return res.status(200).json( geojson ); }); } diff --git a/controller/suggest_admin.js b/controller/suggest_admin.js new file mode 100644 index 00000000..743865a5 --- /dev/null +++ b/controller/suggest_admin.js @@ -0,0 +1,50 @@ + +/** + README: http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-suggest +**/ + +var geojsonify = require('../helper/geojsonify').suggest; + +function setup( backend, query ){ + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + query = query || require('../query/suggest_admin'); + + function controller( req, res, next ){ + + // backend command + var cmd = { + index: 'pelias', + body: query( req.clean ) + }; + + // query backend + backend().client.suggest( cmd, function( err, data ){ + + var docs = []; + + // handle backend errors + if( err ){ return next( err ); } + + // map response to a valid FeatureCollection + if( data && Array.isArray( data.pelias ) && data.pelias.length ){ + docs = data['pelias'][0].options || []; + } + + // convert docs to geojson + var geojson = geojsonify( docs ); + + // response envelope + geojson.date = new Date().getTime(); + + // respond + return res.status(200).json( geojson ); + }); + + } + + return controller; +} + +module.exports = setup; \ No newline at end of file diff --git a/controller/suggest_poi.js b/controller/suggest_poi.js new file mode 100644 index 00000000..9e30b41a --- /dev/null +++ b/controller/suggest_poi.js @@ -0,0 +1,46 @@ + +var geojsonify = require('../helper/geojsonify').suggest; + +function setup( backend, query ){ + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + query = query || require('../query/suggest_poi'); + + function controller( req, res, next ){ + + // backend command + var cmd = { + index: 'pelias', + body: query( req.clean ) + }; + + // query backend + backend().client.suggest( cmd, function( err, data ){ + + var docs = []; + + // handle backend errors + if( err ){ return next( err ); } + + // map response to a valid FeatureCollection + if( data && Array.isArray( data.pelias ) && data.pelias.length ){ + docs = data['pelias'][0].options || []; + } + + // convert docs to geojson + var geojson = geojsonify( docs ); + + // response envelope + geojson.date = new Date().getTime(); + + // respond + return res.status(200).json( geojson ); + }); + + } + + return controller; +} + +module.exports = setup; \ No newline at end of file diff --git a/query/suggest_admin.js b/query/suggest_admin.js new file mode 100644 index 00000000..bb004fd8 --- /dev/null +++ b/query/suggest_admin.js @@ -0,0 +1,29 @@ + +var logger = require('../src/logger'); + +// Build pelias suggest query +function generate( params ){ + + var cmd = { + 'pelias' : { + 'text' : params.input, + 'completion' : { + 'size' : params.size, + 'field' : 'suggest', + 'context': { + 'dataset': ['admin0','admin1','admin2'], + 'location': { + 'value': [ params.lon, params.lat ], + 'precision': 1 + } + } + } + } + }; + + // logger.log( 'cmd', JSON.stringify( cmd, null, 2 ) ); + return cmd; + +} + +module.exports = generate; \ No newline at end of file diff --git a/query/suggest.js b/query/suggest_poi.js similarity index 100% rename from query/suggest.js rename to query/suggest_poi.js From 8771fc04daa58ad20f6ebd2be777eb729329d493 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Fri, 10 Oct 2014 13:04:28 -0400 Subject: [PATCH 02/21] query_poi should require suggest_poi --- controller/suggest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/suggest.js b/controller/suggest.js index 27da5946..1fc58812 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -10,7 +10,7 @@ function setup( backend, query ){ // allow overriding of dependencies backend = backend || require('../src/backend'); var query_admin = require('../query/suggest_admin'); - var query_poi = require('../query/suggest_admin'); + var query_poi = require('../query/suggest_poi'); // **query_poi** command var cmd = { From 5f8c3da14e849ec3fc3b5d91c104562aa0daa012 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Fri, 10 Oct 2014 13:59:43 -0400 Subject: [PATCH 03/21] using async (parallel) --- controller/suggest.js | 63 +++++++++++++++++++------------------------ package.json | 3 ++- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/controller/suggest.js b/controller/suggest.js index 1fc58812..8e792ff8 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -1,5 +1,6 @@ var geojsonify = require('../helper/geojsonify').suggest; +var async = require('async'); function setup( backend, query ){ @@ -11,59 +12,49 @@ function setup( backend, query ){ backend = backend || require('../src/backend'); var query_admin = require('../query/suggest_admin'); var query_poi = require('../query/suggest_poi'); - - // **query_poi** command var cmd = { - index: 'pelias', - body: query_poi( req.clean ) + index: 'pelias' }; - - // query backend - backend().client.suggest( cmd, function( err, data ){ - - var docs = []; - - // handle backend errors - if( err ){ return next( err ); } - - // map response to a valid FeatureCollection - if( data && Array.isArray( data.pelias ) && data.pelias.length ){ - docs = data['pelias'][0].options || []; - } - - // **query_admin** command - var cmd = { - index: 'pelias', - body: query_admin( req.clean ) - }; - + var query_backend = function(cmd, callback) { // query backend backend().client.suggest( cmd, function( err, data ){ - var docs2 = []; + var docs = []; // handle backend errors if( err ){ return next( err ); } // map response to a valid FeatureCollection if( data && Array.isArray( data.pelias ) && data.pelias.length ){ - docs2 = data['pelias'][0].options || []; + docs = data['pelias'][0].options || []; } - /** --- combine 2 doc sets --- **/ - var combined = docs2.slice(0, 3).concat(docs); - - // convert docs to geojson - var geojson = geojsonify( combined ); + callback(null, docs); + }); + }; - // response envelope - geojson.date = new Date().getTime(); + async.parallel({ + admin: function(callback){ + cmd.body = query_admin( req.clean ); + query_backend(cmd, callback); + }, + poi: function(callback){ + cmd.body = query_poi( req.clean ); + query_backend(cmd, callback); + } + }, + function(err, results) { + // results is now equals to: {admin: docs, poi: docs} + var combined = results.poi.slice(0, 3).concat(results.admin); - // respond - return res.status(200).json( geojson ); + // convert docs to geojson + var geojson = geojsonify( combined ); - }); + // response envelope + geojson.date = new Date().getTime(); + // respond + return res.status(200).json( geojson ); }); } diff --git a/package.json b/package.json index 4d0bfce9..aacccf89 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "geojson": "^0.2.0", "geopipes-elasticsearch-backend": "0.0.8", "pelias-esclient": "0.0.25", - "toobusy": "^0.2.4" + "toobusy": "^0.2.4", + "async": "^0.9.0" }, "devDependencies": { "ciao": "^0.3.4", From 81a62482e7b7ec863a8dfd13587df3579cdff38c Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Fri, 10 Oct 2014 14:11:00 -0400 Subject: [PATCH 04/21] query/suggest does not exist anymore. temp fix - using suggest_poi. changing controller/suggest to controller/suggest_poi as well --- test/unit/controller/suggest.js | 2 +- test/unit/query/suggest.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/controller/suggest.js b/test/unit/controller/suggest.js index f67f5da7..bb3f2cc4 100644 --- a/test/unit/controller/suggest.js +++ b/test/unit/controller/suggest.js @@ -1,5 +1,5 @@ -var setup = require('../../../controller/suggest'), +var setup = require('../../../controller/suggest_poi'), mockBackend = require('../mock/backend'), mockQuery = require('../mock/query'); diff --git a/test/unit/query/suggest.js b/test/unit/query/suggest.js index 74e138ff..bd5bc3a6 100644 --- a/test/unit/query/suggest.js +++ b/test/unit/query/suggest.js @@ -1,5 +1,5 @@ -var generate = require('../../../query/suggest'); +var generate = require('../../../query/suggest_poi'); module.exports.tests = {}; From a824d209aaa6ff1ad777842c7531f237bb04b354 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 13 Oct 2014 16:41:25 -0400 Subject: [PATCH 05/21] play with precisions --- controller/suggest.js | 12 ++++++++++-- query/suggest_poi.js | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/controller/suggest.js b/controller/suggest.js index 8e792ff8..e163789c 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -41,11 +41,19 @@ function setup( backend, query ){ poi: function(callback){ cmd.body = query_poi( req.clean ); query_backend(cmd, callback); + }, + poi3: function(callback){ + cmd.body = query_poi( req.clean, 3 ); + query_backend(cmd, callback); + }, + poi1: function(callback){ + cmd.body = query_poi( req.clean, 1 ); + query_backend(cmd, callback); } }, function(err, results) { - // results is now equals to: {admin: docs, poi: docs} - var combined = results.poi.slice(0, 3).concat(results.admin); + // results is now equals to: {admin: docs, poi: docs, poi1: docs, poi3: docs} + var combined = results.poi.concat(results.admin).concat(results.poi1).concat(results.poi3); // convert docs to geojson var geojson = geojsonify( combined ); diff --git a/query/suggest_poi.js b/query/suggest_poi.js index ba086f88..29f2f0b3 100644 --- a/query/suggest_poi.js +++ b/query/suggest_poi.js @@ -2,7 +2,7 @@ var logger = require('../src/logger'); // Build pelias suggest query -function generate( params ){ +function generate( params, precision ){ var getPrecision = function(zoom) { switch (true) { @@ -29,7 +29,7 @@ function generate( params ){ 'dataset': params.layers, 'location': { 'value': [ params.lon, params.lat ], - 'precision': getPrecision(params.zoom) + 'precision': precision || getPrecision(params.zoom) } } } From 6b192d598d9fca4147bdbc1b9e86decaf4c737fa Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 13 Oct 2014 17:04:17 -0400 Subject: [PATCH 06/21] removing poi3 (mostly redundant) - deduping the results --- controller/suggest.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/controller/suggest.js b/controller/suggest.js index e163789c..a97a05e7 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -42,10 +42,6 @@ function setup( backend, query ){ cmd.body = query_poi( req.clean ); query_backend(cmd, callback); }, - poi3: function(callback){ - cmd.body = query_poi( req.clean, 3 ); - query_backend(cmd, callback); - }, poi1: function(callback){ cmd.body = query_poi( req.clean, 1 ); query_backend(cmd, callback); @@ -53,8 +49,18 @@ function setup( backend, query ){ }, function(err, results) { // results is now equals to: {admin: docs, poi: docs, poi1: docs, poi3: docs} - var combined = results.poi.concat(results.admin).concat(results.poi1).concat(results.poi3); - + var combined = results.poi.splice(0,3).concat(results.admin.splice(0,4)).concat(results.poi1.splice(0,4)); + + //dedup + var unique_ids = []; + combined = combined.filter(function(item, pos) { + if (unique_ids.indexOf(item.payload.id) == -1) { + unique_ids.push(item.payload.id); + return true; + } + return false; + }); + // convert docs to geojson var geojson = geojsonify( combined ); From 81c064308f01d406e28312e20771fed7f1d09a71 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 13 Oct 2014 18:02:53 -0400 Subject: [PATCH 07/21] equal distribution (mostly equal to the size requested or less) --- controller/suggest.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/controller/suggest.js b/controller/suggest.js index a97a05e7..e46c8071 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -48,9 +48,10 @@ function setup( backend, query ){ } }, function(err, results) { - // results is now equals to: {admin: docs, poi: docs, poi1: docs, poi3: docs} - var combined = results.poi.splice(0,3).concat(results.admin.splice(0,4)).concat(results.poi1.splice(0,4)); - + var splice_length = req.clean.size / 3; + // results is now equals to: {admin: docs, poi: docs, poi1: docs} + var combined = results.poi.splice(0,splice_length).concat(results.admin.splice(0,splice_length)).concat(results.poi1.splice(0,splice_length)); + //dedup var unique_ids = []; combined = combined.filter(function(item, pos) { From a8642cd97dc9b99b2381274949c7df27ec845b70 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 13 Oct 2014 18:54:32 -0400 Subject: [PATCH 08/21] optional precision param --- query/suggest_admin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/query/suggest_admin.js b/query/suggest_admin.js index bb004fd8..46883744 100644 --- a/query/suggest_admin.js +++ b/query/suggest_admin.js @@ -2,7 +2,7 @@ var logger = require('../src/logger'); // Build pelias suggest query -function generate( params ){ +function generate( params, precision ){ var cmd = { 'pelias' : { @@ -14,7 +14,7 @@ function generate( params ){ 'dataset': ['admin0','admin1','admin2'], 'location': { 'value': [ params.lon, params.lat ], - 'precision': 1 + 'precision': precision || 1 } } } From e75aac5570415fa1611700858ba85c4aaa56800e Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 13 Oct 2014 18:54:49 -0400 Subject: [PATCH 09/21] when query is eual to or less than three letters - give admin* preference (with precision 1,3) and then fallback to pois. --- controller/suggest.js | 72 +++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/controller/suggest.js b/controller/suggest.js index e46c8071..31ea58e1 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -33,43 +33,69 @@ function setup( backend, query ){ }); }; - async.parallel({ - admin: function(callback){ - cmd.body = query_admin( req.clean ); - query_backend(cmd, callback); - }, - poi: function(callback){ - cmd.body = query_poi( req.clean ); - query_backend(cmd, callback); - }, - poi1: function(callback){ - cmd.body = query_poi( req.clean, 1 ); - query_backend(cmd, callback); - } - }, - function(err, results) { - var splice_length = req.clean.size / 3; - // results is now equals to: {admin: docs, poi: docs, poi1: docs} - var combined = results.poi.splice(0,splice_length).concat(results.admin.splice(0,splice_length)).concat(results.poi1.splice(0,splice_length)); - - //dedup + var dedup = function(combined) { var unique_ids = []; - combined = combined.filter(function(item, pos) { + return combined.filter(function(item, pos) { if (unique_ids.indexOf(item.payload.id) == -1) { unique_ids.push(item.payload.id); return true; } return false; }); - + }; + + var respond = function(data) { // convert docs to geojson - var geojson = geojsonify( combined ); + var geojson = geojsonify( data ); // response envelope geojson.date = new Date().getTime(); // respond return res.status(200).json( geojson ); + }; + + var async_query; + + if (req.clean.input.length < 4) { + async_query = { + a: function(callback){ + cmd.body = query_admin( req.clean, [3,1] ); + query_backend(cmd, callback); + }, + b: function(callback) { + cmd.body = query_poi( req.clean ); + query_backend(cmd, callback); + } + } + } else { + async_query = { + a: function(callback){ + cmd.body = query_poi( req.clean ); + query_backend(cmd, callback); + }, + b: function(callback){ + cmd.body = query_admin( req.clean ); + query_backend(cmd, callback); + }, + c: function(callback){ + cmd.body = query_poi( req.clean, 1 ); + query_backend(cmd, callback); + } + } + } + + async.parallel(async_query, function(err, results) { + var splice_length = parseInt((req.clean.size / Object.keys(results).length), 10); + + // results is equal to: {one: docs, two: docs, three: docs} + var combined = []; + for (keys in results) { + combined = combined.concat(results[keys].splice(0,splice_length)); + } + + combined = dedup(combined); + respond(combined); }); } From db29e85a3c1e5909bde87e2195f07d3fd9af2a84 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 13 Oct 2014 19:20:45 -0400 Subject: [PATCH 10/21] precision as an array [3,1] resulted in mostly precision:1 values. so, splitting it up --- controller/suggest.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/controller/suggest.js b/controller/suggest.js index 31ea58e1..645c0d73 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -60,13 +60,17 @@ function setup( backend, query ){ if (req.clean.input.length < 4) { async_query = { a: function(callback){ - cmd.body = query_admin( req.clean, [3,1] ); + cmd.body = query_admin( req.clean, 4 ); query_backend(cmd, callback); }, b: function(callback) { cmd.body = query_poi( req.clean ); query_backend(cmd, callback); - } + }, + c: function(callback){ + cmd.body = query_admin( req.clean ); + query_backend(cmd, callback); + }, } } else { async_query = { @@ -88,7 +92,7 @@ function setup( backend, query ){ async.parallel(async_query, function(err, results) { var splice_length = parseInt((req.clean.size / Object.keys(results).length), 10); - // results is equal to: {one: docs, two: docs, three: docs} + // results is equal to: {a: docs, b: docs, c: docs} var combined = []; for (keys in results) { combined = combined.concat(results[keys].splice(0,splice_length)); From b6b0c80f30e3be0d37614b1f0360fb794e336462 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 14 Oct 2014 15:19:41 -0400 Subject: [PATCH 11/21] 5/3/1 precisions for suggest that start with a number or if more than 3 characters. if its less than 3 characters give preference to admins --- controller/suggest.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/controller/suggest.js b/controller/suggest.js index 645c0d73..1dbe985c 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -57,10 +57,10 @@ function setup( backend, query ){ var async_query; - if (req.clean.input.length < 4) { + if (req.clean.input.length < 4 && isNaN(parseInt(req.clean.input, 10))) { async_query = { a: function(callback){ - cmd.body = query_admin( req.clean, 4 ); + cmd.body = query_admin( req.clean, 3 ); query_backend(cmd, callback); }, b: function(callback) { @@ -75,14 +75,18 @@ function setup( backend, query ){ } else { async_query = { a: function(callback){ - cmd.body = query_poi( req.clean ); + cmd.body = query_poi( req.clean, 5); query_backend(cmd, callback); }, b: function(callback){ - cmd.body = query_admin( req.clean ); + cmd.body = query_poi( req.clean, 3); query_backend(cmd, callback); }, c: function(callback){ + cmd.body = query_admin( req.clean ); + query_backend(cmd, callback); + }, + d: function(callback){ cmd.body = query_poi( req.clean, 1 ); query_backend(cmd, callback); } From b4f7366ba164acd9b22aaa40160be497e9eb34a0 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 15 Oct 2014 10:37:20 -0400 Subject: [PATCH 12/21] fine tuning 5-3-1 and admin logic --- controller/suggest.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/controller/suggest.js b/controller/suggest.js index 1dbe985c..8ce97791 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -63,14 +63,14 @@ function setup( backend, query ){ cmd.body = query_admin( req.clean, 3 ); query_backend(cmd, callback); }, - b: function(callback) { - cmd.body = query_poi( req.clean ); + d: function(callback){ + cmd.body = query_admin( req.clean, 1 ); query_backend(cmd, callback); }, - c: function(callback){ - cmd.body = query_admin( req.clean ); + c: function(callback) { + cmd.body = query_poi( req.clean, 3 ); query_backend(cmd, callback); - }, + } } } else { async_query = { @@ -83,11 +83,11 @@ function setup( backend, query ){ query_backend(cmd, callback); }, c: function(callback){ - cmd.body = query_admin( req.clean ); + cmd.body = query_poi( req.clean, 1 ); query_backend(cmd, callback); }, d: function(callback){ - cmd.body = query_poi( req.clean, 1 ); + cmd.body = query_admin( req.clean ); query_backend(cmd, callback); } } From 7f2206d75b0fbcb6b274f0bc9c2f8c58287dfb85 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 28 Oct 2014 13:57:10 -0400 Subject: [PATCH 13/21] moving things around --- controller/suggest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/suggest.js b/controller/suggest.js index 8ce97791..b4a5267e 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -63,7 +63,7 @@ function setup( backend, query ){ cmd.body = query_admin( req.clean, 3 ); query_backend(cmd, callback); }, - d: function(callback){ + b: function(callback){ cmd.body = query_admin( req.clean, 1 ); query_backend(cmd, callback); }, From f1d13c13370994cf2fe1b69ef3d2c3379fe96e11 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 3 Nov 2014 17:39:51 -0500 Subject: [PATCH 14/21] consolidating suggest_poi and suggest_admin into one suggest endpoint. fixing tests (more tests to come) --- app.js | 4 - controller/suggest.js | 120 +++++++++++++++------------ controller/suggest_admin.js | 50 ----------- controller/suggest_poi.js | 46 ---------- query/{suggest_poi.js => suggest.js} | 0 query/suggest_admin.js | 29 ------- test/unit/controller/suggest.js | 15 ++-- test/unit/mock/backend.js | 10 ++- test/unit/query/suggest.js | 2 +- 9 files changed, 83 insertions(+), 193 deletions(-) delete mode 100644 controller/suggest_admin.js delete mode 100644 controller/suggest_poi.js rename query/{suggest_poi.js => suggest.js} (100%) delete mode 100644 query/suggest_admin.js diff --git a/app.js b/app.js index 7bfe52ed..d4cf0257 100644 --- a/app.js +++ b/app.js @@ -22,8 +22,6 @@ var controllers = {}; controllers.index = require('./controller/index'); controllers.doc = require('./controller/doc'); controllers.suggest = require('./controller/suggest'); -controllers.suggest_poi = require('./controller/suggest_poi'); -controllers.suggest_admin = require('./controller/suggest_admin'); controllers.search = require('./controller/search'); /** ----------------------- routes ----------------------- **/ @@ -36,8 +34,6 @@ app.get( '/doc', sanitisers.doc.middleware, controllers.doc() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); -app.get( '/suggest/poi', sanitisers.suggest.middleware, controllers.suggest_poi() ); -app.get( '/suggest/admin', sanitisers.suggest.middleware, controllers.suggest_admin() ); // search API app.get( '/search', sanitisers.search.middleware, controllers.search() ); diff --git a/controller/suggest.js b/controller/suggest.js index b4a5267e..9af8c9ae 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -4,17 +4,19 @@ var async = require('async'); function setup( backend, query ){ - function controller( req, res, next ){ + // allow overriding of dependencies + backend = backend || require('../src/backend'); + query = query || require('../query/suggest'); - // combine the 2 queries + function controller( req, res, next ){ - // allow overriding of dependencies - backend = backend || require('../src/backend'); - var query_admin = require('../query/suggest_admin'); - var query_poi = require('../query/suggest_poi'); var cmd = { - index: 'pelias' + index: 'pelias', + body: query( req.clean ) }; + + var SIZE = req.clean.size || 10; + var query_backend = function(cmd, callback) { // query backend backend().client.suggest( cmd, function( err, data ){ @@ -55,57 +57,67 @@ function setup( backend, query ){ return res.status(200).json( geojson ); }; - var async_query; - - if (req.clean.input.length < 4 && isNaN(parseInt(req.clean.input, 10))) { - async_query = { - a: function(callback){ - cmd.body = query_admin( req.clean, 3 ); - query_backend(cmd, callback); - }, - b: function(callback){ - cmd.body = query_admin( req.clean, 1 ); - query_backend(cmd, callback); - }, - c: function(callback) { - cmd.body = query_poi( req.clean, 3 ); - query_backend(cmd, callback); + if (req.clean.input) { + var async_query; + + // admin only + req.admin = {}; + for (k in req.clean) { req.admin[k] = req.clean[k] } + req.admin.layers = ['admin0','admin1','admin2']; + + if (req.clean.input.length < 4 && isNaN(parseInt(req.clean.input, 10))) { + async_query = { + a: function(callback){ + cmd.body = query( req.admin, 3 ); + query_backend(cmd, callback); + }, + b: function(callback){ + cmd.body = query( req.admin, 1 ); + query_backend(cmd, callback); + }, + c: function(callback) { + cmd.body = query( req.clean, 3 ); + query_backend(cmd, callback); + } } - } - } else { - async_query = { - a: function(callback){ - cmd.body = query_poi( req.clean, 5); - query_backend(cmd, callback); - }, - b: function(callback){ - cmd.body = query_poi( req.clean, 3); - query_backend(cmd, callback); - }, - c: function(callback){ - cmd.body = query_poi( req.clean, 1 ); - query_backend(cmd, callback); - }, - d: function(callback){ - cmd.body = query_admin( req.clean ); - query_backend(cmd, callback); + } else { + async_query = { + a: function(callback){ + cmd.body = query( req.clean, 5); + query_backend(cmd, callback); + }, + b: function(callback){ + cmd.body = query( req.clean, 3); + query_backend(cmd, callback); + }, + c: function(callback){ + cmd.body = query( req.clean, 1 ); + query_backend(cmd, callback); + }, + d: function(callback){ + cmd.body = query( req.admin ); + query_backend(cmd, callback); + } } } - } - - async.parallel(async_query, function(err, results) { - var splice_length = parseInt((req.clean.size / Object.keys(results).length), 10); - - // results is equal to: {a: docs, b: docs, c: docs} - var combined = []; - for (keys in results) { - combined = combined.concat(results[keys].splice(0,splice_length)); - } - combined = dedup(combined); - respond(combined); - }); - + async.parallel(async_query, function(err, results) { + var splice_length = parseInt((SIZE / Object.keys(results).length), 10); + // results is equal to: {a: docs, b: docs, c: docs} + var combined = []; + for (keys in results) { + combined = combined.concat(results[keys].splice(0,splice_length)); + } + + combined = dedup(combined); + respond(combined); + }); + } else { + query_backend(cmd, function(err, results) { + respond(results); + }); + } + } return controller; diff --git a/controller/suggest_admin.js b/controller/suggest_admin.js deleted file mode 100644 index 743865a5..00000000 --- a/controller/suggest_admin.js +++ /dev/null @@ -1,50 +0,0 @@ - -/** - README: http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-suggest -**/ - -var geojsonify = require('../helper/geojsonify').suggest; - -function setup( backend, query ){ - - // allow overriding of dependencies - backend = backend || require('../src/backend'); - query = query || require('../query/suggest_admin'); - - function controller( req, res, next ){ - - // backend command - var cmd = { - index: 'pelias', - body: query( req.clean ) - }; - - // query backend - backend().client.suggest( cmd, function( err, data ){ - - var docs = []; - - // handle backend errors - if( err ){ return next( err ); } - - // map response to a valid FeatureCollection - if( data && Array.isArray( data.pelias ) && data.pelias.length ){ - docs = data['pelias'][0].options || []; - } - - // convert docs to geojson - var geojson = geojsonify( docs ); - - // response envelope - geojson.date = new Date().getTime(); - - // respond - return res.status(200).json( geojson ); - }); - - } - - return controller; -} - -module.exports = setup; \ No newline at end of file diff --git a/controller/suggest_poi.js b/controller/suggest_poi.js deleted file mode 100644 index 9e30b41a..00000000 --- a/controller/suggest_poi.js +++ /dev/null @@ -1,46 +0,0 @@ - -var geojsonify = require('../helper/geojsonify').suggest; - -function setup( backend, query ){ - - // allow overriding of dependencies - backend = backend || require('../src/backend'); - query = query || require('../query/suggest_poi'); - - function controller( req, res, next ){ - - // backend command - var cmd = { - index: 'pelias', - body: query( req.clean ) - }; - - // query backend - backend().client.suggest( cmd, function( err, data ){ - - var docs = []; - - // handle backend errors - if( err ){ return next( err ); } - - // map response to a valid FeatureCollection - if( data && Array.isArray( data.pelias ) && data.pelias.length ){ - docs = data['pelias'][0].options || []; - } - - // convert docs to geojson - var geojson = geojsonify( docs ); - - // response envelope - geojson.date = new Date().getTime(); - - // respond - return res.status(200).json( geojson ); - }); - - } - - return controller; -} - -module.exports = setup; \ No newline at end of file diff --git a/query/suggest_poi.js b/query/suggest.js similarity index 100% rename from query/suggest_poi.js rename to query/suggest.js diff --git a/query/suggest_admin.js b/query/suggest_admin.js deleted file mode 100644 index 46883744..00000000 --- a/query/suggest_admin.js +++ /dev/null @@ -1,29 +0,0 @@ - -var logger = require('../src/logger'); - -// Build pelias suggest query -function generate( params, precision ){ - - var cmd = { - 'pelias' : { - 'text' : params.input, - 'completion' : { - 'size' : params.size, - 'field' : 'suggest', - 'context': { - 'dataset': ['admin0','admin1','admin2'], - 'location': { - 'value': [ params.lon, params.lat ], - 'precision': precision || 1 - } - } - } - } - }; - - // logger.log( 'cmd', JSON.stringify( cmd, null, 2 ) ); - return cmd; - -} - -module.exports = generate; \ No newline at end of file diff --git a/test/unit/controller/suggest.js b/test/unit/controller/suggest.js index bb3f2cc4..57051964 100644 --- a/test/unit/controller/suggest.js +++ b/test/unit/controller/suggest.js @@ -1,5 +1,5 @@ -var setup = require('../../../controller/suggest_poi'), +var setup = require('../../../controller/suggest'), mockBackend = require('../mock/backend'), mockQuery = require('../mock/query'); @@ -24,7 +24,7 @@ module.exports.tests.functional_success = function(test, common) { coordinates: [ 101, -10.1 ] }, properties: { - id: 'mockid', + id: 'mockid1', type: 'mocktype', value: 1 } @@ -35,7 +35,7 @@ module.exports.tests.functional_success = function(test, common) { coordinates: [ 101, -10.1 ] }, properties: { - id: 'mockid', + id: 'mockid2', type: 'mocktype', value: 2 } @@ -43,7 +43,12 @@ module.exports.tests.functional_success = function(test, common) { test('functional success', function(t) { var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){ - t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); + if (cmd.body.layers) { + // layers are set exclusively for admin: test for admin-only layers + t.deepEqual(cmd, { body: { input: 'b', layers: [ 'admin0', 'admin1', 'admin2' ] }, index: 'pelias' }, 'correct backend command'); + } else { + t.deepEqual(cmd, { body: { input: 'b' }, index: 'pelias' }, 'correct backend command'); + } }); var controller = setup( backend, mockQuery() ); var res = { @@ -60,7 +65,7 @@ module.exports.tests.functional_success = function(test, common) { t.end(); } }; - controller( { clean: { a: 'b' } }, res ); + controller( { clean: { input: 'b' } }, res ); }); }; diff --git a/test/unit/mock/backend.js b/test/unit/mock/backend.js index 63378423..91560a35 100644 --- a/test/unit/mock/backend.js +++ b/test/unit/mock/backend.js @@ -1,12 +1,14 @@ -var mockPayload = { - id: 'mocktype/mockid', - geo: '101,-10.1' +var mockPayload = function(id){ + return { + id: 'mocktype/mockid'+id, + geo: '101,-10.1' + } }; var responses = {}; responses['client/suggest/ok/1'] = function( cmd, cb ){ - return cb( undefined, suggestEnvelope([ { value: 1, payload: mockPayload }, { value: 2, payload: mockPayload } ]) ); + return cb( undefined, suggestEnvelope([ { value: 1, payload: mockPayload(1) }, { value: 2, payload: mockPayload(2) } ]) ); }; responses['client/suggest/fail/1'] = function( cmd, cb ){ return cb( 'a backend error occurred' ); diff --git a/test/unit/query/suggest.js b/test/unit/query/suggest.js index c072d224..77c3f099 100644 --- a/test/unit/query/suggest.js +++ b/test/unit/query/suggest.js @@ -1,5 +1,5 @@ -var generate = require('../../../query/suggest_poi'); +var generate = require('../../../query/suggest'); module.exports.tests = {}; From be39fb5a8f45e5a1f818e78814fa4043f1da59c9 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 4 Nov 2014 13:51:59 -0500 Subject: [PATCH 15/21] adding tests for the doc controller --- test/unit/controller/doc.js | 93 +++++++++++++++++++++++++++++++++++++ test/unit/mock/backend.js | 27 +++++++++++ test/unit/run.js | 1 + 3 files changed, 121 insertions(+) create mode 100644 test/unit/controller/doc.js diff --git a/test/unit/controller/doc.js b/test/unit/controller/doc.js new file mode 100644 index 00000000..98b5a174 --- /dev/null +++ b/test/unit/controller/doc.js @@ -0,0 +1,93 @@ + +var setup = require('../../../controller/doc'), + mockBackend = require('../mock/backend'), + mockQuery = require('../mock/query'); + +module.exports.tests = {}; + +module.exports.tests.interface = function(test, common) { + test('valid interface', function(t) { + t.equal(typeof setup, 'function', 'setup is a function'); + t.equal(typeof setup(), 'function', 'setup returns a controller'); + t.end(); + }); +}; + +// functionally test controller (backend success) +module.exports.tests.functional_success = function(test, common) { + + // expected geojson features for 'client/doc/ok/1' fixture + var expected = [{ + type: 'Feature', + geometry: { + type: 'Point', + coordinates: [ -50.5, 100.1 ] + }, + properties: { + name: 'test name1', + admin0: 'country1', + admin1: 'state1', + admin2: 'city1' + } + }, { + type: 'Feature', + geometry: { + type: 'Point', + coordinates: [ -51.5, 100.2 ] + }, + properties: { + name: 'test name2', + admin0: 'country2', + admin1: 'state2', + admin2: 'city2' + } + }]; + + test('functional success', function(t) { + var backend = mockBackend( 'client/doc/ok/1', function( cmd ){ + t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command'); + }); + var controller = setup( backend, mockQuery() ); + var res = { + status: function( code ){ + t.equal(code, 200, 'status set'); + return res; + }, + json: function( json ){ + t.equal(typeof json, 'object', 'returns json'); + t.equal(typeof json.date, 'number', 'date set'); + t.equal(json.type, 'FeatureCollection', 'valid geojson'); + t.true(Array.isArray(json.features), 'features is array'); + t.deepEqual(json.features, expected, 'values correctly mapped'); + t.end(); + } + }; + controller( { clean: { ids: [ {'id' : 123, 'type': 'a' } ] } }, res ); + }); +}; + +// functionally test controller (backend failure) +module.exports.tests.functional_failure = function(test, common) { + test('functional failure', function(t) { + var backend = mockBackend( 'client/doc/fail/1', function( cmd ){ + t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'b' } ] } }, 'correct backend command'); + }); + var controller = setup( backend, mockQuery() ); + var next = function( message ){ + t.equal(message,'a backend error occurred','error passed to errorHandler'); + t.end(); + }; + controller( { clean: { ids: [ {'id' : 123, 'type': 'b' } ] } }, undefined, next ); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('GET /doc ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; \ No newline at end of file diff --git a/test/unit/mock/backend.js b/test/unit/mock/backend.js index 63378423..1d0c3aaf 100644 --- a/test/unit/mock/backend.js +++ b/test/unit/mock/backend.js @@ -32,10 +32,33 @@ responses['client/search/fail/1'] = function( cmd, cb ){ return cb( 'a backend error occurred' ); }; +responses['client/doc/ok/1'] = function( cmd, cb ){ + return cb( undefined, docEnvelope([{ + _source: { + value: 1, + center_point: { lat: 100.1, lon: -50.5 }, + name: { default: 'test name1' }, + admin0: 'country1', admin1: 'state1', admin2: 'city1' + } + }, { + _source: { + value: 2, + center_point: { lat: 100.2, lon: -51.5 }, + name: { default: 'test name2' }, + admin0: 'country2', admin1: 'state2', admin2: 'city2' + } + }])); +}; +responses['client/doc/fail/1'] = responses['client/search/fail/1']; + function setup( key, cmdCb ){ function backend( a, b ){ return { client: { + mget: function( cmd, cb ){ + if( 'function' === typeof cmdCb ){ cmdCb( cmd ); } + return responses[key].apply( this, arguments ); + }, suggest: function( cmd, cb ){ if( 'function' === typeof cmdCb ){ cmdCb( cmd ); } return responses[key].apply( this, arguments ); @@ -50,6 +73,10 @@ function setup( key, cmdCb ){ return backend; } +function docEnvelope( options ){ + return { docs: options }; +} + function suggestEnvelope( options ){ return { pelias: [{ options: options }]}; } diff --git a/test/unit/run.js b/test/unit/run.js index bad988db..6b1398f9 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -4,6 +4,7 @@ var common = {}; var tests = [ require('./controller/index'), + require('./controller/doc'), require('./controller/suggest'), require('./controller/search'), require('./sanitiser/suggest'), From a53a6fd6d8e4a9c4e2877c32b3952ae8f78164d8 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 4 Nov 2014 14:34:03 -0500 Subject: [PATCH 16/21] no query needed --- test/unit/controller/doc.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/unit/controller/doc.js b/test/unit/controller/doc.js index 98b5a174..7b481a9f 100644 --- a/test/unit/controller/doc.js +++ b/test/unit/controller/doc.js @@ -1,7 +1,6 @@ var setup = require('../../../controller/doc'), - mockBackend = require('../mock/backend'), - mockQuery = require('../mock/query'); + mockBackend = require('../mock/backend'); module.exports.tests = {}; @@ -47,7 +46,7 @@ module.exports.tests.functional_success = function(test, common) { var backend = mockBackend( 'client/doc/ok/1', function( cmd ){ t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command'); }); - var controller = setup( backend, mockQuery() ); + var controller = setup( backend ); var res = { status: function( code ){ t.equal(code, 200, 'status set'); @@ -72,7 +71,7 @@ module.exports.tests.functional_failure = function(test, common) { var backend = mockBackend( 'client/doc/fail/1', function( cmd ){ t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'b' } ] } }, 'correct backend command'); }); - var controller = setup( backend, mockQuery() ); + var controller = setup( backend ); var next = function( message ){ t.equal(message,'a backend error occurred','error passed to errorHandler'); t.end(); From b1424797cda6d2aeb8eb2578f2ac7a9389fec231 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 4 Nov 2014 15:42:52 -0500 Subject: [PATCH 17/21] adding tests for service wrapper --- test/unit/run.js | 3 ++ test/unit/service/mget.js | 82 ++++++++++++++++++++++++++++++++++++ test/unit/service/search.js | 82 ++++++++++++++++++++++++++++++++++++ test/unit/service/suggest.js | 77 +++++++++++++++++++++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 test/unit/service/mget.js create mode 100644 test/unit/service/search.js create mode 100644 test/unit/service/suggest.js diff --git a/test/unit/run.js b/test/unit/run.js index 6b1398f9..ec1309be 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -7,6 +7,9 @@ var tests = [ require('./controller/doc'), require('./controller/suggest'), require('./controller/search'), + require('./service/mget'), + require('./service/search'), + require('./service/suggest'), require('./sanitiser/suggest'), require('./sanitiser/doc'), require('./query/indeces'), diff --git a/test/unit/service/mget.js b/test/unit/service/mget.js new file mode 100644 index 00000000..0b43f717 --- /dev/null +++ b/test/unit/service/mget.js @@ -0,0 +1,82 @@ + +var setup = require('../../../service/mget'), + mockBackend = require('../mock/backend'); + +module.exports.tests = {}; + +module.exports.tests.interface = function(test, common) { + test('valid interface', function(t) { + t.equal(typeof setup, 'function', 'setup is a function'); + t.end(); + }); +}; + +// functionally test service +module.exports.tests.functional_success = function(test, common) { + + var expected = [ + { + value: 1, + center_point: { lat: 100.1, lon: -50.5 }, + name: { default: 'test name1' }, + admin0: 'country1', admin1: 'state1', admin2: 'city1' + }, + { + value: 2, + center_point: { lat: 100.2, lon: -51.5 }, + name: { default: 'test name2' }, + admin0: 'country2', admin1: 'state2', admin2: 'city2' + } + ]; + + test('valid query', function(t) { + var backend = mockBackend( 'client/doc/ok/1', function( cmd ){ + t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command'); + }); + setup( backend, [ { _id: 123, _index: 'pelias', _type: 'a' } ], function(err, data) { + t.true(Array.isArray(data), 'returns an array'); + data.forEach(function(d) { + t.true(typeof d === 'object', 'valid object'); + }); + t.deepEqual(data, expected, 'values correctly mapped') + t.end(); + }); + }); + +}; + +// functionally test service +module.exports.tests.functional_failure = function(test, common) { + + test('invalid query', function(t) { + var invalid_queries = [ + { _id: 123, _index: 'pelias' }, + { _id: 123, _type: 'a' }, + { _index: 'pelias', _type: 'a' }, + { } + ]; + + var backend = mockBackend( 'client/doc/fail/1', function( cmd ){ + t.notDeepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'incorrect backend command'); + }); + invalid_queries.forEach(function(query) { + setup( backend, [ query ], function(err, data) { + t.equal(err, 'a backend error occurred','error passed to errorHandler'); + t.equal(data, undefined, 'data is undefined'); + }); + }); + t.end(); + }); + +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('SERVICE /mget ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; \ No newline at end of file diff --git a/test/unit/service/search.js b/test/unit/service/search.js new file mode 100644 index 00000000..2e1b3393 --- /dev/null +++ b/test/unit/service/search.js @@ -0,0 +1,82 @@ + +var setup = require('../../../service/search'), + mockBackend = require('../mock/backend'); + +var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' }; + +module.exports.tests = {}; + +module.exports.tests.interface = function(test, common) { + test('valid interface', function(t) { + t.equal(typeof setup, 'function', 'setup is a function'); + t.end(); + }); +}; + +// functionally test service +module.exports.tests.functional_success = function(test, common) { + + var expected = [ + { + value: 1, + center_point: { lat: 100.1, lon: -50.5 }, + name: { default: 'test name1' }, + admin0: 'country1', admin1: 'state1', admin2: 'city1' + }, + { + value: 2, + center_point: { lat: 100.2, lon: -51.5 }, + name: { default: 'test name2' }, + admin0: 'country2', admin1: 'state2', admin2: 'city2' + } + ]; + + test('valid ES query', function(t) { + var backend = mockBackend( 'client/search/ok/1', function( cmd ){ + t.deepEqual(cmd, example_valid_es_query, 'no change to the command'); + }); + setup( backend, example_valid_es_query, function(err, data) { + t.true(Array.isArray(data), 'returns an array'); + data.forEach(function(d) { + t.true(typeof d === 'object', 'valid object'); + }); + t.deepEqual(data, expected, 'values correctly mapped') + t.end(); + }); + }); + +}; + +// functionally test service +module.exports.tests.functional_failure = function(test, common) { + + test('invalid ES query', function(t) { + var invalid_queries = [ + { }, + { foo: 'bar' } + ]; + + var backend = mockBackend( 'client/search/fail/1', function( cmd ){ + t.notDeepEqual(cmd, example_valid_es_query, 'incorrect backend command'); + }); + invalid_queries.forEach(function(query) { + setup( backend, [ query ], function(err, data) { + t.equal(err, 'a backend error occurred','error passed to errorHandler'); + t.equal(data, undefined, 'data is undefined'); + }); + }); + t.end(); + }); + +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('SERVICE /search ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; \ No newline at end of file diff --git a/test/unit/service/suggest.js b/test/unit/service/suggest.js new file mode 100644 index 00000000..0f57e02c --- /dev/null +++ b/test/unit/service/suggest.js @@ -0,0 +1,77 @@ + +var setup = require('../../../service/suggest'), + mockBackend = require('../mock/backend'); + +var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' }; + +module.exports.tests = {}; + +module.exports.tests.interface = function(test, common) { + test('valid interface', function(t) { + t.equal(typeof setup, 'function', 'setup is a function'); + t.end(); + }); +}; + +// functionally test service +module.exports.tests.functional_success = function(test, common) { + + var mockPayload = { + id: 'mocktype/mockid', + geo: '101,-10.1' + }; + + var expected = [ + { value: 1, payload: mockPayload }, + { value: 2, payload: mockPayload } + ]; + + test('valid ES query', function(t) { + var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){ + t.deepEqual(cmd, example_valid_es_query, 'no change to the command'); + }); + setup( backend, example_valid_es_query, function(err, data) { + t.true(Array.isArray(data), 'returns an array'); + data.forEach(function(d) { + t.true(typeof d === 'object', 'valid object'); + }); + t.deepEqual(data, expected, 'values correctly mapped') + t.end(); + }); + }); + +}; + +// functionally test service +module.exports.tests.functional_failure = function(test, common) { + + test('invalid ES query', function(t) { + var invalid_queries = [ + { }, + { foo: 'bar' } + ]; + + var backend = mockBackend( 'client/suggest/fail/1', function( cmd ){ + t.notDeepEqual(cmd, example_valid_es_query, 'incorrect backend command'); + }); + invalid_queries.forEach(function(query) { + setup( backend, [ query ], function(err, data) { + t.equal(err, 'a backend error occurred','error passed to errorHandler'); + t.equal(data, undefined, 'data is undefined'); + }); + }); + t.end(); + }); + +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('SERVICE /suggest ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; \ No newline at end of file From dbd800b97f633cf920d99317a36d212dfe693cbe Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 4 Nov 2014 16:33:38 -0500 Subject: [PATCH 18/21] maintain the order --- controller/suggest.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/controller/suggest.js b/controller/suggest.js index e5f87084..425b7415 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -62,34 +62,34 @@ function setup( backend, query ){ if (req.clean.input.length < 4 && isNaN(parseInt(req.clean.input, 10))) { async_query = { - a: function(callback){ + admin_3p: function(callback){ cmd.body = query( req.admin, 3 ); query_backend(cmd, callback); }, - b: function(callback){ + admin_1p: function(callback){ cmd.body = query( req.admin, 1 ); query_backend(cmd, callback); }, - c: function(callback) { + all_3p: function(callback) { cmd.body = query( req.clean, 3 ); query_backend(cmd, callback); } } } else { async_query = { - a: function(callback){ + all_5p: function(callback){ cmd.body = query( req.clean, 5); query_backend(cmd, callback); }, - b: function(callback){ + all_3p: function(callback){ cmd.body = query( req.clean, 3); query_backend(cmd, callback); }, - c: function(callback){ + all_1p: function(callback){ cmd.body = query( req.clean, 1 ); query_backend(cmd, callback); }, - d: function(callback){ + admin_1p: function(callback){ cmd.body = query( req.admin ); query_backend(cmd, callback); } @@ -97,12 +97,14 @@ function setup( backend, query ){ } async.parallel(async_query, function(err, results) { - var splice_length = parseInt((SIZE / Object.keys(results).length), 10); // results is equal to: {a: docs, b: docs, c: docs} + var splice_length = parseInt((SIZE / Object.keys(results).length), 10); + var results_keys = Object.keys(async_query); + var combined = []; - for (keys in results) { - combined = combined.concat(results[keys].splice(0,splice_length)); - } + results_keys.forEach(function(key){ + combined = combined.concat(results[key].splice(0,splice_length)); + }); combined = dedup(combined); respond(combined); From 886aa498f5743049f763be761c74162a80db4301 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 5 Nov 2014 12:35:06 -0500 Subject: [PATCH 19/21] bringing back vanilla suggest (suggestions based on lat/lon/zoom only) - calling it suggest_near_me and adding a new endpoint (plus tests) --- app.js | 2 + controller/suggest_near_me.js | 40 +++++++++++ test/unit/controller/suggest_near_me.js | 91 +++++++++++++++++++++++++ test/unit/run.js | 1 + 4 files changed, 134 insertions(+) create mode 100644 controller/suggest_near_me.js create mode 100644 test/unit/controller/suggest_near_me.js diff --git a/app.js b/app.js index d4cf0257..51eada83 100644 --- a/app.js +++ b/app.js @@ -22,6 +22,7 @@ var controllers = {}; controllers.index = require('./controller/index'); controllers.doc = require('./controller/doc'); controllers.suggest = require('./controller/suggest'); +controllers.suggest_near_me = require('./controller/suggest_near_me'); controllers.search = require('./controller/search'); /** ----------------------- routes ----------------------- **/ @@ -34,6 +35,7 @@ app.get( '/doc', sanitisers.doc.middleware, controllers.doc() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); +app.get( '/suggest_near_me', sanitisers.suggest.middleware, controllers.suggest_near_me() ); // search API app.get( '/search', sanitisers.search.middleware, controllers.search() ); diff --git a/controller/suggest_near_me.js b/controller/suggest_near_me.js new file mode 100644 index 00000000..956363b8 --- /dev/null +++ b/controller/suggest_near_me.js @@ -0,0 +1,40 @@ + +var service = { suggest: require('../service/suggest') }; +var geojsonify = require('../helper/geojsonify').suggest; + +function setup( backend, query ){ + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + query = query || require('../query/suggest'); + + function controller( req, res, next ){ + + // backend command + var cmd = { + index: 'pelias', + body: query( req.clean ) + }; + + // query backend + service.suggest( backend, cmd, function( err, docs ){ + + // error handler + if( err ){ return next( err ); } + + // convert docs to geojson + var geojson = geojsonify( docs ); + + // response envelope + geojson.date = new Date().getTime(); + + // respond + return res.status(200).json( geojson ); + }); + + } + + return controller; +} + +module.exports = setup; \ No newline at end of file diff --git a/test/unit/controller/suggest_near_me.js b/test/unit/controller/suggest_near_me.js new file mode 100644 index 00000000..bfb96195 --- /dev/null +++ b/test/unit/controller/suggest_near_me.js @@ -0,0 +1,91 @@ + +var setup = require('../../../controller/suggest'), + mockBackend = require('../mock/backend'), + mockQuery = require('../mock/query'); + +module.exports.tests = {}; + +module.exports.tests.interface = function(test, common) { + test('valid interface', function(t) { + t.equal(typeof setup, 'function', 'setup is a function'); + t.equal(typeof setup(), 'function', 'setup returns a controller'); + t.end(); + }); +}; + +// functionally test controller (backend success) +module.exports.tests.functional_success = function(test, common) { + + // expected geojson features for 'client/suggest/ok/1' fixture + var expected = [{ + type: 'Feature', + geometry: { + type: 'Point', + coordinates: [ 101, -10.1 ] + }, + properties: { + id: 'mockid1', + type: 'mocktype', + value: 1 + } + }, { + type: 'Feature', + geometry: { + type: 'Point', + coordinates: [ 101, -10.1 ] + }, + properties: { + id: 'mockid2', + type: 'mocktype', + value: 2 + } + }]; + + test('functional success', function(t) { + var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){ + t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); + }); + var controller = setup( backend, mockQuery() ); + var res = { + status: function( code ){ + t.equal(code, 200, 'status set'); + return res; + }, + json: function( json ){ + t.equal(typeof json, 'object', 'returns json'); + t.equal(typeof json.date, 'number', 'date set'); + t.equal(json.type, 'FeatureCollection', 'valid geojson'); + t.true(Array.isArray(json.features), 'features is array'); + t.deepEqual(json.features, expected, 'values correctly mapped'); + t.end(); + } + }; + controller( { clean: { a: 'b' } }, res ); + }); +}; + +// functionally test controller (backend failure) +module.exports.tests.functional_failure = function(test, common) { + test('functional failure', function(t) { + var backend = mockBackend( 'client/suggest/fail/1', function( cmd ){ + t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); + }); + var controller = setup( backend, mockQuery() ); + var next = function( message ){ + t.equal(message,'a backend error occurred','error passed to errorHandler'); + t.end(); + }; + controller( { clean: { a: 'b' } }, undefined, next ); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('GET /suggest ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; \ No newline at end of file diff --git a/test/unit/run.js b/test/unit/run.js index ec1309be..f828c6c5 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -6,6 +6,7 @@ var tests = [ require('./controller/index'), require('./controller/doc'), require('./controller/suggest'), + require('./controller/suggest_near_me'), require('./controller/search'), require('./service/mget'), require('./service/search'), From 407c32e782099627970cba7abbb73878a842caa5 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 5 Nov 2014 16:43:19 -0500 Subject: [PATCH 20/21] suggest_near_me to /suggest/nearby and suggest_near_me.js to suggest_nearby.js --- app.js | 4 ++-- controller/{suggest_near_me.js => suggest_nearby.js} | 0 .../unit/controller/{suggest_near_me.js => suggest_nearby.js} | 0 test/unit/run.js | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename controller/{suggest_near_me.js => suggest_nearby.js} (100%) rename test/unit/controller/{suggest_near_me.js => suggest_nearby.js} (100%) diff --git a/app.js b/app.js index 51eada83..21b465f0 100644 --- a/app.js +++ b/app.js @@ -22,7 +22,7 @@ var controllers = {}; controllers.index = require('./controller/index'); controllers.doc = require('./controller/doc'); controllers.suggest = require('./controller/suggest'); -controllers.suggest_near_me = require('./controller/suggest_near_me'); +controllers.suggest_nearby = require('./controller/suggest_nearby'); controllers.search = require('./controller/search'); /** ----------------------- routes ----------------------- **/ @@ -35,7 +35,7 @@ app.get( '/doc', sanitisers.doc.middleware, controllers.doc() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); -app.get( '/suggest_near_me', sanitisers.suggest.middleware, controllers.suggest_near_me() ); +app.get( '/suggest/nearby', sanitisers.suggest.middleware, controllers.suggest_nearby() ); // search API app.get( '/search', sanitisers.search.middleware, controllers.search() ); diff --git a/controller/suggest_near_me.js b/controller/suggest_nearby.js similarity index 100% rename from controller/suggest_near_me.js rename to controller/suggest_nearby.js diff --git a/test/unit/controller/suggest_near_me.js b/test/unit/controller/suggest_nearby.js similarity index 100% rename from test/unit/controller/suggest_near_me.js rename to test/unit/controller/suggest_nearby.js diff --git a/test/unit/run.js b/test/unit/run.js index f828c6c5..4428903c 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -6,7 +6,7 @@ var tests = [ require('./controller/index'), require('./controller/doc'), require('./controller/suggest'), - require('./controller/suggest_near_me'), + require('./controller/suggest_nearby'), require('./controller/search'), require('./service/mget'), require('./service/search'), From 49a63f1861e5cea2b09ce1d363bf8cb7934f0284 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Thu, 6 Nov 2014 11:46:06 -0500 Subject: [PATCH 21/21] Documentation and some ciao tests --- docs/404.md | 12 +- docs/cors.md | 4 +- docs/doc/msuccess.md | 20 +-- docs/doc/success.md | 20 +-- docs/index.md | 28 +-- docs/jsonp.md | 14 +- docs/reverse/success.md | 24 +-- docs/search/success.md | 18 +- docs/suggest/success.md | 71 ++++++-- docs/suggest/success_nearby.md | 223 ++++++++++++++++++++++++ test/ciao/suggest/success_nearby.coffee | 16 ++ 11 files changed, 369 insertions(+), 81 deletions(-) create mode 100644 docs/suggest/success_nearby.md create mode 100644 test/ciao/suggest/success_nearby.coffee diff --git a/docs/404.md b/docs/404.md index 66a3567d..917c5ad4 100644 --- a/docs/404.md +++ b/docs/404.md @@ -1,6 +1,6 @@ # invalid path -*Generated: Thu Oct 23 2014 11:58:14 GMT-0400 (EDT)* +*Generated: Thu Nov 06 2014 11:44:19 GMT-0500 (EST)* ## Request ```javascript { @@ -27,7 +27,7 @@ Status: 404 "content-type": "application/json; charset=utf-8", "content-length": "35", "etag": "W/\"23-dfdfa185\"", - "date": "Thu, 23 Oct 2014 15:58:14 GMT", + "date": "Thu, 06 Nov 2014 16:44:19 GMT", "connection": "close" } ``` @@ -39,9 +39,9 @@ Status: 404 ## Tests -### ✓ cache-control header correctly set +### ✓ not found ```javascript -response.should.have.header 'Cache-Control','public,max-age=300' +response.statusCode.should.equal 404 ``` ### ✓ content-type header correctly set @@ -49,9 +49,9 @@ response.should.have.header 'Cache-Control','public,max-age=300' response.should.have.header 'Content-Type','application/json; charset=utf-8' ``` -### ✓ not found +### ✓ cache-control header correctly set ```javascript -response.statusCode.should.equal 404 +response.should.have.header 'Cache-Control','public,max-age=300' ``` ### ✓ should respond in json with server info diff --git a/docs/cors.md b/docs/cors.md index 1f6e7e46..17ddda39 100644 --- a/docs/cors.md +++ b/docs/cors.md @@ -1,6 +1,6 @@ # cross-origin resource sharing -*Generated: Thu Oct 23 2014 11:58:14 GMT-0400 (EDT)* +*Generated: Thu Nov 06 2014 11:44:19 GMT-0500 (EST)* ## Request ```javascript { @@ -27,7 +27,7 @@ Status: 200 "content-type": "application/json; charset=utf-8", "content-length": "50", "etag": "W/\"32-85536434\"", - "date": "Thu, 23 Oct 2014 15:58:14 GMT", + "date": "Thu, 06 Nov 2014 16:44:19 GMT", "connection": "close" } ``` diff --git a/docs/doc/msuccess.md b/docs/doc/msuccess.md index c9f3d8c9..1f510a42 100644 --- a/docs/doc/msuccess.md +++ b/docs/doc/msuccess.md @@ -1,6 +1,6 @@ # valid doc query -*Generated: Thu Oct 23 2014 11:58:14 GMT-0400 (EDT)* +*Generated: Thu Nov 06 2014 11:44:19 GMT-0500 (EST)* ## Request ```javascript { @@ -26,8 +26,8 @@ Status: 200 "access-control-allow-credentials": "true", "content-type": "application/json; charset=utf-8", "content-length": "555", - "etag": "W/\"22b-6aa14642\"", - "date": "Thu, 23 Oct 2014 15:58:14 GMT", + "etag": "W/\"22b-dd736629\"", + "date": "Thu, 06 Nov 2014 16:44:19 GMT", "connection": "close" } ``` @@ -70,7 +70,7 @@ Status: 200 } } ], - "date": 1414079894512 + "date": 1415292259726 } ``` @@ -81,6 +81,12 @@ Status: 200 response.statusCode.should.equal 200 ``` +### ✓ valid geojson +```javascript +json.type.should.equal 'FeatureCollection' +json.features.should.be.instanceof Array +``` + ### ✓ valid response ```javascript now = new Date().getTime() @@ -89,9 +95,3 @@ should.not.exist json.error json.date.should.be.within now-5000, now+5000 ``` -### ✓ valid geojson -```javascript -json.type.should.equal 'FeatureCollection' -json.features.should.be.instanceof Array -``` - diff --git a/docs/doc/success.md b/docs/doc/success.md index 34179af0..a6dd6c00 100644 --- a/docs/doc/success.md +++ b/docs/doc/success.md @@ -1,6 +1,6 @@ # valid doc query -*Generated: Thu Oct 23 2014 11:58:14 GMT-0400 (EDT)* +*Generated: Thu Nov 06 2014 11:44:20 GMT-0500 (EST)* ## Request ```javascript { @@ -26,8 +26,8 @@ Status: 200 "access-control-allow-credentials": "true", "content-type": "application/json; charset=utf-8", "content-length": "311", - "etag": "W/\"137-ab9138f7\"", - "date": "Thu, 23 Oct 2014 15:58:14 GMT", + "etag": "W/\"137-1644173e\"", + "date": "Thu, 06 Nov 2014 16:44:20 GMT", "connection": "close" } ``` @@ -53,12 +53,18 @@ Status: 200 } } ], - "date": 1414079894512 + "date": 1415292260057 } ``` ## Tests +### ✓ valid geojson +```javascript +json.type.should.equal 'FeatureCollection' +json.features.should.be.instanceof Array +``` + ### ✓ valid response ```javascript now = new Date().getTime() @@ -72,9 +78,3 @@ json.date.should.be.within now-5000, now+5000 response.statusCode.should.equal 200 ``` -### ✓ valid geojson -```javascript -json.type.should.equal 'FeatureCollection' -json.features.should.be.instanceof Array -``` - diff --git a/docs/index.md b/docs/index.md index a386d38f..cde683b5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,6 +1,6 @@ # api root -*Generated: Thu Oct 23 2014 11:58:14 GMT-0400 (EDT)* +*Generated: Thu Nov 06 2014 11:44:19 GMT-0500 (EST)* ## Request ```javascript { @@ -27,7 +27,7 @@ Status: 200 "content-type": "application/json; charset=utf-8", "content-length": "50", "etag": "W/\"32-85536434\"", - "date": "Thu, 23 Oct 2014 15:58:14 GMT", + "date": "Thu, 06 Nov 2014 16:44:19 GMT", "connection": "close" } ``` @@ -42,6 +42,11 @@ Status: 200 ## Tests +### ✓ charset header correctly set +```javascript +response.should.have.header 'Charset','utf8' +``` + ### ✓ endpoint available ```javascript response.statusCode.should.equal 200 @@ -53,16 +58,6 @@ response.should.have.header 'Server' response.headers.server.should.match /Pelias\/\d{1,2}\.\d{1,2}\.\d{1,2}/ ``` -### ✓ vanity header correctly set -```javascript -response.should.have.header 'X-Powered-By','mapzen' -``` - -### ✓ cache-control header correctly set -```javascript -response.should.have.header 'Cache-Control','public,max-age=60' -``` - ### ✓ should respond in json with server info ```javascript should.exist json @@ -70,13 +65,18 @@ should.exist json.name should.exist json.version ``` +### ✓ vanity header correctly set +```javascript +response.should.have.header 'X-Powered-By','mapzen' +``` + ### ✓ content-type header correctly set ```javascript response.should.have.header 'Content-Type','application/json; charset=utf-8' ``` -### ✓ charset header correctly set +### ✓ cache-control header correctly set ```javascript -response.should.have.header 'Charset','utf8' +response.should.have.header 'Cache-Control','public,max-age=60' ``` diff --git a/docs/jsonp.md b/docs/jsonp.md index f4ba0eca..b8c08577 100644 --- a/docs/jsonp.md +++ b/docs/jsonp.md @@ -1,6 +1,6 @@ # jsonp -*Generated: Thu Oct 23 2014 11:58:14 GMT-0400 (EDT)* +*Generated: Thu Nov 06 2014 11:44:19 GMT-0500 (EST)* ## Request ```javascript { @@ -27,7 +27,7 @@ Status: 200 "content-type": "application/javascript; charset=utf-8", "content-length": "57", "etag": "W/\"39-b8a2aba1\"", - "date": "Thu, 23 Oct 2014 15:58:14 GMT", + "date": "Thu, 06 Nov 2014 16:44:19 GMT", "connection": "close" } ``` @@ -37,14 +37,14 @@ test({"name":"pelias-api","version":{"number":"0.0.0"}}); ## Tests -### ✓ should respond with jsonp +### ✓ content-type header correctly set ```javascript -should.exist response.body -response.body.substr(0,5).should.equal 'test('; +response.should.have.header 'Content-Type','application/javascript; charset=utf-8' ``` -### ✓ content-type header correctly set +### ✓ should respond with jsonp ```javascript -response.should.have.header 'Content-Type','application/javascript; charset=utf-8' +should.exist response.body +response.body.substr(0,5).should.equal 'test('; ``` diff --git a/docs/reverse/success.md b/docs/reverse/success.md index 1813aab7..cb7e84e1 100644 --- a/docs/reverse/success.md +++ b/docs/reverse/success.md @@ -1,6 +1,6 @@ # valid reverse query -*Generated: Thu Oct 23 2014 11:58:15 GMT-0400 (EDT)* +*Generated: Thu Nov 06 2014 11:44:19 GMT-0500 (EST)* ## Request ```javascript { @@ -26,8 +26,8 @@ Status: 200 "access-control-allow-credentials": "true", "content-type": "application/json; charset=utf-8", "content-length": "282", - "etag": "W/\"11a-95fc1500\"", - "date": "Thu, 23 Oct 2014 15:58:15 GMT", + "etag": "W/\"11a-efcd00c9\"", + "date": "Thu, 06 Nov 2014 16:44:19 GMT", "connection": "close" } ``` @@ -53,12 +53,20 @@ Status: 200 } } ], - "date": 1414079895606 + "date": 1415292259729 } ``` ## Tests +### ✓ valid response +```javascript +now = new Date().getTime() +should.exist json +should.not.exist json.error +json.date.should.be.within now-5000, now+5000 +``` + ### ✓ 200 ok ```javascript response.statusCode.should.equal 200 @@ -70,11 +78,3 @@ json.type.should.equal 'FeatureCollection' json.features.should.be.instanceof Array ``` -### ✓ valid response -```javascript -now = new Date().getTime() -should.exist json -should.not.exist json.error -json.date.should.be.within now-5000, now+5000 -``` - diff --git a/docs/search/success.md b/docs/search/success.md index 6b64c548..50789c87 100644 --- a/docs/search/success.md +++ b/docs/search/success.md @@ -1,6 +1,6 @@ # valid search query -*Generated: Thu Oct 23 2014 11:58:15 GMT-0400 (EDT)* +*Generated: Thu Nov 06 2014 11:44:19 GMT-0500 (EST)* ## Request ```javascript { @@ -26,8 +26,8 @@ Status: 200 "access-control-allow-credentials": "true", "content-type": "application/json; charset=utf-8", "content-length": "2398", - "etag": "W/\"0tqT2h50EMVuqDtvmB5nAQ==\"", - "date": "Thu, 23 Oct 2014 15:58:15 GMT", + "etag": "W/\"NldeHivz2maJ3rqa73a+2w==\"", + "date": "Thu, 06 Nov 2014 16:44:19 GMT", "connection": "close" } ``` @@ -206,7 +206,7 @@ Status: 200 } } ], - "date": 1414079895605 + "date": 1415292259730 } ``` @@ -220,14 +220,14 @@ should.not.exist json.error json.date.should.be.within now-5000, now+5000 ``` -### ✓ valid geojson +### ✓ 200 ok ```javascript -json.type.should.equal 'FeatureCollection' -json.features.should.be.instanceof Array +response.statusCode.should.equal 200 ``` -### ✓ 200 ok +### ✓ valid geojson ```javascript -response.statusCode.should.equal 200 +json.type.should.equal 'FeatureCollection' +json.features.should.be.instanceof Array ``` diff --git a/docs/suggest/success.md b/docs/suggest/success.md index bc120b2c..8c430b53 100644 --- a/docs/suggest/success.md +++ b/docs/suggest/success.md @@ -1,6 +1,6 @@ # valid suggest query -*Generated: Thu Oct 23 2014 11:58:14 GMT-0400 (EDT)* +*Generated: Thu Nov 06 2014 11:44:19 GMT-0500 (EST)* ## Request ```javascript { @@ -25,27 +25,71 @@ Status: 200 "access-control-allow-headers": "X-Requested-With,content-type", "access-control-allow-credentials": "true", "content-type": "application/json; charset=utf-8", - "content-length": "63", - "etag": "W/\"3f-200731a6\"", - "date": "Thu, 23 Oct 2014 15:58:14 GMT", + "content-length": "571", + "etag": "W/\"23b-5d6e3dd3\"", + "date": "Thu, 06 Nov 2014 16:44:19 GMT", "connection": "close" } ``` ```javascript { "type": "FeatureCollection", - "features": [], - "date": 1414079894479 + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -8.481618, + 43.125692 + ] + }, + "properties": { + "text": "A Coruña", + "score": 14, + "type": "admin1", + "id": "3374:adm1:es:esp:a_coru_a" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + 7.56019, + 5.419786 + ] + }, + "properties": { + "text": "Abia", + "score": 14, + "type": "admin1", + "id": "1775:adm1:ng:nga:abia" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + 33.772337, + 2.826081 + ] + }, + "properties": { + "text": "Abim", + "score": 14, + "type": "admin1", + "id": "2848:adm1:ug:uga:abim" + } + } + ], + "date": 1415292259700 } ``` ## Tests -### ✓ 200 ok -```javascript -response.statusCode.should.equal 200 -``` - ### ✓ valid geojson ```javascript json.type.should.equal 'FeatureCollection' @@ -60,3 +104,8 @@ should.not.exist json.error json.date.should.be.within now-5000, now+5000 ``` +### ✓ 200 ok +```javascript +response.statusCode.should.equal 200 +``` + diff --git a/docs/suggest/success_nearby.md b/docs/suggest/success_nearby.md new file mode 100644 index 00000000..c29643c2 --- /dev/null +++ b/docs/suggest/success_nearby.md @@ -0,0 +1,223 @@ +# valid suggest query + +*Generated: Thu Nov 06 2014 11:44:20 GMT-0500 (EST)* +## Request +```javascript +{ + "protocol": "http:", + "host": "localhost", + "method": "GET", + "port": 3100, + "path": "/suggest/nearby?input=a&lat=29.49136&lon=-82.50622" +} +``` + +## Response +```javascript +Status: 200 +{ + "x-powered-by": "mapzen", + "charset": "utf8", + "cache-control": "public,max-age=60", + "server": "Pelias/0.0.0", + "access-control-allow-origin": "*", + "access-control-allow-methods": "GET", + "access-control-allow-headers": "X-Requested-With,content-type", + "access-control-allow-credentials": "true", + "content-type": "application/json; charset=utf-8", + "content-length": "2034", + "etag": "W/\"Do9VJ5hCbynTxDjtm5fNlg==\"", + "date": "Thu, 06 Nov 2014 16:44:19 GMT", + "connection": "close" +} +``` +```javascript +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.05231, + 29.17998 + ] + }, + "properties": { + "text": "Abiding Hope E V Lutheran Church, Marion County, Florida", + "score": 1, + "type": "geoname", + "id": "4145572" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.10231, + 29.21942 + ] + }, + "properties": { + "text": "Abundant Harvest Ministries, Marion County, Florida", + "score": 1, + "type": "geoname", + "id": "4145578" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.50622, + 29.49136 + ] + }, + "properties": { + "text": "Adam, Alachua County, Florida", + "score": 1, + "type": "geoname", + "id": "4145612" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.75374, + 35.17789 + ] + }, + "properties": { + "text": "Adams Branch, Transylvania County, North Carolina", + "score": 1, + "type": "geoname", + "id": "4452189" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.83012, + 29.4783 + ] + }, + "properties": { + "text": "Adamsville Cemetery, Levy County, Florida", + "score": 1, + "type": "geoname", + "id": "4145634" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.01511, + 35.17289 + ] + }, + "properties": { + "text": "Africa School (historical), Spartanburg County, South Carolina", + "score": 1, + "type": "geoname", + "id": "4569065" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.20426, + 29.25192 + ] + }, + "properties": { + "text": "Agape Baptist Church, Marion County, Florida", + "score": 1, + "type": "geoname", + "id": "4145673" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.14954, + 29.19248 + ] + }, + "properties": { + "text": "Agnew Cemetery, Marion County, Florida", + "score": 1, + "type": "geoname", + "id": "4145677" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.75429, + 35.16928 + ] + }, + "properties": { + "text": "Aiken Mountain, Transylvania County, North Carolina", + "score": 1, + "type": "geoname", + "id": "4452268" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.15912, + 29.47877 + ] + }, + "properties": { + "text": "Alachua County Fire Rescue Station 31, Alachua County, Florida", + "score": 1, + "type": "geoname", + "id": "4152402" + } + } + ], + "date": 1415292259785 +} +``` + +## Tests + +### ✓ 200 ok +```javascript +response.statusCode.should.equal 200 +``` + +### ✓ valid response +```javascript +now = new Date().getTime() +should.exist json +should.not.exist json.error +json.date.should.be.within now-5000, now+5000 +``` + +### ✓ valid geojson +```javascript +json.type.should.equal 'FeatureCollection' +json.features.should.be.instanceof Array +``` + diff --git a/test/ciao/suggest/success_nearby.coffee b/test/ciao/suggest/success_nearby.coffee new file mode 100644 index 00000000..ca958720 --- /dev/null +++ b/test/ciao/suggest/success_nearby.coffee @@ -0,0 +1,16 @@ + +#> valid suggest query +path: '/suggest/nearby?input=a&lat=29.49136&lon=-82.50622' + +#? 200 ok +response.statusCode.should.equal 200 + +#? valid response +now = new Date().getTime() +should.exist json +should.not.exist json.error +json.date.should.be.within now-5000, now+5000 + +#? valid geojson +json.type.should.equal 'FeatureCollection' +json.features.should.be.instanceof Array \ No newline at end of file