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 ----------------------- **/
// api root
app.get( '/', require('./controller/index') );
app.get( '/', require('./controller/index')() );
// suggest API
app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest') );
app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest')() );
/** ----------------------- error middleware ----------------------- **/
app.use( require('./middleware/404') );
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');
function controller( req, res, next ){
function setup(){
// stats
res.json({
name: pkg.name,
version: {
number: pkg.version
}
});
function controller( req, res, next ){
// stats
res.json({
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'),
backend = require('../src/backend');
function setup( backend, query ){
function controller( req, res, next ){
// allow overriding of dependencies
backend = backend || require('../src/backend');
query = query || require('../query/suggest');
// backend command
var cmd = {
index: 'pelias',
body: query( req.clean )
};
function controller( req, res, next ){
// query backend
backend().client.suggest( cmd, function( err, data ){
// backend command
var cmd = {
index: 'pelias',
body: query( req.clean )
};
var docs = [];
// query backend
backend().client.suggest( cmd, function( err, data ){
// handle backend errors
if( err ){ return next( err ); }
var docs = [];
// map response to a valid FeatureCollection
if( data && Array.isArray( data.pelias ) && data.pelias.length ){
docs = data['pelias'][0].options || [];
}
// handle backend errors
if( err ){ return next( err ); }
// respond
return res.status(200).json({
date: new Date().getTime(),
body: docs
// map response to a valid FeatureCollection
if( data && Array.isArray( data.pelias ) && data.pelias.length ){
docs = data['pelias'][0].options || [];
}
// 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'),
app = require('./app');
app = require('./app'),
multicore = false,
port = ( process.env.PORT || 3100 );
cluster(app)
.use(cluster.stats())
.listen( process.env.PORT || 3100 );
/** cluster webserver across all cores **/
if( multicore ){
// @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'),
backends = {},
client;
// set env specific client
if( process.env.NODE_ENV === 'test' ){
client = require('./pelias-mockclient');
} else {
client = require('pelias-esclient')();
}
client = require('pelias-esclient')(),
backends = {};
function getBackend( 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.interface = function(test, common) {
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();
});
};
module.exports.tests.info = function(test, common) {
test('returns server info', function(t) {
var controller = setup();
var res = { json: function( json ){
t.equal(typeof json, 'object', 'returns json');
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 tests = [
require('./controller/index')
require('./controller/index'),
require('./controller/suggest')
];
tests.map(function(t) {

Loading…
Cancel
Save