Browse Source

Merge pull request #39 from pelias/service_tests

Testing the service wrapper.
pull/42/merge
Harish Krishna 10 years ago
parent
commit
ff0bee128c
  1. 92
      test/unit/controller/doc.js
  2. 27
      test/unit/mock/backend.js
  3. 4
      test/unit/run.js
  4. 82
      test/unit/service/mget.js
  5. 82
      test/unit/service/search.js
  6. 77
      test/unit/service/suggest.js

92
test/unit/controller/doc.js

@ -0,0 +1,92 @@
var setup = require('../../../controller/doc'),
mockBackend = require('../mock/backend');
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) {
t.equal(typeof setup, 'function', 'setup is a function');
t.equal(typeof setup(), 'function', 'setup returns a controller');
t.end();
});
};
// functionally test controller (backend success)
module.exports.tests.functional_success = function(test, common) {
// expected geojson features for 'client/doc/ok/1' fixture
var expected = [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -50.5, 100.1 ]
},
properties: {
name: 'test name1',
admin0: 'country1',
admin1: 'state1',
admin2: 'city1'
}
}, {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ -51.5, 100.2 ]
},
properties: {
name: 'test name2',
admin0: 'country2',
admin1: 'state2',
admin2: 'city2'
}
}];
test('functional success', function(t) {
var backend = mockBackend( 'client/doc/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command');
});
var controller = setup( backend );
var res = {
status: function( code ){
t.equal(code, 200, 'status set');
return res;
},
json: function( json ){
t.equal(typeof json, 'object', 'returns json');
t.equal(typeof json.date, 'number', 'date set');
t.equal(json.type, 'FeatureCollection', 'valid geojson');
t.true(Array.isArray(json.features), 'features is array');
t.deepEqual(json.features, expected, 'values correctly mapped');
t.end();
}
};
controller( { clean: { ids: [ {'id' : 123, 'type': 'a' } ] } }, res );
});
};
// functionally test controller (backend failure)
module.exports.tests.functional_failure = function(test, common) {
test('functional failure', function(t) {
var backend = mockBackend( 'client/doc/fail/1', function( cmd ){
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'b' } ] } }, 'correct backend command');
});
var controller = setup( backend );
var next = function( message ){
t.equal(message,'a backend error occurred','error passed to errorHandler');
t.end();
};
controller( { clean: { ids: [ {'id' : 123, 'type': 'b' } ] } }, undefined, next );
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('GET /doc ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

27
test/unit/mock/backend.js

@ -32,10 +32,33 @@ responses['client/search/fail/1'] = function( cmd, cb ){
return cb( 'a backend error occurred' );
};
responses['client/doc/ok/1'] = function( cmd, cb ){
return cb( undefined, docEnvelope([{
_source: {
value: 1,
center_point: { lat: 100.1, lon: -50.5 },
name: { default: 'test name1' },
admin0: 'country1', admin1: 'state1', admin2: 'city1'
}
}, {
_source: {
value: 2,
center_point: { lat: 100.2, lon: -51.5 },
name: { default: 'test name2' },
admin0: 'country2', admin1: 'state2', admin2: 'city2'
}
}]));
};
responses['client/doc/fail/1'] = responses['client/search/fail/1'];
function setup( key, cmdCb ){
function backend( a, b ){
return {
client: {
mget: function( cmd, cb ){
if( 'function' === typeof cmdCb ){ cmdCb( cmd ); }
return responses[key].apply( this, arguments );
},
suggest: function( cmd, cb ){
if( 'function' === typeof cmdCb ){ cmdCb( cmd ); }
return responses[key].apply( this, arguments );
@ -50,6 +73,10 @@ function setup( key, cmdCb ){
return backend;
}
function docEnvelope( options ){
return { docs: options };
}
function suggestEnvelope( options ){
return { pelias: [{ options: options }]};
}

4
test/unit/run.js

@ -4,8 +4,12 @@ var common = {};
var tests = [
require('./controller/index'),
require('./controller/doc'),
require('./controller/suggest'),
require('./controller/search'),
require('./service/mget'),
require('./service/search'),
require('./service/suggest'),
require('./sanitiser/suggest'),
require('./sanitiser/doc'),
require('./query/indeces'),

82
test/unit/service/mget.js

@ -0,0 +1,82 @@
var setup = require('../../../service/mget'),
mockBackend = require('../mock/backend');
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) {
t.equal(typeof setup, 'function', 'setup is a function');
t.end();
});
};
// functionally test service
module.exports.tests.functional_success = function(test, common) {
var expected = [
{
value: 1,
center_point: { lat: 100.1, lon: -50.5 },
name: { default: 'test name1' },
admin0: 'country1', admin1: 'state1', admin2: 'city1'
},
{
value: 2,
center_point: { lat: 100.2, lon: -51.5 },
name: { default: 'test name2' },
admin0: 'country2', admin1: 'state2', admin2: 'city2'
}
];
test('valid query', function(t) {
var backend = mockBackend( 'client/doc/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command');
});
setup( backend, [ { _id: 123, _index: 'pelias', _type: 'a' } ], function(err, data) {
t.true(Array.isArray(data), 'returns an array');
data.forEach(function(d) {
t.true(typeof d === 'object', 'valid object');
});
t.deepEqual(data, expected, 'values correctly mapped')
t.end();
});
});
};
// functionally test service
module.exports.tests.functional_failure = function(test, common) {
test('invalid query', function(t) {
var invalid_queries = [
{ _id: 123, _index: 'pelias' },
{ _id: 123, _type: 'a' },
{ _index: 'pelias', _type: 'a' },
{ }
];
var backend = mockBackend( 'client/doc/fail/1', function( cmd ){
t.notDeepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'incorrect backend command');
});
invalid_queries.forEach(function(query) {
setup( backend, [ query ], function(err, data) {
t.equal(err, 'a backend error occurred','error passed to errorHandler');
t.equal(data, undefined, 'data is undefined');
});
});
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SERVICE /mget ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

82
test/unit/service/search.js

@ -0,0 +1,82 @@
var setup = require('../../../service/search'),
mockBackend = require('../mock/backend');
var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' };
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) {
t.equal(typeof setup, 'function', 'setup is a function');
t.end();
});
};
// functionally test service
module.exports.tests.functional_success = function(test, common) {
var expected = [
{
value: 1,
center_point: { lat: 100.1, lon: -50.5 },
name: { default: 'test name1' },
admin0: 'country1', admin1: 'state1', admin2: 'city1'
},
{
value: 2,
center_point: { lat: 100.2, lon: -51.5 },
name: { default: 'test name2' },
admin0: 'country2', admin1: 'state2', admin2: 'city2'
}
];
test('valid ES query', function(t) {
var backend = mockBackend( 'client/search/ok/1', function( cmd ){
t.deepEqual(cmd, example_valid_es_query, 'no change to the command');
});
setup( backend, example_valid_es_query, function(err, data) {
t.true(Array.isArray(data), 'returns an array');
data.forEach(function(d) {
t.true(typeof d === 'object', 'valid object');
});
t.deepEqual(data, expected, 'values correctly mapped')
t.end();
});
});
};
// functionally test service
module.exports.tests.functional_failure = function(test, common) {
test('invalid ES query', function(t) {
var invalid_queries = [
{ },
{ foo: 'bar' }
];
var backend = mockBackend( 'client/search/fail/1', function( cmd ){
t.notDeepEqual(cmd, example_valid_es_query, 'incorrect backend command');
});
invalid_queries.forEach(function(query) {
setup( backend, [ query ], function(err, data) {
t.equal(err, 'a backend error occurred','error passed to errorHandler');
t.equal(data, undefined, 'data is undefined');
});
});
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SERVICE /search ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

77
test/unit/service/suggest.js

@ -0,0 +1,77 @@
var setup = require('../../../service/suggest'),
mockBackend = require('../mock/backend');
var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' };
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) {
t.equal(typeof setup, 'function', 'setup is a function');
t.end();
});
};
// functionally test service
module.exports.tests.functional_success = function(test, common) {
var mockPayload = {
id: 'mocktype/mockid',
geo: '101,-10.1'
};
var expected = [
{ value: 1, payload: mockPayload },
{ value: 2, payload: mockPayload }
];
test('valid ES query', function(t) {
var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){
t.deepEqual(cmd, example_valid_es_query, 'no change to the command');
});
setup( backend, example_valid_es_query, function(err, data) {
t.true(Array.isArray(data), 'returns an array');
data.forEach(function(d) {
t.true(typeof d === 'object', 'valid object');
});
t.deepEqual(data, expected, 'values correctly mapped')
t.end();
});
});
};
// functionally test service
module.exports.tests.functional_failure = function(test, common) {
test('invalid ES query', function(t) {
var invalid_queries = [
{ },
{ foo: 'bar' }
];
var backend = mockBackend( 'client/suggest/fail/1', function( cmd ){
t.notDeepEqual(cmd, example_valid_es_query, 'incorrect backend command');
});
invalid_queries.forEach(function(query) {
setup( backend, [ query ], function(err, data) {
t.equal(err, 'a backend error occurred','error passed to errorHandler');
t.equal(data, undefined, 'data is undefined');
});
});
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SERVICE /suggest ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
Loading…
Cancel
Save