diff --git a/controller/predicates/is_single_field_analysis.js b/controller/predicates/is_single_field_analysis.js new file mode 100644 index 00000000..ca49b9b5 --- /dev/null +++ b/controller/predicates/is_single_field_analysis.js @@ -0,0 +1,17 @@ +const _ = require('lodash'); +const Debug = require('../../helper/debug'); +const debugLog = new Debug('controller:predicates:is_request_sources_only_whosonfirst'); +const stackTraceLine = require('../../helper/stackTraceLine'); + +// returns true IFF req.clean.parsed_text contains exactly 1 field +module.exports = (req, res) => { + const is_single_field_analysis = _.keys(_.get(req, ['clean', 'parsed_text'])).length === 1; + + debugLog.push(req, () => ({ + reply: is_single_field_analysis, + stack_trace: stackTraceLine() + })); + + return is_single_field_analysis; + +}; diff --git a/test/unit/controller/predicates/is_single_field_analysis.js b/test/unit/controller/predicates/is_single_field_analysis.js new file mode 100644 index 00000000..d5e80ba0 --- /dev/null +++ b/test/unit/controller/predicates/is_single_field_analysis.js @@ -0,0 +1,83 @@ +'use strict'; + +const _ = require('lodash'); +const is_single_field_analysis = require('../../../../controller/predicates/is_single_field_analysis'); + +module.exports.tests = {}; + +module.exports.tests.interface = (test, common) => { + test('valid interface', (t) => { + t.ok(_.isFunction(is_single_field_analysis), 'is_single_field_analysis is a function'); + t.end(); + }); + +}; + +module.exports.tests.true_conditions = (test, common) => { + test('req.clean.parsed_text with a single field should return true', t => { + const req = { + clean: { + parsed_text: { + property: 'value' + } + } + }; + + t.ok(is_single_field_analysis(req)); + t.end(); + + }); + +}; + +module.exports.tests.false_conditions = (test, common) => { + test('undefined request should return false', t => { + t.notOk(is_single_field_analysis()); + t.end(); + + }); + + test('undefined req.clean should return false', t => { + t.notOk(is_single_field_analysis({})); + t.end(); + + }); + + test('undefined req.clean.parsed_text should return false', t => { + t.notOk(is_single_field_analysis({ clean: {} })); + t.end(); + + }); + + test('req.clean.parsed_text with 0 properties should return false', t => { + t.notOk(is_single_field_analysis({ clean: { parsed_text: {} } })); + t.end(); + + }); + + test('req.clean.parsed_text with 2 properties should return false', t => { + const req = { + clean: { + parsed_text: { + property1: 'property1 value', + property2: 'property2 value' + } + } + }; + + t.notOk(is_single_field_analysis(req)); + t.end(); + + }); + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`GET /is_single_field_analysis ${name}`, testFunction); + } + + for( const testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +};