mirror of https://github.com/pelias/api.git
Diana Shkolnikov
9 years ago
11 changed files with 534 additions and 82 deletions
@ -0,0 +1,13 @@
|
||||
/** |
||||
* These values specify how much a document that matches certain parts of an address |
||||
* should be boosted in elasticsearch results. |
||||
*/ |
||||
|
||||
module.exports = { |
||||
number: 1, |
||||
street: 3, |
||||
zip: 3, |
||||
admin2: 2, |
||||
admin1_abbr: 3, |
||||
alpha3: 5 |
||||
}; |
@ -0,0 +1,42 @@
|
||||
var peliasSchema = require('pelias-schema'); |
||||
var peliasLogger = require( 'pelias-logger' ).get( 'api' ); |
||||
|
||||
var ADMIN_FIELDS = [ |
||||
'admin0', |
||||
'admin1', |
||||
'admin1_abbr', |
||||
'admin2', |
||||
'local_admin', |
||||
'locality', |
||||
'neighborhood' |
||||
]; |
||||
|
||||
/** |
||||
* Get all admin fields that were expected and also found in schema |
||||
* |
||||
* @param {Object} [schema] optional: for testing only |
||||
* @param {Array} [expectedFields] optional: for testing only |
||||
* @param {Object} [logger] optional: for testing only |
||||
* @returns {Array.<string>} |
||||
*/ |
||||
function getAvailableAdminFields(schema, expectedFields, logger) { |
||||
|
||||
schema = schema || peliasSchema; |
||||
expectedFields = expectedFields || ADMIN_FIELDS; |
||||
logger = logger || peliasLogger; |
||||
|
||||
var actualFields = Object.keys(schema.mappings._default_.properties); |
||||
|
||||
// check if expected fields are actually in current schema
|
||||
var available = expectedFields.filter(function (field) { |
||||
return (actualFields.indexOf(field) !== -1); |
||||
}); |
||||
|
||||
if (available.length === 0) { |
||||
logger.error('helper/adminFields: no expected admin fields found in schema'); |
||||
} |
||||
|
||||
return available; |
||||
} |
||||
|
||||
module.exports = getAvailableAdminFields; |
@ -0,0 +1,84 @@
|
||||
var adminFields = require('../../../helper/adminFields'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('validate fields', function(t) { |
||||
t.assert(adminFields instanceof Function, 'adminFields is a function'); |
||||
t.assert(adminFields() instanceof Array, 'adminFields() returns an array'); |
||||
t.assert(adminFields().length > 0, 'adminFields array is not empty'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.lookupExistance = function(test, common) { |
||||
test('all expected fields in schema', function(t) { |
||||
|
||||
var expectedFields = [ |
||||
'one', |
||||
'two', |
||||
'three', |
||||
'four' |
||||
]; |
||||
var schema = { mappings: { _default_: { properties: {} } } }; |
||||
|
||||
// inject all expected fields into schema mock
|
||||
expectedFields.forEach(function (field) { |
||||
schema.mappings._default_.properties[field] = {}; |
||||
}); |
||||
|
||||
var res = adminFields(schema, expectedFields); |
||||
|
||||
t.deepEquals(res, expectedFields, 'all expected fields are returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('some expected fields in schema', function(t) { |
||||
|
||||
var expectedFields = [ |
||||
'one', |
||||
'two', |
||||
'three', |
||||
'four' |
||||
]; |
||||
var schema = { mappings: { _default_: { properties: {} } } }; |
||||
|
||||
// inject only some of the expected fields into schema mock
|
||||
expectedFields.slice(0, 3).forEach(function (field) { |
||||
schema.mappings._default_.properties[field] = {}; |
||||
}); |
||||
|
||||
var res = adminFields(schema, expectedFields); |
||||
|
||||
t.deepEquals(res, expectedFields.slice(0, 3), 'only matching expected fields are returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('no expected fields in schema', function(t) { |
||||
|
||||
var schema = { mappings: { _default_: { properties: { foo: {} } } } }; |
||||
|
||||
var logErrorCalled = false; |
||||
var logger = { |
||||
error: function () { |
||||
logErrorCalled = true; |
||||
}}; |
||||
|
||||
var res = adminFields(schema, undefined, logger); |
||||
|
||||
t.deepEquals(res, [], 'no admin fields found'); |
||||
t.assert(logErrorCalled, 'log error called'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('adminFields: ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue