Browse Source

refactor/ add more tests

pull/7/head
Peter Johnson 10 years ago
parent
commit
18f4f6b256
  1. 6
      app.js
  2. 24
      controller/index.js
  3. 52
      controller/suggest.js
  4. 20
      index.js
  5. 11
      src/backend.js
  6. 6
      test/unit/controller/index.js
  7. 64
      test/unit/controller/suggest.js
  8. 28
      test/unit/mock/backend.js
  9. 10
      test/unit/mock/query.js
  10. 3
      test/unit/run.js

6
app.js

@ -11,14 +11,14 @@ app.use( require('./middleware/jsonp') );
/** ----------------------- routes ----------------------- **/ /** ----------------------- routes ----------------------- **/
// api root // api root
app.get( '/', require('./controller/index') ); app.get( '/', require('./controller/index')() );
// suggest API // suggest API
app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest') ); app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest')() );
/** ----------------------- error middleware ----------------------- **/ /** ----------------------- error middleware ----------------------- **/
app.use( require('./middleware/404') ); app.use( require('./middleware/404') );
app.use( require('./middleware/500') ); app.use( require('./middleware/500') );
app.listen( process.env.PORT || 3100 ); module.exports = app;

24
controller/index.js

@ -1,16 +1,22 @@
var pkg = require('../package'); var pkg = require('../package');
function controller( req, res, next ){ function setup(){
// stats function controller( req, res, next ){
res.json({
name: pkg.name, // stats
version: { res.json({
number: pkg.version name: pkg.name,
} version: {
}); number: pkg.version
}
});
}
return controller;
} }
module.exports = controller; module.exports = setup;

52
controller/suggest.js

@ -1,35 +1,41 @@
var query = require('../query/suggest'), function setup( backend, query ){
backend = require('../src/backend');
function controller( req, res, next ){ // allow overriding of dependencies
backend = backend || require('../src/backend');
query = query || require('../query/suggest');
// backend command function controller( req, res, next ){
var cmd = {
index: 'pelias',
body: query( req.clean )
};
// query backend // backend command
backend().client.suggest( cmd, function( err, data ){ var cmd = {
index: 'pelias',
body: query( req.clean )
};
var docs = []; // query backend
backend().client.suggest( cmd, function( err, data ){
// handle backend errors var docs = [];
if( err ){ return next( err ); }
// map response to a valid FeatureCollection // handle backend errors
if( data && Array.isArray( data.pelias ) && data.pelias.length ){ if( err ){ return next( err ); }
docs = data['pelias'][0].options || [];
}
// respond // map response to a valid FeatureCollection
return res.status(200).json({ if( data && Array.isArray( data.pelias ) && data.pelias.length ){
date: new Date().getTime(), docs = data['pelias'][0].options || [];
body: docs }
// respond
return res.status(200).json({
date: new Date().getTime(),
body: docs
});
}); });
});
}
return controller;
} }
module.exports = controller; module.exports = setup;

20
index.js

@ -1,9 +1,17 @@
/** cluster webserver across all cores **/
var cluster = require('cluster'), var cluster = require('cluster'),
app = require('./app'); app = require('./app'),
multicore = false,
port = ( process.env.PORT || 3100 );
cluster(app) /** cluster webserver across all cores **/
.use(cluster.stats()) if( multicore ){
.listen( process.env.PORT || 3100 ); // @todo: not finished yet
// cluster(app)
// .use(cluster.stats())
// .listen( process.env.PORT || 3100 );
}
else {
console.log( 'listening on ' + port );
app.listen( process.env.PORT || 3100 );
}

11
src/backend.js

@ -1,14 +1,7 @@
var Backend = require('geopipes-elasticsearch-backend'), var Backend = require('geopipes-elasticsearch-backend'),
backends = {}, client = require('pelias-esclient')(),
client; backends = {};
// set env specific client
if( process.env.NODE_ENV === 'test' ){
client = require('./pelias-mockclient');
} else {
client = require('pelias-esclient')();
}
function getBackend( index, type ){ function getBackend( index, type ){
var key = ( index + ':' + type ); var key = ( index + ':' + type );

6
test/unit/controller/index.js

@ -1,17 +1,19 @@
var controller = require('../../../controller/index'); var setup = require('../../../controller/index');
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) { test('valid interface', function(t) {
t.equal(typeof controller, 'function', 'controller is a function'); t.equal(typeof setup, 'function', 'setup is a function');
t.equal(typeof setup(), 'function', 'setup returns a controller');
t.end(); t.end();
}); });
}; };
module.exports.tests.info = function(test, common) { module.exports.tests.info = function(test, common) {
test('returns server info', function(t) { test('returns server info', function(t) {
var controller = setup();
var res = { json: function( json ){ var res = { json: function( json ){
t.equal(typeof json, 'object', 'returns json'); t.equal(typeof json, 'object', 'returns json');
t.equal(typeof json.name, 'string', 'name'); t.equal(typeof json.name, 'string', 'name');

64
test/unit/controller/suggest.js

@ -0,0 +1,64 @@
var setup = require('../../../controller/suggest'),
mockBackend = require('../mock/backend'),
mockQuery = require('../mock/query');
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) {
test('functional test', function(t) {
var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command');
});
var controller = setup( backend, mockQuery() );
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.true(Array.isArray(json.body), 'body is array');
t.deepEqual(json.body, [ { value: 1 }, { value: 2 } ], 'values correctly mapped');
t.end();
}
};
controller( { clean: { a: 'b' } }, res );
});
};
// functionally test controller (backend failure)
module.exports.tests.functional_failure = function(test, common) {
test('functional test', function(t) {
var backend = mockBackend( 'client/suggest/fail/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command');
});
var controller = setup( backend, mockQuery() );
var next = function( message ){
t.equal(message,'a backend error occurred','error passed to errorHandler');
t.end();
};
controller( { clean: { a: 'b' } }, undefined, next );
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('GET /suggest ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

28
test/unit/mock/backend.js

@ -0,0 +1,28 @@
var responses = {};
responses['client/suggest/ok/1'] = function( cmd, cb ){
return cb( undefined, suggestEnvelope([ { value: 1 }, { value: 2 } ]) );
};
responses['client/suggest/fail/1'] = function( cmd, cb ){
return cb( 'a backend error occurred' );
};
function setup( key, cmdCb ){
function backend( a, b ){
return {
client: {
suggest: function( cmd, cb ){
if( 'function' === typeof cmdCb ){ cmdCb( cmd ); }
return responses[key].apply( this, arguments );
}
}
};
}
return backend;
}
function suggestEnvelope( options ){
return { pelias: [{ options: options }]};
}
module.exports = setup;

10
test/unit/mock/query.js

@ -0,0 +1,10 @@
function setup(){
return query;
}
function query( clean ){
return clean;
}
module.exports = setup;

3
test/unit/run.js

@ -3,7 +3,8 @@ var tape = require('tape');
var common = {}; var common = {};
var tests = [ var tests = [
require('./controller/index') require('./controller/index'),
require('./controller/suggest')
]; ];
tests.map(function(t) { tests.map(function(t) {

Loading…
Cancel
Save