Browse Source

add check for new debug:force-libpostal param in search query

force-libpostal-param
Diana Shkolnikov 8 years ago
parent
commit
4918e81e52
  1. 12
      query/search.js
  2. 29
      sanitizer/_debug.js
  3. 50
      test/unit/sanitizer/_debug.js

12
query/search.js

@ -5,6 +5,7 @@ const defaults = require('./search_defaults');
const textParser = require('./text_parser'); const textParser = require('./text_parser');
const check = require('check-types'); const check = require('check-types');
const logger = require('pelias-logger').get('api'); const logger = require('pelias-logger').get('api');
const _ = require('lodash');
//------------------------------ //------------------------------
// general-purpose search query // general-purpose search query
@ -129,7 +130,7 @@ function generateQuery( clean ){
textParser( clean.parsed_text, vs ); textParser( clean.parsed_text, vs );
} }
var q = getQuery(vs); var q = getQuery(clean, vs);
//console.log(JSON.stringify(q, null, 2)); //console.log(JSON.stringify(q, null, 2));
@ -143,8 +144,9 @@ function generateQuery( clean ){
return q; return q;
} }
function getQuery(vs) { function getQuery(params, vs) {
if (hasStreet(vs) || if (forceLibpostal(params) ||
hasStreet(vs) ||
isCityStateOnlyWithOptionalCountry(vs) || isCityStateOnlyWithOptionalCountry(vs) ||
isCityCountryOnly(vs) || isCityCountryOnly(vs) ||
isPostalCodeOnly(vs)) { isPostalCodeOnly(vs)) {
@ -160,6 +162,10 @@ function getQuery(vs) {
} }
function forceLibpostal(params) {
return _.isSet(params, 'force_libpostal');
}
function hasStreet(vs) { function hasStreet(vs) {
return vs.isset('input:street'); return vs.isset('input:street');
} }

29
sanitizer/_debug.js

@ -0,0 +1,29 @@
'use strict';
const _ = require('lodash');
var DEFAULT_FORCE_LIBPOSTAL = false;
// validate inputs, convert types and apply defaults
function sanitize( raw, clean ){
// error & warning messages
var messages = { errors: [], warnings: [] };
clean.force_libpostal = isTruthy(_.get(raw, 'debug:force_libpostal', false));
return messages;
}
// be lenient with 'truthy' values
function isTruthy(val) {
if( _.isString( val ) ){
return _.includes( ['true', '1', 'yes', 'y'], val );
}
return val === 1 || val === true;
}
// export function
module.exports = sanitize;

50
test/unit/sanitizer/_debug.js

@ -0,0 +1,50 @@
var sanitize = require('../../../sanitizer/_debug');
module.exports.tests = {};
module.exports.tests.sanitize_force_libpostal = function(test, common) {
test('force_libpostal(default)', function(t) {
const raw = {};
const clean = {};
sanitize(raw, clean);
t.equal(clean.force_libpostal, false, 'defaults to false');
t.end();
});
var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(value) {
test('force_libpostal(true) ' + value, function(t) {
const raw = {
'debug:force_libpostal': value
};
const clean = {};
sanitize(raw, clean);
t.equal(clean.force_libpostal, true, 'set to true');
t.end();
});
});
var valid_false_values = ['false', false, 0, '0', 'no', 'n', null, -1, 123, NaN, 'abc'];
valid_false_values.forEach(function(value) {
test('force_libpostal(false) ' + value, function(t) {
const raw = {
'debug:force_libpostal': value
};
const clean = {};
sanitize(raw, clean);
t.equal(clean.force_libpostal, false, 'set to false');
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE _debug ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
Loading…
Cancel
Save