Browse Source

Rename `/search?input`, replacing it with '/search?text' parameter instead

changing parameter name input to text
https://github.com/pelias/api/issues/172

Rebased by @orangejulius
pull/215/head
Harish Krishna 10 years ago committed by Julian Simioni
parent
commit
86424b8bbe
  1. 6
      helper/query_parser.js
  2. 42
      query/search.js
  3. 6
      query/sort.js
  4. 11
      sanitiser/_text.js
  5. 2
      sanitiser/search.js
  6. 20
      test/unit/query/search.js
  7. 8
      test/unit/sanitiser/_text.js
  8. 112
      test/unit/sanitiser/search.js

6
helper/query_parser.js

@ -62,13 +62,13 @@ module.exports.get_parsed_address = function get_parsed_address(query) {
'admin_parts' 'admin_parts'
]; ];
var parsed_input = {}; var parsed_text = {};
address_parts.forEach(function(part){ address_parts.forEach(function(part){
if (parsedAddress[part]) { if (parsedAddress[part]) {
parsed_input[part] = parsedAddress[part]; parsed_text[part] = parsedAddress[part];
} }
}); });
return parsed_input; return parsed_text;
}; };

42
query/search.js

@ -16,7 +16,7 @@ function generate( params ){
} }
var query = queries.distance( centroid, { size: params.size } ); var query = queries.distance( centroid, { size: params.size } );
var input = params.input; var text = params.text;
if (params.bbox) { if (params.bbox) {
query = queries.bbox ( centroid, { size: params.size, bbox: params.bbox } ); query = queries.bbox ( centroid, { size: params.size, bbox: params.bbox } );
@ -29,22 +29,22 @@ function generate( params ){
} }
}; };
if (params.parsed_input) { if (params.parsed_text) {
// update input // update text
if (params.parsed_input.number && params.parsed_input.street) { if (params.parsed_text.number && params.parsed_text.street) {
input = params.parsed_input.number + ' ' + params.parsed_input.street; text = params.parsed_text.number + ' ' + params.parsed_text.street;
} else if (params.parsed_input.admin_parts) { } else if (params.parsed_text.admin_parts) {
input = params.parsed_input.name; text = params.parsed_text.name;
} }
addParsedMatch(query, input, params.parsed_input); addParsedMatch(query, text, params.parsed_text);
} }
// add search condition to distance query // add search condition to distance query
query.query.filtered.query.bool.must.push({ query.query.filtered.query.bool.must.push({
'match': { 'match': {
'name.default': { 'name.default': {
'query': input, 'query': text,
'analyzer': 'peliasOneEdgeGram' 'analyzer': 'peliasOneEdgeGram'
} }
} }
@ -55,7 +55,7 @@ function generate( params ){
query.query.filtered.query.bool.should.push({ query.query.filtered.query.bool.should.push({
'match': { 'match': {
'phrase.default': { 'phrase.default': {
'query': input, 'query': text,
'analyzer': 'peliasPhrase', 'analyzer': 'peliasPhrase',
'type': 'phrase', 'type': 'phrase',
'slop': 2 'slop': 2
@ -69,14 +69,14 @@ function generate( params ){
} }
/** /**
* Traverse the parsed input object, containing all the address parts detected in query string. * Traverse the parsed text object, containing all the address parts detected in query string.
* Add matches to query for each identifiable component. * Add matches to query for each identifiable component.
* *
* @param {Object} query * @param {Object} query
* @param {string} defaultInput * @param {string} defaultText
* @param {Object} parsedInput * @param {Object} parsedText
*/ */
function addParsedMatch(query, defaultInput, parsedInput) { function addParsedMatch(query, defaultText, parsedText) {
query.query.filtered.query.bool.should = query.query.filtered.query.bool.should || []; query.query.filtered.query.bool.should = query.query.filtered.query.bool.should || [];
// copy expected admin fields so we can remove them as we parse the address // copy expected admin fields so we can remove them as we parse the address
@ -84,23 +84,23 @@ function addParsedMatch(query, defaultInput, parsedInput) {
// address // address
// number, street, postalcode // number, street, postalcode
addMatch(query, unmatchedAdminFields, 'address.number', parsedInput.number, addressWeights.number); addMatch(query, unmatchedAdminFields, 'address.number', parsedText.number, addressWeights.number);
addMatch(query, unmatchedAdminFields, 'address.street', parsedInput.street, addressWeights.street); addMatch(query, unmatchedAdminFields, 'address.street', parsedText.street, addressWeights.street);
addMatch(query, unmatchedAdminFields, 'address.zip', parsedInput.postalcode, addressWeights.zip); addMatch(query, unmatchedAdminFields, 'address.zip', parsedText.postalcode, addressWeights.zip);
// city // city
// admin2, locality, local_admin, neighborhood // admin2, locality, local_admin, neighborhood
addMatch(query, unmatchedAdminFields, 'admin2', parsedInput.city, addressWeights.admin2); addMatch(query, unmatchedAdminFields, 'admin2', parsedText.city, addressWeights.admin2);
// state // state
// admin1, admin1_abbr // admin1, admin1_abbr
addMatch(query, unmatchedAdminFields, 'admin1_abbr', parsedInput.state, addressWeights.admin1_abbr); addMatch(query, unmatchedAdminFields, 'admin1_abbr', parsedText.state, addressWeights.admin1_abbr);
// country // country
// admin0, alpha3 // admin0, alpha3
addMatch(query, unmatchedAdminFields, 'alpha3', parsedInput.country, addressWeights.alpha3); addMatch(query, unmatchedAdminFields, 'alpha3', parsedText.country, addressWeights.alpha3);
addUnmatchedAdminFieldsToQuery(query, unmatchedAdminFields, parsedInput, defaultInput); addUnmatchedAdminFieldsToQuery(query, unmatchedAdminFields, parsedText, defaultText);
} }
/** /**

6
query/sort.js

@ -66,9 +66,9 @@ module.exports = function( params ){
}; };
function getCategoryWeights(params) { function getCategoryWeights(params) {
if (params && params.hasOwnProperty('parsed_input') && if (params && params.hasOwnProperty('parsed_text') &&
(params.parsed_input.hasOwnProperty('number') || (params.parsed_text.hasOwnProperty('number') ||
params.parsed_input.hasOwnProperty('street'))) { params.parsed_text.hasOwnProperty('street'))) {
return category_weights.address; return category_weights.address;
} }
return category_weights.default; return category_weights.default;

11
sanitiser/_input.js → sanitiser/_text.js

@ -11,20 +11,19 @@ function sanitize( req ){
params = {}; params = {};
} }
// input text if('string' !== typeof params.text || !params.text.length){
if('string' !== typeof params.input || !params.input.length){
return { return {
'error': true, 'error': true,
'message': 'invalid param \'input\': text length, must be >0' 'message': 'invalid param \'text\': text length, must be >0'
}; };
} }
req.clean.input = params.input; req.clean.text = params.text;
req.clean.parsed_input = query_parser.get_parsed_address(params.input); req.clean.parsed_text = query_parser.get_parsed_address(params.text);
req.clean.types = req.clean.layers || {}; req.clean.types = req.clean.layers || {};
req.clean.types.from_address_parsing = query_parser.get_layers(params.input); req.clean.types.from_address_parsing = query_parser.get_layers(params.text);
return { 'error': false }; return { 'error': false };
} }

2
sanitiser/search.js

@ -1,7 +1,7 @@
var _sanitize = require('../sanitiser/_sanitize'), var _sanitize = require('../sanitiser/_sanitize'),
sanitizers = { sanitizers = {
input: require('../sanitiser/_input'), text: require('../sanitiser/_text'),
size: require('../sanitiser/_size'), size: require('../sanitiser/_size'),
layers: require('../sanitiser/_layers'), layers: require('../sanitiser/_layers'),
source: require('../sanitiser/_source'), source: require('../sanitiser/_source'),

20
test/unit/query/search.js

@ -128,7 +128,7 @@ var expected = {
module.exports.tests.query = function(test, common) { module.exports.tests.query = function(test, common) {
test('valid query', function(t) { test('valid query', function(t) {
var query = generate({ var query = generate({
input: 'test', size: 10, text: 'test', size: 10,
lat: 29.49136, lon: -82.50622, lat: 29.49136, lon: -82.50622,
bbox: { bbox: {
top: 47.47, top: 47.47,
@ -145,7 +145,7 @@ module.exports.tests.query = function(test, common) {
test('valid query without lat/lon', function(t) { test('valid query without lat/lon', function(t) {
var query = generate({ var query = generate({
input: 'test', size: 10, text: 'test', size: 10,
bbox: { bbox: {
top: 47.47, top: 47.47,
right: -61.84, right: -61.84,
@ -161,7 +161,7 @@ module.exports.tests.query = function(test, common) {
test('valid query with no lat/lon and no bbox', function(t) { test('valid query with no lat/lon and no bbox', function(t) {
var query = generate({ var query = generate({
input: 'test', size: 10, text: 'test', size: 10,
layers: ['test'] layers: ['test']
}); });
@ -208,7 +208,7 @@ module.exports.tests.query = function(test, common) {
test('valid query without bbox', function(t) { test('valid query without bbox', function(t) {
var query = generate({ var query = generate({
input: 'test', size: 10, text: 'test', size: 10,
lat: 29.49136, lon: -82.50622, lat: 29.49136, lon: -82.50622,
layers: ['test'] layers: ['test']
}); });
@ -269,12 +269,12 @@ module.exports.tests.query = function(test, common) {
test('valid query with a full valid address', function(t) { test('valid query with a full valid address', function(t) {
var address = '123 main st new york ny 10010 US'; var address = '123 main st new york ny 10010 US';
var query = generate({ input: address, var query = generate({ text: address,
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: true, details: true,
parsed_input: parser.get_parsed_address(address), parsed_text: parser.get_parsed_address(address)
}); });
var expected = { var expected = {
@ -470,12 +470,12 @@ module.exports.tests.query = function(test, common) {
test('valid query with partial address', function(t) { test('valid query with partial address', function(t) {
var partial_address = 'soho grand, new york'; var partial_address = 'soho grand, new york';
var query = generate({ input: partial_address, var query = generate({ text: partial_address,
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: true, details: true,
parsed_input: parser.get_parsed_address(partial_address), parsed_text: parser.get_parsed_address(partial_address)
}); });
var expected = { var expected = {
@ -637,12 +637,12 @@ module.exports.tests.query = function(test, common) {
test('valid query with regions in address', function(t) { test('valid query with regions in address', function(t) {
var partial_address = '1 water st manhattan ny'; var partial_address = '1 water st manhattan ny';
var query = generate({ input: partial_address, var query = generate({ text: partial_address,
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: true, details: true,
parsed_input: parser.get_parsed_address(partial_address), parsed_text: parser.get_parsed_address(partial_address)
}); });
var expected = { var expected = {

8
test/unit/sanitiser/_input.js → test/unit/sanitiser/_text.js

@ -1,18 +1,18 @@
var input = require('../../../sanitiser/_input'), var text = require('../../../sanitiser/_text'),
parser = require('../../../helper/query_parser'), parser = require('../../../helper/query_parser'),
delim = ',', delim = ',',
defaultError = 'invalid param \'input\': text length, must be >0', defaultError = 'invalid param \'text\': text length, must be >0',
allLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', allLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ], 'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
nonAddressLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', nonAddressLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin' ], 'locality', 'local_admin' ],
defaultParsed= { }, defaultParsed= { },
defaultClean = { input: 'test', defaultClean = { text: 'test',
layers: allLayers, layers: allLayers,
size: 10, size: 10,
details: true, details: true,
parsed_input: defaultParsed, parsed_text: defaultParsed,
lat:0, lat:0,
lon:0 lon:0
}, },

112
test/unit/sanitiser/search.js

@ -1,18 +1,18 @@
var search = require('../../../sanitiser/search'), var search = require('../../../sanitiser/search'),
_input = require('../sanitiser/_input'), _text = require('../sanitiser/_text'),
parser = require('../../../helper/query_parser'), parser = require('../../../helper/query_parser'),
defaultParsed = _input.defaultParsed, defaultParsed = _text.defaultParsed,
_sanitize = search.sanitize, _sanitize = search.sanitize,
middleware = search.middleware, middleware = search.middleware,
delim = ',', delim = ',',
defaultError = 'invalid param \'input\': text length, must be >0', defaultError = 'invalid param \'text\': text length, must be >0',
defaultClean = { input: 'test', defaultClean = { text: 'test',
types: { types: {
}, },
size: 10, size: 10,
details: true, details: true,
parsed_input: defaultParsed, parsed_text: defaultParsed
}, },
sanitize = function(query, cb) { _sanitize({'query':query}, cb); }; sanitize = function(query, cb) { _sanitize({'query':query}, cb); };
@ -31,12 +31,12 @@ module.exports.tests.interface = function(test, common) {
}); });
}; };
module.exports.tests.sanitize_invalid_input = function(test, common) { module.exports.tests.sanitize_invalid_text = function(test, common) {
test('invalid input', function(t) { test('invalid text', function(t) {
var invalid = [ '', 100, null, undefined, new Date() ]; var invalid = [ '', 100, null, undefined, new Date() ];
invalid.forEach( function( input ){ invalid.forEach( function( text ){
sanitize({ input: input }, function( err, clean ){ sanitize({ text: text }, function( err, clean ){
t.equal(err, 'invalid param \'input\': text length, must be >0', input + ' is an invalid input'); t.equal(err, 'invalid param \'text\': text length, must be >0', text + ' is an invalid text');
t.equal(clean, undefined, 'clean not set'); t.equal(clean, undefined, 'clean not set');
}); });
}); });
@ -44,41 +44,41 @@ module.exports.tests.sanitize_invalid_input = function(test, common) {
}); });
}; };
module.exports.tests.sanitise_valid_input = function(test, common) { module.exports.tests.sanitise_valid_text = function(test, common) {
test('valid short input', function(t) { test('valid short text', function(t) {
sanitize({ input: 'a' }, function( err, clean ){ sanitize({ text: 'a' }, function( err, clean ){
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
}); });
t.end(); t.end();
}); });
test('valid not-quite-as-short input', function(t) { test('valid not-quite-as-short text', function(t) {
sanitize({ input: 'aa' }, function( err, clean ){ sanitize({ text: 'aa' }, function( err, clean ){
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
}); });
t.end(); t.end();
}); });
test('valid longer input', function(t) { test('valid longer text', function(t) {
sanitize({ input: 'aaaaaaaa' }, function( err, clean ){ sanitize({ text: 'aaaaaaaa' }, function( err, clean ){
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
}); });
t.end(); t.end();
}); });
}; };
module.exports.tests.sanitize_input_with_delim = function(test, common) { module.exports.tests.sanitize_text_with_delim = function(test, common) {
var inputs = [ 'a,bcd', '123 main st, admin1', ',,,', ' ' ]; var texts = [ 'a,bcd', '123 main st, admin1', ',,,', ' ' ];
test('valid inputs with a comma', function(t) { test('valid texts with a comma', function(t) {
inputs.forEach( function( input ){ texts.forEach( function( text ){
sanitize({ input: input }, function( err, clean ){ sanitize({ text: text }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean )); var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.input = input; expected.text = text;
expected.parsed_input = parser.get_parsed_address(input); expected.parsed_text = parser.get_parsed_address(text);
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
t.equal(clean.parsed_input.name, expected.parsed_input.name, 'clean name set correctly'); t.equal(clean.parsed_text.name, expected.parsed_text.name, 'clean name set correctly');
}); });
}); });
@ -93,7 +93,7 @@ module.exports.tests.sanitize_lat = function(test, common) {
}; };
test('invalid lat', function(t) { test('invalid lat', function(t) {
lats.invalid.forEach( function( lat ){ lats.invalid.forEach( function( lat ){
sanitize({ input: 'test', lat: lat, lon: 0 }, function( err, clean ){ sanitize({ text: 'test', lat: lat, lon: 0 }, function( err, clean ){
t.equal(err, 'invalid param \'lat\': must be >-90 and <90', lat + ' is an invalid latitude'); t.equal(err, 'invalid param \'lat\': must be >-90 and <90', lat + ' is an invalid latitude');
t.equal(clean, undefined, 'clean not set'); t.equal(clean, undefined, 'clean not set');
}); });
@ -102,7 +102,7 @@ module.exports.tests.sanitize_lat = function(test, common) {
}); });
test('valid lat', function(t) { test('valid lat', function(t) {
lats.valid.forEach( function( lat ){ lats.valid.forEach( function( lat ){
sanitize({ input: 'test', lat: lat, lon: 0 }, function( err, clean ){ sanitize({ text: 'test', lat: lat, lon: 0 }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean )); var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.lat = parseFloat( lat ); expected.lat = parseFloat( lat );
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
@ -119,7 +119,7 @@ module.exports.tests.sanitize_lon = function(test, common) {
}; };
test('valid lon', function(t) { test('valid lon', function(t) {
lons.valid.forEach( function( lon ){ lons.valid.forEach( function( lon ){
sanitize({ input: 'test', lat: 0, lon: lon }, function( err, clean ){ sanitize({ text: 'test', lat: 0, lon: lon }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean )); var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.lon = parseFloat( lon ); expected.lon = parseFloat( lon );
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
@ -132,7 +132,7 @@ module.exports.tests.sanitize_lon = function(test, common) {
module.exports.tests.sanitize_optional_geo = function(test, common) { module.exports.tests.sanitize_optional_geo = function(test, common) {
test('no lat/lon', function(t) { test('no lat/lon', function(t) {
sanitize({ input: 'test' }, function( err, clean ){ sanitize({ text: 'test' }, function( err, clean ){
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
t.equal(clean.lat, undefined, 'clean set without lat'); t.equal(clean.lat, undefined, 'clean set without lat');
t.equal(clean.lon, undefined, 'clean set without lon'); t.equal(clean.lon, undefined, 'clean set without lon');
@ -140,7 +140,7 @@ module.exports.tests.sanitize_optional_geo = function(test, common) {
t.end(); t.end();
}); });
test('no lat', function(t) { test('no lat', function(t) {
sanitize({ input: 'test', lon: 0 }, function( err, clean ){ sanitize({ text: 'test', lon: 0 }, function( err, clean ){
var expected_lon = 0; var expected_lon = 0;
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
t.deepEqual(clean.lon, expected_lon, 'clean set correctly (without any lat)'); t.deepEqual(clean.lon, expected_lon, 'clean set correctly (without any lat)');
@ -148,7 +148,7 @@ module.exports.tests.sanitize_optional_geo = function(test, common) {
t.end(); t.end();
}); });
test('no lon', function(t) { test('no lon', function(t) {
sanitize({ input: 'test', lat: 0 }, function( err, clean ){ sanitize({ text: 'test', lat: 0 }, function( err, clean ){
var expected_lat = 0; var expected_lat = 0;
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
t.deepEqual(clean.lat, expected_lat, 'clean set correctly (without any lon)'); t.deepEqual(clean.lat, expected_lat, 'clean set correctly (without any lon)');
@ -187,7 +187,7 @@ module.exports.tests.sanitize_bbox = function(test, common) {
}; };
test('invalid bbox', function(t) { test('invalid bbox', function(t) {
bboxes.invalid.forEach( function( bbox ){ bboxes.invalid.forEach( function( bbox ){
sanitize({ input: 'test', bbox: bbox }, function( err, clean ){ sanitize({ text: 'test', bbox: bbox }, function( err, clean ){
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
t.equal(clean.bbox, undefined, 'falling back on 50km distance from centroid'); t.equal(clean.bbox, undefined, 'falling back on 50km distance from centroid');
}); });
@ -196,7 +196,7 @@ module.exports.tests.sanitize_bbox = function(test, common) {
}); });
test('valid bbox', function(t) { test('valid bbox', function(t) {
bboxes.valid.forEach( function( bbox ){ bboxes.valid.forEach( function( bbox ){
sanitize({ input: 'test', bbox: bbox }, function( err, clean ){ sanitize({ text: 'test', bbox: bbox }, function( err, clean ){
var bboxArray = bbox.split(',').map(function(i) { var bboxArray = bbox.split(',').map(function(i) {
return parseInt(i); return parseInt(i);
}); });
@ -216,19 +216,19 @@ module.exports.tests.sanitize_bbox = function(test, common) {
module.exports.tests.sanitize_zoom = function(test, common) { module.exports.tests.sanitize_zoom = function(test, common) {
test('invalid zoom value', function(t) { test('invalid zoom value', function(t) {
sanitize({ zoom: 'a', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ sanitize({ zoom: 'a', text: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.zoom, undefined, 'zoom not set'); t.equal(clean.zoom, undefined, 'zoom not set');
t.end(); t.end();
}); });
}); });
test('below min zoom value', function(t) { test('below min zoom value', function(t) {
sanitize({ zoom: -100, input: 'test', lat: 0, lon: 0 }, function( err, clean ){ sanitize({ zoom: -100, text: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.zoom, 1, 'min zoom set'); t.equal(clean.zoom, 1, 'min zoom set');
t.end(); t.end();
}); });
}); });
test('above max zoom value', function(t) { test('above max zoom value', function(t) {
sanitize({ zoom: 9999, input: 'test', lat: 0, lon: 0 }, function( err, clean ){ sanitize({ zoom: 9999, text: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.zoom, 18, 'max zoom set'); t.equal(clean.zoom, 18, 'max zoom set');
t.end(); t.end();
}); });
@ -237,19 +237,19 @@ module.exports.tests.sanitize_zoom = function(test, common) {
module.exports.tests.sanitize_size = function(test, common) { module.exports.tests.sanitize_size = function(test, common) {
test('invalid size value', function(t) { test('invalid size value', function(t) {
sanitize({ size: 'a', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ sanitize({ size: 'a', text: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.size, 10, 'default size set'); t.equal(clean.size, 10, 'default size set');
t.end(); t.end();
}); });
}); });
test('below min size value', function(t) { test('below min size value', function(t) {
sanitize({ size: -100, input: 'test', lat: 0, lon: 0 }, function( err, clean ){ sanitize({ size: -100, text: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.size, 1, 'min size set'); t.equal(clean.size, 1, 'min size set');
t.end(); t.end();
}); });
}); });
test('above max size value', function(t) { test('above max size value', function(t) {
sanitize({ size: 9999, input: 'test', lat: 0, lon: 0 }, function( err, clean ){ sanitize({ size: 9999, text: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.size, 40, 'max size set'); t.equal(clean.size, 40, 'max size set');
t.end(); t.end();
}); });
@ -260,7 +260,7 @@ module.exports.tests.sanitize_details = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc']; var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(details) { invalid_values.forEach(function(details) {
test('invalid details param ' + details, function(t) { test('invalid details param ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ sanitize({ text: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){
t.equal(clean.details, false, 'default details set (to false)'); t.equal(clean.details, false, 'default details set (to false)');
t.end(); t.end();
}); });
@ -270,7 +270,7 @@ module.exports.tests.sanitize_details = function(test, common) {
var valid_values = ['true', true, 1, '1', 'yes', 'y']; var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(details) { valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) { test('valid details param ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ sanitize({ text: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){
t.equal(clean.details, true, 'details set to true'); t.equal(clean.details, true, 'details set to true');
t.end(); t.end();
}); });
@ -280,7 +280,7 @@ module.exports.tests.sanitize_details = function(test, common) {
var valid_false_values = ['false', false, 0, '0', 'no', 'n']; var valid_false_values = ['false', false, 0, '0', 'no', 'n'];
valid_false_values.forEach(function(details) { valid_false_values.forEach(function(details) {
test('test setting false explicitly ' + details, function(t) { test('test setting false explicitly ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ sanitize({ text: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){
t.equal(clean.details, false, 'details set to false'); t.equal(clean.details, false, 'details set to false');
t.end(); t.end();
}); });
@ -288,7 +288,7 @@ module.exports.tests.sanitize_details = function(test, common) {
}); });
test('test default behavior', function(t) { test('test default behavior', function(t) {
sanitize({ input: 'test', lat: 0, lon: 0 }, function( err, clean ){ sanitize({ text: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.details, true, 'details set to true'); t.equal(clean.details, true, 'details set to true');
t.end(); t.end();
}); });
@ -297,13 +297,13 @@ module.exports.tests.sanitize_details = function(test, common) {
module.exports.tests.sanitize_layers = function(test, common) { module.exports.tests.sanitize_layers = function(test, common) {
test('unspecified', function(t) { test('unspecified', function(t) {
sanitize({ layers: undefined, input: 'test' }, function( err, clean ){ sanitize({ layers: undefined, text: 'test' }, function( err, clean ){
t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set'); t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set');
t.end(); t.end();
}); });
}); });
test('invalid layer', function(t) { test('invalid layer', function(t) {
sanitize({ layers: 'test_layer', input: 'test' }, function( err, clean ){ sanitize({ layers: 'test_layer', text: 'test' }, function( err, clean ){
var msg = 'invalid param \'layers\': must be one or more of '; var msg = 'invalid param \'layers\': must be one or more of ';
t.true(err.match(msg), 'invalid layer requested'); t.true(err.match(msg), 'invalid layer requested');
t.true(err.length > msg.length, 'invalid error message'); t.true(err.length > msg.length, 'invalid error message');
@ -312,30 +312,30 @@ module.exports.tests.sanitize_layers = function(test, common) {
}); });
test('poi (alias) layer', function(t) { test('poi (alias) layer', function(t) {
var poi_layers = ['geoname','osmnode','osmway']; var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi', input: 'test' }, function( err, clean ){ sanitize({ layers: 'poi', text: 'test' }, function( err, clean ){
t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers set'); t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers set');
t.end(); t.end();
}); });
}); });
test('admin (alias) layer', function(t) { test('admin (alias) layer', function(t) {
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin'];
sanitize({ layers: 'admin', input: 'test' }, function( err, clean ){ sanitize({ layers: 'admin', text: 'test' }, function( err, clean ){
t.deepEqual(clean.types.from_layers, admin_layers, 'admin layers set'); t.deepEqual(clean.types.from_layers, admin_layers, 'admin layers set');
t.end(); t.end();
}); });
}); });
test('address (alias) layer', function(t) { test('address (alias) layer', function(t) {
var address_layers = ['osmaddress','openaddresses']; var address_layers = ['osmaddress','openaddresses'];
sanitize({ layers: 'address', input: 'test' }, function( err, clean ){ sanitize({ layers: 'address', text: 'test' }, function( err, clean ){
t.deepEqual(clean.types.from_layers, address_layers, 'types from layers set'); t.deepEqual(clean.types.from_layers, address_layers, 'types from layers set');
t.deepEqual(clean.types.from_address_parser, _input.allLayers, 'address parser uses default layers'); t.deepEqual(clean.types.from_address_parser, _text.allLayers, 'address parser uses default layers');
t.end(); t.end();
}); });
}); });
test('poi alias layer plus regular layers', function(t) { test('poi alias layer plus regular layers', function(t) {
var poi_layers = ['geoname','osmnode','osmway']; var poi_layers = ['geoname','osmnode','osmway'];
var reg_layers = ['admin0', 'admin1']; var reg_layers = ['admin0', 'admin1'];
sanitize({ layers: 'poi,admin0,admin1', input: 'test' }, function( err, clean ){ sanitize({ layers: 'poi,admin0,admin1', text: 'test' }, function( err, clean ){
t.deepEqual(clean.types.from_layers, reg_layers.concat(poi_layers), 'poi + regular layers'); t.deepEqual(clean.types.from_layers, reg_layers.concat(poi_layers), 'poi + regular layers');
t.end(); t.end();
}); });
@ -343,7 +343,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
test('admin alias layer plus regular layers', function(t) { test('admin alias layer plus regular layers', function(t) {
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin'];
var reg_layers = ['geoname', 'osmway']; var reg_layers = ['geoname', 'osmway'];
sanitize({ layers: 'admin,geoname,osmway', input: 'test' }, function( err, clean ){ sanitize({ layers: 'admin,geoname,osmway', text: 'test' }, function( err, clean ){
t.deepEqual(clean.types.from_layers, reg_layers.concat(admin_layers), 'admin + regular layers set'); t.deepEqual(clean.types.from_layers, reg_layers.concat(admin_layers), 'admin + regular layers set');
t.end(); t.end();
}); });
@ -351,21 +351,21 @@ module.exports.tests.sanitize_layers = function(test, common) {
test('address alias layer plus regular layers', function(t) { test('address alias layer plus regular layers', function(t) {
var address_layers = ['osmaddress','openaddresses']; var address_layers = ['osmaddress','openaddresses'];
var reg_layers = ['geoname', 'osmway']; var reg_layers = ['geoname', 'osmway'];
sanitize({ layers: 'address,geoname,osmway', input: 'test' }, function( err, clean ){ sanitize({ layers: 'address,geoname,osmway', text: 'test' }, function( err, clean ){
t.deepEqual(clean.types.from_layers, reg_layers.concat(address_layers), 'address + regular layers set'); t.deepEqual(clean.types.from_layers, reg_layers.concat(address_layers), 'address + regular layers set');
t.end(); t.end();
}); });
}); });
test('alias layer plus regular layers (no duplicates)', function(t) { test('alias layer plus regular layers (no duplicates)', function(t) {
var poi_layers = ['geoname','osmnode','osmway']; var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi,geoname,osmnode', input: 'test' }, function( err, clean ){ sanitize({ layers: 'poi,geoname,osmnode', text: 'test' }, function( err, clean ){
t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers found (no duplicates)'); t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers found (no duplicates)');
t.end(); t.end();
}); });
}); });
test('multiple alias layers (no duplicates)', function(t) { test('multiple alias layers (no duplicates)', function(t) {
var alias_layers = ['geoname','osmnode','osmway','admin0','admin1','admin2','neighborhood','locality','local_admin']; var alias_layers = ['geoname','osmnode','osmway','admin0','admin1','admin2','neighborhood','locality','local_admin'];
sanitize({ layers: 'poi,admin', input: 'test' }, function( err, clean ){ sanitize({ layers: 'poi,admin', text: 'test' }, function( err, clean ){
t.deepEqual(clean.types.from_layers, alias_layers, 'all layers found (no duplicates)'); t.deepEqual(clean.types.from_layers, alias_layers, 'all layers found (no duplicates)');
t.end(); t.end();
}); });
@ -373,7 +373,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
}; };
module.exports.tests.invalid_params = function(test, common) { module.exports.tests.invalid_params = function(test, common) {
test('invalid input params', function(t) { test('invalid text params', function(t) {
sanitize( undefined, function( err, clean ){ sanitize( undefined, function( err, clean ){
t.equal(err, defaultError, 'handle invalid params gracefully'); t.equal(err, defaultError, 'handle invalid params gracefully');
t.end(); t.end();
@ -396,7 +396,7 @@ module.exports.tests.middleware_failure = function(test, common) {
module.exports.tests.middleware_success = function(test, common) { module.exports.tests.middleware_success = function(test, common) {
test('middleware success', function(t) { test('middleware success', function(t) {
var req = { query: { input: 'test' }}; var req = { query: { text: 'test' }};
var next = function( message ){ var next = function( message ){
t.equal(message, undefined, 'no error message set'); t.equal(message, undefined, 'no error message set');
t.end(); t.end();

Loading…
Cancel
Save