diff --git a/README.md b/README.md index 79c08840..4d88193f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # hills-carpal [![CircleCI](https://circleci.com/gh/RHoKAustralia/hills-carpal.svg?style=svg)](https://circleci.com/gh/RHoKAustralia/hills-carpal) + +# Frontend + + + diff --git a/backend/readme.md b/backend/readme.md index 24d36b66..501e24ce 100644 --- a/backend/readme.md +++ b/backend/readme.md @@ -1,5 +1,39 @@ ## Initial Setup +### Database +The project holds sql scripts that can be used to incrementally apply changes to the database. +You can therefore also use this scripts to initialize your local database. +To run it: +``` +npm run refresh-db +``` +By default it will try to connect to a local mysql database with username `root` and password `admin`. If you want to change those configurations you can set as environment variable, for instance: +``` +MYSQL_USER=myuser MYSQL_PW=myPassword MYSQL_HOST=myHost MYSQL_PORT=3316 MYSQL_DB=myDB npm run refresh-db +``` + +### Running the application locally + +The application was designed to work with AWS lambdas. Although an *expressJs* layer was added to be able to run the application locally. To start it: + +``` +npm start +``` +**or** to be able to debug it: +``` +node ./backend/src/test/expressApis.js +``` + +**Attention**: The application and tests make use of the database, therefore make sure that you have your environment variables set to configure them to run against the right mysql instance. + + +### Running the tests +In the `backend/` directory run: + +``` +npm test +``` + ### Deploying with serverless Run the following in the ./infrastructure/services directory to create the API Gateway config and lambdas: ``` diff --git a/backend/src/main/database/DatabaseManager.js b/backend/src/main/database/DatabaseManager.js index 018699c6..fa3b4eb0 100644 --- a/backend/src/main/database/DatabaseManager.js +++ b/backend/src/main/database/DatabaseManager.js @@ -4,11 +4,11 @@ class DatabaseManager { constructor(databaseConfig) { this.databaseConfig = databaseConfig || { - host: process.env.MYSQL_HOST, - port: process.env.MYSQL_PORT, - user: process.env.MYSQL_USER, - password: process.env.MYSQL_PW, - database: 'carpal', + host: process.env.MYSQL_HOST || 'localhost', + port: process.env.MYSQL_PORT || 3306, + user: process.env.MYSQL_USER || 'root', + password: process.env.MYSQL_PW || 'admin', + database: process.env.MYSQL_DB || 'carpal', multipleStatements: true }; } diff --git a/backend/src/test/database/databaseTestConfig.js b/backend/src/test/database/databaseTestConfig.js deleted file mode 100644 index d964334c..00000000 --- a/backend/src/test/database/databaseTestConfig.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - host: 'localhost', - port: 3306, - user: 'root', - password: 'admin', - database: 'carpal', - multipleStatements: true -}; \ No newline at end of file diff --git a/backend/src/test/database/refreshDatabase.js b/backend/src/test/database/refreshDatabase.js deleted file mode 100644 index d64e44a8..00000000 --- a/backend/src/test/database/refreshDatabase.js +++ /dev/null @@ -1,13 +0,0 @@ -const DatabaseManager = require("../../main/database/DatabaseManager"); -const RefreshDatabase = require("../../main/database/RefreshDatabase"); -const config = Object.assign({}, require('./databaseTestConfig')); -delete config.database; -const refreshDatabase = new RefreshDatabase(new DatabaseManager(config)); - -process.on('unhandledRejection', error => { - console.log('unhandledRejection', error); -}); - - -refreshDatabase.executeAll(refreshDatabase) - .catch(e => console.log(e)); \ No newline at end of file diff --git a/backend/src/test/expressApis.js b/backend/src/test/expressApis.js index 70c0096f..4946516b 100644 --- a/backend/src/test/expressApis.js +++ b/backend/src/test/expressApis.js @@ -8,10 +8,9 @@ const UpdateRideService = require('../main/rides/UpdateRideService'); const DatabaseManager = require('../main/database/DatabaseManager'); const ExpressRideApis = require('./rides/express/ExpressRidesApis'); const ExpressAuthApis = require('./auth/ExpressAuthApis'); -const databaseConfig = require('./database/databaseTestConfig'); const AwsLambdaRideApis = require('../main/rides/aws/AwsLambdaRideApis'); const bodyParser = require('body-parser'); -const databaseManager = new DatabaseManager(databaseConfig); +const databaseManager = new DatabaseManager(); const https = require('https'); const http = require('http'); diff --git a/backend/src/test/rides/CreateRide.integration.test.js b/backend/src/test/rides/CreateRide.integration.test.js index 1924c64e..3459c250 100644 --- a/backend/src/test/rides/CreateRide.integration.test.js +++ b/backend/src/test/rides/CreateRide.integration.test.js @@ -2,7 +2,6 @@ const DatabaseManager = require('../../main/database/DatabaseManager'); const RideStatus = require('../../main/rides/RideStatus'); const CreateRideService = require('../../main/rides/CreateRideService'); const FindRideTestRepository = require('./RideTestRepository'); -const databaseTestConfig = require('../database/databaseTestConfig'); const RandomUtils = require('../RandomUtils'); const moment = require('moment'); const chai = require('chai'); @@ -20,8 +19,8 @@ let loginData; let pickupTimeAndDateInUTC; before(async () => { - databaseManager = new DatabaseManager(databaseTestConfig); - mockDatabaseManager = new DatabaseManager(databaseTestConfig); + databaseManager = new DatabaseManager(); + mockDatabaseManager = new DatabaseManager(); connection = databaseManager.createConnection() ; mockDatabaseManager.createConnection = () => connection; mockDatabaseManager.closeConnection = () => null; diff --git a/backend/src/test/rides/FindOneRide.integration.test.js b/backend/src/test/rides/FindOneRide.integration.test.js index 6a3027be..2a6f57a0 100644 --- a/backend/src/test/rides/FindOneRide.integration.test.js +++ b/backend/src/test/rides/FindOneRide.integration.test.js @@ -1,7 +1,6 @@ const DatabaseManager = require('../../main/database/DatabaseManager'); const FindOneRideService = require('../../main/rides/FindOneRideService'); const RideTestRepository = require('./RideTestRepository'); -const databaseTestConfig = require('../database/databaseTestConfig'); const RideEntityBuilder = require('./RideEntityBuilder'); const RideRepository = require('../../main/rides/RideRepository'); const RandomUtils = require('../RandomUtils'); @@ -19,8 +18,8 @@ const assert = chai.assert; chai.use(chaiExclude); before(async () => { - databaseManager = new DatabaseManager(databaseTestConfig); - mockDatabaseManager = new DatabaseManager(databaseTestConfig); + databaseManager = new DatabaseManager(); + mockDatabaseManager = new DatabaseManager(); connection = databaseManager.createConnection(); mockDatabaseManager.createConnection = () => connection; mockDatabaseManager.closeConnection = () => Promise.resolve(null); diff --git a/backend/src/test/rides/ListRides.integration.test.js b/backend/src/test/rides/ListRides.integration.test.js index 580d681d..42d96784 100644 --- a/backend/src/test/rides/ListRides.integration.test.js +++ b/backend/src/test/rides/ListRides.integration.test.js @@ -1,7 +1,6 @@ const DatabaseManager = require('../../main/database/DatabaseManager'); const ListRidesController = require('../../main/rides/ListRidesService'); const FindRideTestRepository = require('./RideTestRepository'); -const databaseTestConfig = require('../database/databaseTestConfig'); const RideEntityBuilder = require('./RideEntityBuilder'); const RideRepository = require('../../main/rides/RideRepository'); const RandomUtils = require('../RandomUtils'); @@ -19,8 +18,8 @@ const assert = chai.assert; chai.use(chaiExclude); before(async () => { - databaseManager = new DatabaseManager(databaseTestConfig); - mockDatabaseManager = new DatabaseManager(databaseTestConfig); + databaseManager = new DatabaseManager(); + mockDatabaseManager = new DatabaseManager(); connection = databaseManager.createConnection(); mockDatabaseManager.createConnection = () => connection; mockDatabaseManager.closeConnection = () => Promise.resolve(null); @@ -80,9 +79,12 @@ describe('When listing rides', async () => { await databaseContainsRides(ride1, ride2, ride3); // when - const rides = await listRideController.listRides({}, loginData); + const rides = (await listRideController.listRides({}, loginData)).map(removeId); - assert.deepEqualExcluding(rides, [ride1, ride3], 'id'); + assert.deepInclude(rides, ride1); + assert.deepInclude(rides, ride3); + assert.notDeepInclude(rides, ride2); + // assert.deepEqualExcluding(rides, [ride1, ride3], 'id'); }); async function databaseContainsRides(...rides) { diff --git a/backend/src/test/rides/UpdateRide.integration.test.js b/backend/src/test/rides/UpdateRide.integration.test.js index c6e49c68..d63a07db 100644 --- a/backend/src/test/rides/UpdateRide.integration.test.js +++ b/backend/src/test/rides/UpdateRide.integration.test.js @@ -2,7 +2,6 @@ const DatabaseManager = require('../../main/database/DatabaseManager'); const RideStatus = require('../../main/rides/RideStatus'); const UpdateRideService = require('../../main/rides/UpdateRideService'); const FindRideTestRepository = require('./RideTestRepository'); -const databaseTestConfig = require('../database/databaseTestConfig'); const RideEntityBuilder = require('./RideEntityBuilder'); const RideRepository = require('../../main/rides/RideRepository'); const RandomUtils = require('../RandomUtils'); @@ -23,8 +22,8 @@ let pickupTimeAndDateInUTC; let rideRepository; before(async () => { - databaseManager = new DatabaseManager(databaseTestConfig); - mockDatabaseManager = new DatabaseManager(databaseTestConfig); + databaseManager = new DatabaseManager(); + mockDatabaseManager = new DatabaseManager(); rideRepository = new RideRepository(databaseManager); connection = databaseManager.createConnection(); mockDatabaseManager.createConnection = () => connection;