Browse Source

location bias feature - sets default focus.point

pull/928/head
Lily He 8 years ago
parent
commit
f67664c3e0
  1. 18
      sanitizer/_location_bias.js
  2. 6
      schema.js
  3. 1
      test/unit/run.js
  4. 90
      test/unit/sanitizer/_location_bias.js
  5. 78
      test/unit/schema.js

18
sanitizer/_location_bias.js

@ -0,0 +1,18 @@
/*
Set a focus.lat and focus.lon if specified in pelias config
*/
var _ = require('lodash');
function setup(defaultParameters){
return function setLocationBias(req, res, next){
if (_.isUndefined(req.clean) || _.isUndefined(defaultParameters['focus.point.lat']) || _.isUndefined(defaultParameters['focus.point.lon'])) {
return next();
}
req.clean['focus.point.lat'] = defaultParameters['focus.point.lat'];
req.clean['focus.point.lon'] = defaultParameters['focus.point.lon'];
next();
};
}
module.exports = setup;

6
schema.js

@ -39,7 +39,11 @@ module.exports = Joi.object().keys({
timeout: Joi.number().integer().optional().default(250).min(0),
retries: Joi.number().integer().optional().default(3).min(0),
}).unknown(false).requiredKeys('url')
}).unknown(false).default({}) // default api.services to an empty object
}).unknown(false).default({}), // default api.services to an empty object
defaultParameters: Joi.object().keys({
'focus.point.lat': Joi.number().optional().min(-90).max(90),
'focus.point.lon': Joi.number().optional().min(-180).max(180),
}).unknown(true).default({})
}).requiredKeys('version', 'indexName', 'host').unknown(true),
esclient: Joi.object().keys({

1
test/unit/run.js

@ -64,6 +64,7 @@ var tests = [
require('./sanitizer/_ids'),
require('./sanitizer/_iso2_to_iso3'),
require('./sanitizer/_layers'),
require('./sanitizer/_location_bias'),
require('./sanitizer/_city_name_standardizer'),
require('./sanitizer/_single_scalar_parameters'),
require('./sanitizer/_size'),

90
test/unit/sanitizer/_location_bias.js

@ -0,0 +1,90 @@
var setup = require('../../../sanitizer/_location_bias');
module.exports.tests = {};
module.exports.tests.setLocationBias = function(test, common) {
test('set focus point', t => {
var defaultParameters = { // specify focus point latitude and longitude
'focus.point.lat': 12.12121212,
'focus.point.lon': 21.21212121
};
var locationBias = setup(defaultParameters);
var req = {
clean: {}
};
var expected = {
clean: {
'focus.point.lat': 12.12121212,
'focus.point.lon': 21.21212121
}
};
locationBias(req, undefined, () => {
t.deepEqual(req, expected, 'focus point should be set');
t.end();
});
});
test('undefined req.clean', t => {
var defaultParameters = {
'focus.point.lat': 12.12121212,
'focus.point.lon': 21.21212121
};
var locationBias = setup(defaultParameters);
var req = {};
var expected = {};
locationBias(req, undefined, () => {
t.deepEqual(req, expected, 'should be unmodified' );
t.end();
});
});
test('focusPointLat is undefined', t => {
var defaultParameters = {
'focus.point.lon': 12.2121212
};
var locationBias = setup(defaultParameters);
var req = {
clean: {}
};
var expected = {
clean: {}
};
locationBias(req, undefined, () => {
t.deepEqual(req, expected, 'should be unmodified' );
t.end();
});
});
test('focusPointLon is undefined', t => {
var defaultParameters = {
'focus.point.lat': 12.2121212
};
var locationBias = setup(defaultParameters);
var req = {
clean: {}
};
var expected = {
clean: {}
};
locationBias(req, undefined, () => {
t.deepEqual(req, expected, 'should be unmodified' );
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('[middleware] locationBias: ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

78
test/unit/schema.js

@ -26,6 +26,10 @@ module.exports.tests.completely_valid = (test, common) => {
placeholder: {
url: 'http://locahost'
}
},
defaultParameters: {
'focus.point.lat': 19,
'focus.point.lon': 91
}
},
esclient: {
@ -457,6 +461,80 @@ module.exports.tests.api_validation = (test, common) => {
});
test('non-number defaultParameters.focus.point.lat should throw error', (t) => {
[null, 'string', {}, [], false].forEach((value) => {
const config = {
api: {
version: 'version value',
indexName: 'index name value',
host: 'host value',
defaultParameters: {
'focus.point.lat': value
}
},
esclient: {}
};
const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1);
t.equals(result.error.details[0].message, '"focus.point.lat" must be a number');
});
t.end();
});
test('non-number defaultParameters.focus.point.lon should throw error', (t) => {
[null, 'string', {}, [], false].forEach((value) => {
const config = {
api: {
version: 'version value',
indexName: 'index name value',
host: 'host value',
defaultParameters: {
'focus.point.lon': value
}
},
esclient: {}
};
const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1);
t.equals(result.error.details[0].message, '"focus.point.lon" must be a number');
});
t.end();
});
test('non-object api.defaultParameters should throw error', (t) => {
[null, 17, false, [], 'string'].forEach((value) => {
var config = {
api: {
version: 'version value',
indexName: 'index name value',
host: 'host value',
defaultParameters: value
},
esclient: {}
};
const result = Joi.validate(config, schema);
t.equals(result.error.details.length, 1);
t.equals(result.error.details[0].message, '"defaultParameters" must be an object');
});
t.end();
});
};
module.exports.tests.api_services_validation = (test, common) => {

Loading…
Cancel
Save