forked from apis-is/apis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding integration tests for a few endpoints using mocha
- car - company - currency - flight
- Loading branch information
Showing
11 changed files
with
156 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,10 @@ | |
"contributors": [ | ||
{ | ||
"name": "Ragnar Þór Valgeirsson" | ||
}, | ||
{ | ||
"name": "Arnór Heiðar Sigurðsson", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"dependencies" : { | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
|
||
|
This file was deleted.
Oops, something went wrong.