Browse Source

Merge branch 'master' into staging

pull/756/head
Julian Simioni 8 years ago
parent
commit
aacb7afb3e
No known key found for this signature in database
GPG Key ID: 6DAD08919FDBF563
  1. 4
      package.json
  2. 20
      query/search.js
  3. 18
      sanitizer/_synthesize_analysis.js
  4. 1
      sanitizer/search.js
  5. 158
      test/unit/query/search.js
  6. 16
      test/unit/sanitizer/_synthesize_analysis.js
  7. 5
      test/unit/sanitizer/search.js

4
package.json

@ -53,11 +53,11 @@
"morgan": "1.7.0", "morgan": "1.7.0",
"pelias-categories": "1.1.0", "pelias-categories": "1.1.0",
"pelias-config": "2.3.1", "pelias-config": "2.3.1",
"pelias-labels": "1.5.0", "pelias-labels": "1.5.1",
"pelias-logger": "0.1.0", "pelias-logger": "0.1.0",
"pelias-model": "4.3.0", "pelias-model": "4.3.0",
"pelias-query": "8.9.0", "pelias-query": "8.9.0",
"pelias-text-analyzer": "1.5.0", "pelias-text-analyzer": "1.6.0",
"stats-lite": "2.0.3", "stats-lite": "2.0.3",
"through2": "2.0.1" "through2": "2.0.1"
}, },

20
query/search.js

