Browse Source

fixed bug where _geonames_warnings was throwing exception on undefined clean.parsed_text

refactored a bit for clarity
pull/850/head
Stephen Hess 8 years ago
parent
commit
234f9f9738
  1. 18
      sanitizer/_geonames_warnings.js
  2. 12
      test/unit/sanitizer/_geonames_warnings.js

18
sanitizer/_geonames_warnings.js

@ -1,26 +1,30 @@
const _ = require('lodash'); const _ = require('lodash');
function isAdminOnly(parsed_text) { const non_admin_fields = ['number', 'street', 'query', 'category'];
return ['number', 'street', 'query', 'category'].every((prop) => {
return _.isEmpty(parsed_text[prop]); function hasAnyNonAdminFields(parsed_text) {
}); return !_.isEmpty(
_.intersection(
_.keys(parsed_text),
non_admin_fields));
} }
function sanitize( raw, clean ){ function sanitize( raw, clean ){
// error & warning messages // error & warning messages
const messages = { errors: [], warnings: [] }; const messages = { errors: [], warnings: [] };
// bail early if analysis wasn't admin-only // bail early if analysis isn't admin-only
if (!isAdminOnly(clean.parsed_text)) { if (_.isUndefined(clean.parsed_text) || hasAnyNonAdminFields(clean.parsed_text)) {
return messages; return messages;
} }
// the analysis is admin-only, so add errors or warnings if geonames was requested
if (_.isEqual(clean.sources, ['geonames'])) { if (_.isEqual(clean.sources, ['geonames'])) {
// if requested sources is only geonames, return an error // if requested sources is only geonames, return an error
messages.errors.push('input contains only administrative area data, ' + messages.errors.push('input contains only administrative area data, ' +
'no results will be returned when sources=geonames'); 'no results will be returned when sources=geonames');
} else if (_.indexOf(clean.sources, 'geonames') !== -1) { } else if (_.includes(clean.sources, 'geonames')) {
// if there are other sources besides geonames, return an warning // if there are other sources besides geonames, return an warning
messages.warnings.push('input contains only administrative area data, ' + messages.warnings.push('input contains only administrative area data, ' +
'geonames results will not be returned'); 'geonames results will not be returned');

12
test/unit/sanitizer/_geonames_warnings.js

@ -8,6 +8,18 @@ const adminProperties = ['neighbourhood', 'borough', 'city', 'county', 'state',
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.no_errors = (test, common) => { module.exports.tests.no_errors = (test, common) => {
test('undefined clean.parsed_text should exit early', (t) => {
const clean = {
sources: ['geonames'],
};
const messages = geonames_warnings(undefined, clean);
t.deepEquals(messages, { errors: [], warnings: [] });
t.end();
});
test('any non-admin analysis field with only geonames sources should exit early', (t) => { test('any non-admin analysis field with only geonames sources should exit early', (t) => {
adminProperties.forEach((adminProperty) => { adminProperties.forEach((adminProperty) => {
nonAdminProperties.forEach((nonAdminProperty) => { nonAdminProperties.forEach((nonAdminProperty) => {

Loading…
Cancel
Save