Browse Source

fixed text analysis tests

proxyquire is now used because the text_analyzer package requires node_postal which isn't guaranteed to be available
pull/666/head
Stephen Hess 8 years ago
parent
commit
c18baaf775
  1. 3
      sanitiser/_text.js
  2. 130
      test/unit/sanitiser/_text.js

3
sanitiser/_text.js

@ -8,13 +8,14 @@ function sanitize( raw, clean ){
var messages = { errors: [], warnings: [] };
// invalid input 'text'
// must call `!check.nonEmptyString` since `check.emptyString` returns
// `false` for `undefined` and `null`
if( !check.nonEmptyString( raw.text ) ){
messages.errors.push('invalid param \'text\': text length, must be >0');
}
// valid input 'text'
else {
// valid text
clean.text = raw.text;

130
test/unit/sanitiser/_text.js

@ -1,23 +1,147 @@
var sanitiser = require('../../../sanitiser/_text');
var type_mapping = require('../../../helper/type_mapping');
var proxyquire = require('proxyquire').noCallThru();
module.exports.tests = {};
module.exports.tests.text_parser = function(test, common) {
test('short input text has admin layers set ', function(t) {
test('non-empty raw.text should call analyzer and set clean.text and clean.parsed_text', function(t) {
var mock_analyzer_response = {
key1: 'value 1',
key2: 'value 2'
};
var sanitiser = proxyquire('../../../sanitiser/_text', {
'pelias-text-analyzer': { parse: function(query) {
return mock_analyzer_response;
}
}});
var raw = {
text: 'emp' //start of empire state building
text: 'raw input'
};
var clean = {
};
var expected_clean = {
text: raw.text,
parsed_text: mock_analyzer_response
};
var messages = sanitiser(raw, clean);
t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, [], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings');
t.end();
});
test('empty raw.text should add error message', function(t) {
var sanitiser = proxyquire('../../../sanitiser/_text', {
'pelias-text-analyzer': { parse: function(query) {
throw new Error('analyzer should not have been called');
}
}});
var raw = {
text: ''
};
var clean = {
};
var expected_clean = {
};
var messages = sanitiser(raw, clean);
t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, ['invalid param \'text\': text length, must be >0'], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings');
t.end();
});
test('undefined raw.text should add error message', function(t) {
var sanitiser = proxyquire('../../../sanitiser/_text', {
'pelias-text-analyzer': { parse: function(query) {
throw new Error('analyzer should not have been called');
}
}});
var raw = {
text: undefined
};
var clean = {
};
var expected_clean = {
};
var messages = sanitiser(raw, clean);
t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, ['invalid param \'text\': text length, must be >0'], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings');
t.end();
});
test('text_analyzer.parse returning undefined should not overwrite clean.parsed_text', function(t) {
var sanitiser = proxyquire('../../../sanitiser/_text', {
'pelias-text-analyzer': { parse: function(query) {
return undefined;
}
}});
var raw = {
text: 'raw input'
};
var clean = {
parsed_text: 'original clean.parsed_text'
};
var expected_clean = {
text: raw.text,
parsed_text: 'original clean.parsed_text'
};
var messages = sanitiser(raw, clean);
t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, [], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings');
t.end();
});
test('text_analyzer.parse returning null should not overwrite clean.parsed_text', function(t) {
var sanitiser = proxyquire('../../../sanitiser/_text', {
'pelias-text-analyzer': { parse: function(query) {
return null;
}
}});
var raw = {
text: 'raw input'
};
var clean = {
parsed_text: 'original clean.parsed_text'
};
var expected_clean = {
text: raw.text,
parsed_text: 'original clean.parsed_text'
};
var messages = sanitiser(raw, clean);
t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, [], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings');
t.end();
});
};
module.exports.all = function (tape, common) {

Loading…
Cancel
Save