Skip to content

Commit

Permalink
Merge pull request #40 from DoESLiverpool/feature/37-add-test-database
Browse files Browse the repository at this point in the history
#37 Added test database
  • Loading branch information
JackiePease authored Sep 7, 2021
2 parents 161d9f5 + 3e4353e commit c2b0d5c
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
OPTIMISM_API_PORT = 3001
OPTIMISM_WEBSITE_PORT = 3000
OPTIMISM_API_URL = 'http://localhost:3001/api'

OPTIMISM_ENABLE_DETAILED_ERROR_MESSAGES = 1
OPTIMISM_DB_FILENAME = "./db/optimism_test.sqlite3"

16 changes: 8 additions & 8 deletions api/db.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var knex = require('knex')({
client: 'sqlite3',
connection: {
filename: "./db/optimism.sqlite3"
},
useNullAsDefault: true
});
var environment = process.env.NODE_ENV || 'development';
var config = require('../knexfile.js')[environment];
console.log('db test');
console.log(process.env.NODE_ENV);

useNullAsDefault: true;

module.exports = require('knex')(config);

module.exports = knex;
10 changes: 10 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ module.exports = {
directory: __dirname + '/seeds/development'
}
},
testing: {
client: 'sqlite3',

connection: {
filename: './db/optimism_test.sqlite3'
},
seeds: {
directory: __dirname + '/seeds/testing'
}
}
};
8 changes: 4 additions & 4 deletions seeds/development/1_resource_types_seed.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

