Skip to content

Commit

Permalink
Drop tables and migrate before testing, not after
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeorge committed Mar 8, 2022
1 parent 4f670d3 commit 4dc498f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 78 deletions.
17 changes: 2 additions & 15 deletions app/tests/api/badges-api.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')()
Expand Down
13 changes: 3 additions & 10 deletions app/tests/api/organization-api.test.js
Original file line number Diff line number Diff line change
@@ -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 })
Expand All @@ -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
*/
Expand Down
13 changes: 3 additions & 10 deletions app/tests/api/organization-model.test.js
Original file line number Diff line number Diff line change
@@ -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 })
Expand All @@ -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.
Expand Down
13 changes: 3 additions & 10 deletions app/tests/api/profile-api.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const path = require('path')
const test = require('ava')
const sinon = require('sinon')

Expand All @@ -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 })
Expand All @@ -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
*/
Expand Down
14 changes: 3 additions & 11 deletions app/tests/api/profile-model.test.js
Original file line number Diff line number Diff line change
@@ -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 })
Expand All @@ -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'
Expand Down
13 changes: 3 additions & 10 deletions app/tests/api/team-api.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const path = require('path')
const test = require('ava')
const sinon = require('sinon')
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 })
Expand All @@ -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' })
Expand Down
13 changes: 3 additions & 10 deletions app/tests/api/team-model.test.js
Original file line number Diff line number Diff line change
@@ -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 })
Expand All @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions app/tests/utils.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

0 comments on commit 4dc498f

Please sign in to comment.