Browse Source

removed `query` parameter support

as it's not required functionality right now

modified error message to be joined list of available parameters instead of hardcoded string
pull/712/head
Stephen Hess 8 years ago
parent
commit
91e0520f87
  1. 8
      sanitizer/_synthesize_analysis.js
  2. 36
      test/unit/sanitizer/_synthesize_analysis.js

8
sanitizer/_synthesize_analysis.js

@ -1,7 +1,7 @@
const _ = require('lodash'); const _ = require('lodash');
const fields = ['query', 'address', 'neighbourhood', 'borough', 'city', const fields = ['address', 'neighbourhood', 'borough', 'city', 'county',
'county', 'state', 'postalcode', 'country']; 'state', 'postalcode', 'country'];
function normalizeWhitespaceToSingleSpace(val) { function normalizeWhitespaceToSingleSpace(val) {
return _.replace(_.trim(val), /\s+/g, ' '); return _.replace(_.trim(val), /\s+/g, ' ');
@ -23,8 +23,8 @@ function sanitize( raw, clean ){
}, {}); }, {});
if (_.isEmpty(Object.keys(clean.parsed_text))) { if (_.isEmpty(Object.keys(clean.parsed_text))) {
messages.errors.push('at least one of the following fields is required: ' + messages.errors.push(
'query, address, neighbourhood, borough, city, county, state, postalcode, country'); `at least one of the following fields is required: ${fields.join(', ')}`);
} }
return messages; return messages;

36
test/unit/sanitizer/_synthesize_analysis.js

@ -1,11 +1,11 @@
var sanitizer = require('../../../sanitizer/_synthesize_analysis'); const sanitizer = require('../../../sanitizer/_synthesize_analysis');
var _ = require('lodash'); const _ = require('lodash');
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.text_parser = function(test, common) { module.exports.tests.text_parser = function(test, common) {
test('all variables should be parsed', function(t) { test('all variables should be parsed', function(t) {
var raw = { const raw = {
query: ' \t query \t value \t ', query: ' \t query \t value \t ',
address: ' \t address \t value \t ', address: ' \t address \t value \t ',
neighbourhood: ' \t neighbourhood \t value \t ', neighbourhood: ' \t neighbourhood \t value \t ',
@ -17,11 +17,10 @@ module.exports.tests.text_parser = function(test, common) {
country: ' \t country \t value \t ' country: ' \t country \t value \t '
}; };
var clean = {}; const clean = {};
var expected_clean = { const expected_clean = {
parsed_text: { parsed_text: {
query: 'query value',
address: 'address value', address: 'address value',
neighbourhood: 'neighbourhood value', neighbourhood: 'neighbourhood value',
borough: 'borough value', borough: 'borough value',
@ -33,7 +32,7 @@ module.exports.tests.text_parser = function(test, common) {
} }
}; };
var messages = sanitizer(raw, clean); const messages = sanitizer(raw, clean);
t.deepEquals(clean, expected_clean); t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, [], 'no errors'); t.deepEquals(messages.errors, [], 'no errors');
@ -48,8 +47,7 @@ module.exports.tests.text_parser = function(test, common) {
return _.sample([{}, [], false, '', ' \t ', 17, undefined]); return _.sample([{}, [], false, '', ' \t ', 17, undefined]);
} }
var raw = { const raw = {
query: getInvalidValue(),
address: getInvalidValue(), address: getInvalidValue(),
neighbourhood: getInvalidValue(), neighbourhood: getInvalidValue(),
borough: getInvalidValue(), borough: getInvalidValue(),
@ -60,34 +58,34 @@ module.exports.tests.text_parser = function(test, common) {
country: getInvalidValue() country: getInvalidValue()
}; };
var clean = {}; const clean = {};
var expected_clean = { const expected_clean = {
parsed_text: {} parsed_text: {}
}; };
var messages = sanitizer(raw, clean); const messages = sanitizer(raw, clean);
t.deepEquals(clean, expected_clean); t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, ['at least one of the following fields is required: ' + t.deepEquals(messages.errors, ['at least one of the following fields is required: ' +
'query, address, neighbourhood, borough, city, county, state, postalcode, country'], 'no errors'); 'address, neighbourhood, borough, city, county, state, postalcode, country'], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings'); t.deepEquals(messages.warnings, [], 'no warnings');
t.end(); t.end();
}); });
test('no supplied fields should return error', function(t) { test('no supplied fields should return error', function(t) {
var raw = {}; const raw = {};
var clean = {}; const clean = {};
var expected_clean = { parsed_text: {} }; const expected_clean = { parsed_text: {} };
var messages = sanitizer(raw, clean); const messages = sanitizer(raw, clean);
t.deepEquals(clean, expected_clean); t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, ['at least one of the following fields is required: ' + t.deepEquals(messages.errors, ['at least one of the following fields is required: ' +
'query, address, neighbourhood, borough, city, county, state, postalcode, country'], 'no errors'); 'address, neighbourhood, borough, city, county, state, postalcode, country'], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings'); t.deepEquals(messages.warnings, [], 'no warnings');
t.end(); t.end();
@ -100,7 +98,7 @@ module.exports.all = function (tape, common) {
return tape('sanitizer _synthesize_analysis: ' + name, testFunction); return tape('sanitizer _synthesize_analysis: ' + name, testFunction);
} }
for( var testCase in module.exports.tests ){ for( const testCase in module.exports.tests ){
module.exports.tests[testCase](test, common); module.exports.tests[testCase](test, common);
} }
}; };

Loading…
Cancel
Save