Browse Source

switched to has_parsed_text_properties

pull/936/head
Stephen Hess 7 years ago
parent
commit
951b18e656
  1. 18
      controller/predicates/has_any_parsed_text_property.js
  2. 15
      routes/v1.js
  3. 94
      test/unit/controller/predicates/has_any_parsed_text_property.js
  4. 1
      test/unit/run.js

18
controller/predicates/has_any_parsed_text_property.js

@ -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'], {}))
)
);
};

15
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

94
test/unit/controller/predicates/has_any_parsed_text_property.js

@ -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);
}
};

1
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'),

Loading…
Cancel
Save