Skip to content

Commit

Permalink
Adding integration tests for a few endpoints using mocha
Browse files Browse the repository at this point in the history
- car
- company
- currency
- flight
  • Loading branch information
arnorhs committed Apr 28, 2013
1 parent d08b803 commit 8cac098
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 16 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ Tilgangur [Apis.is](http://apis.is) er að veita forriturum og áhugamönnum að

##Áhugaverðar síður:
+ [arnastofnun.is/page/gagnasofn](http://arnastofnun.is/page/gagnasofn)
+ [opingogn.net/wiki](http://opingogn.net/wiki/)
+ [opingogn.net/wiki](http://opingogn.net/wiki/)

##Prófanir
Hægt er að keyra integration tests fyrir hluta af vefþjónustunum með því að nota skipunina:

node_modules/mocha/bin/mocha test/integration
12 changes: 12 additions & 0 deletions endpoints/car/tests/integration_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var request = require('request');
var assert = require('assert');
var helpers = require('../../../lib/test_helpers.js');

describe('cars', function() {
it("should return an array of objects containing correct fields", function(done) {
var fieldsToCheckFor = ["registryNumber","number","factoryNumber","type","subType","color","registeredAt","status","nextCheck","pollution","weight"];
var params = helpers.testRequestParams("/car", { number: "AA031" });
var resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor);
request.post(params, resultHandler);
});
});
38 changes: 38 additions & 0 deletions endpoints/company/tests/integration_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var request = require('request');
var assert = require('assert');
var helpers = require('../../../lib/test_helpers.js');

describe('company', function() {
this.timeout(6000); // This endpoint is SLOW, need more time

var fieldsToCheckFor = ["name","sn","active","address"];

describe('searching by name', function() {
it("should return an array of objects containing correct fields", function(done) {
var params = helpers.testRequestParams("/company", { name: "hagar" });
var resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor);
request.post(params, resultHandler);
});
});
describe('searching by address', function() {
it("should return an array of objects containing correct fields", function(done) {
var params = helpers.testRequestParams("/company", { address: "Hagasmára" });
var resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor);
request.post(params, resultHandler);
});
});
describe('search by socialnumber', function() {
it("should return an array of objects containing correct fields", function(done) {
var params = helpers.testRequestParams("/company", { socialnumber: "4307003590" });
var resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor);
request.post(params, resultHandler);
});
});
describe('search by vsknr', function() {
it("should return an array of objects containing correct fields", function(done) {
var params = helpers.testRequestParams("/company", { vsknr: "78874" });
var resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor);
request.post(params, resultHandler);
});
});
});
24 changes: 24 additions & 0 deletions endpoints/currency/tests/integration_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var request = require('request');
var assert = require('assert');
var helpers = require('../../../lib/test_helpers.js');

describe('currency', function() {
// The only thing that changes is the form attribute, so why not just re-use the object
var fieldsToCheckFor = ["shortName", "longName", "value", "askValue", "bidValue", "changeCur", "changePer"];

describe('searching using provider "m5"', function() {
this.timeout(6000); // This endpoint is SLOW, need more time
it("should return an array of objects containing correct fields", function(done) {
var params = helpers.testRequestParams("/currency", { provider: "m5" });
var resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor);
request(params, resultHandler);
});
});
describe('searching using provider "arion"', function() {
it("should return an array of objects containing correct fields", function(done) {
var params = helpers.testRequestParams("/currency", { provider: "arion" });
var resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor);
request(params, resultHandler);
});
});
});
15 changes: 15 additions & 0 deletions endpoints/flight/tests/integration_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var request = require('request');
var assert = require('assert');
var helpers = require('../../../lib/test_helpers.js');

describe('flight', function() {
it("should return an array of objects containing correct fields", function(done) {
var fieldsToCheckFor = ["date","flightNumber","to","plannedArrival","realArrival","status"];
var params = helpers.testRequestParams("/flight", {
language: "en",
type: "departures"
});
var resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor);
request.post(params, resultHandler);
});
});
4 changes: 1 addition & 3 deletions lib/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ exports.load = function(server){
//walk is blocking on purpose because the server is not allowed
//to listen yet
fileModule.walk('./endpoints', function(a, dirPath, dirs, files){
if(files){
if(files && dirPath.indexOf("/test") < 0){
files.forEach(function(file,key){
//Setup the endpoint via the setup function
require('../'+file).setup(server);
});
}else{
console.log('There is no endpoint to load!');
}
});
}
38 changes: 38 additions & 0 deletions lib/test_helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var assert = require('assert');

function assertResults(json) {
assert(json.results && typeof json.results.length !== "undefined", "Does not contain a 'results' field");
assert(json.results.length > 0, "Results are empty");
};
function assertPresenceOfFields(fields, arr) {
arr.forEach(function(result, i) {
fields.forEach(function(field) {
assert(typeof result[field] !== "undefined", "Missing field '" + field + "' in result #" + i);
});
});
};
// always returns the same fields, so we'll just reuse this function for both cases
// (I may be going a bit overboard on this)
exports.testRequestHandlerForFields = function(done, fieldsToCheckFor) {
return function(err, res, body) {
if (err) throw err;
var json = JSON.parse(body);

// Check for the presence of the results property
assertResults(json);

// Check for the presence of all expected fields
assertPresenceOfFields(fieldsToCheckFor, json.results);

done();
};
};
// Generate http request params for a particular endpoint
exports.testRequestParams = function(path, form) {
return {
url: "http://localhost:3100" + path,
method: "POST",
form: form,
headers: [ "Content-type: application/json" ]
};
};
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"contributors": [
{
"name": "Ragnar Þór Valgeirsson"
},
{
"name": "Arnór Heiðar Sigurðsson",
"email": "[email protected]"
}
],
"dependencies" : {
Expand Down
6 changes: 0 additions & 6 deletions test/helpers.js

This file was deleted.

18 changes: 18 additions & 0 deletions test/integration/endpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var fs = require('fs');
var fileModule = require('file');
var testDir = '/tests';
var testFileName = 'integration_test.js';

describe('endpoint', function() {
fileModule.walkSync('./endpoints', function(dirPath, dirs, files){
if (dirPath.indexOf(testDir) < 0) return;
files.forEach(function(file){
if (file != testFileName) return;
var path = dirPath + '/' + file;
if (!fs.existsSync(path)) return;
require('../../' + path);
});
});
});


6 changes: 0 additions & 6 deletions test/test-server.js

This file was deleted.

0 comments on commit 8cac098

Please sign in to comment.