From e158c2168532caf7b8e91f070a374ec9584ce3d3 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Fri, 9 Jun 2017 14:18:27 -0400 Subject: [PATCH] added has_parsed_text_property predicate --- .../predicates/has_parsed_text_property.js | 13 +++ routes/v1.js | 10 ++- .../predicates/has_parsed_text_property.js | 82 +++++++++++++++++++ test/unit/run.js | 1 + 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 controller/predicates/has_parsed_text_property.js create mode 100644 test/unit/controller/predicates/has_parsed_text_property.js diff --git a/controller/predicates/has_parsed_text_property.js b/controller/predicates/has_parsed_text_property.js new file mode 100644 index 00000000..f6a51371 --- /dev/null +++ b/controller/predicates/has_parsed_text_property.js @@ -0,0 +1,13 @@ +const _ = require('lodash'); + +// returns a function that returns true if any result.layer is in any of the +// supplied layers using array intersection + +// example usage: determining if the response contains only admin results + +module.exports = (property) => { + return (request, response) => { + return _.has(request, ['clean', 'parsed_text', property]); + }; + +}; diff --git a/routes/v1.js b/routes/v1.js index 83d2fb78..51d5ac7f 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -66,6 +66,7 @@ var postProc = { }; // predicates that drive whether controller/search runs +const hasParsedTextProperty = require('../controller/predicates/has_parsed_text_property'); const hasResponseData = require('../controller/predicates/has_response_data'); const hasRequestErrors = require('../controller/predicates/has_request_errors'); const isCoarseReverse = require('../controller/predicates/is_coarse_reverse'); @@ -103,7 +104,14 @@ function addRoutes(app, peliasConfig) { ); const placeholderShouldExecute = all( - not(hasResponseDataOrRequestErrors), isPlaceholderServiceEnabled, isAdminOnlyAnalysis + not(hasResponseDataOrRequestErrors), + isPlaceholderServiceEnabled, + not( + any( + hasParsedTextProperty('venue'), + hasParsedTextProperty('category') + ) + ) ); // execute under the following conditions: diff --git a/test/unit/controller/predicates/has_parsed_text_property.js b/test/unit/controller/predicates/has_parsed_text_property.js new file mode 100644 index 00000000..a6b06be1 --- /dev/null +++ b/test/unit/controller/predicates/has_parsed_text_property.js @@ -0,0 +1,82 @@ +'use strict'; + +const _ = require('lodash'); +const has_parsed_text_property = require('../../../../controller/predicates/has_parsed_text_property'); + +module.exports.tests = {}; + +module.exports.tests.interface = (test, common) => { + test('valid interface', (t) => { + t.equal(typeof has_parsed_text_property, 'function', 'has_parsed_text_property is a function'); + t.end(); + }); +}; + +module.exports.tests.true_conditions = (test, common) => { + test('defined request.clean.parsed_text.property should return true', (t) => { + const req = { + clean: { + parsed_text: { + property: 'value' + } + } + }; + const res = {}; + + t.ok(has_parsed_text_property('property')(req, res)); + t.end(); + + }); + +}; + +module.exports.tests.false_conditions = (test, common) => { + test('undefined request should return false', (t) => { + const req = {}; + + t.notOk(has_parsed_text_property('property')(req, undefined)); + t.end(); + + }); + + test('undefined request.clean should return false', (t) => { + const req = {}; + + t.notOk(has_parsed_text_property('property')(req, undefined)); + t.end(); + + }); + + test('undefined request.clean.parsed_text should return false', (t) => { + const req = { + clean: {} + }; + + t.notOk(has_parsed_text_property('property')(req, undefined)); + t.end(); + + }); + + test('undefined request.clean.parsed_text.property should return false', (t) => { + const req = { + clean: { + parsed_text: {} + } + }; + + t.notOk(has_parsed_text_property('property')(req, undefined)); + t.end(); + + }); + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`GET /has_parsed_text_property ${name}`, testFunction); + } + + for( const testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/run.js b/test/unit/run.js index b60f06c5..8447553b 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -16,6 +16,7 @@ var tests = [ require('./controller/place'), require('./controller/placeholder'), require('./controller/search'), + require('./controller/predicates/has_parsed_text_property'), require('./controller/predicates/has_response_data'), require('./controller/predicates/has_results_at_layers'), require('./controller/predicates/has_request_errors'),