Browse Source

Merge branch 'input-text'

pull/222/head
Julian Simioni 9 years ago
parent
commit
13d1a3861e
  1. 2
      docs/search/success.md
  2. 2
      docs/suggest/success.md
  3. 2
      docs/suggest/success_nearby.md
  4. 6
      helper/query_parser.js
  5. 4
      public/apiDoc.md
  6. 42
      query/search.js
  7. 6
      query/sort.js
  8. 11
      sanitiser/_text.js
  9. 2
      sanitiser/search.js
  10. 2
      test/ciao/search/success.coffee
  11. 2
      test/ciao/suggest/nearby_nobias.coffee
  12. 2
      test/ciao/suggest/success.coffee
  13. 2
      test/ciao/suggest/success_nearby.coffee
  14. 20
      test/unit/query/search.js
  15. 8
      test/unit/sanitiser/_text.js
  16. 40
      test/unit/sanitiser/reverse.js
  17. 112
      test/unit/sanitiser/search.js

2
docs/search/success.md

@ -8,7 +8,7 @@
"host": "localhost",
"method": "GET",
"port": 3100,
"path": "/search?input=lake&lat=29.49136&lon=-82.50622"
"path": "/search?text=lake&lat=29.49136&lon=-82.50622"
}
```

2
docs/suggest/success.md

@ -8,7 +8,7 @@
"host": "localhost",
"method": "GET",
"port": 3100,
"path": "/suggest?input=a&lat=0&lon=0"
"path": "/suggest?text=a&lat=0&lon=0"
}
```

2
docs/suggest/success_nearby.md

