Browse Source

Move cmd.type setting logic to types helper

pull/221/head
Julian Simioni 9 years ago
parent
commit
258fa4390e
  1. 13
      controller/search.js
  2. 15
      helper/types.js
  3. 67
      test/unit/helper/types.js
  4. 1
      test/unit/run.js

13
controller/search.js

@ -1,4 +1,5 @@
var service = { search: require('../service/search') }; var service = { search: require('../service/search') };
var types = require ( '../helper/types' );
function setup( backend, query ){ function setup( backend, query ){
@ -15,13 +16,11 @@ function setup( backend, query ){
body: query( req.clean ) body: query( req.clean )
}; };
if (req.clean.types && req.clean.types.from_layers) { // don't directly set cmd.type from types helper to avoid sometimes
cmd.type = req.clean.types.from_layers; // setting cmd.type to undefined (having the key not set is cleaner)
} var type = types(req.clean.types);
if (type !== undefined) {
// set type if input suggests targeting a layer(s) cmd.type = type;
if (req.clean.default_layers_set && req.clean.parsed_input) {
cmd.type = req.clean.parsed_input.target_layer || cmd.type;
} }
// query backend // query backend

15
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;
}
};

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

1
test/unit/run.js

@ -19,6 +19,7 @@ var tests = [
require('./helper/geojsonify'), require('./helper/geojsonify'),
require('./helper/outputSchema'), require('./helper/outputSchema'),
require('./helper/adminFields'), require('./helper/adminFields'),
require('./helper/types'),
]; ];
tests.map(function(t) { tests.map(function(t) {

Loading…
Cancel
Save