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 0f8767d..0000000 Binary files a/test/.app.test.js.swp and /dev/null differ 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')