@ -8,7 +8,7 @@
"host": "localhost",
"method": "GET",
"port": 3100,
"path": "/suggest/nearby?input=a&lat=29.49136&lon=-82.50622"
"path": "/suggest/nearby?text=a&lat=29.49136&lon=-82.50622"
}
```

6
helper/query_parser.js

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

4
public/apiDoc.md

@ -3,7 +3,7 @@
The full text search endpoint that matches the name of a place to points on the planet.
#### Required Parameters
* `input`: the string to search for (eg `new york city` or `london`)
* `text`: the string to search for (eg `new york city` or `london`)
#### Optional Parameters
* `lat`, `lon`: the latitude/longitude coordinates to bias search results towards (may increase relevancy)
@ -43,7 +43,7 @@ The autocompletion endpoint that offers very fast response times; ideal for comp
results from around the provided lat/lon coordinates and also from precision level 1 and 3.
#### Required Parameters
* `input`: query string
* `text`: query string
* `lat`, `lon`: The latitude/longitude coordinates to bias results towards.
* `lat`/`lon` are currently **required** because of this [open issue](https://github.com/elasticsearch/elasticsearch/issues/6444)

42
query/search.js

@ -16,7 +16,7 @@ function generate( params ){
}
var query = queries.distance( centroid, { size: params.size } );
var input = params.input;
var text = params.text;
if (params.bbox) {
query = queries.bbox ( centroid, { size: params.size, bbox: params.bbox } );
@ -29,22 +29,22 @@ function generate( params ){
}
};
if (params.parsed_input) {
// update input
if (params.parsed_input.number && params.parsed_input.street) {
input = params.parsed_input.number + ' ' + params.parsed_input.street;
} else if (params.parsed_input.admin_parts) {
input = params.parsed_input.name;
if (params.parsed_text) {
// update text
if (params.parsed_text.number && params.parsed_text.street) {
text = params.parsed_text.number + ' ' + params.parsed_text.street;
} else if (params.parsed_text.admin_parts) {
text = params.parsed_text.name;
}
addParsedMatch(query, input, params.parsed_input);
addParsedMatch(query, text, params.parsed_text);
}
// add search condition to distance query
query.query.filtered.query.bool.must.push({
'match': {
'name.default': {
'query': input,
'query': text,
'analyzer': 'peliasOneEdgeGram'
}
}
@ -55,7 +55,7 @@ function generate( params ){
query.query.filtered.query.bool.should.push({
'match': {
'phrase.default': {
'query': input,
'query': text,
'analyzer': 'peliasPhrase',
'type': 'phrase',
'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.
*
* @param {Object} query
* @param {string} defaultInput
* @param {Object} parsedInput
* @param {string} defaultText
* @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 || [];
// copy expected admin fields so we can remove them as we parse the address
@ -84,23 +84,23 @@ function addParsedMatch(query, defaultInput, parsedInput) {
// address
// number, street, postalcode
addMatch(query, unmatchedAdminFields, 'address.number', parsedInput.number, addressWeights.number);
addMatch(query, unmatchedAdminFields, 'address.street', parsedInput.street, addressWeights.street);
addMatch(query, unmatchedAdminFields, 'address.zip', parsedInput.postalcode, addressWeights.zip);
addMatch(query, unmatchedAdminFields, 'address.number', parsedText.number, addressWeights.number);
addMatch(query, unmatchedAdminFields, 'address.street', parsedText.street, addressWeights.street);
addMatch(query, unmatchedAdminFields, 'address.zip', parsedText.postalcode, addressWeights.zip);
// city
// admin2, locality, local_admin, neighborhood
addMatch(query, unmatchedAdminFields, 'admin2', parsedInput.city, addressWeights.admin2);
addMatch(query, unmatchedAdminFields, 'admin2', parsedText.city, addressWeights.admin2);
// state
// admin1, admin1_abbr
addMatch(query, unmatchedAdminFields, 'admin1_abbr', parsedInput.state, addressWeights.admin1_abbr);
addMatch(query, unmatchedAdminFields, 'admin1_abbr', parsedText.state, addressWeights.admin1_abbr);
// country
// 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) {
if (params && params.hasOwnProperty('parsed_input') &&
(params.parsed_input.hasOwnProperty('number') ||
params.parsed_input.hasOwnProperty('street'))) {
if (params && params.hasOwnProperty('parsed_text') &&
(params.parsed_text.hasOwnProperty('number') ||
params.parsed_text.hasOwnProperty('street'))) {
return category_weights.address;
}
return category_weights.default;

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

@ -11,20 +11,19 @@ function sanitize( req ){
params = {};
}
// input text
if('string' !== typeof params.input || !params.input.length){
if('string' !== typeof params.text || !params.text.length){
return {
'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.from_address_parsing = query_parser.get_layers(params.input);
req.clean.types.from_address_parsing = query_parser.get_layers(params.text);
return { 'error': false };
}

2
sanitiser/search.js

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

2
test/ciao/search/success.coffee

@ -1,5 +1,5 @@
#> valid search query
path: '/v1/search?input=lake&lat=29.49136&lon=-82.50622'
path: '/v1/search?text=lake&lat=29.49136&lon=-82.50622'
#? 200 ok
response.statusCode.should.equal 200

2
test/ciao/suggest/nearby_nobias.coffee

@ -1,6 +1,6 @@
#> suggest without geo bias
path: '/v1/suggest/nearby?input=a'
path: '/v1/suggest/nearby?text=a'
#? 400 bad request
response.statusCode.should.equal 400

2
test/ciao/suggest/success.coffee

@ -1,6 +1,6 @@
#> valid suggest query
path: '/v1/suggest?input=a&lat=0&lon=0'
path: '/v1/suggest?text=a&lat=0&lon=0'
#? 200 ok
response.statusCode.should.equal 200

2
test/ciao/suggest/success_nearby.coffee

@ -1,6 +1,6 @@
#> valid suggest query
path: '/v1/suggest/nearby?input=a&lat=29.49136&lon=-82.50622'
path: '/v1/suggest/nearby?text=a&lat=29.49136&lon=-82.50622'
#? 200 ok
response.statusCode.should.equal 200

20
test/unit/query/search.js

@ -128,7 +128,7 @@ var expected = {
module.exports.tests.query = function(test, common) {
test('valid query', function(t) {
var query = generate({
input: 'test', size: 10,
text: 'test', size: 10,
lat: 29.49136, lon: -82.50622,
bbox: {
top: 47.47,
@ -145,7 +145,7 @@ module.exports.tests.query = function(test, common) {
test('valid query without lat/lon', function(t) {
var query = generate({
input: 'test', size: 10,
text: 'test', size: 10,
bbox: {
top: 47.47,
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) {
var query = generate({
input: 'test', size: 10,
text: 'test', size: 10,
layers: ['test']
});
@ -208,7 +208,7 @@ module.exports.tests.query = function(test, common) {
test('valid query without bbox', function(t) {
var query = generate({
input: 'test', size: 10,
text: 'test', size: 10,
lat: 29.49136, lon: -82.50622,
layers: ['test']
});
@ -269,12 +269,12 @@ module.exports.tests.query = function(test, common) {
test('valid query with a full valid address', function(t) {
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',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
size: 10,
details: true,
parsed_input: parser.get_parsed_address(address),
parsed_text: parser.get_parsed_address(address)
});
var expected = {
@ -470,12 +470,12 @@ module.exports.tests.query = function(test, common) {
test('valid query with partial address', function(t) {
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',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
size: 10,
details: true,
parsed_input: parser.get_parsed_address(partial_address),
parsed_text: parser.get_parsed_address(partial_address)
});
var expected = {
@ -637,12 +637,12 @@ module.exports.tests.query = function(test, common) {
test('valid query with regions in address', function(t) {
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',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
size: 10,
details: true,
parsed_input: parser.get_parsed_address(partial_address),
parsed_text: parser.get_parsed_address(partial_address)
});
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'),
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',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
nonAddressLayers = [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin' ],
defaultParsed= { },
defaultClean = { input: 'test',
defaultClean = { text: 'test',
layers: allLayers,
size: 10,
details: true,
parsed_input: defaultParsed,
parsed_text: defaultParsed,
lat:0,
lon:0
},

40
test/unit/sanitiser/reverse.js

@ -73,7 +73,7 @@ module.exports.tests.sanitize_lon = function(test, common) {
};
test('valid lon', function(t) {
lons.valid.forEach( function( lon ){
sanitize({ input: 'test', lat: 0, lon: lon }, function( err, clean ){
sanitize({ lat: 0, lon: lon }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.lon = parseFloat( lon );
t.equal(err, undefined, 'no error');
@ -96,19 +96,19 @@ module.exports.tests.sanitize_lon = function(test, common) {
module.exports.tests.sanitize_size = function(test, common) {
test('invalid size value', function(t) {
sanitize({ size: 'a', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ size: 'a', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.size, 10, 'default size set');
t.end();
});
});
test('below min size value', function(t) {
sanitize({ size: -100, input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ size: -100, lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.size, 1, 'min size set');
t.end();
});
});
test('above max size value', function(t) {
sanitize({ size: 9999, input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ size: 9999, lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.size, 40, 'max size set');
t.end();
});
@ -119,7 +119,7 @@ module.exports.tests.sanitize_details = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(details) {
test('invalid details param ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){
sanitize({ lat: 0, lon: 0, details: details }, function( err, clean ){
t.equal(clean.details, false, 'details set to false');
t.end();
});
@ -129,7 +129,7 @@ module.exports.tests.sanitize_details = function(test, common) {
var valid_values = [true, 'true', 1, '1', 'yes', 'y'];
valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){
sanitize({ lat: 0, lon: 0, details: details }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
t.end();
});
@ -137,7 +137,7 @@ module.exports.tests.sanitize_details = function(test, common) {
});
test('test default behavior', function(t) {
sanitize({ input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
t.end();
});
@ -146,7 +146,7 @@ module.exports.tests.sanitize_details = function(test, common) {
var valid_false_values = ['false', false, 0, '0', 'no', 'n'];
valid_false_values.forEach(function(details) {
test('test setting false explicitly ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){
sanitize({ lat: 0, lon: 0, details: details }, function( err, clean ){
t.equal(clean.details, false, 'details set to false');
t.end();
});
@ -156,13 +156,13 @@ module.exports.tests.sanitize_details = function(test, common) {
module.exports.tests.sanitize_layers = function(test, common) {
test('unspecified', function(t) {
sanitize({ layers: undefined, input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ layers: undefined, lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set');
t.end();
});
});
test('invalid layer', function(t) {
sanitize({ layers: 'test_layer', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ layers: 'test_layer', lat: 0, lon: 0 }, function( err, clean ){
var msg = 'invalid param \'layers\': must be one or more of ';
t.true(err.match(msg), 'invalid layer requested');
t.true(err.length > msg.length, 'invalid error message');
@ -171,21 +171,21 @@ module.exports.tests.sanitize_layers = function(test, common) {
});
test('poi (alias) layer', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ layers: 'poi', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers set');
t.end();
});
});
test('admin (alias) layer', function(t) {
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin'];
sanitize({ layers: 'admin', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ layers: 'admin', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.types.from_layers, admin_layers, 'admin layers set');
t.end();
});
});
test('address (alias) layer', function(t) {
var address_layers = ['osmaddress','openaddresses'];
sanitize({ layers: 'address', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ layers: 'address', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.types.from_layers, address_layers, 'address layers set');
t.end();
});
@ -193,7 +193,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
test('poi alias layer plus regular layers', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
var reg_layers = ['admin0', 'admin1'];
sanitize({ layers: 'poi,admin0,admin1', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ layers: 'poi,admin0,admin1', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.types.from_layers, reg_layers.concat(poi_layers), 'poi + regular layers');
t.end();
});
@ -201,7 +201,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
test('admin alias layer plus regular layers', function(t) {
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin'];
var reg_layers = ['geoname', 'osmway'];
sanitize({ layers: 'admin,geoname,osmway', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ layers: 'admin,geoname,osmway', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.types.from_layers, reg_layers.concat(admin_layers), 'admin + regular layers set');
t.end();
});
@ -209,21 +209,21 @@ module.exports.tests.sanitize_layers = function(test, common) {
test('address alias layer plus regular layers', function(t) {
var address_layers = ['osmaddress','openaddresses'];
var reg_layers = ['geoname', 'osmway'];
sanitize({ layers: 'address,geoname,osmway', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ layers: 'address,geoname,osmway', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.types.from_layers, reg_layers.concat(address_layers), 'address + regular layers set');
t.end();
});
});
test('alias layer plus regular layers (no duplicates)', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi,geoname,osmnode', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ layers: 'poi,geoname,osmnode', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers found (no duplicates)');
t.end();
});
});
test('multiple alias layers (no duplicates)', function(t) {
var alias_layers = ['geoname','osmnode','osmway','admin0','admin1','admin2','neighborhood','locality','local_admin'];
sanitize({ layers: 'poi,admin', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
sanitize({ layers: 'poi,admin', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.types.from_layers, alias_layers, 'all layers found (no duplicates)');
t.end();
});
@ -231,7 +231,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
};
module.exports.tests.sanitize_categories = function(test, common) {
var queryParams = { input: 'test', lat: 0, lon: 0 };
var queryParams = { lat: 0, lon: 0 };
test('unspecified', function(t) {
queryParams.categories = undefined;
sanitize(queryParams, function( err, clean ){
@ -284,7 +284,7 @@ module.exports.tests.middleware_failure = function(test, common) {
module.exports.tests.middleware_success = function(test, common) {
test('middleware success', function(t) {
var req = { query: { input: 'test', lat: 0, lon: 0 }};
var req = { query: { lat: 0, lon: 0 }};
var next = function( message ){
t.equal(message, undefined, 'no error message set');
t.deepEqual(req.clean, defaultClean);

112
test/unit/sanitiser/search.js

@ -1,18 +1,18 @@
var search = require('../../../sanitiser/search'),
_input = require('../sanitiser/_input'),
_text = require('../sanitiser/_text'),
parser = require('../../../helper/query_parser'),
defaultParsed = _input.defaultParsed,
defaultParsed = _text.defaultParsed,
_sanitize = search.sanitize,
middleware = search.middleware,
delim = ',',
defaultError = 'invalid param \'input\': text length, must be >0',
defaultClean = { input: 'test',
defaultError = 'invalid param \'text\': text length, must be >0',
defaultClean = { text: 'test',
types: {
},
size: 10,
details: true,
parsed_input: defaultParsed,
parsed_text: defaultParsed
},
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) {
test('invalid input', function(t) {
module.exports.tests.sanitize_invalid_text = function(test, common) {
test('invalid text', function(t) {
var invalid = [ '', 100, null, undefined, new Date() ];
invalid.forEach( function( input ){
sanitize({ input: input }, function( err, clean ){
t.equal(err, 'invalid param \'input\': text length, must be >0', input + ' is an invalid input');
invalid.forEach( function( text ){
sanitize({ text: text }, function( err, clean ){
t.equal(err, 'invalid param \'text\': text length, must be >0', text + ' is an invalid text');
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) {
test('valid short input', function(t) {
sanitize({ input: 'a' }, function( err, clean ){
module.exports.tests.sanitise_valid_text = function(test, common) {
test('valid short text', function(t) {
sanitize({ text: 'a' }, function( err, clean ){
t.equal(err, undefined, 'no error');
});
t.end();
});
test('valid not-quite-as-short input', function(t) {
sanitize({ input: 'aa' }, function( err, clean ){
test('valid not-quite-as-short text', function(t) {
sanitize({ text: 'aa' }, function( err, clean ){
t.equal(err, undefined, 'no error');
});
t.end();
});
test('valid longer input', function(t) {
sanitize({ input: 'aaaaaaaa' }, function( err, clean ){
test('valid longer text', function(t) {
sanitize({ text: 'aaaaaaaa' }, function( err, clean ){
t.equal(err, undefined, 'no error');
});
t.end();
});
};
module.exports.tests.sanitize_input_with_delim = function(test, common) {
var inputs = [ 'a,bcd', '123 main st, admin1', ',,,', ' ' ];
module.exports.tests.sanitize_text_with_delim = function(test, common) {
var texts = [ 'a,bcd', '123 main st, admin1', ',,,', ' ' ];
test('valid inputs with a comma', function(t) {
inputs.forEach( function( input ){
sanitize({ input: input }, function( err, clean ){
test('valid texts with a comma', function(t) {
texts.forEach( function( text ){
sanitize({ text: text }, function( err, clean ){
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(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) {
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(clean, undefined, 'clean not set');
});
@ -102,7 +102,7 @@ module.exports.tests.sanitize_lat = function(test, common) {
});
test('valid lat', function(t) {
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 ));
expected.lat = parseFloat( lat );
t.equal(err, undefined, 'no error');
@ -119,7 +119,7 @@ module.exports.tests.sanitize_lon = function(test, common) {
};
test('valid lon', function(t) {
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 ));
expected.lon = parseFloat( lon );
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) {
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(clean.lat, undefined, 'clean set without lat');
t.equal(clean.lon, undefined, 'clean set without lon');
@ -140,7 +140,7 @@ module.exports.tests.sanitize_optional_geo = function(test, common) {
t.end();
});
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;
t.equal(err, undefined, 'no error');
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();
});
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;
t.equal(err, undefined, 'no error');
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) {
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(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) {
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) {
return parseInt(i);
});
@ -216,19 +216,19 @@ module.exports.tests.sanitize_bbox = function(test, common) {
module.exports.tests.sanitize_zoom = function(test, common) {
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.end();
});
});
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.end();
});
});
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.end();
});
@ -237,19 +237,19 @@ module.exports.tests.sanitize_zoom = function(test, common) {
module.exports.tests.sanitize_size = function(test, common) {
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.end();
});
});
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.end();
});
});
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.end();
});
@ -260,7 +260,7 @@ module.exports.tests.sanitize_details = function(test, common) {
var invalid_values = [null, -1, 123, NaN, 'abc'];
invalid_values.forEach(function(details) {
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.end();
});
@ -270,7 +270,7 @@ module.exports.tests.sanitize_details = function(test, common) {
var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(details) {
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.end();
});
@ -280,7 +280,7 @@ module.exports.tests.sanitize_details = function(test, common) {
var valid_false_values = ['false', false, 0, '0', 'no', 'n'];
valid_false_values.forEach(function(details) {
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.end();
});
@ -288,7 +288,7 @@ module.exports.tests.sanitize_details = function(test, common) {
});
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.end();
});
@ -297,13 +297,13 @@ module.exports.tests.sanitize_details = function(test, common) {
module.exports.tests.sanitize_layers = function(test, common) {
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.end();
});
});
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 ';
t.true(err.match(msg), 'invalid layer requested');
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) {
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.end();
});
});
test('admin (alias) layer', function(t) {
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.end();
});
});
test('address (alias) layer', function(t) {
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_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();
});
});
test('poi alias layer plus regular layers', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
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.end();
});
@ -343,7 +343,7 @@ module.exports.tests.sanitize_layers = function(test, common) {
test('admin alias layer plus regular layers', function(t) {
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin'];
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.end();
});
@ -351,21 +351,21 @@ module.exports.tests.sanitize_layers = function(test, common) {
test('address alias layer plus regular layers', function(t) {
var address_layers = ['osmaddress','openaddresses'];
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.end();
});
});
test('alias layer plus regular layers (no duplicates)', function(t) {
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.end();
});
});
test('multiple alias layers (no duplicates)', function(t) {
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.end();
});
@ -373,7 +373,7 @@ module.exports.tests.sanitize_layers = 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 ){
t.equal(err, defaultError, 'handle invalid params gracefully');
t.end();
@ -396,7 +396,7 @@ module.exports.tests.middleware_failure = function(test, common) {
module.exports.tests.middleware_success = function(test, common) {
test('middleware success', function(t) {
var req = { query: { input: 'test' }};
var req = { query: { text: 'test' }};
var next = function( message ){
t.equal(message, undefined, 'no error message set');
t.end();

Loading…
Cancel
Save