diff --git a/app/tests/api/badges-api.test.js b/app/tests/api/badges-api.test.js index fc053303..0329224c 100644 --- a/app/tests/api/badges-api.test.js +++ b/app/tests/api/badges-api.test.js @@ -1,19 +1,10 @@ -const path = require('path') const test = require('ava') const sinon = require('sinon') const db = require('../../db') const hydra = require('../../lib/hydra') -const { dropTables } = require('../utils') - -const migrationsDirectory = path.join( - __dirname, - '..', - '..', - 'db', - 'migrations' -) +const { resetDb } = require('../utils') let app let dbClient @@ -55,11 +46,7 @@ test.before(async () => { console.log('Connecting to test database...') dbClient = await db() - console.log('Dropping tables...') - await dropTables(dbClient) - - console.log('Migrating...') - await dbClient.migrate.latest({ directory: migrationsDirectory }) + await resetDb(dbClient) console.log('Starting server...') app = await require('../../index')() diff --git a/app/tests/api/organization-api.test.js b/app/tests/api/organization-api.test.js index f6d88400..41388338 100644 --- a/app/tests/api/organization-api.test.js +++ b/app/tests/api/organization-api.test.js @@ -1,19 +1,18 @@ -const path = require('path') const test = require('ava') const sinon = require('sinon') const db = require('../../db') +const { resetDb } = require('../utils') const team = require('../../lib/team') const organization = require('../../lib/organization') const permissions = require('../../manage/permissions') -const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations') - let agent test.before(async () => { const conn = await db() - await conn.migrate.latest({ directory: migrationsDirectory }) + + await resetDb(conn) // seed await conn('users').insert({ id: 1 }) @@ -36,12 +35,6 @@ test.before(async () => { agent = require('supertest').agent(await require('../../index')()) }) -test.after.always(async () => { - const conn = await db() - await conn.migrate.rollback({ directory: migrationsDirectory }) - conn.destroy() -}) - /** * Test create an organization */ diff --git a/app/tests/api/organization-model.test.js b/app/tests/api/organization-model.test.js index 913e842b..372acfde 100644 --- a/app/tests/api/organization-model.test.js +++ b/app/tests/api/organization-model.test.js @@ -1,15 +1,14 @@ -const path = require('path') const test = require('ava') const { prop, map, contains } = require('ramda') const db = require('../../db') const organization = require('../../lib/organization') const team = require('../../lib/team') - -const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations') +const { resetDb } = require('../utils') test.before(async () => { const conn = await db() - await conn.migrate.latest({ directory: migrationsDirectory }) + + await resetDb(conn) // seed await conn('users').insert({ id: 1 }) @@ -18,12 +17,6 @@ test.before(async () => { await conn('users').insert({ id: 4 }) }) -test.after.always(async () => { - const conn = await db() - await conn.migrate.rollback({ directory: migrationsDirectory }) - conn.destroy() -}) - /** * Test organization creation * An organization is created by a user. diff --git a/app/tests/api/profile-api.test.js b/app/tests/api/profile-api.test.js index 2be3ff1d..e1f386b6 100644 --- a/app/tests/api/profile-api.test.js +++ b/app/tests/api/profile-api.test.js @@ -1,4 +1,3 @@ -const path = require('path') const test = require('ava') const sinon = require('sinon') @@ -7,16 +6,16 @@ const team = require('../../lib/team') const org = require('../../lib/organization') const permissions = require('../../manage/permissions') const profile = require('../../lib/profile') +const { resetDb } = require('../utils') const { prop, concat, includes, propEq, find } = require('ramda') -const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations') - let agent test.before(async () => { const conn = await db() - await conn.migrate.latest({ directory: migrationsDirectory }) + + await resetDb(conn) // seed await conn('users').insert({ id: 1 }) @@ -39,12 +38,6 @@ test.before(async () => { agent = require('supertest').agent(await require('../../index')()) }) -test.after.always(async () => { - const conn = await db() - await conn.migrate.rollback({ directory: migrationsDirectory }) - conn.destroy() -}) - /** * Get a team user profile with correct visibility */ diff --git a/app/tests/api/profile-model.test.js b/app/tests/api/profile-model.test.js index 9cea4307..6449fd40 100644 --- a/app/tests/api/profile-model.test.js +++ b/app/tests/api/profile-model.test.js @@ -1,18 +1,16 @@ - -const path = require('path') const { range, map, contains, prop, propEq, find, includes } = require('ramda') const test = require('ava') const db = require('../../db') const organization = require('../../lib/organization') const team = require('../../lib/team') const profile = require('../../lib/profile') +const { resetDb } = require('../utils') const { ValidationError, PropertyRequiredError } = require('../../lib/utils') -const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations') - test.before(async () => { const conn = await db() - await conn.migrate.latest({ directory: migrationsDirectory }) + + await resetDb(conn) // seed await conn('users').insert({ id: 1 }) @@ -26,12 +24,6 @@ test.before(async () => { await conn('users').insert({ id: 9 }) }) -test.after.always(async () => { - const conn = await db() - await conn.migrate.rollback({ directory: migrationsDirectory }) - conn.destroy() -}) - test('add attributes for a public user profile', async (t) => { const name = 'Age' const visibility = 'public' diff --git a/app/tests/api/team-api.test.js b/app/tests/api/team-api.test.js index aed3475a..59364047 100644 --- a/app/tests/api/team-api.test.js +++ b/app/tests/api/team-api.test.js @@ -1,4 +1,3 @@ -const path = require('path') const test = require('ava') const sinon = require('sinon') const { any } = require('ramda') @@ -6,13 +5,13 @@ const { any } = require('ramda') const db = require('../../db') const team = require('../../lib/team') const permissions = require('../../manage/permissions') - -const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations') +const { resetDb } = require('../utils') let agent test.before(async () => { const conn = await db() - await conn.migrate.latest({ directory: migrationsDirectory }) + + await resetDb(conn) // seed await conn('users').insert({ id: 1 }) @@ -39,12 +38,6 @@ test.before(async () => { agent = require('supertest').agent(await require('../../index')()) }) -test.after.always(async () => { - const conn = await db() - await conn.migrate.rollback({ directory: migrationsDirectory }) - conn.destroy() -}) - test('create a team', async t => { let res = await agent.post('/api/teams') .send({ name: 'road team 1' }) diff --git a/app/tests/api/team-model.test.js b/app/tests/api/team-model.test.js index faaebd81..faccf33e 100644 --- a/app/tests/api/team-model.test.js +++ b/app/tests/api/team-model.test.js @@ -1,14 +1,13 @@ -const path = require('path') const test = require('ava') const db = require('../../db') const team = require('../../lib/team') const { prop } = require('ramda') - -const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations') +const { resetDb } = require('../utils') test.before(async () => { const conn = await db() - await conn.migrate.latest({ directory: migrationsDirectory }) + + await resetDb(conn) // seed await conn('users').insert({ id: 1 }) @@ -17,12 +16,6 @@ test.before(async () => { await conn('users').insert({ id: 4 }) }) -test.after.always(async () => { - const conn = await db() - await conn.migrate.rollback({ directory: migrationsDirectory }) - conn.destroy() -}) - test('create a team', async (t) => { const data = await team.create({ name: 'map team 1' }, 1) const members = await team.getMembers(data.id) diff --git a/app/tests/utils.js b/app/tests/utils.js index 33496596..763e5733 100644 --- a/app/tests/utils.js +++ b/app/tests/utils.js @@ -1,4 +1,14 @@ -async function dropTables (db) { +const path = require('path') + +const migrationsDirectory = path.join( + __dirname, + '..', + 'db', + 'migrations' +) + +async function resetDb (db) { + console.log('Dropping tables...') const pgres = await db.raw(` SELECT 'drop table "' || tablename || '" cascade;' AS drop @@ -12,8 +22,11 @@ async function dropTables (db) { for (const r of pgres.rows) { await db.raw(r.drop) } + + console.log('Migrating...') + await db.migrate.latest({ directory: migrationsDirectory }) } module.exports = { - dropTables + resetDb }