From 3e4353e94d0a7e9a00cdf47fc01397ef4ccf435d Mon Sep 17 00:00:00 2001 From: jackie1050 Date: Tue, 6 Jul 2021 21:15:48 +0100 Subject: [PATCH] Amended to use environment variable in db.js to control whether testing or development (export NODE_ENV=testing) --- .test.env | 7 ++++ api/db.js | 16 ++++----- knexfile.js | 10 ++++++ seeds/development/1_resource_types_seed.js | 8 ++--- seeds/development/2_resources_seed.js | 3 ++ seeds/development/3_slots_seed.js | 3 ++ seeds/development/4_resources_slots_seed.js | 3 ++ seeds/testing/1_resource_types_seed.js | 13 ++++++++ seeds/testing/2_resources_seed.js | 16 +++++++++ seeds/testing/3_slots_seed.js | 29 ++++++++++++++++ seeds/testing/4_resources_slots_seed.js | 35 ++++++++++++++++++++ test/.app.test.js.swp | Bin 12288 -> 0 bytes test/app.test.js | 12 ++++++- 13 files changed, 142 insertions(+), 13 deletions(-) create mode 100644 .test.env create mode 100644 seeds/testing/1_resource_types_seed.js create mode 100644 seeds/testing/2_resources_seed.js create mode 100644 seeds/testing/3_slots_seed.js create mode 100644 seeds/testing/4_resources_slots_seed.js delete mode 100644 test/.app.test.js.swp diff --git a/.test.env b/.test.env new file mode 100644 index 0000000..669f8e1 --- /dev/null +++ b/.test.env @@ -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" + diff --git a/api/db.js b/api/db.js index 0f5523b..3442173 100644 --- a/api/db.js +++ b/api/db.js @@ -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; diff --git a/knexfile.js b/knexfile.js index 49a4027..7e3137c 100644 --- a/knexfile.js +++ b/knexfile.js @@ -10,4 +10,14 @@ module.exports = { directory: __dirname + '/seeds/development' } }, + testing: { + client: 'sqlite3', + + connection: { + filename: './db/optimism_test.sqlite3' + }, + seeds: { + directory: __dirname + '/seeds/testing' + } + } }; diff --git a/seeds/development/1_resource_types_seed.js b/seeds/development/1_resource_types_seed.js index f66e4db..0f3b78c 100644 --- a/seeds/development/1_resource_types_seed.js +++ b/seeds/development/1_resource_types_seed.js @@ -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'} ]); }); }; diff --git a/seeds/development/2_resources_seed.js b/seeds/development/2_resources_seed.js index 5402c5d..209c28e 100644 --- a/seeds/development/2_resources_seed.js +++ b/seeds/development/2_resources_seed.js @@ -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([ diff --git a/seeds/development/3_slots_seed.js b/seeds/development/3_slots_seed.js index e254b23..d33f776 100644 --- a/seeds/development/3_slots_seed.js +++ b/seeds/development/3_slots_seed.js @@ -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([ diff --git a/seeds/development/4_resources_slots_seed.js b/seeds/development/4_resources_slots_seed.js index 16f897b..255d36e 100644 --- a/seeds/development/4_resources_slots_seed.js +++ b/seeds/development/4_resources_slots_seed.js @@ -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([ diff --git a/seeds/testing/1_resource_types_seed.js b/seeds/testing/1_resource_types_seed.js new file mode 100644 index 0000000..5f6184e --- /dev/null +++ b/seeds/testing/1_resource_types_seed.js @@ -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'} + ]); + }); +}; diff --git a/seeds/testing/2_resources_seed.js b/seeds/testing/2_resources_seed.js new file mode 100644 index 0000000..b1dfb1b --- /dev/null +++ b/seeds/testing/2_resources_seed.js @@ -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}, + ]); + }); +}; diff --git a/seeds/testing/3_slots_seed.js b/seeds/testing/3_slots_seed.js new file mode 100644 index 0000000..33ab18d --- /dev/null +++ b/seeds/testing/3_slots_seed.js @@ -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'} + ]); + }); +}; diff --git a/seeds/testing/4_resources_slots_seed.js b/seeds/testing/4_resources_slots_seed.js new file mode 100644 index 0000000..26c2a2f --- /dev/null +++ b/seeds/testing/4_resources_slots_seed.js @@ -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} + ]); + }); +}; diff --git a/test/.app.test.js.swp b/test/.app.test.js.swp deleted file mode 100644 index 0f8767d123e4788fedf192e100c9002e550e9532..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&x;&I6vt~N5I1p+KW<9kIVv*)=^r!6gs8+7^pKDrf*Q@m#G3BfncAK0>U334 zc9G~Mc+!gp6+Gxs5HBj;C3n5)AE1zbA$amz-P5y8Z_IA8s4P^&hpy><^%C&x z8isG5y+Eh@KEw4GW1oKV(|gkc*}`*AEwNP|tw~{D-1{o+yz?SaQ{~B6@RY~iAPl|U z;Ov{{=AAz%4Fk=Sc&Ijlku>9c7^u|9b*a|_BeV&4n)970W}{x7 z-4`17*-v+K3OEIv0!{&^fK$LJ;1qBQI0c*nP64NYQ(zw|z+=WezF!nK62NSRio(6v%X6z2Q1wI2;K^y#eh_SE1SKtfqIk*6x2Md6K-xm=dd;_k6 z%itYw1l-14e*oWun?QpJSObL)`|vxuAv*<}0!{&^fK%W!}0n?dDZL}8-{GwpBm4t1C#5nZXTPGp(NaME$Z)F#-v zs7UA0n5rqYx}`Y91Z=bEW}8MQ!V`0ii0quzMEDam+WIiH(Dza^QD!WY(O%TUU~1~g z<*)P)=?$W#CDY_~WOlZ;hcPO-z( z?R0L74Ln-2(yIJcb)#s^WvlC#FOX}@KJarbI<|4fnA9e6)vony+h^Vw25neOd4?3{ OvS!e-zgyj7#Qp|g!M<<+ diff --git a/test/app.test.js b/test/app.test.js index 8d0857c..951c6af 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -1,3 +1,5 @@ +process.env.NODE_ENV = 'testing'; + var chai = require("chai"), chaiHttp = require('chai-http'); chai.use(chaiHttp); @@ -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"); @@ -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')