From 8cac098f092d02927439f2edff2ce1c819c865a7 Mon Sep 17 00:00:00 2001 From: arnorhs Date: Sun, 28 Apr 2013 09:58:16 +0000 Subject: [PATCH] Adding integration tests for a few endpoints using mocha - car - company - currency - flight --- README.md | 7 +++- endpoints/car/tests/integration_test.js | 12 +++++++ endpoints/company/tests/integration_test.js | 38 ++++++++++++++++++++ endpoints/currency/tests/integration_test.js | 24 +++++++++++++ endpoints/flight/tests/integration_test.js | 15 ++++++++ lib/endpoints.js | 4 +-- lib/test_helpers.js | 38 ++++++++++++++++++++ package.json | 4 +++ test/helpers.js | 6 ---- test/integration/endpoints.js | 18 ++++++++++ test/test-server.js | 6 ---- 11 files changed, 156 insertions(+), 16 deletions(-) create mode 100644 endpoints/car/tests/integration_test.js create mode 100644 endpoints/company/tests/integration_test.js create mode 100644 endpoints/currency/tests/integration_test.js create mode 100644 endpoints/flight/tests/integration_test.js create mode 100644 lib/test_helpers.js delete mode 100644 test/helpers.js create mode 100644 test/integration/endpoints.js delete mode 100644 test/test-server.js diff --git a/README.md b/README.md index e100d0b9..23688b61 100644 --- a/README.md +++ b/README.md @@ -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/) \ No newline at end of file ++ [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 \ No newline at end of file diff --git a/endpoints/car/tests/integration_test.js b/endpoints/car/tests/integration_test.js new file mode 100644 index 00000000..254ad48e --- /dev/null +++ b/endpoints/car/tests/integration_test.js @@ -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); + }); +}); diff --git a/endpoints/company/tests/integration_test.js b/endpoints/company/tests/integration_test.js new file mode 100644 index 00000000..4d1b5728 --- /dev/null +++ b/endpoints/company/tests/integration_test.js @@ -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); + }); + }); +}); diff --git a/endpoints/currency/tests/integration_test.js b/endpoints/currency/tests/integration_test.js new file mode 100644 index 00000000..a37a0a38 --- /dev/null +++ b/endpoints/currency/tests/integration_test.js @@ -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); + }); + }); +}); diff --git a/endpoints/flight/tests/integration_test.js b/endpoints/flight/tests/integration_test.js new file mode 100644 index 00000000..6d9ff455 --- /dev/null +++ b/endpoints/flight/tests/integration_test.js @@ -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); + }); +}); diff --git a/lib/endpoints.js b/lib/endpoints.js index f9d82455..e51fe7cf 100644 --- a/lib/endpoints.js +++ b/lib/endpoints.js @@ -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!'); } }); } diff --git a/lib/test_helpers.js b/lib/test_helpers.js new file mode 100644 index 00000000..e411b2c3 --- /dev/null +++ b/lib/test_helpers.js @@ -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" ] + }; +}; diff --git a/package.json b/package.json index ba5f4f82..8a381933 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,10 @@ "contributors": [ { "name": "Ragnar Þór Valgeirsson" + }, + { + "name": "Arnór Heiðar Sigurðsson", + "email": "arnorhs@gmail.com" } ], "dependencies" : { diff --git a/test/helpers.js b/test/helpers.js deleted file mode 100644 index f86540ee..00000000 --- a/test/helpers.js +++ /dev/null @@ -1,6 +0,0 @@ -//Incomplete -exports['check-globals'] = function (test) { - var server = require('../server.js'); - test.ok(server.moment,'Moment.js module not loaded properly'); - test.done(); -}; \ No newline at end of file diff --git a/test/integration/endpoints.js b/test/integration/endpoints.js new file mode 100644 index 00000000..595d9320 --- /dev/null +++ b/test/integration/endpoints.js @@ -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); + }); + }); +}); + + diff --git a/test/test-server.js b/test/test-server.js deleted file mode 100644 index 02fa4cc2..00000000 --- a/test/test-server.js +++ /dev/null @@ -1,6 +0,0 @@ -//Incomplete -exports['check-globals'] = function (test) { - var server = require('../server.js'); - test.ok(server.moment); - test.done(); -}; \ No newline at end of file