From c8c969db745651878177155b4c4f448fcad2d89d Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 26 Jul 2017 16:38:02 -0400 Subject: [PATCH] added predicate for any/all parsed_text fields --- .../predicates/has_parsed_text_properties.js | 33 ++++ .../predicates/has_parsed_text_properties.js | 163 ++++++++++++++++++ test/unit/run.js | 1 + 3 files changed, 197 insertions(+) create mode 100644 controller/predicates/has_parsed_text_properties.js create mode 100644 test/unit/controller/predicates/has_parsed_text_properties.js diff --git a/controller/predicates/has_parsed_text_properties.js b/controller/predicates/has_parsed_text_properties.js new file mode 100644 index 00000000..79b839ed --- /dev/null +++ b/controller/predicates/has_parsed_text_properties.js @@ -0,0 +1,33 @@ +const _ = require('lodash'); + +// "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 = { + all: function() { + // save off property names for future reference + const properties = _.values(arguments); + + // return true if ALL of the supplied properties are in clean.parsed_text + return request => _.isEmpty( + _.difference( + _.values(properties), + _.keys(_.get(request, ['clean', 'parsed_text'], {})) + ) + ); + + }, + any: function() { + // save off property names for future reference + const properties = _.values(arguments); + + // return true if ANY of the supplied properties are in clean.parsed_text + return request => !_.isEmpty( + _.intersection( + _.values(properties), + _.keys(_.get(request, ['clean', 'parsed_text'], {})) + ) + ); + + } + +}; diff --git a/test/unit/controller/predicates/has_parsed_text_properties.js b/test/unit/controller/predicates/has_parsed_text_properties.js new file mode 100644 index 00000000..9307d46d --- /dev/null +++ b/test/unit/controller/predicates/has_parsed_text_properties.js @@ -0,0 +1,163 @@ +'use strict'; + +const _ = require('lodash'); +const has_parsed_text_properties = require('../../../../controller/predicates/has_parsed_text_properties'); + +module.exports.tests = {}; + +module.exports.tests.interface = (test, common) => { + test('valid interface', (t) => { + t.ok(_.isFunction(has_parsed_text_properties.all), 'has_parsed_text_properties.all is a function'); + t.ok(_.isFunction(has_parsed_text_properties.any), 'has_parsed_text_properties.any is a function'); + t.end(); + }); + +}; + +module.exports.tests.true_conditions = (test, common) => { + test('all: defined request.clean.parsed_text.property should return true', (t) => { + const req = { + clean: { + parsed_text: { + property: 'value' + } + } + }; + + t.ok(has_parsed_text_properties.all('property')(req)); + t.end(); + + }); + + test('all: clean.parsed_text with any property should return true ', (t) => { + const req = { + clean: { + parsed_text: { + property1: 'value1', + property2: 'value2' + } + } + }; + + t.ok(has_parsed_text_properties.all('property2', 'property1')(req)); + t.end(); + + }); + + test('any: defined request.clean.parsed_text.property should return true', (t) => { + const req = { + clean: { + parsed_text: { + property: 'value' + } + } + }; + + t.ok(has_parsed_text_properties.any('property')(req)); + t.end(); + + }); + + test('any: clean.parsed_text with any property should return true ', (t) => { + const req = { + clean: { + parsed_text: { + property2: 'value2', + property3: 'value3' + } + } + }; + + t.ok(has_parsed_text_properties.any('property1', 'property3')(req)); + t.end(); + + }); + +}; + +module.exports.tests.false_conditions = (test, common) => { + test('all: undefined request should return false', (t) => { + t.notOk(has_parsed_text_properties.all('property')()); + t.end(); + + }); + + test('all: undefined request.clean should return false', (t) => { + const req = {}; + + t.notOk(has_parsed_text_properties.all('property')(req)); + t.end(); + + }); + + test('all: undefined request.clean.parsed_text should return false', (t) => { + const req = { + clean: {} + }; + + t.notOk(has_parsed_text_properties.all('property')(req)); + t.end(); + + }); + + test('all: request.clean.parsed_text with none of the supplied properties should return false', (t) => { + const req = { + clean: { + parsed_text: { + property1: 'value1' + } + } + }; + + t.notOk(has_parsed_text_properties.all('property1', 'property2')(req)); + t.end(); + + }); + + test('any: undefined request should return false', (t) => { + t.notOk(has_parsed_text_properties.any('property')()); + t.end(); + + }); + + test('any: undefined request.clean should return false', (t) => { + const req = {}; + + t.notOk(has_parsed_text_properties.any('property')(req)); + t.end(); + + }); + + test('any: undefined request.clean.parsed_text should return false', (t) => { + const req = { + clean: {} + }; + + t.notOk(has_parsed_text_properties.any('property')(req)); + t.end(); + + }); + + test('any: request.clean.parsed_text with none of the supplied properties should return false', (t) => { + const req = { + clean: { + parsed_text: {} + } + }; + + t.notOk(has_parsed_text_properties.any('property1', 'property2')(req)); + t.end(); + + }); + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`GET /has_parsed_text_properties ${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 66730d89..1ff2fc42 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -19,6 +19,7 @@ var tests = [ 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'), require('./controller/predicates/has_results_at_layers'),