From 4e8a5385f4aafa031c4c305b9c4e654c65049934 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Tue, 23 Oct 2018 14:55:53 +0200 Subject: [PATCH] feat(libpostal_patch): additional tests --- controller/libpostal.js | 4 +- test/unit/controller/libpostal.js | 120 ++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 2 deletions(-) diff --git a/controller/libpostal.js b/controller/libpostal.js index ab178530..3a4badd3 100644 --- a/controller/libpostal.js +++ b/controller/libpostal.js @@ -166,8 +166,8 @@ function patchBuggyResponses(response){ if( _.isPlainObject(house_number) && !_.isPlainObject(unit) && _.isString(house_number.value) ){ let split = _.trim(_.trim(house_number.value),'/').split('/'); if( split.length === 2 ){ - response[house_number._pos].value = split[1]; // second part (house number) - response.push({ label: 'unit', value: split[0] }); // first part (unit number) + response[house_number._pos].value = _.trim(split[1]); // second part (house number) + response.push({ label: 'unit', value: _.trim(split[0]) }); // first part (unit number) } } diff --git a/test/unit/controller/libpostal.js b/test/unit/controller/libpostal.js index 4ea530fc..129cce4a 100644 --- a/test/unit/controller/libpostal.js +++ b/test/unit/controller/libpostal.js @@ -456,6 +456,126 @@ module.exports.tests.bug_fixes = (test, common) => { }); + test('bug fix: correctly parse australian-style unit numbers - with plus', t => { + const service = (req, callback) => { + const response = [ + { + 'label': 'house_number', + 'value': '2+3/32' + }, + { + 'label': 'road', + 'value': 'dixon street' + }, + { + 'label': 'suburb', + 'value': 'strathpine' + }, + { + 'label': 'postcode', + 'value': '4500' + }, + { + 'label': 'state', + 'value': 'qld' + } + ]; + + callback(null, response); + }; + + const controller = libpostal(service, () => true); + + const req = { + clean: { + text: 'original query' + }, + errors: [] + }; + + controller(req, undefined, () => { + t.deepEquals(req, { + clean: { + text: 'original query', + parser: 'libpostal', + parsed_text: { + unit: '2+3', + number: '32', + street: 'dixon street', + neighbourhood: 'strathpine', + postalcode: '4500', + state: 'qld' + } + }, + errors: [] + }, 'req should not have been modified'); + + t.end(); + + }); + + }); + + test('bug fix: correctly parse australian-style unit numbers - with unit spelled out', t => { + const service = (req, callback) => { + const response = [ + { + 'label': 'house_number', + 'value': 'unit 3 /30' + }, + { + 'label': 'road', + 'value': 'dan rees street' + }, + { + 'label': 'suburb', + 'value': 'wallsend' + }, + { + 'label': 'postcode', + 'value': '2287' + }, + { + 'label': 'state', + 'value': 'nsw' + } + ]; + + callback(null, response); + }; + + const controller = libpostal(service, () => true); + + const req = { + clean: { + text: 'original query' + }, + errors: [] + }; + + controller(req, undefined, () => { + t.deepEquals(req, { + clean: { + text: 'original query', + parser: 'libpostal', + parsed_text: { + unit: 'unit 3', + number: '30', + street: 'dan rees street', + neighbourhood: 'wallsend', + postalcode: '2287', + state: 'nsw' + } + }, + errors: [] + }, 'req should not have been modified'); + + t.end(); + + }); + + }); + test('bug fix: correctly parse australian-style unit numbers - no-op if "unit" already assigned', t => { const service = (req, callback) => { const response = [