From c602794ed8b8dfd59bc0d0ce76860280e2ec4db7 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Thu, 25 May 2017 00:07:36 -0400 Subject: [PATCH] added predicate for finding results at given layers --- .../predicates/has_results_at_layers.js | 19 +++ .../predicates/has_results_at_layers.js | 122 ++++++++++++++++++ test/unit/run.js | 1 + 3 files changed, 142 insertions(+) create mode 100644 controller/predicates/has_results_at_layers.js create mode 100644 test/unit/controller/predicates/has_results_at_layers.js diff --git a/controller/predicates/has_results_at_layers.js b/controller/predicates/has_results_at_layers.js new file mode 100644 index 00000000..bb1cfcae --- /dev/null +++ b/controller/predicates/has_results_at_layers.js @@ -0,0 +1,19 @@ +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 = (layers) => { + return (request, response) => { + return !_.isEmpty( + _.intersection( + // convert layers to an array if it isn't already one + _.castArray(layers), + // pull all the layer properties into an array + _.map(response.data, _.property('layer')) + )); + }; + +}; diff --git a/test/unit/controller/predicates/has_results_at_layers.js b/test/unit/controller/predicates/has_results_at_layers.js new file mode 100644 index 00000000..f81e9045 --- /dev/null +++ b/test/unit/controller/predicates/has_results_at_layers.js @@ -0,0 +1,122 @@ +'use strict'; + +const _ = require('lodash'); +const has_results_at_layers = require('../../../../controller/predicates/has_results_at_layers'); + +module.exports.tests = {}; + +module.exports.tests.interface = (test, common) => { + test('valid interface', (t) => { + t.equal(typeof has_results_at_layers, 'function', 'has_results_at_layers is a function'); + t.equal(has_results_at_layers.length, 1); + t.end(); + }); +}; + +module.exports.tests.true_conditions = (test, common) => { + test('should return true when any result.layer matches any layer in array', (t) => { + const req = {}; + const res = { + data: [ + { + layer: 'layer 1' + }, + { + layer: 'layer 2' + }, + { + layer: 'layer 3' + } + ] + }; + + t.ok(has_results_at_layers(['layer 2', 'layer 4'])(req, res)); + t.end(); + + }); + + test('should return true when any result.layer matches layer string', (t) => { + const req = {}; + const res = { + data: [ + { + layer: 'layer 1' + }, + { + layer: 'layer 2' + }, + { + layer: 'layer 3' + } + ] + }; + + t.ok(has_results_at_layers('layer 2')(req, res)); + t.end(); + + }); + +}; + +module.exports.tests.false_conditions = (test, common) => { + test('should return false when response has undefined data', (t) => { + const req = {}; + const res = {}; + + t.notOk(has_results_at_layers('layer')(req, res)); + t.end(); + + }); + + test('should return false when response has empty data array', (t) => { + const req = {}; + const res = { + data: [] + }; + + t.notOk(has_results_at_layers('layer')(req, res)); + t.end(); + + }); + + test('should return false when layer is a substring of non-array string layers parameter', (t) => { + const req = {}; + const res = { + data: [ + { + layer: 'aye' + } + ] + }; + + t.notOk(has_results_at_layers('layer')(req, res)); + t.end(); + + }); + + test('should return false when no results have layer in supplied layers', (t) => { + const req = {}; + const res = { + data: [ + { + layer: 'layer 1' + } + ] + }; + + t.notOk(has_results_at_layers(['layer 2', 'layer 3'])(req, res)); + t.end(); + + }); + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`GET /has_results_at_layers ${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 b672e451..3d994ac5 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -17,6 +17,7 @@ var tests = [ require('./controller/placeholder'), require('./controller/search'), require('./controller/predicates/has_response_data'), + require('./controller/predicates/has_results_at_layers'), require('./controller/predicates/has_request_errors'), require('./controller/predicates/is_admin_only_analysis'), require('./controller/predicates/is_coarse_reverse'),