Skip to content

Commit f882e26

Browse files
committed
changed setupDatabase to force sync depending on environment
If the environment is testing then the database will force sync the database otherwise it will not force. This helps to keep the code DRY in my opinion.
1 parent 3aed391 commit f882e26

4 files changed

Lines changed: 11 additions & 18 deletions

File tree

source/database/setupDatabase.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const env = process.env.NODE_ENV || 'development';
66
const { logger } = require('../logger.js');
77
const config = require('./config.js')[env];
88

9-
function setupDatabase() {
9+
async function setupDatabase() {
1010
logger.info(`Setting up ${env} Database`);
1111
// defaults to the devleopment database
1212
const sequelize = new Sequelize(
@@ -27,6 +27,14 @@ function setupDatabase() {
2727
Guild.hasMany(Score);
2828
Score.belongsTo(Guild);
2929

30+
// testing environment needs to be renewed each time the database is initalized as opposed to a
31+
// production environment.
32+
if (env == "testing") {
33+
await sequelize.sync({force: true});
34+
} else {
35+
await sequelize.sync();
36+
}
37+
3038
return sequelize;
3139
};
3240

tests/helpers/testDatabase.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ const setupDatabase = require('../../source/database/setupDatabase.js');
33

44
// runs the setup then returns the sequelize object to be used in tests.
55
async function setupTestDatabase() {
6-
const sequelize = setupDatabase();
7-
await sequelize.sync();
8-
9-
// inserts the dummy data into the sequelize instance.
6+
const sequelize = await setupDatabase();
107
await insertDummyData(sequelize);
118

129
return sequelize;

tests/insertDummyData.test.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/models/guild.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('Guild score functionality', () => {
6565
test('getScore method', async () => {
6666
const guild = await Guild.getGuild(existingGuildId);
6767
expect(guild.getScore(existingUserId)).resolves.toEqual(12);
68-
})
68+
});
6969

7070
test('minus two', async () => {
7171
const guild = await Guild.getGuild(existingGuildId);

0 commit comments

Comments
 (0)