Browse Source

converted to 'any' property for brevity

pull/912/head
Stephen Hess 7 years ago
parent
commit
9ae838903a
  1. 17
      controller/predicates/has_any_parsed_text_property.js
  2. 13
      controller/predicates/has_parsed_text_property.js
  3. 17
      routes/v1.js
  4. 36
      test/unit/controller/predicates/has_any_parsed_text_property.js
  5. 2
      test/unit/run.js

17
controller/predicates/has_any_parsed_text_property.js

@ -0,0 +1,17 @@
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 (request, response) => !_.isEmpty(
_.intersection(
properties,
_.keys(_.get(request, ['clean', 'parsed_text'], {}))
)
);
};

13
controller/predicates/has_parsed_text_property.js

@ -1,13 +0,0 @@
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]);
};
};

17
routes/v1.js

@ -68,7 +68,7 @@ var postProc = {
};
// predicates that drive whether controller/search runs
const hasParsedTextProperty = require('../controller/predicates/has_parsed_text_property');
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');
@ -80,13 +80,8 @@ const hasResponseDataOrRequestErrors = any(hasResponseData, hasRequestErrors);
const hasAdminOnlyResults = not(hasResultsAtLayers(['venue', 'address', 'street']));
const hasNumberButNotStreet = all(
hasParsedTextProperty('number'),
not(hasParsedTextProperty('street'))
);
const hasQueryOrCategory = any(
hasParsedTextProperty('query'),
hasParsedTextProperty('category')
hasAnyParsedTextProperty('number'),
not(hasAnyParsedTextProperty('street'))
);
const serviceWrapper = require('pelias-microservice-wrapper').service;
@ -121,15 +116,15 @@ function addRoutes(app, peliasConfig) {
// don't run placeholder if there's a number but no street
not(hasNumberButNotStreet),
// don't run placeholder if there's a query or category
not(hasQueryOrCategory)
not(hasAnyParsedTextProperty('query', 'category'))
);
const searchWithIdsShouldExecute = all(
not(hasRequestErrors),
// don't search-with-ids if there's a query or category
not(hasQueryOrCategory),
not(hasAnyParsedTextProperty('query', 'category')),
// there must be a street
hasParsedTextProperty('street')
hasAnyParsedTextProperty('street')
);
// execute under the following conditions:

36
test/unit/controller/predicates/has_parsed_text_property.js → test/unit/controller/predicates/has_any_parsed_text_property.js

@ -1,13 +1,13 @@
'use strict';
const _ = require('lodash');
const has_parsed_text_property = require('../../../../controller/predicates/has_parsed_text_property');
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.equal(typeof has_parsed_text_property, 'function', 'has_parsed_text_property is a function');
t.ok(_.isFunction(has_any_parsed_text_property), 'has_any_parsed_text_property is a function');
t.end();
});
};
@ -21,9 +21,23 @@ module.exports.tests.true_conditions = (test, common) => {
}
}
};
const res = {};
t.ok(has_parsed_text_property('property')(req, res));
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();
});
@ -32,9 +46,7 @@ module.exports.tests.true_conditions = (test, common) => {
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.notOk(has_any_parsed_text_property('property')());
t.end();
});
@ -42,7 +54,7 @@ module.exports.tests.false_conditions = (test, common) => {
test('undefined request.clean should return false', (t) => {
const req = {};
t.notOk(has_parsed_text_property('property')(req, undefined));
t.notOk(has_any_parsed_text_property('property')(req));
t.end();
});
@ -52,19 +64,19 @@ module.exports.tests.false_conditions = (test, common) => {
clean: {}
};
t.notOk(has_parsed_text_property('property')(req, undefined));
t.notOk(has_any_parsed_text_property('property')(req));
t.end();
});
test('undefined request.clean.parsed_text.property should return false', (t) => {
test('request.clean.parsed_text with none of the supplied properties should return false', (t) => {
const req = {
clean: {
parsed_text: {}
}
};
t.notOk(has_parsed_text_property('property')(req, undefined));
t.notOk(has_any_parsed_text_property('property1', 'property2')(req));
t.end();
});
@ -73,7 +85,7 @@ module.exports.tests.false_conditions = (test, common) => {
module.exports.all = (tape, common) => {
function test(name, testFunction) {
return tape(`GET /has_parsed_text_property ${name}`, testFunction);
return tape(`GET /has_any_parsed_text_property ${name}`, testFunction);
}
for( const testCase in module.exports.tests ){

2
test/unit/run.js

@ -17,7 +17,7 @@ var tests = [
require('./controller/placeholder'),
require('./controller/search'),
require('./controller/search_with_ids'),
require('./controller/predicates/has_parsed_text_property'),
require('./controller/predicates/has_any_parsed_text_property'),
require('./controller/predicates/has_response_data'),
require('./controller/predicates/has_results_at_layers'),
require('./controller/predicates/has_request_errors'),

Loading…
Cancel
Save