Browse Source

switched to t.plan for easier testing with next()

pull/899/head
Stephen Hess 8 years ago
parent
commit
615f7b720e
  1. 114
      test/unit/controller/coarse_reverse.js

114
test/unit/controller/coarse_reverse.js

@ -2,6 +2,7 @@
const setup = require('../../../controller/coarse_reverse'); const setup = require('../../../controller/coarse_reverse');
const proxyquire = require('proxyquire').noCallThru(); const proxyquire = require('proxyquire').noCallThru();
const _ = require('lodash');
module.exports.tests = {}; module.exports.tests = {};
@ -15,12 +16,13 @@ module.exports.tests.interface = (test, common) => {
module.exports.tests.early_exit_conditions = (test, common) => { module.exports.tests.early_exit_conditions = (test, common) => {
test('should_execute returning false should not call service', (t) => { test('should_execute returning false should not call service', (t) => {
t.plan(2);
const service = () => { const service = () => {
throw Error('service should not have been called'); throw Error('service should not have been called');
}; };
const should_execute = () => { return false; }; const controller = setup(service, _.constant(false));
const controller = setup(service, should_execute);
const req = { const req = {
clean: { clean: {
@ -30,14 +32,12 @@ module.exports.tests.early_exit_conditions = (test, common) => {
}; };
// verify that next was called // verify that next was called
let next_was_called = false;
const next = () => { const next = () => {
next_was_called = true; t.pass('next() was called');
}; };
// passing res=undefined verifies that it wasn't interacted with // passing res=undefined verifies that it wasn't interacted with
t.doesNotThrow(controller.bind(null, req, undefined, next)); t.doesNotThrow(controller.bind(null, req, undefined, next));
t.ok(next_was_called);
t.end(); t.end();
}); });
@ -46,6 +46,8 @@ module.exports.tests.early_exit_conditions = (test, common) => {
module.exports.tests.error_conditions = (test, common) => { module.exports.tests.error_conditions = (test, common) => {
test('service error should log and call next', (t) => { test('service error should log and call next', (t) => {
t.plan(3);
const service = (req, callback) => { const service = (req, callback) => {
t.deepEquals(req, { clean: { layers: ['locality'] } } ); t.deepEquals(req, { clean: { layers: ['locality'] } } );
callback('this is an error'); callback('this is an error');
@ -53,10 +55,9 @@ module.exports.tests.error_conditions = (test, common) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();
const should_execute = () => { return true; };
const controller = proxyquire('../../../controller/coarse_reverse', { const controller = proxyquire('../../../controller/coarse_reverse', {
'pelias-logger': logger 'pelias-logger': logger
})(service, should_execute); })(service, _.constant(true));
const req = { const req = {
clean: { clean: {
@ -65,16 +66,14 @@ module.exports.tests.error_conditions = (test, common) => {
}; };
// verify that next was called // verify that next was called
let next_was_called = false;
const next = () => { const next = () => {
next_was_called = true; t.pass('next() was called');
}; };
// passing res=undefined verifies that it wasn't interacted with // passing res=undefined verifies that it wasn't interacted with
controller(req, undefined, next); controller(req, undefined, next);
t.ok(logger.isErrorMessage('this is an error')); t.ok(logger.isErrorMessage('this is an error'));
t.ok(next_was_called);
t.end(); t.end();
}); });
@ -83,6 +82,8 @@ module.exports.tests.error_conditions = (test, common) => {
module.exports.tests.success_conditions = (test, common) => { module.exports.tests.success_conditions = (test, common) => {
test('service returning results should use first entry for each layer', (t) => { test('service returning results should use first entry for each layer', (t) => {
t.plan(4);
const service = (req, callback) => { const service = (req, callback) => {
t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } ); t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } );
@ -143,10 +144,9 @@ module.exports.tests.success_conditions = (test, common) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();
const should_execute = () => { return true; };
const controller = proxyquire('../../../controller/coarse_reverse', { const controller = proxyquire('../../../controller/coarse_reverse', {
'pelias-logger': logger 'pelias-logger': logger
})(service, should_execute); })(service, _.constant(true));
const req = { const req = {
clean: { clean: {
@ -157,9 +157,8 @@ module.exports.tests.success_conditions = (test, common) => {
const res = { }; const res = { };
// verify that next was called // verify that next was called
let next_was_called = false;
const next = () => { const next = () => {
next_was_called = true; t.pass('next() was called');
}; };
controller(req, res, next); controller(req, res, next);
@ -221,14 +220,14 @@ module.exports.tests.success_conditions = (test, common) => {
}; };
t.deepEquals(res, expected); t.deepEquals(res, expected);
t.notOk(logger.hasErrorMessages()); t.notOk(logger.hasErrorMessages());
t.ok(next_was_called);
t.end(); t.end();
}); });
test('layers missing from results should be ignored', (t) => { test('layers missing from results should be ignored', (t) => {
t.plan(4);
const service = (req, callback) => { const service = (req, callback) => {
t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } ); t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } );
@ -251,10 +250,9 @@ module.exports.tests.success_conditions = (test, common) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();
const should_execute = () => { return true; };
const controller = proxyquire('../../../controller/coarse_reverse', { const controller = proxyquire('../../../controller/coarse_reverse', {
'pelias-logger': logger 'pelias-logger': logger
})(service, should_execute); })(service, _.constant(true));
const req = { const req = {
clean: { clean: {
@ -265,9 +263,8 @@ module.exports.tests.success_conditions = (test, common) => {
const res = { }; const res = { };
// verify that next was called // verify that next was called
let next_was_called = false;
const next = () => { const next = () => {
next_was_called = true; t.pass('next() was called');
}; };
controller(req, res, next); controller(req, res, next);
@ -302,14 +299,14 @@ module.exports.tests.success_conditions = (test, common) => {
}; };
t.deepEquals(res, expected); t.deepEquals(res, expected);
t.notOk(logger.hasErrorMessages()); t.notOk(logger.hasErrorMessages());
t.ok(next_was_called);
t.end(); t.end();
}); });
test('most granular layer missing centroid should not set', (t) => { test('most granular layer missing centroid should not set', (t) => {
t.plan(4);
const service = (req, callback) => { const service = (req, callback) => {
t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } ); t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } );
@ -329,10 +326,9 @@ module.exports.tests.success_conditions = (test, common) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();
const should_execute = () => { return true; };
const controller = proxyquire('../../../controller/coarse_reverse', { const controller = proxyquire('../../../controller/coarse_reverse', {
'pelias-logger': logger 'pelias-logger': logger
})(service, should_execute); })(service, _.constant(true));
const req = { const req = {
clean: { clean: {
@ -343,9 +339,8 @@ module.exports.tests.success_conditions = (test, common) => {
const res = { }; const res = { };
// verify that next was called // verify that next was called
let next_was_called = false;
const next = () => { const next = () => {
next_was_called = true; t.pass('next() was called');
}; };
controller(req, res, next); controller(req, res, next);
@ -376,14 +371,14 @@ module.exports.tests.success_conditions = (test, common) => {
}; };
t.deepEquals(res, expected); t.deepEquals(res, expected);
t.notOk(logger.hasErrorMessages()); t.notOk(logger.hasErrorMessages());
t.ok(next_was_called);
t.end(); t.end();
}); });
test('most granular layer missing bounding_box should not set', (t) => { test('most granular layer missing bounding_box should not set', (t) => {
t.plan(4);
const service = (req, callback) => { const service = (req, callback) => {
t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } ); t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } );
@ -406,10 +401,9 @@ module.exports.tests.success_conditions = (test, common) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();
const should_execute = () => { return true; };
const controller = proxyquire('../../../controller/coarse_reverse', { const controller = proxyquire('../../../controller/coarse_reverse', {
'pelias-logger': logger 'pelias-logger': logger
})(service, should_execute); })(service, _.constant(true));
const req = { const req = {
clean: { clean: {
@ -420,9 +414,8 @@ module.exports.tests.success_conditions = (test, common) => {
const res = { }; const res = { };
// verify that next was called // verify that next was called
let next_was_called = false;
const next = () => { const next = () => {
next_was_called = true; t.pass('next() was called');
}; };
controller(req, res, next); controller(req, res, next);
@ -456,9 +449,7 @@ module.exports.tests.success_conditions = (test, common) => {
}; };
t.deepEquals(res, expected); t.deepEquals(res, expected);
t.notOk(logger.hasErrorMessages()); t.notOk(logger.hasErrorMessages());
t.ok(next_was_called);
t.end(); t.end();
}); });
@ -469,10 +460,9 @@ module.exports.tests.success_conditions = (test, common) => {
// by plan'ing the number of tests, we can verify that next() was called w/o // by plan'ing the number of tests, we can verify that next() was called w/o
// additional bookkeeping // additional bookkeeping
t.plan(5); t.plan(4);
const service = (point, do_not_track, callback) => { const service = (req, callback) => {
t.equals(do_not_track, 'do_not_track value');
const results = { const results = {
neighbourhood: [ neighbourhood: [
{ {
@ -488,15 +478,9 @@ module.exports.tests.success_conditions = (test, common) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();
const should_execute = () => { return true; };
const controller = proxyquire('../../../controller/coarse_reverse', { const controller = proxyquire('../../../controller/coarse_reverse', {
'pelias-logger': logger, 'pelias-logger': logger
'../helper/logging': { })(service, _.constant(true));
isDNT: () => {
return 'do_not_track value';
}
}
})(service, should_execute);
const req = { const req = {
clean: { clean: {
@ -553,15 +537,14 @@ module.exports.tests.success_conditions = (test, common) => {
// this test is used to test coarse reverse fallback for when non-coarse reverse // this test is used to test coarse reverse fallback for when non-coarse reverse
// was requested but no non-coarse results were found // was requested but no non-coarse results were found
const non_coarse_layers = ['venue', 'address', 'street']; const non_coarse_layers = ['venue', 'address', 'street'];
const tests_per_non_coarse_layer = 5; const tests_per_non_coarse_layer = 4;
// by plan'ing the number of tests, we can verify that next() was called w/o // by plan'ing the number of tests, we can verify that next() was called w/o
// additional bookkeeping // additional bookkeeping
t.plan(non_coarse_layers.length * tests_per_non_coarse_layer); t.plan(non_coarse_layers.length * tests_per_non_coarse_layer);
non_coarse_layers.forEach((non_coarse_layer) => { non_coarse_layers.forEach((non_coarse_layer) => {
const service = (point, do_not_track, callback) => { const service = (req, callback) => {
t.equals(do_not_track, 'do_not_track value');
const results = { const results = {
neighbourhood: [ neighbourhood: [
{ {
@ -577,15 +560,9 @@ module.exports.tests.success_conditions = (test, common) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();
const should_execute = () => { return true; };
const controller = proxyquire('../../../controller/coarse_reverse', { const controller = proxyquire('../../../controller/coarse_reverse', {
'pelias-logger': logger, 'pelias-logger': logger
'../helper/logging': { })(service, _.constant(true));
isDNT: () => {
return 'do_not_track value';
}
}
})(service, should_execute);
const req = { const req = {
clean: { clean: {
@ -644,15 +621,14 @@ module.exports.tests.success_conditions = (test, common) => {
// this test is used to test coarse reverse fallback for when non-coarse reverse // this test is used to test coarse reverse fallback for when non-coarse reverse
// was requested but no non-coarse results were found // was requested but no non-coarse results were found
const non_coarse_layers = ['venue', 'address', 'street']; const non_coarse_layers = ['venue', 'address', 'street'];
const tests_per_non_coarse_layer = 5; const tests_per_non_coarse_layer = 4;
// by plan'ing the number of tests, we can verify that next() was called w/o // by plan'ing the number of tests, we can verify that next() was called w/o
// additional bookkeeping // additional bookkeeping
t.plan(non_coarse_layers.length * tests_per_non_coarse_layer); t.plan(non_coarse_layers.length * tests_per_non_coarse_layer);
non_coarse_layers.forEach((non_coarse_layer) => { non_coarse_layers.forEach((non_coarse_layer) => {
const service = (point, do_not_track, callback) => { const service = (req, callback) => {
t.equals(do_not_track, 'do_not_track value');
const results = { const results = {
neighbourhood: [ neighbourhood: [
{ {
@ -668,15 +644,9 @@ module.exports.tests.success_conditions = (test, common) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();
const should_execute = () => { return true; };
const controller = proxyquire('../../../controller/coarse_reverse', { const controller = proxyquire('../../../controller/coarse_reverse', {
'pelias-logger': logger, 'pelias-logger': logger
'../helper/logging': { })(service, _.constant(true));
isDNT: () => {
return 'do_not_track value';
}
}
})(service, should_execute);
const req = { const req = {
clean: { clean: {
@ -735,6 +705,8 @@ module.exports.tests.success_conditions = (test, common) => {
module.exports.tests.failure_conditions = (test, common) => { module.exports.tests.failure_conditions = (test, common) => {
test('service returning 0 results at the requested layer should return nothing', (t) => { test('service returning 0 results at the requested layer should return nothing', (t) => {
t.plan(4);
const service = (req, callback) => { const service = (req, callback) => {
t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } ); t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } );
@ -783,10 +755,9 @@ module.exports.tests.failure_conditions = (test, common) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();
const should_execute = () => { return true; };
const controller = proxyquire('../../../controller/coarse_reverse', { const controller = proxyquire('../../../controller/coarse_reverse', {
'pelias-logger': logger 'pelias-logger': logger
})(service, should_execute); })(service, _.constant(true));
const req = { const req = {
clean: { clean: {
@ -797,9 +768,8 @@ module.exports.tests.failure_conditions = (test, common) => {
const res = { }; const res = { };
// verify that next was called // verify that next was called
let next_was_called = false;
const next = () => { const next = () => {
next_was_called = true; t.pass('next() was called');
}; };
controller(req, res, next); controller(req, res, next);
@ -810,9 +780,7 @@ module.exports.tests.failure_conditions = (test, common) => {
}; };
t.deepEquals(res, expected); t.deepEquals(res, expected);
t.notOk(logger.hasErrorMessages()); t.notOk(logger.hasErrorMessages());
t.ok(next_was_called);
t.end(); t.end();
}); });

Loading…
Cancel
Save