mirror of https://github.com/pelias/api.git
Diana Shkolnikov
9 years ago
11 changed files with 466 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,30 @@
|
||||
var schema = require('pelias-schema'); |
||||
var logger = require( 'pelias-logger' ).get( 'api' ); |
||||
|
||||
var ADMIN_FIELDS = [ |
||||
'admin0', |
||||
'admin1', |
||||
'admin1_abbr', |
||||
'admin2', |
||||
'local_admin', |
||||
'locality', |
||||
'neighborhood' |
||||
]; |
||||
|
||||
function getAvailableAdminFields() { |
||||
var actualFields = Object.keys(schema.mappings._default_.properties); |
||||
|
||||
// check if expected fields are actually in current schema
|
||||
var available = ADMIN_FIELDS.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.availableFields = getAvailableAdminFields(); |
||||
module.exports.expectedFields = ADMIN_FIELDS; |
@ -0,0 +1,84 @@
|
||||
var proxyquire = require('proxyquire'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('validate fields', function(t) { |
||||
var adminFields = require('../../../helper/adminFields').availableFields; |
||||
t.assert(adminFields instanceof Array, 'adminFields is an array'); |
||||
t.assert(adminFields.length > 0, 'adminFields array is not empty'); |
||||
t.end(); |
||||
}); |
||||
test('validate fields', function(t) { |
||||
var adminFields = require('../../../helper/adminFields').expectedFields; |
||||
t.assert(adminFields instanceof Array, 'adminFields is 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 = require('../../../helper/adminFields').expectedFields; |
||||
var schemaMock = { mappings: { _default_: { properties: {} } } }; |
||||
|
||||
// inject all expected fields into schema mock
|
||||
expectedFields.forEach(function (field) { |
||||
schemaMock.mappings._default_.properties[field] = {}; |
||||
}); |
||||
|
||||
var adminFields = proxyquire('../../../helper/adminFields', {'pelias-schema': schemaMock}); |
||||
|
||||
t.deepEquals(adminFields.availableFields, adminFields.expectedFields, 'all expected fields are returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('some expected fields in schema', function(t) { |
||||
|
||||
var expectedFields = require('../../../helper/adminFields').expectedFields.slice(0, 3); |
||||
var schemaMock = { mappings: { _default_: { properties: {} } } }; |
||||
|
||||
// inject all expected fields into schema mock
|
||||
expectedFields.forEach(function (field) { |
||||
schemaMock.mappings._default_.properties[field] = {}; |
||||
}); |
||||
|
||||
var adminFields = proxyquire('../../../helper/adminFields', {'pelias-schema': schemaMock}); |
||||
|
||||
t.deepEquals(adminFields.availableFields, expectedFields, 'only matching expected fields are returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('no expected fields in schema', function(t) { |
||||
|
||||
var schemaMock = { mappings: { _default_: { properties: { foo: {} } } } }; |
||||
|
||||
var loggerMock = { get: function (name) { |
||||
t.equal(name, 'api'); |
||||
return { |
||||
error: function () {} |
||||
}; |
||||
}}; |
||||
|
||||
var adminFields = proxyquire('../../../helper/adminFields', |
||||
{ |
||||
'pelias-schema': schemaMock, |
||||
'pelias-logger': loggerMock |
||||
}); |
||||
|
||||
t.deepEquals([], adminFields.availableFields, 'no admin fields found'); |
||||
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