From b60e0113b376782ba901197f11cdfcc10387923f Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Mon, 25 Sep 2017 12:44:00 -0400 Subject: [PATCH] check for missing parent object in confidence score computation --- middleware/confidenceScore.js | 7 +++--- test/unit/middleware/confidenceScore.js | 32 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/middleware/confidenceScore.js b/middleware/confidenceScore.js index 8f5b61fb1..5f82ddf6b 100644 --- a/middleware/confidenceScore.js +++ b/middleware/confidenceScore.js @@ -94,7 +94,8 @@ function checkForDealBreakers(req, hit) { return false; } - if (check.assigned(req.clean.parsed_text.state) && hit.parent.region_a && req.clean.parsed_text.state !== hit.parent.region_a[0]) { + if (check.assigned(req.clean.parsed_text.state) && check.assigned(hit.parent) && + hit.parent.region_a && req.clean.parsed_text.state !== hit.parent.region_a[0]) { logger.debug('[confidence][deal-breaker]: state !== region_a'); return true; } @@ -220,8 +221,8 @@ function checkAddress(text, hit) { res += propMatch(text.number, (hit.address_parts ? hit.address_parts.number : null), false); res += propMatch(text.street, (hit.address_parts ? hit.address_parts.street : null), false); res += propMatch(text.postalcode, (hit.address_parts ? hit.address_parts.zip: null), true); - res += propMatch(text.state, (hit.parent.region_a ? hit.parent.region_a[0] : null), true); - res += propMatch(text.country, (hit.parent.country_a ? hit.parent.country_a[0] :null), true); + res += propMatch(text.state, ((hit.parent && hit.parent.region_a) ? hit.parent.region_a[0] : null), true); + res += propMatch(text.country, ((hit.parent && hit.parent.country_a) ? hit.parent.country_a[0] :null), true); res /= checkCount; } diff --git a/test/unit/middleware/confidenceScore.js b/test/unit/middleware/confidenceScore.js index a0b4de6f8..9e10775f3 100644 --- a/test/unit/middleware/confidenceScore.js +++ b/test/unit/middleware/confidenceScore.js @@ -169,6 +169,38 @@ module.exports.tests.confidenceScore = function(test, common) { t.false(res.data[0].hasOwnProperty('confidence'), 'score was not set'); t.end(); }); + + test('missing parent object should not throw an exception', function(t) { + var req = { + clean: { + text: '123 Main St, City, NM', + parsed_text: { + number: 123, + street: 'Main St', + state: 'NM' + } + } + }; + var res = { + data: [{ + _score: 10, + found: true, + value: 1, + center_point: { lat: 100.1, lon: -50.5 }, + name: { default: 'test name1' }, + }], + meta: { + scores: [10], + query_type: 'original' + } + }; + + t.doesNotThrow(() => { + confidenceScore(req, res, () => {}); + }); + t.equal(res.data[0].confidence, 0.28, 'score was set'); + t.end(); + }); }; module.exports.all = function (tape, common) {