Browse Source

don't call libpostal if sources=whosonfirst

pull/912/head
Stephen Hess 7 years ago
parent
commit
30cd30236a
  1. 20
      sanitizer/_text.js
  2. 61
      test/unit/sanitizer/_text.js

20
sanitizer/_text.js

@ -1,31 +1,33 @@
var check = require('check-types'), const check = require('check-types');
text_analyzer = require('pelias-text-analyzer'); const text_analyzer = require('pelias-text-analyzer');
const _ = require('lodash');
// validate texts, convert types and apply defaults // validate texts, convert types and apply defaults
function sanitize( raw, clean ){ function sanitize( raw, clean ){
// error & warning messages // error & warning messages
var messages = { errors: [], warnings: [] }; const messages = { errors: [], warnings: [] };
// invalid input 'text' // invalid input 'text'
// must call `!check.nonEmptyString` since `check.emptyString` returns // must call `!check.nonEmptyString` since `check.emptyString` returns
// `false` for `undefined` and `null` // `false` for `undefined` and `null`
if( !check.nonEmptyString( raw.text ) ){ if( !check.nonEmptyString( raw.text ) ){
messages.errors.push('invalid param \'text\': text length, must be >0'); messages.errors.push('invalid param \'text\': text length, must be >0');
}
// valid input 'text' } else {
else {
// valid text
clean.text = raw.text; clean.text = raw.text;
// only call libpostal if there are other sources besides whosonfirst
// since placeholder will take care of it later
if (!_.isEqual(clean.sources, ['whosonfirst'])) {
// parse text with query parser // parse text with query parser
var parsed_text = text_analyzer.parse(clean.text); const parsed_text = text_analyzer.parse(clean.text);
if (check.assigned(parsed_text)) { if (check.assigned(parsed_text)) {
clean.parsed_text = parsed_text; clean.parsed_text = parsed_text;
} }
} }
}
return messages; return messages;
} }

61
test/unit/sanitizer/_text.js

@ -142,6 +142,67 @@ module.exports.tests.text_parser = function(test, common) {
}); });
test('sources=whosonfirst should not call text_analyzer and set clean.text from raw.text', (t) => {
const sanitizer = proxyquire('../../../sanitizer/_text', {
'pelias-text-analyzer': { parse: query => t.fail('should not have been called') }
});
const raw = {
text: 'raw clean.text'
};
const clean = {
sources: ['whosonfirst'],
text: 'original clean.text'
};
const expected_clean = {
sources: ['whosonfirst'],
text: 'raw clean.text'
};
const messages = sanitizer(raw, clean);
t.deepEquals(clean, expected_clean);
t.deepEquals(messages, { errors: [], warnings: [] });
t.end();
});
test('sources with whosonfirst + others should call analyzer', (t) => {
const sanitizer = proxyquire('../../../sanitizer/_text', {
'pelias-text-analyzer': { parse: function(query) {
return {
key1: 'value 1',
key2: 'value 2'
};
}
}});
const raw = {
text: 'raw text'
};
const clean = {
sources: ['whosonfirst', 'another source'],
text: 'clean text'
};
const expected_clean = {
sources: ['whosonfirst', 'another source'],
text: 'raw text',
parsed_text: {
key1: 'value 1',
key2: 'value 2'
}
};
const messages = sanitizer(raw, clean);
t.deepEquals(clean, expected_clean);
t.deepEquals(messages, { errors: [], warnings: [] });
t.end();
});
}; };
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {

Loading…
Cancel
Save