Browse Source

add early exit condition to stop unneeded re-parsing

This is required in the case where the libpostal parse + ES search was successful (res.data is non-empty) so we want to skip falling back to the current production behavior.
pull/666/head
Stephen Hess 9 years ago
parent
commit
ad5d2bf36a
  1. 8
      sanitiser/search_fallback.js
  2. 82
      test/unit/sanitiser/search_fallback.js

8
sanitiser/search_fallback.js

@ -7,7 +7,15 @@ var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); };
// middleware
module.exports.middleware = function( req, res, next ){
// if res.data already has results then don't call the _text_autocomplete sanitiser
// this has been put into place for when the libpostal integration way of querying
// ES doesn't return anything and we want to fallback to the old logic
if (res && res.hasOwnProperty('data') && res.data.length > 0) {
return next();
}
sanitize( req, function( err, clean ){
next();
});
};

82
test/unit/sanitiser/search_fallback.js

@ -3,7 +3,32 @@ var proxyquire = require('proxyquire').noCallThru();
module.exports.tests = {};
module.exports.tests.sanitize = function(test, common) {
test('verify that all sanitisers were called as expected', function(t) {
test('verify that all sanitisers were called as expected when `res` is undefined', function(t) {
var called_sanitisers = [];
// rather than re-verify the functionality of all the sanitisers, this test just verifies that they
// were all called correctly
var search = proxyquire('../../../sanitiser/search_fallback', {
'../sanitiser/_text_autocomplete': function() {
called_sanitisers.push('_text_autocomplete');
return { errors: [], warnings: [] };
}
});
var expected_sanitisers = [
'_text_autocomplete'
];
var req = {};
search.middleware(req, undefined, function(){
t.deepEquals(called_sanitisers, expected_sanitisers);
t.end();
});
});
test('verify that all sanitisers were called as expected when `res` has no `data` property', function(t) {
var called_sanitisers = [];
// rather than re-verify the functionality of all the sanitisers, this test just verifies that they
@ -26,7 +51,62 @@ module.exports.tests.sanitize = function(test, common) {
t.deepEquals(called_sanitisers, expected_sanitisers);
t.end();
});
});
test('verify that all sanitisers were called as expected when res.data is empty', function(t) {
var called_sanitisers = [];
// rather than re-verify the functionality of all the sanitisers, this test just verifies that they
// were all called correctly
var search = proxyquire('../../../sanitiser/search_fallback', {
'../sanitiser/_text_autocomplete': function() {
called_sanitisers.push('_text_autocomplete');
return { errors: [], warnings: [] };
}
});
var expected_sanitisers = [
'_text_autocomplete'
];
var req = {};
var res = {
data: []
};
search.middleware(req, res, function(){
t.deepEquals(called_sanitisers, expected_sanitisers);
t.end();
});
});
test('non-empty res.data should not call the _text_autocomplete sanitiser', function(t) {
var called_sanitisers = [];
// rather than re-verify the functionality of all the sanitisers, this test just verifies that they
// were all called correctly
var search = proxyquire('../../../sanitiser/search_fallback', {
'../sanitiser/_text_autocomplete': function() {
throw new Error('_text_autocomplete sanitiser should not have been called');
}
});
var expected_sanitisers = [];
var req = {};
var res = {
data: [{}]
};
search.middleware(req, res, function(){
t.deepEquals(called_sanitisers, expected_sanitisers);
t.end();
});
});
};
module.exports.all = function (tape, common) {

Loading…
Cancel
Save