exports.seed = function(knex) {
// Deletes ALL existing entries
return knex('resource_types')
return knex('resource_types').del()
.then(function () {
// Inserts seed entries
return knex('resource_types').insert([
{id: 1, name: 'room'},
{id: 2, name: 'laser-cutter'},
{id: 3, name: 'hot-desk'}
{id: 1, name: 'room - dev'},
{id: 2, name: 'laser-cutter - dev'},
{id: 3, name: 'hot-desk - dev'}
]);
});
};
3 changes: 3 additions & 0 deletions seeds/development/2_resources_seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
exports.seed = function(knex) {
// Deletes ALL existing entries
return knex('resources')
.then(function () {
// Deleted ALL existing entries
return knex('resources').del()})
.then(function () {
// Inserts seed entries
return knex('resources').insert([
Expand Down
3 changes: 3 additions & 0 deletions seeds/development/3_slots_seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
exports.seed = function(knex) {
// Deletes ALL existing entries
return knex('slots')
.then(function () {
// Deleted ALL existing entries
return knex('slots').del()})
.then(function () {
// Inserts seed entries
return knex('slots').insert([
Expand Down
3 changes: 3 additions & 0 deletions seeds/development/4_resources_slots_seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
exports.seed = function(knex) {
// Deletes ALL existing entries
return knex('resources_slots')
.then(function () {
// Deletes ALL existing entries
return knex('resources_slots').del()})
.then(function () {
// Inserts seed entries
return knex('resources_slots').insert([
Expand Down
13 changes: 13 additions & 0 deletions seeds/testing/1_resource_types_seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

exports.seed = function(knex) {
// Deletes ALL existing entries
return knex('resource_types').del()
.then(function () {
// Inserts seed entries
return knex('resource_types').insert([
{id: 1, name: 'room'},
{id: 2, name: 'laser-cutter'},
{id: 3, name: 'hot-desk'}
]);
});
};
16 changes: 16 additions & 0 deletions seeds/testing/2_resources_seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

exports.seed = function(knex) {
// Deletes ALL existing entries
return knex('resources').del()
.then(function () {
// Inserts seed entries
return knex('resources').insert([
{id: 1, resource_type_id: 1, name: 'Dinky', capacity: 1, min_minutes: 30, max_minutes: 480},
{id: 2, resource_type_id: 2, name: 'Sophia', capacity: 1, min_minutes: 240, max_minutes: 480},
{id: 3, resource_type_id: 2, name: 'Gerald', capacity: 1, min_minutes: 240, max_minutes: 480},
{id: 4, resource_type_id: 1, name: 'Events Room', capacity: 1, min_minutes: 30, max_minutes: 480},
{id: 5, resource_type_id: 3, name: 'Hot Desk', capacity: 10, min_minutes: 240, max_minutes: 480},
{id: 6, resource_type_id: 3, name: 'Test Resource', capacity: 10, min_minutes: 240, max_minutes: 480},
]);
});
};
29 changes: 29 additions & 0 deletions seeds/testing/3_slots_seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

exports.seed = function(knex) {
// Deletes ALL existing entries
return knex('slots').del()
.then(function () {
// Inserts seed entries
return knex('slots').insert([
{id: 1, name: 'Weekday AM', day: 31, starts: '09:30', ends: '13:30'},
{id: 2, name: 'Weekday PM', day: 31, starts: '13:30', ends: '17:30'},
{id: 3, name: 'Weekday', day: 31, starts: '09:30', ends: '17:30'},
{id: 4, name: '9:30 Meeting', day: 31, starts: '09:30', ends: '10:00'},
{id: 5, name: '10:00 Meeting', day: 31, starts: '10:00', ends: '10:30'},
{id: 6, name: '10:30 Meeting', day: 31, starts: '10:30', ends: '11:00'},
{id: 7, name: '11:00 Meeting', day: 31, starts: '11:00', ends: '11:30'},
{id: 8, name: '11:30 Meeting', day: 31, starts: '11:30', ends: '12:00'},
{id: 9, name: '12:00 Meeting', day: 31, starts: '12:00', ends: '12:30'},
{id: 10, name: '12:30 Meeting', day: 31, starts: '12:30', ends: '13:00'},
{id: 11, name: '13:00 Meeting', day: 31, starts: '13:00', ends: '13:30'},
{id: 12, name: '13:30 Meeting', day: 31, starts: '13:30', ends: '14:00'},
{id: 13, name: '14:00 Meeting', day: 31, starts: '14:00', ends: '14:30'},
{id: 14, name: '14:30 Meeting', day: 31, starts: '14:30', ends: '15:00'},
{id: 15, name: '15:00 Meeting', day: 31, starts: '15:00', ends: '15:30'},
{id: 16, name: '15:30 Meeting', day: 31, starts: '15:30', ends: '16:00'},
{id: 17, name: '16:00 Meeting', day: 31, starts: '16:00', ends: '16:30'},
{id: 18, name: '16:30 Meeting', day: 31, starts: '16:30', ends: '17:00'},
{id: 19, name: '17:00 Meeting', day: 31, starts: '17:00', ends: '17:30'}
]);
});
};
35 changes: 35 additions & 0 deletions seeds/testing/4_resources_slots_seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

exports.seed = function(knex) {
// Deletes ALL existing entries
return knex('resources_slots').del()
.then(function () {
// Inserts seed entries
return knex('resources_slots').insert([
{slot_id: 1, resource_id: 2},
{slot_id: 2, resource_id: 2},
{slot_id: 3, resource_id: 2},
{slot_id: 1, resource_id: 3},
{slot_id: 2, resource_id: 3},
{slot_id: 3, resource_id: 3},
{slot_id: 1, resource_id: 5},
{slot_id: 2, resource_id: 5},
{slot_id: 3, resource_id: 5},
{slot_id: 4, resource_id: 1},
{slot_id: 5, resource_id: 1},
{slot_id: 6, resource_id: 1},
{slot_id: 7, resource_id: 1},
{slot_id: 8, resource_id: 1},
{slot_id: 9, resource_id: 1},
{slot_id: 10, resource_id: 1},
{slot_id: 11, resource_id: 1},
{slot_id: 12, resource_id: 1},
{slot_id: 13, resource_id: 1},
{slot_id: 14, resource_id: 1},
{slot_id: 15, resource_id: 1},
{slot_id: 16, resource_id: 1},
{slot_id: 17, resource_id: 1},
{slot_id: 18, resource_id: 1},
{slot_id: 19, resource_id: 1}
]);
});
};
Binary file removed test/.app.test.js.swp
Binary file not shown.
12 changes: 11 additions & 1 deletion test/app.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
process.env.NODE_ENV = 'testing';

var chai = require("chai"),
chaiHttp = require('chai-http');
chai.use(chaiHttp);
Expand Down Expand Up @@ -31,7 +33,6 @@ describe('app.js integration test', () => {

expect(response.status).to.equal(200)
expect(response).to.be.json;
// expect(response.body).to.equal('"id":5,"name":"Hot Desk","resourceTypeName":"hot-desk"');
expect(response.body).to.have.property("id");
expect(response.body.id).to.equal(5);
expect(response.body).to.have.property("name");
Expand All @@ -41,6 +42,15 @@ describe('app.js integration test', () => {

});

it('Returns all resources', async () => {
const response = await request(app)
.get('/api/resources')
expect(response.status).to.equal(200);
expect(response.body).to.be.json;
expect(response.body).to.be.a('array');
expect(response.body).length.to.equal(5);
});

it('Returns a 404 status on invalid /calendar endpoint', async () => {
const response = await request(app)
.get('/api/calendar/nothing')
Expand Down

0 comments on commit c2b0d5c

Please sign in to comment.