Browse Source

resolve merge conflict with master

pull/229/head
Peter Johnson 9 years ago
parent
commit
7e7e7083c2
  1. 2
      docs/search/success.md
  2. 2
      docs/suggest/success.md
  3. 2
      docs/suggest/success_nearby.md
  4. 3
      helper/query_parser.js
  5. 4
      public/apiDoc.md
  6. 2
      test/ciao/search/success.coffee
  7. 2
      test/ciao/suggest/nearby_nobias.coffee
  8. 2
      test/ciao/suggest/success.coffee
  9. 2
      test/ciao/suggest/success_nearby.coffee
  10. 3
      test/unit/helper/query_parser.js
  11. 40
      test/unit/sanitiser/reverse.js
  12. 391
      test/unit/sanitiser/suggest.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"
}
```

3
helper/query_parser.js

@ -13,9 +13,6 @@ module.exports.get_layers = function get_layers(query) {
if (query.length <= 3 ) {
// no address parsing required
return get_layers_helper(['admin']);
} else if (tokenized.length === 1 || (tokenized.length < 3 && !hasNumber)) {
// no need to hit address layers if there's only one (or two) token(s)
return get_layers_helper(['admin', 'poi']);
}
};

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)

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

3
test/unit/helper/query_parser.js

@ -63,6 +63,7 @@ module.exports.tests.parse_one_or_more_tokens = function(test, common) {
var two_tokens_nonum = ['small town', 'biggg city', 'another empire'];
var two_tokens_withnum= ['123 main', 'sixty 1', '123-980 house'];
// parse address is now always true to fix pelias/api#194
var testParse = function(query, parse_address) {
test('query with one or more tokens (' + query + ')', function(t) {
var address = parser.get_parsed_address(query);
@ -83,7 +84,7 @@ module.exports.tests.parse_one_or_more_tokens = function(test, common) {
var queries = one_token_queries.concat(two_tokens_nonum);
for (var key in queries) {
testParse( queries[key] );
testParse( queries[key], true );
}
for (key in two_tokens_withnum) {
testParse( two_tokens_withnum[key], true );

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);

391
test/unit/sanitiser/suggest.js

@ -1,391 +0,0 @@
var suggest = require('../../../sanitiser/suggest'),
_sanitize = suggest.sanitize,
middleware = suggest.middleware,
_input = require('../sanitiser/_input'),
parser = require('../../../helper/query_parser'),
defaultParsed = _input.defaultParsed,
delim = ',',
defaultError = 'invalid param \'input\': text length, must be >0',
defaultClean = { input: 'test',
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'locality', 'local_admin', 'osmaddress', 'openaddresses' ],
size: 10,
details: true,
lat:0,
lon:0,
parsed_input: defaultParsed,
},
sanitize = function(query, cb) { _sanitize({'query':query}, cb); };
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('sanitize interface', function(t) {
t.equal(typeof sanitize, 'function', 'sanitize is a function');
t.equal(sanitize.length, 2, 'sanitize interface');
t.end();
});
test('middleware interface', function(t) {
t.equal(typeof middleware, 'function', 'middleware is a function');
t.equal(middleware.length, 3, 'sanitizee has a valid middleware');
t.end();
});
};
module.exports.tests.sanitize_input = function(test, common) {
var inputs = {
invalid: [ '', 100, null, undefined, new Date() ],
valid: [ 'a', 'aa', 'aaaaaaaa' ]
};
test('invalid input', function(t) {
inputs.invalid.forEach( function( input ){
sanitize({ input: input, lat: 0, lon: 0 }, function( err, clean ){
t.equal(err, 'invalid param \'input\': text length, must be >0', input + ' is an invalid input');
t.equal(clean, undefined, 'clean not set');
});
});
t.end();
});
test('valid input', function(t) {
inputs.valid.forEach( function( input ){
sanitize({ input: input, lat: 0, lon: 0 }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.input = input;
t.equal(err, undefined, 'no error');
expected.parsed_input = parser(input);
t.deepEqual(clean, expected, 'clean set correctly (' + input + ')');
});
});
t.end();
});
};
module.exports.tests.sanitize_input_with_delim = function(test, common) {
var inputs = [ 'a,bcd', '123 main st, admin1', ',,,', ' ' ];
test('valid inputs with a comma', function(t) {
inputs.forEach( function( input ){
sanitize({ input: input, lat: 0, lon: 0 }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.input = input;
expected.parsed_input = parser(input);
t.equal(err, undefined, 'no error');
t.deepEqual(clean, expected, 'clean set correctly (' + input + ')');
});
});
t.end();
});
};
module.exports.tests.sanitize_lat = function(test, common) {
var lats = {
invalid: [],
valid: [ 0, 45, 90, -0, '0', '45', '90', -181, -120, -91, 91, 120, 181 ]
};
test('invalid lat', function(t) {
lats.invalid.forEach( function( lat ){
sanitize({ input: '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');
});
});
t.end();
});
test('valid lat', function(t) {
lats.valid.forEach( function( lat ){
sanitize({ input: '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');
expected.parsed_input = parser('test');
t.deepEqual(clean, expected, 'clean set correctly (' + lat + ')');
});
});
t.end();
});
};
module.exports.tests.sanitize_lon = function(test, common) {
var lons = {
valid: [ -381, -181, -180, -1, -0, 0, 45, 90, '-180', '0', '180', 181 ]
};
test('valid lon', function(t) {
lons.valid.forEach( function( lon ){
sanitize({ input: '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');
expected.parsed_input = parser('test');
t.deepEqual(clean, expected, 'clean set correctly (' + lon + ')');
});
});
t.end();
});
};
module.exports.tests.sanitize_bbox = function(test, common) {
var bboxes = {
invalid_coordinates: [
],
invalid: [
'91;-181,-91,181', // invalid - semicolon between coordinates
'91, -181, -91', // invalid - missing a coordinate
'123,12', // invalid - missing coordinates
'' // invalid - empty param
],
valid: [
'-179,90,34,-80', // valid top_right lon/lat, bottom_left lon/lat
'0,0,0,0', // valid top_right lat/lon, bottom_left lat/lon
'-181,90,34,-180', // wrapped top_right lon, bottom_left lat
'-170,91,-181,45', // wrapped top_right lat, bottom_left lon
'-181,91,181,-91', // wrapped top_right lon/lat, bottom_left lon/lat
'91, -181,-91,181',// valid - spaces between coordinates
]
};
test('invalid bbox coordinates', function(t) {
bboxes.invalid_coordinates.forEach( function( bbox ){
sanitize({ input: 'test', lat: 0, lon: 0, bbox: bbox }, function( err, clean ){
t.ok(err.match(/Invalid (lat|lon)/), bbox + ' is invalid');
t.equal(clean, undefined, 'clean not set');
});
});
t.end();
});
test('invalid bbox', function(t) {
bboxes.invalid.forEach( function( bbox ){
sanitize({ input: 'test', lat: 0, lon: 0, bbox: bbox }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean ));
t.equal(err, undefined, 'no error');
expected.parsed_input = parser('test');
t.deepEqual(clean, expected, 'falling back on 50km distance from centroid');
});
});
t.end();
});
test('valid bbox', function(t) {
bboxes.valid.forEach( function( bbox ){
sanitize({ input: 'test', lat: 0, lon: 0, bbox: bbox }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean ));
var bboxArray = bbox.split(',').map(function(i) {
return parseInt(i);
});
expected.bbox = {
right: Math.max(bboxArray[0], bboxArray[2]),
top: Math.max(bboxArray[1], bboxArray[3]),
left: Math.min(bboxArray[0], bboxArray[2]),
bottom: Math.min(bboxArray[1], bboxArray[3])
};
t.equal(err, undefined, 'no error');
expected.parsed_input = parser('test');
t.deepEqual(clean, expected, 'clean set correctly (' + bbox + ')');
});
});
t.end();
});
};
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 ){
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 ){
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 ){
t.equal(clean.zoom, 18, 'max zoom set');
t.end();
});
});
};
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 ){
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 ){
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 ){
t.equal(clean.size, 40, 'max size set');
t.end();
});
});
};
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 ){
t.equal(clean.details, false, 'default details set (to false)');
t.end();
});
});
});
var valid_values = ['true', true, 1];
valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
t.end();
});
});
});
var valid_false_values = ['false', false, 0];
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 ){
t.equal(clean.details, false, 'details set to false');
t.end();
});
});
});
test('test default behavior', function(t) {
sanitize({ input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.details, true, 'details set to true');
t.end();
});
});
};
module.exports.tests.sanitize_layers = function(test, common) {
test('unspecified', function(t) {
sanitize({ layers: undefined, input: 'test', 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 ){
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');
t.end();
});
});
test('poi (alias) layer', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.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 ){
t.deepEqual(clean.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 ){
t.deepEqual(clean.layers, address_layers, 'address layers set');
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', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, reg_layers.concat(poi_layers), 'poi + regular layers');
t.end();
});
});
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 ){
t.deepEqual(clean.layers, reg_layers.concat(admin_layers), 'admin + regular layers set');
t.end();
});
});
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 ){
t.deepEqual(clean.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 ){
t.deepEqual(clean.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 ){
t.deepEqual(clean.layers, alias_layers, 'all layers found (no duplicates)');
t.end();
});
});
};
module.exports.tests.invalid_params = function(test, common) {
test('invalid input params', function(t) {
sanitize( undefined, function( err, clean ){
t.equal(err, defaultError, 'handle invalid params gracefully');
t.end();
});
});
};
module.exports.tests.middleware_failure = function(test, common) {
test('middleware failure', function(t) {
var res = { status: function( code ){
t.equal(code, 400, 'status set');
}};
var next = function( message ){
t.equal(message, defaultError);
t.end();
};
middleware( {}, res, next );
});
};
module.exports.tests.middleware_success = function(test, common) {
test('middleware success', function(t) {
var req = { query: { input: 'test', lat: 0, lon: 0 }};
var next = function( message ){
t.equal(message, undefined, 'no error message set');
req.clean.parsed_input = parser('test');
t.deepEqual(req.clean, defaultClean);
t.end();
};
middleware( req, undefined, next );
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE /suggest ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
Loading…
Cancel
Save