From 951b18e65684dbc16e9e68be063d9e7fb30c1e36 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 31 Jul 2017 13:59:12 -0400 Subject: [PATCH] switched to has_parsed_text_properties --- .../has_any_parsed_text_property.js | 18 ---- routes/v1.js | 15 ++- .../has_any_parsed_text_property.js | 94 ------------------- test/unit/run.js | 1 - 4 files changed, 7 insertions(+), 121 deletions(-) delete mode 100644 controller/predicates/has_any_parsed_text_property.js delete mode 100644 test/unit/controller/predicates/has_any_parsed_text_property.js diff --git a/controller/predicates/has_any_parsed_text_property.js b/controller/predicates/has_any_parsed_text_property.js deleted file mode 100644 index 66f3a8ac..00000000 --- a/controller/predicates/has_any_parsed_text_property.js +++ /dev/null @@ -1,18 +0,0 @@ -const _ = require('lodash'); - -// return true if any setup parameter is a key of request.clean.parsed_text -// "arguments" is only available in long-form function declarations, cannot be shortened to fat arrow syntax -// potential improvement: inject set operator to allow for any/all functionality -module.exports = function() { - // save off requested properties since arguments can't be referenced later - const properties = _.values(arguments); - - // return true if any of the supplied properties are in clean.parsed_text - return (request, response) => !_.isEmpty( - _.intersection( - properties, - _.keys(_.get(request, ['clean', 'parsed_text'], {})) - ) - ); - -}; diff --git a/routes/v1.js b/routes/v1.js index 1eb8d08e..d300d20e 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -69,7 +69,6 @@ var postProc = { }; // predicates that drive whether controller/search runs -const hasAnyParsedTextProperty = require('../controller/predicates/has_any_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'); @@ -88,8 +87,8 @@ const hasResponseDataOrRequestErrors = any(hasResponseData, hasRequestErrors); const hasAdminOnlyResults = not(hasResultsAtLayers(['venue', 'address', 'street'])); const hasNumberButNotStreet = all( - hasAnyParsedTextProperty('number'), - not(hasAnyParsedTextProperty('street')) + hasParsedTextProperties.any('number'), + not(hasParsedTextProperties.any('street')) ); const serviceWrapper = require('pelias-microservice-wrapper').service; @@ -162,20 +161,20 @@ function addRoutes(app, peliasConfig) { // check clean.parsed_text for several conditions that must all be true all( // run placeholder if clean.parsed_text has 'street' - hasAnyParsedTextProperty('street'), + hasParsedTextProperties.any('street'), // don't run placeholder if there's a query or category - not(hasAnyParsedTextProperty('query', 'category')), + not(hasParsedTextProperties.any('query', 'category')), // run placeholder if there are any adminareas identified - hasAnyParsedTextProperty('neighbourhood', 'borough', 'city', 'county', 'state', 'country') + hasParsedTextProperties.any('neighbourhood', 'borough', 'city', 'county', 'state', 'country') ) ); const searchWithIdsShouldExecute = all( not(hasRequestErrors), // don't search-with-ids if there's a query or category - not(hasAnyParsedTextProperty('query', 'category')), + not(hasParsedTextProperties.any('query', 'category')), // there must be a street - hasAnyParsedTextProperty('street') + hasParsedTextProperties.any('street') ); // placeholder should have executed, useful for determining whether to actually diff --git a/test/unit/controller/predicates/has_any_parsed_text_property.js b/test/unit/controller/predicates/has_any_parsed_text_property.js deleted file mode 100644 index 4a985bbe..00000000 --- a/test/unit/controller/predicates/has_any_parsed_text_property.js +++ /dev/null @@ -1,94 +0,0 @@ -'use strict'; - -const _ = require('lodash'); -const has_any_parsed_text_property = require('../../../../controller/predicates/has_any_parsed_text_property'); - -module.exports.tests = {}; - -module.exports.tests.interface = (test, common) => { - test('valid interface', (t) => { - t.ok(_.isFunction(has_any_parsed_text_property), 'has_any_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' - } - } - }; - - t.ok(has_any_parsed_text_property('property')(req)); - t.end(); - - }); - - test('clean.parsed_text with any property should return true ', (t) => { - const req = { - clean: { - parsed_text: { - property2: 'value2', - property3: 'value3' - } - } - }; - - t.ok(has_any_parsed_text_property('property1', 'property3')(req)); - t.end(); - - }); - -}; - -module.exports.tests.false_conditions = (test, common) => { - test('undefined request should return false', (t) => { - t.notOk(has_any_parsed_text_property('property')()); - t.end(); - - }); - - test('undefined request.clean should return false', (t) => { - const req = {}; - - t.notOk(has_any_parsed_text_property('property')(req)); - t.end(); - - }); - - test('undefined request.clean.parsed_text should return false', (t) => { - const req = { - clean: {} - }; - - t.notOk(has_any_parsed_text_property('property')(req)); - t.end(); - - }); - - test('request.clean.parsed_text with none of the supplied properties should return false', (t) => { - const req = { - clean: { - parsed_text: {} - } - }; - - t.notOk(has_any_parsed_text_property('property1', 'property2')(req)); - t.end(); - - }); - -}; - -module.exports.all = (tape, common) => { - function test(name, testFunction) { - return tape(`GET /has_any_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 1ff2fc42..47425ee4 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -18,7 +18,6 @@ var tests = [ require('./controller/placeholder'), require('./controller/search'), require('./controller/search_with_ids'), - require('./controller/predicates/has_any_parsed_text_property'), require('./controller/predicates/has_parsed_text_properties'), require('./controller/predicates/has_request_parameter'), require('./controller/predicates/has_response_data'),