mirror of https://github.com/pelias/api.git
Riordan
9 years ago
55 changed files with 518 additions and 558 deletions
@ -1,45 +0,0 @@
|
||||
|
||||
var _ = require('lodash'), |
||||
peliasSchema = require('pelias-schema'), |
||||
peliasLogger = require( 'pelias-logger' ).get( 'api' ); |
||||
|
||||
var ADMIN_FIELDS = [ |
||||
'admin0', |
||||
'admin1', |
||||
'admin1_abbr', |
||||
'admin2', |
||||
'local_admin', |
||||
'locality', |
||||
'neighborhood', |
||||
'address.zip' |
||||
]; |
||||
|
||||
/** |
||||
* 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 _.includes( actualFields, field ); |
||||
}); |
||||
|
||||
if (available.length === 0) { |
||||
logger.error('helper/adminFields: no expected admin fields found in schema'); |
||||
} |
||||
|
||||
return available; |
||||
} |
||||
|
||||
module.exports = getAvailableAdminFields; |
@ -1,84 +0,0 @@
|
||||
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