diff --git a/controller/search.js b/controller/search.js index 4371e274..0ed1f9cd 100644 --- a/controller/search.js +++ b/controller/search.js @@ -1,4 +1,5 @@ var service = { search: require('../service/search') }; +var types = require ( '../helper/types' ); function setup( backend, query ){ @@ -15,13 +16,11 @@ function setup( backend, query ){ body: query( req.clean ) }; - if (req.clean.types && req.clean.types.from_layers) { - cmd.type = req.clean.types.from_layers; - } - - // set type if input suggests targeting a layer(s) - if (req.clean.default_layers_set && req.clean.parsed_input) { - cmd.type = req.clean.parsed_input.target_layer || cmd.type; + // don't directly set cmd.type from types helper to avoid sometimes + // setting cmd.type to undefined (having the key not set is cleaner) + var type = types(req.clean.types); + if (type !== undefined) { + cmd.type = type; } // query backend diff --git a/helper/types.js b/helper/types.js new file mode 100644 index 00000000..30ab74d5 --- /dev/null +++ b/helper/types.js @@ -0,0 +1,15 @@ +var valid_types = require( '../query/indeces' ); + +module.exports = function calculate_types(clean_types) { + if (!clean_types) { + return undefined; + } + + if (clean_types.from_layers) { + return clean_types.from_layers; + } + + if (clean_types.from_address_parser) { + return clean_types.from_address_parser; + } +}; diff --git a/test/unit/helper/types.js b/test/unit/helper/types.js new file mode 100644 index 00000000..1e7d1813 --- /dev/null +++ b/test/unit/helper/types.js @@ -0,0 +1,67 @@ +var types = require('../../../helper/types'); + +var valid_types = require( '../../../query/indeces' ); +module.exports.tests = {}; + +module.exports.tests.no_cleaned_types = function(test, common) { + test('no cleaned types', function(t) { + var actual = types(undefined); + t.equal(actual, undefined, 'all valid types returned for empty input'); + t.end(); + }); + + test('no cleaned types', function(t) { + var cleaned_types = {}; + var actual = types(cleaned_types); + t.equal(actual, undefined, 'all valid types returned for empty input'); + t.end(); + }); +}; + +module.exports.tests.address_parser = function(test, common) { + test('address parser specifies only admin layers', function(t) { + var cleaned_types = { + from_address_parser: ['admin0'] // simplified return value from address parser + }; + var actual = types(cleaned_types); + var expected = ['admin0']; // simplified expected value for all admin layers + t.deepEqual(actual, expected, 'only layers specified by address parser returned'); + t.end(); + }); +}; + +module.exports.tests.layers_parameter = function(test, common) { + test('layers parameter specifies only some layers', function(t) { + var cleaned_types = { + from_layers: ['geonames'] + }; + var actual = types(cleaned_types); + var expected = ['geonames']; + t.deepEqual(actual, expected, 'only types specified by layers parameter returned'); + t.end(); + }); +}; + +module.exports.tests.layers_parameter_and_address_parser = function(test, common) { + test('layers parameter and address parser present', function(t) { + var cleaned_types = { + from_layers: ['geonames'], + from_address_parser: ['admin0'] // simplified return value from address parse + }; + var actual = types(cleaned_types); + var expected = ['geonames']; + t.deepEqual(actual, expected, 'layers parameter overrides address parser completely'); + t.end(); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('types: ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/run.js b/test/unit/run.js index 619da765..90a14278 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -19,6 +19,7 @@ var tests = [ require('./helper/geojsonify'), require('./helper/outputSchema'), require('./helper/adminFields'), + require('./helper/types'), ]; tests.map(function(t) {