From ad5d2bf36a02d1dab445e438318be99b5f65a384 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 17 Aug 2016 13:55:56 -0400 Subject: [PATCH] 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. --- sanitiser/search_fallback.js | 8 +++ test/unit/sanitiser/search_fallback.js | 82 +++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/sanitiser/search_fallback.js b/sanitiser/search_fallback.js index 382b548b..ebe6b53e 100644 --- a/sanitiser/search_fallback.js +++ b/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(); }); + }; diff --git a/test/unit/sanitiser/search_fallback.js b/test/unit/sanitiser/search_fallback.js index d2ce4c01..2003b19f 100644 --- a/test/unit/sanitiser/search_fallback.js +++ b/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) {