Browse Source

fix: Merge pull request #1165 from tiranno/pelias/pelias#670

Fix for Pelias/Pelias#670
pull/1171/head
Julian Simioni 7 years ago committed by GitHub
parent
commit
dd6f360c70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      controller/predicates/is_request_sources_includes_whosonfirst.js
  2. 16
      controller/predicates/is_request_sources_undefined.js
  3. 14
      routes/v1.js
  4. 92
      test/unit/controller/predicates/is_request_sources_includes_whosonfirst.js
  5. 78
      test/unit/controller/predicates/is_request_sources_undefined.js
  6. 2
      test/unit/run.js

16
controller/predicates/is_request_sources_includes_whosonfirst.js

@ -0,0 +1,16 @@
const _ = require('lodash');
const Debug = require('../../helper/debug');
const debugLog = new Debug('controller:predicates:is_request_sources_includes_whosonfirst');
const stackTraceLine = require('../../helper/stackTraceLine');
// returns true IFF 'whosonfirst' is included in the requested sources
module.exports = (req, res) => {
const is_request_sources_includes_whosonfirst = _.get(req, 'clean.sources', []).includes(
'whosonfirst'
);
debugLog.push(req, () => ({
reply: is_request_sources_includes_whosonfirst,
stack_trace: stackTraceLine()
}));
return is_request_sources_includes_whosonfirst;
};

16
controller/predicates/is_request_sources_undefined.js

@ -0,0 +1,16 @@
const _ = require('lodash');
const Debug = require('../../helper/debug');
const debugLog = new Debug('controller:predicates:is_request_sources_undefined');
const stackTraceLine = require('../../helper/stackTraceLine');
// returns true IFF there are no requested sources
module.exports = (req, res) => {
const is_request_sources_undefined = _.isEmpty(
_.get(req, 'clean.sources')
);
debugLog.push(req, () => ({
reply: is_request_sources_undefined,
stack_trace: stackTraceLine()
}));
return is_request_sources_undefined;
};

14
routes/v1.js

@ -80,6 +80,9 @@ const hasRequestCategories = require('../controller/predicates/has_request_param
const isOnlyNonAdminLayers = require('../controller/predicates/is_only_non_admin_layers');
// this can probably be more generalized
const isRequestSourcesOnlyWhosOnFirst = require('../controller/predicates/is_request_sources_only_whosonfirst');
const isRequestSourcesIncludesWhosOnFirst = require('../controller/predicates/is_request_sources_includes_whosonfirst');
const isRequestSourcesUndefined = require('../controller/predicates/is_request_sources_undefined');
const hasRequestParameter = require('../controller/predicates/has_request_parameter');
const hasParsedTextProperties = require('../controller/predicates/has_parsed_text_properties');
@ -167,9 +170,14 @@ function addRoutes(app, peliasConfig) {
)
),
any(
// only geodisambiguate if libpostal returned only admin areas or libpostal was skipped
isAdminOnlyAnalysis,
isRequestSourcesOnlyWhosOnFirst
isRequestSourcesOnlyWhosOnFirst,
all(
isAdminOnlyAnalysis,
any(
isRequestSourcesUndefined,
isRequestSourcesIncludesWhosOnFirst
)
)
)
);

92
test/unit/controller/predicates/is_request_sources_includes_whosonfirst.js

@ -0,0 +1,92 @@
const _ = require('lodash');
const is_request_sources_includes_whosonfirst = require('../../../../controller/predicates/is_request_sources_includes_whosonfirst');
module.exports.tests = {};
module.exports.tests.interface = (test, common) => {
test('valid interface', (t) => {
t.ok(_.isFunction(is_request_sources_includes_whosonfirst), 'is_request_sources_includes_whosonfirst is a function');
t.end();
});
};
module.exports.tests.true_conditions = (test, common) => {
test('sources includes \'whosonfirst\' should return true', (t) => {
const req = {
clean: {
sources: [
'whosonfirst',
'not whosonfirst'
]
}
};
t.ok(is_request_sources_includes_whosonfirst(req));
t.end();
});
test('empty req.clean.sources should return false', (t) => {
const req = {
clean: {
sources: []
}
};
t.notOk(is_request_sources_includes_whosonfirst(req));
t.end();
});
};
module.exports.tests.false_conditions = (test, common) => {
test('undefined req should return false', (t) => {
t.notOk(is_request_sources_includes_whosonfirst(undefined));
t.end();
});
test('undefined req.clean should return false', (t) => {
const req = {};
t.notOk(is_request_sources_includes_whosonfirst(req));
t.end();
});
test('undefined req.clean.sources should return false', (t) => {
const req = {
clean: {}
};
t.notOk(is_request_sources_includes_whosonfirst(req));
t.end();
});
test('sources not \'whosonfirst\' should return false', (t) => {
const req = {
clean: {
sources: [
'not whosonfirst'
]
}
};
t.notOk(is_request_sources_includes_whosonfirst(req));
t.end();
})
};
module.exports.all = (tape, common) => {
function test(name, testFunction) {
return tape(`GET /is_request_sources_includes_whosonfirst ${name}`, testFunction);
}
for( const testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

78
test/unit/controller/predicates/is_request_sources_undefined.js

@ -0,0 +1,78 @@
const _ = require('lodash');
const is_request_sources_undefined = require('../../../../controller/predicates/is_request_sources_undefined');
module.exports.tests = {};
module.exports.tests.interface = (test, common) => {
test('valid interface', (t) => {
t.ok(_.isFunction(is_request_sources_undefined), 'is_request_sources_undefined is a function');
t.end();
});
};
module.exports.tests.true_conditions = (test, common) => {
test('undefined req should return true', (t) => {
t.ok(is_request_sources_undefined(undefined));
t.end();
});
test('undefined req.clean should return true', (t) => {
const req = {};
t.ok(is_request_sources_undefined(req));
t.end();
});
test('undefined req.clean.sources should return true', (t) => {
const req = {
clean: {}
};
t.ok(is_request_sources_undefined(req));
t.end();
});
test('empty req.clean.sources should return true', (t) => {
const req = {
clean: {
sources: []
}
};
t.ok(is_request_sources_undefined(req));
t.end();
});
};
module.exports.tests.false_conditions = (test, common) => {
test('sources not empty should return false', (t) => {
const req = {
clean: {
sources: [
'not empty'
]
}
};
t.notOk(is_request_sources_undefined(req));
t.end();
});
};
module.exports.all = (tape, common) => {
function test(name, testFunction) {
return tape(`GET /is_request_sources_undefined ${name}`, testFunction);
}
for( const testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

2
test/unit/run.js

@ -29,7 +29,9 @@ var tests = [
require('./controller/predicates/is_admin_only_analysis'),
require('./controller/predicates/is_coarse_reverse'),
require('./controller/predicates/is_only_non_admin_layers'),
require('./controller/predicates/is_request_sources_includes_whosonfirst'),
require('./controller/predicates/is_request_sources_only_whosonfirst'),
require('./controller/predicates/is_request_sources_undefined'),
require('./helper/debug'),
require('./helper/diffPlaces'),
require('./helper/fieldValue'),

Loading…
Cancel
Save