@ -120,7 +120,7 @@ function generateQuery( clean ){
} }
function getQuery(vs) { function getQuery(vs) {
if (hasStreet(vs) || isCityStateOnlyWithOptionalCountry(vs)) { if (hasStreet(vs) || isCityStateOnlyWithOptionalCountry(vs) || isCityCountryOnly(vs)) {
return { return {
type: 'fallback', type: 'fallback',
body: fallbackQuery.render(vs) body: fallbackQuery.render(vs)
@ -138,8 +138,8 @@ function hasStreet(vs) {
} }
function isCityStateOnlyWithOptionalCountry(vs) { function isCityStateOnlyWithOptionalCountry(vs) {
var isSet = function(layer) { var isSet = (layer) => {
return vs.isset('input:' + layer); return vs.isset(`input:${layer}`);
}; };
var allowedFields = ['locality', 'region']; var allowedFields = ['locality', 'region'];
@ -150,4 +150,18 @@ function isCityStateOnlyWithOptionalCountry(vs) {
} }
function isCityCountryOnly(vs) {
var isSet = (layer) => {
return vs.isset(`input:${layer}`);
};
var allowedFields = ['locality', 'country'];
var disallowedFields = ['query', 'category', 'housenumber', 'street',
'neighbourhood', 'borough', 'postcode', 'county', 'region'];
return allowedFields.every(isSet) &&
!disallowedFields.some(isSet);
}
module.exports = generateQuery; module.exports = generateQuery;

18
sanitizer/_synthesize_analysis.js

@ -1,7 +1,15 @@
const _ = require('lodash'); const _ = require('lodash');
const fields = ['address', 'neighbourhood', 'borough', 'city', 'county', const fields = {
'state', 'postalcode', 'country']; 'address': 'address',
'neighbourhood': 'neighbourhood',
'borough': 'borough',
'locality': 'city',
'county': 'county',
'region': 'state',
'postalcode': 'postalcode',
'country': 'country'
};
function normalizeWhitespaceToSingleSpace(val) { function normalizeWhitespaceToSingleSpace(val) {
return _.replace(_.trim(val), /\s+/g, ' '); return _.replace(_.trim(val), /\s+/g, ' ');
@ -18,9 +26,9 @@ function sanitize( raw, clean ){
const messages = { errors: [], warnings: [] }; const messages = { errors: [], warnings: [] };
// collect all the valid values into a single object // collect all the valid values into a single object
clean.parsed_text = fields.reduce( (o, f) => { clean.parsed_text = Object.keys(fields).reduce( (o, f) => {
if (_.isString(raw[f]) && !_.isEmpty(_.trim(raw[f]))) { if (_.isString(raw[f]) && !_.isEmpty(_.trim(raw[f]))) {
o[f] = normalizeWhitespaceToSingleSpace(raw[f]); o[fields[f]] = normalizeWhitespaceToSingleSpace(raw[f]);
} }
return o; return o;
@ -32,7 +40,7 @@ function sanitize( raw, clean ){
} }
else if (_.isEmpty(Object.keys(clean.parsed_text))) { else if (_.isEmpty(Object.keys(clean.parsed_text))) {
messages.errors.push( messages.errors.push(
`at least one of the following fields is required: ${fields.join(', ')}`); `at least one of the following fields is required: ${Object.keys(fields).join(', ')}`);
} }
return messages; return messages;

1
sanitizer/search.js

@ -5,6 +5,7 @@ var sanitizeAll = require('../sanitizer/sanitizeAll'),
quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'), quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'),
singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), singleScalarParameters: require('../sanitizer/_single_scalar_parameters'),
text: require('../sanitizer/_text'), text: require('../sanitizer/_text'),
iso2_to_iso3: require('../sanitizer/_iso2_to_iso3'),
size: require('../sanitizer/_size')(/* use defaults*/), size: require('../sanitizer/_size')(/* use defaults*/),
layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping),
sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping),

158
test/unit/query/search.js

@ -447,6 +447,164 @@ module.exports.tests.city_state = function(test, common) {
}; };
module.exports.tests.city_country = function(test, common) {
test('only city and country set should return query', function(t) {
var clean = {
parsed_text: {
'city': 'city value',
'country': 'country value'
}
};
var query = generate(clean);
t.notEqual(query, undefined, 'should not have returned undefined');
t.end();
});
test('city-only should return undefined', function(t) {
var clean = {
parsed_text: {
city: 'city value'
}
};
var query = generate(clean);
t.equals(query, undefined, 'should have returned undefined');
t.end();
});
test('country-only should return undefined', function(t) {
var clean = {
parsed_text: {
country: 'country value'
}
};
var query = generate(clean);
t.equals(query, undefined, 'should have returned undefined');
t.end();
});
test('city/country with query should return undefined', function(t) {
var clean = {
parsed_text: {
city: 'city value',
country: 'country value',
query: 'query value'
}
};
var query = generate(clean);
t.equals(query, undefined, 'should have returned undefined');
t.end();
});
test('city/country with category should return undefined', function(t) {
var clean = {
parsed_text: {
city: 'city value',
country: 'country value',
category: 'category value'
}
};
var query = generate(clean);
t.equals(query, undefined, 'should have returned undefined');
t.end();
});
test('city/country with number should return undefined', function(t) {
var clean = {
parsed_text: {
city: 'city value',
country: 'country value',
number: 'number value'
}
};
var query = generate(clean);
t.equals(query, undefined, 'should have returned undefined');
t.end();
});
test('city/country with neighbourhood should return undefined', function(t) {
var clean = {
parsed_text: {
city: 'city value',
country: 'country value',
neighbourhood: 'neighbourhood value'
}
};
var query = generate(clean);
t.equals(query, undefined, 'should have returned undefined');
t.end();
});
test('city/country with borough should return undefined', function(t) {
var clean = {
parsed_text: {
city: 'city value',
country: 'country value',
borough: 'borough value'
}
};
var query = generate(clean);
t.equals(query, undefined, 'should have returned undefined');
t.end();
});
test('city/country with postalcode should return undefined', function(t) {
var clean = {
parsed_text: {
city: 'city value',
country: 'country value',
postalcode: 'postalcode value'
}
};
var query = generate(clean);
t.equals(query, undefined, 'should have returned undefined');
t.end();
});
test('city/country with county should return undefined', function(t) {
var clean = {
parsed_text: {
city: 'city value',
country: 'country value',
county: 'county value'
}
};
var query = generate(clean);
t.equals(query, undefined, 'should have returned undefined');
t.end();
});
};
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {
function test(name, testFunction) { function test(name, testFunction) {
return tape('search query ' + name, testFunction); return tape('search query ' + name, testFunction);

16
test/unit/sanitizer/_synthesize_analysis.js

@ -10,9 +10,9 @@ module.exports.tests.text_parser = function(test, common) {
address: ' \t address \t value \t ', address: ' \t address \t value \t ',
neighbourhood: ' \t neighbourhood \t value \t ', neighbourhood: ' \t neighbourhood \t value \t ',
borough: ' \t borough \t value \t ', borough: ' \t borough \t value \t ',
city: ' \t city \t value \t ', locality: ' \t locality \t value \t ',
county: ' \t county \t value \t ', county: ' \t county \t value \t ',
state: ' \t state \t value \t ', region: ' \t region \t value \t ',
postalcode: ' \t postalcode \t value \t ', postalcode: ' \t postalcode \t value \t ',
country: ' \t country \t value \t ' country: ' \t country \t value \t '
}; };
@ -24,9 +24,9 @@ module.exports.tests.text_parser = function(test, common) {
address: 'address value', address: 'address value',
neighbourhood: 'neighbourhood value', neighbourhood: 'neighbourhood value',
borough: 'borough value', borough: 'borough value',
city: 'city value', city: 'locality value',
county: 'county value', county: 'county value',
state: 'state value', state: 'region value',
postalcode: 'postalcode value', postalcode: 'postalcode value',
country: 'country value' country: 'country value'
} }
@ -51,9 +51,9 @@ module.exports.tests.text_parser = function(test, common) {
address: getInvalidValue(), address: getInvalidValue(),
neighbourhood: getInvalidValue(), neighbourhood: getInvalidValue(),
borough: getInvalidValue(), borough: getInvalidValue(),
city: getInvalidValue(), locality: getInvalidValue(),
county: getInvalidValue(), county: getInvalidValue(),
state: getInvalidValue(), region: getInvalidValue(),
postalcode: getInvalidValue(), postalcode: getInvalidValue(),
country: getInvalidValue() country: getInvalidValue()
}; };
@ -68,7 +68,7 @@ module.exports.tests.text_parser = function(test, common) {
t.deepEquals(clean, expected_clean); t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, ['at least one of the following fields is required: ' + t.deepEquals(messages.errors, ['at least one of the following fields is required: ' +
'address, neighbourhood, borough, city, county, state, postalcode, country'], 'no errors'); 'address, neighbourhood, borough, locality, county, region, postalcode, country'], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings'); t.deepEquals(messages.warnings, [], 'no warnings');
t.end(); t.end();
@ -85,7 +85,7 @@ module.exports.tests.text_parser = function(test, common) {
t.deepEquals(clean, expected_clean); t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, ['at least one of the following fields is required: ' + t.deepEquals(messages.errors, ['at least one of the following fields is required: ' +
'address, neighbourhood, borough, city, county, state, postalcode, country'], 'no errors'); 'address, neighbourhood, borough, locality, county, region, postalcode, country'], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings'); t.deepEquals(messages.warnings, [], 'no warnings');
t.end(); t.end();

5
test/unit/sanitizer/search.js

@ -21,6 +21,10 @@ module.exports.tests.sanitize = function(test, common) {
called_sanitizers.push('_text'); called_sanitizers.push('_text');
return { errors: [], warnings: [] }; return { errors: [], warnings: [] };
}, },
'../sanitizer/_iso2_to_iso3': function() {
called_sanitizers.push('_iso2_to_iso3');
return { errors: [], warnings: [] };
},
'../sanitizer/_size': function() { '../sanitizer/_size': function() {
if (arguments.length === 0) { if (arguments.length === 0) {
return function() { return function() {
@ -81,6 +85,7 @@ module.exports.tests.sanitize = function(test, common) {
'_deprecate_quattroshapes', '_deprecate_quattroshapes',
'_single_scalar_parameters', '_single_scalar_parameters',
'_text', '_text',
'_iso2_to_iso3',
'_size', '_size',
'_targets/layers', '_targets/layers',
'_targets/sources', '_targets/sources',

Loading…
Cancel
Save