Browse Source

cleanup + tests

pull/59/head
Harish Krishna 10 years ago
parent
commit
bcf823c519
  1. 4
      controller/suggest.js
  2. 6
      query/suggest.js
  3. 7
      service/suggest.js
  4. 14
      test/unit/controller/suggest.js
  5. 6
      test/unit/mock/backend.js
  6. 6
      test/unit/query/suggest.js
  7. 6
      test/unit/service/suggest.js

4
controller/suggest.js

@ -21,6 +21,8 @@ function setup( backend, query, query_mixer ){
body: query( req.clean, query_mixer ) body: query( req.clean, query_mixer )
}; };
var size = req.clean.size || 10;
// responder // responder
function reply( docs ){ function reply( docs ){
@ -41,7 +43,7 @@ function setup( backend, query, query_mixer ){
if( err ){ return next( err ); } if( err ){ return next( err ); }
// pick the required number of results // pick the required number of results
suggested = resultsHelper.picker(suggested, req.clean.size); suggested = resultsHelper.picker(suggested, size);
// no documents suggested, return empty array to avoid ActionRequestValidationException // no documents suggested, return empty array to avoid ActionRequestValidationException
if( !Array.isArray( suggested ) || !suggested.length ){ if( !Array.isArray( suggested ) || !suggested.length ){

6
query/suggest.js

@ -51,14 +51,14 @@ function generate( params, query_mixer, fuzziness ){
query_mixer.forEach(function(item, index){ query_mixer.forEach(function(item, index){
if (item.precision && Array.isArray( item.precision ) && item.precision.length ) { if (item.precision && Array.isArray( item.precision ) && item.precision.length ) {
item.precision.forEach(function(precision) { item.precision.forEach(function(precision) {
cmd.add_suggester('pelias_'+index, precision, item.layers, item.fuzzy); cmd.add_suggester(index, precision, item.layers, item.fuzzy);
}); });
} else { } else {
cmd.add_suggester('pelias_'+index, undefined, item.layers, item.fuzzy); cmd.add_suggester(index, undefined, item.layers, item.fuzzy);
} }
}); });
} else { } else {
cmd.add_suggester('pelias_0'); cmd.add_suggester(0);
} }

7
service/suggest.js

@ -20,15 +20,14 @@ function service( backend, cmd, cb ){
return Array.isArray( obj ) && obj.length && obj[0].options && obj[0].options.length; return Array.isArray( obj ) && obj.length && obj[0].options && obj[0].options.length;
}; };
for (var i=0, j=0; i<num_keys && j<num_keys; i++) { for (var i=0, j=0; i<num_keys && j<num_keys; i++) {
var keys = 'pelias_'+i; if ( has_docs(data[i]) ){
if ( has_docs(data[keys]) ){
docs[i] = docs[i] || []; docs[i] = docs[i] || [];
var res = data[keys][0].options[0]; var res = data[i][0].options[0];
if (unique_ids.indexOf(res.text) === -1) { if (unique_ids.indexOf(res.text) === -1) {
docs[i].push(res); docs[i].push(res);
unique_ids.push(res.text); unique_ids.push(res.text);
} }
data[keys][0].options.splice(0,1); data[i][0].options.splice(0,1);
} else { } else {
j++; j++;
} }

14
test/unit/controller/suggest.js

@ -57,18 +57,10 @@ module.exports.tests.functional_success = function(test, common) {
if( cmd.body.docs ){ if( cmd.body.docs ){
t.deepEqual(cmd, { t.deepEqual(cmd, {
body: { docs: [ body: { docs: [
{ _id: 'mockid2', _index: 'pelias', _type: 'mocktype' }, { _id: 'mockid1', _index: 'pelias', _type: 'mocktype' },
{ _id: 'mockid1', _index: 'pelias', _type: 'mocktype' } { _id: 'mockid2', _index: 'pelias', _type: 'mocktype' }
]} ]}
}, 'correct mget command'); }, 'correct mget command');
} else if (cmd.body.layers) {
// layers are set exclusively for admin: test for admin-only layers
t.deepEqual(cmd, {
body: {
input: 'b',
layers: [ 'admin0', 'admin1', 'admin2' ]
}, index: 'pelias'
}, 'correct suggest/admin command');
} else { } else {
t.deepEqual(cmd, { body: { input: 'b' }, index: 'pelias' }, 'correct suggest command'); t.deepEqual(cmd, { body: { input: 'b' }, index: 'pelias' }, 'correct suggest command');
} }
@ -84,7 +76,7 @@ module.exports.tests.functional_success = function(test, common) {
t.equal(typeof json.date, 'number', 'date set'); t.equal(typeof json.date, 'number', 'date set');
t.equal(json.type, 'FeatureCollection', 'valid geojson'); t.equal(json.type, 'FeatureCollection', 'valid geojson');
t.true(Array.isArray(json.features), 'features is array'); t.true(Array.isArray(json.features), 'features is array');
// t.deepEqual(json.features, expected, 'values correctly mapped'); t.deepEqual(json.features, expected, 'values correctly mapped');
t.end(); t.end();
} }
}; };

6
test/unit/mock/backend.js

@ -1,7 +1,7 @@
var responses = {}; var responses = {};
responses['client/suggest/ok/1'] = function( cmd, cb ){ responses['client/suggest/ok/1'] = function( cmd, cb ){
return cb( undefined, suggestEnvelope([ { score: 1, text: 'mocktype:mockid1' }, { score: 2, text: 'mocktype:mockid2' } ]) ); return cb( undefined, suggestEnvelope([ { score: 1, text: 'mocktype:mockid1' } ], [ { score: 2, text: 'mocktype:mockid2' } ]) );
}; };
responses['client/suggest/fail/1'] = function( cmd, cb ){ responses['client/suggest/fail/1'] = function( cmd, cb ){
return cb( 'a backend error occurred' ); return cb( 'a backend error occurred' );
@ -82,8 +82,8 @@ function mgetEnvelope( options ){
return { docs: options }; return { docs: options };
} }
function suggestEnvelope( options ){ function suggestEnvelope( options1, options2 ){
return { pelias: [{ options: options }]}; return { 0: [{ options: options1 }], 1: [{ options: options2 }]};
} }
function searchEnvelope( options ){ function searchEnvelope( options ){

6
test/unit/query/suggest.js

@ -19,7 +19,7 @@ module.exports.tests.query = function(test, common) {
}); });
var expected = { var expected = {
text: 'test', text: 'test',
pelias_0: { 0: {
completion: { completion: {
field: 'suggest', field: 'suggest',
size: 10, size: 10,
@ -73,7 +73,7 @@ module.exports.tests.precision = function(test, common) {
}); });
var expected = { var expected = {
text: 'test', text: 'test',
pelias_0: { 0: {
completion: { completion: {
field: 'suggest', field: 'suggest',
size: 10, size: 10,
@ -105,7 +105,7 @@ module.exports.tests.fuzziness = function(test, common) {
}, undefined, test_case); }, undefined, test_case);
var expected = { var expected = {
text: 'test', text: 'test',
pelias_0: { 0: {
completion: { completion: {
field: 'suggest', field: 'suggest',
size: 10, size: 10,

6
test/unit/service/suggest.js

@ -17,8 +17,8 @@ module.exports.tests.interface = function(test, common) {
module.exports.tests.functional_success = function(test, common) { module.exports.tests.functional_success = function(test, common) {
var expected = [ var expected = [
{ score: 1, text: 'mocktype:mockid1' }, [{ score: 1, text: 'mocktype:mockid1' }],
{ score: 2, text: 'mocktype:mockid2' } [{ score: 2, text: 'mocktype:mockid2' }]
]; ];
test('valid ES query', function(t) { test('valid ES query', function(t) {
@ -30,7 +30,7 @@ module.exports.tests.functional_success = function(test, common) {
data.forEach(function(d) { data.forEach(function(d) {
t.true(typeof d === 'object', 'valid object'); t.true(typeof d === 'object', 'valid object');
}); });
// t.deepEqual(data, expected, 'values correctly mapped'); t.deepEqual(data, expected, 'values correctly mapped');
t.end(); t.end();
}); });
}); });

Loading…
Cancel
Save