Browse Source

adding a parameter called details (defaults to false for all endpoint except reverse) + fixing tests

pull/103/head
Harish Krishna 10 years ago
parent
commit
c99c5f712a
  1. 2
      controller/doc.js
  2. 2
      controller/search.js
  3. 2
      controller/suggest.js
  4. 32
      helper/geojsonify.js
  5. 23
      sanitiser/_details.js
  6. 3
      sanitiser/doc.js
  7. 4
      sanitiser/reverse.js
  8. 1
      sanitiser/search.js
  9. 1
      sanitiser/suggest.js
  10. 26
      test/unit/controller/doc.js
  11. 26
      test/unit/controller/search.js
  12. 26
      test/unit/controller/suggest.js
  13. 4
      test/unit/helper/geojsonify.js
  14. 7
      test/unit/sanitiser/doc.js
  15. 3
      test/unit/sanitiser/reverse.js
  16. 3
      test/unit/sanitiser/search.js
  17. 3
      test/unit/sanitiser/suggest.js

2
controller/doc.js

@ -23,7 +23,7 @@ function setup( backend ){
if( err ){ return next( err ); } if( err ){ return next( err ); }
// convert docs to geojson // convert docs to geojson
var geojson = geojsonify( docs ); var geojson = geojsonify( docs, req.clean.details );
// response envelope // response envelope
geojson.date = new Date().getTime(); geojson.date = new Date().getTime();

2
controller/search.js

@ -28,7 +28,7 @@ function setup( backend, query ){
if( err ){ return next( err ); } if( err ){ return next( err ); }
// convert docs to geojson // convert docs to geojson
var geojson = geojsonify( docs ); var geojson = geojsonify( docs, req.clean.details );
// response envelope // response envelope
geojson.date = new Date().getTime(); geojson.date = new Date().getTime();

2
controller/suggest.js

@ -27,7 +27,7 @@ function setup( backend, query, query_mixer ){
function reply( docs ){ function reply( docs ){
// convert docs to geojson // convert docs to geojson
var geojson = geojsonify( docs ); var geojson = geojsonify( docs, req.clean.details );
// response envelope // response envelope
geojson.date = new Date().getTime(); geojson.date = new Date().getTime();

32
helper/geojsonify.js

@ -3,7 +3,7 @@ var GeoJSON = require('geojson'),
extent = require('geojson-extent'), extent = require('geojson-extent'),
outputGenerator = require('./outputGenerator'); outputGenerator = require('./outputGenerator');
function search( docs ){ function search( docs, details ){
// emit a warning if the doc format is invalid // emit a warning if the doc format is invalid
// @note: if you see this error, fix it ASAP! // @note: if you see this error, fix it ASAP!
@ -12,6 +12,8 @@ function search( docs ){
return false; // remove offending doc from results return false; // remove offending doc from results
} }
details = details || false;
// flatten & expand data for geojson conversion // flatten & expand data for geojson conversion
var geodata = docs.map( function( doc ){ var geodata = docs.map( function( doc ){
@ -29,19 +31,21 @@ function search( docs ){
output.lat = parseFloat( doc.center_point.lat ); output.lat = parseFloat( doc.center_point.lat );
output.lng = parseFloat( doc.center_point.lon ); output.lng = parseFloat( doc.center_point.lon );
// map name if (details) {
if( !doc.name || !doc.name.default ) { return warning(); } // map name
output.name = doc.name.default; if( !doc.name || !doc.name.default ) { return warning(); }
output.name = doc.name.default;
// map admin values
if( doc.alpha3 ){ output.alpha3 = doc.alpha3; } // map admin values
if( doc.admin0 ){ output.admin0 = doc.admin0; } if( doc.alpha3 ){ output.alpha3 = doc.alpha3; }
if( doc.admin1 ){ output.admin1 = doc.admin1; } if( doc.admin0 ){ output.admin0 = doc.admin0; }
if( doc.admin1_abbr ){ output.admin1_abbr = doc.admin1_abbr; } if( doc.admin1 ){ output.admin1 = doc.admin1; }
if( doc.admin2 ){ output.admin2 = doc.admin2; } if( doc.admin1_abbr ){ output.admin1_abbr = doc.admin1_abbr; }
if( doc.local_admin ){ output.local_admin = doc.local_admin; } if( doc.admin2 ){ output.admin2 = doc.admin2; }
if( doc.locality ){ output.locality = doc.locality; } if( doc.local_admin ){ output.local_admin = doc.local_admin; }
if( doc.neighborhood ){ output.neighborhood = doc.neighborhood; } if( doc.locality ){ output.locality = doc.locality; }
if( doc.neighborhood ){ output.neighborhood = doc.neighborhood; }
}
// generate region-specific text string // generate region-specific text string
output.text = outputGenerator( doc ); output.text = outputGenerator( doc );

23
sanitiser/_details.js

@ -0,0 +1,23 @@
// validate inputs, convert types and apply defaults
function sanitize( req, default_value ){
var clean = req.clean || {};
var params= req.query;
default_value = !!default_value;
// ensure the input params are a valid object
if( Object.prototype.toString.call( params ) !== '[object Object]' ){
params = {};
}
clean.details = !!params.details;
req.clean = clean;
return {'error':false};
}
// export function
module.exports = sanitize;

3
sanitiser/doc.js

@ -2,7 +2,8 @@
var logger = require('../src/logger'), var logger = require('../src/logger'),
_sanitize = require('../sanitiser/_sanitize'), _sanitize = require('../sanitiser/_sanitize'),
sanitizers = { sanitizers = {
id: require('../sanitiser/_id') id: require('../sanitiser/_id'),
details: require('../sanitiser/_details')
}; };
var sanitize = function(req, cb) { _sanitize(req, sanitizers, cb); }; var sanitize = function(req, cb) { _sanitize(req, sanitizers, cb); };

4
sanitiser/reverse.js

@ -7,6 +7,10 @@ var logger = require('../src/logger'),
return geo(req, true); return geo(req, true);
}, },
layers: require('../sanitiser/_layers'), layers: require('../sanitiser/_layers'),
details: function( req ) {
var details = require('../sanitiser/_details');
return details(req, true);
},
size: function( req ) { size: function( req ) {
var size = require('../sanitiser/_size'); var size = require('../sanitiser/_size');
return size(req, 1); return size(req, 1);

1
sanitiser/search.js

@ -5,6 +5,7 @@ var logger = require('../src/logger'),
input: require('../sanitiser/_input'), input: require('../sanitiser/_input'),
size: require('../sanitiser/_size'), size: require('../sanitiser/_size'),
layers: require('../sanitiser/_layers'), layers: require('../sanitiser/_layers'),
details: require('../sanitiser/_details'),
latlonzoom: require('../sanitiser/_geo') latlonzoom: require('../sanitiser/_geo')
}; };

1
sanitiser/suggest.js

@ -5,6 +5,7 @@ var logger = require('../src/logger'),
input: require('../sanitiser/_input'), input: require('../sanitiser/_input'),
size: require('../sanitiser/_size'), size: require('../sanitiser/_size'),
layers: require('../sanitiser/_layers'), layers: require('../sanitiser/_layers'),
details: require('../sanitiser/_details'),
latlonzoom: function( req ) { latlonzoom: function( req ) {
var geo = require('../sanitiser/_geo'); var geo = require('../sanitiser/_geo');
return geo(req, true); return geo(req, true);

26
test/unit/controller/doc.js

@ -16,7 +16,7 @@ module.exports.tests.interface = function(test, common) {
module.exports.tests.functional_success = function(test, common) { module.exports.tests.functional_success = function(test, common) {
// expected geojson features for 'client/doc/ok/1' fixture // expected geojson features for 'client/doc/ok/1' fixture
var expected = [{ var expected_details = [{
type: 'Feature', type: 'Feature',
geometry: { geometry: {
type: 'Point', type: 'Point',
@ -48,6 +48,30 @@ module.exports.tests.functional_success = function(test, common) {
} }
}]; }];
var expected = [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -50.5, 100.1 ]
},
properties: {
id: 'myid1',
layer: 'mytype1',
text: 'test name1, city1, state1'
}
}, {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -51.5, 100.2 ]
},
properties: {
id: 'myid2',
layer: 'mytype2',
text: 'test name2, city2, state2'
}
}];
test('functional success', function(t) { test('functional success', function(t) {
var backend = mockBackend( 'client/mget/ok/1', function( cmd ){ var backend = mockBackend( 'client/mget/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command'); t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command');

26
test/unit/controller/search.js

@ -17,7 +17,7 @@ module.exports.tests.interface = function(test, common) {
module.exports.tests.functional_success = function(test, common) { module.exports.tests.functional_success = function(test, common) {
// expected geojson features for 'client/suggest/ok/1' fixture // expected geojson features for 'client/suggest/ok/1' fixture
var expected = [{ var expected_details = [{
type: 'Feature', type: 'Feature',
geometry: { geometry: {
type: 'Point', type: 'Point',
@ -49,6 +49,30 @@ module.exports.tests.functional_success = function(test, common) {
} }
}]; }];
var expected = [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -50.5, 100.1 ]
},
properties: {
id: 'myid1',
layer: 'mytype1',
text: 'test name1, city1, state1'
}
}, {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -51.5, 100.2 ]
},
properties: {
id: 'myid2',
layer: 'mytype2',
text: 'test name2, city2, state2'
}
}];
test('functional success', function(t) { test('functional success', function(t) {
var backend = mockBackend( 'client/search/ok/1', function( cmd ){ var backend = mockBackend( 'client/search/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command'); t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command');

26
test/unit/controller/suggest.js

@ -17,7 +17,7 @@ module.exports.tests.interface = function(test, common) {
module.exports.tests.functional_success = function(test, common) { module.exports.tests.functional_success = function(test, common) {
// expected geojson features for 'client/mget/ok/1' fixture // expected geojson features for 'client/mget/ok/1' fixture
var expected = [{ var expected_details = [{
type: 'Feature', type: 'Feature',
geometry: { geometry: {
type: 'Point', type: 'Point',
@ -49,6 +49,30 @@ module.exports.tests.functional_success = function(test, common) {
} }
}]; }];
var expected = [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -50.5, 100.1 ]
},
properties: {
id: 'myid1',
layer: 'mytype1',
text: 'test name1, city1, state1'
}
}, {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -51.5, 100.2 ]
},
properties: {
id: 'myid2',
layer: 'mytype2',
text: 'test name2, city2, state2'
}
}];
test('functional success', function(t) { test('functional success', function(t) {
var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){ var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){
// the backend executes suggest (vanilla and admin-only) and mget, so we check them all based on cmd // the backend executes suggest (vanilla and admin-only) and mget, so we check them all based on cmd

4
test/unit/helper/geojsonify.js

@ -6,7 +6,7 @@ module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface .search()', function(t) { test('valid interface .search()', function(t) {
t.equal(typeof geojsonify.search, 'function', 'search is a function'); t.equal(typeof geojsonify.search, 'function', 'search is a function');
t.equal(geojsonify.search.length, 1, 'accepts x arguments'); t.equal(geojsonify.search.length, 2, 'accepts x arguments');
t.end(); t.end();
}); });
}; };
@ -179,7 +179,7 @@ module.exports.tests.search = function(test, common) {
}; };
test('geojsonify.search()', function(t) { test('geojsonify.search()', function(t) {
var json = geojsonify.search( input ); var json = geojsonify.search( input, true );
t.deepEqual(json, expected, 'all docs mapped'); t.deepEqual(json, expected, 'all docs mapped');
t.end(); t.end();
}); });

7
test/unit/sanitiser/doc.js

@ -10,7 +10,7 @@ var doc = require('../../../sanitiser/doc'),
defaultMissingTypeError = function(input) { defaultMissingTypeError = function(input) {
var type = input.split(delimiter)[0]; var type = input.split(delimiter)[0];
return type + ' is invalid. It must be one of these values - [' + indeces.join(', ') + ']'; }, return type + ' is invalid. It must be one of these values - [' + indeces.join(', ') + ']'; },
defaultClean = { ids: [ { id: '123', type: 'geoname' } ] }, defaultClean = { ids: [ { id: '123', type: 'geoname' } ], details: false },
sanitize = function(query, cb) { _sanitize({'query':query}, cb); }, sanitize = function(query, cb) { _sanitize({'query':query}, cb); },
inputs = { inputs = {
valid: [ 'geoname:1', 'osmnode:2', 'admin0:53', 'osmway:44', 'geoname:5' ], valid: [ 'geoname:1', 'osmnode:2', 'admin0:53', 'osmway:44', 'geoname:5' ],
@ -56,7 +56,7 @@ module.exports.tests.sanitize_id = function(test, common) {
test('valid input', function(t) { test('valid input', function(t) {
inputs.valid.forEach( function( input ){ inputs.valid.forEach( function( input ){
var input_parts = input.split(delimiter); var input_parts = input.split(delimiter);
var expected = { ids: [ { id: input_parts[1], type: input_parts[0] } ] }; var expected = { ids: [ { id: input_parts[1], type: input_parts[0] } ], details: false };
sanitize({ id: input }, function( err, clean ){ sanitize({ id: input }, function( err, clean ){
t.equal(err, undefined, 'no error (' + input + ')' ); t.equal(err, undefined, 'no error (' + input + ')' );
t.deepEqual(clean, expected, 'clean set correctly (' + input + ')'); t.deepEqual(clean, expected, 'clean set correctly (' + input + ')');
@ -94,6 +94,7 @@ module.exports.tests.sanitize_ids = function(test, common) {
var input_parts = input.split(delimiter); var input_parts = input.split(delimiter);
expected.ids.push({ id: input_parts[1], type: input_parts[0] }); expected.ids.push({ id: input_parts[1], type: input_parts[0] });
}); });
expected.details = false;
sanitize({ id: inputs.valid }, function( err, clean ){ sanitize({ id: inputs.valid }, function( err, clean ){
t.equal(err, undefined, 'no error' ); t.equal(err, undefined, 'no error' );
t.deepEqual(clean, expected, 'clean set correctly'); t.deepEqual(clean, expected, 'clean set correctly');
@ -103,7 +104,7 @@ module.exports.tests.sanitize_ids = function(test, common) {
}; };
module.exports.tests.de_dupe = function(test, common) { module.exports.tests.de_dupe = function(test, common) {
var expected = { ids: [ { id: '1', type: 'geoname' }, { id: '2', type: 'osmnode' } ] }; var expected = { ids: [ { id: '1', type: 'geoname' }, { id: '2', type: 'osmnode' } ], details: false };
test('duplicate ids', function(t) { test('duplicate ids', function(t) {
sanitize( { id: ['geoname:1', 'osmnode:2', 'geoname:1'] }, function( err, clean ){ sanitize( { id: ['geoname:1', 'osmnode:2', 'geoname:1'] }, function( err, clean ){
t.equal(err, undefined, 'no error' ); t.equal(err, undefined, 'no error' );

3
test/unit/sanitiser/reverse.js

@ -8,7 +8,8 @@ var suggest = require('../../../sanitiser/reverse'),
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ], 'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
lon: 0, lon: 0,
size: 1 size: 1,
details: false
}, },
sanitize = function(query, cb) { _sanitize({'query':query}, cb); }; sanitize = function(query, cb) { _sanitize({'query':query}, cb); };

3
test/unit/sanitiser/search.js

@ -7,7 +7,8 @@ var search = require('../../../sanitiser/search'),
defaultClean = { input: 'test', defaultClean = { input: 'test',
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ], 'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
size: 10 size: 10,
details: false
}, },
sanitize = function(query, cb) { _sanitize({'query':query}, cb); }; sanitize = function(query, cb) { _sanitize({'query':query}, cb); };

3
test/unit/sanitiser/suggest.js

@ -9,7 +9,8 @@ var suggest = require('../../../sanitiser/suggest'),
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ], 'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
lon: 0, lon: 0,
size: 10 size: 10,
details: false
}, },
sanitize = function(query, cb) { _sanitize({'query':query}, cb); }; sanitize = function(query, cb) { _sanitize({'query':query}, cb); };

Loading…
Cancel
Save