Skip to content

Commit

Permalink
refactor: cleanup tests to avoid creating too many connections
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 1, 2019
1 parent 72dcabe commit f6f0691
Show file tree
Hide file tree
Showing 12 changed files with 576 additions and 482 deletions.
5 changes: 4 additions & 1 deletion japaFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ require('ts-node/register')

const { configure } = require('japa')
configure({
files: ['test/**/*.spec.ts']
files: ['test/**/*.spec.ts'],
after: [async () => {
await require('fs-extra').remove(require('path').join(__dirname, 'test-helpers', 'tmp'))
}]
})
9 changes: 2 additions & 7 deletions test-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ export async function setup () {
* Does cleanup removes database
*/
export async function cleanup () {
if (process.env.DB === 'sqlite') {
await fs.cleanup()
return
}

const db = knex(getConfig())
await db.schema.dropTableIfExists('users')
await db.schema.dropTableIfExists('profiles')
Expand Down Expand Up @@ -232,8 +227,8 @@ export function getDb () {
/**
* Returns the orm adapter
*/
export function ormAdapter () {
return new Adapter(getDb())
export function ormAdapter (db: DatabaseContract) {
return new Adapter(db)
}

/**
Expand Down
19 changes: 15 additions & 4 deletions test/connection/connection-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ test.group('ConnectionManager', (group) => {
await cleanup()
})

test('do not connect until connect is called', (assert) => {
test('do not connect until connect is called', async (assert) => {
const manager = new ConnectionManager(getLogger())
manager.add('primary', getConfig())

assert.isTrue(manager.has('primary'))
assert.isFalse(manager.isConnected('primary'))
await manager.closeAll()
})

test('connect and set its state to open', async (assert) => {
Expand All @@ -39,6 +40,7 @@ test.group('ConnectionManager', (group) => {

assert.equal(manager.get('primary')!.state, 'open')
assert.isTrue(manager.isConnected('primary'))
await manager.closeAll()
})

test('on disconnect set state to closed', async (assert) => {
Expand All @@ -49,6 +51,7 @@ test.group('ConnectionManager', (group) => {
await manager.connections.get('primary')!.connection!.disconnect()
assert.equal(manager.get('primary')!.state, 'closed')
assert.isFalse(manager.isConnected('primary'))
await manager.closeAll()
})

test('add duplicate connection must be a noop', async (assert) => {
Expand All @@ -58,6 +61,7 @@ test.group('ConnectionManager', (group) => {

manager.add('primary', Object.assign({}, getConfig(), { client: 'foo' }))
assert.notEqual(manager.get('primary')!.config.client, 'foo')
await manager.closeAll()
})

test('patch config when connection is not in open state', async (assert) => {
Expand All @@ -69,6 +73,7 @@ test.group('ConnectionManager', (group) => {

const fn = () => manager.add('primary', getConfig())
assert.doesNotThrow(fn)
await manager.closeAll()
})

test('ignore multiple calls to `connect` on a single connection', async () => {
Expand All @@ -81,6 +86,7 @@ test.group('ConnectionManager', (group) => {
})

manager.connect('primary')
await manager.closeAll()
})

test('releasing a connection must close it first', async (assert) => {
Expand All @@ -104,8 +110,9 @@ test.group('ConnectionManager', (group) => {
const manager = new ConnectionManager(getLogger())
manager.add('primary', getConfig())

manager.on('connect', (connection) => {
manager.on('connect', async (connection) => {
assert.instanceOf(connection, Connection)
await manager.closeAll()
done()
})

Expand Down Expand Up @@ -133,12 +140,14 @@ test.group('ConnectionManager', (group) => {
const manager = new ConnectionManager(getLogger())
manager.add('primary', Object.assign({}, getConfig(), { client: null }))

manager.on('error', ({ message }, connection) => {
manager.on('error', async ({ message }, connection) => {
try {
assert.equal(message, 'knex: Required configuration option \'client\' is missing.')
assert.instanceOf(connection, Connection)
await manager.closeAll()
done()
} catch (error) {
await manager.closeAll()
done(error)
}
})
Expand All @@ -155,7 +164,7 @@ test.group('ConnectionManager', (group) => {
const manager = new ConnectionManager(getLogger())
manager.add('primary', getConfig())

manager.on('disconnect', (connection) => {
manager.on('disconnect', async (connection) => {
try {
assert.deepEqual(connection, connections[0])
assert.equal(manager['_orphanConnections'].size, 0)
Expand All @@ -167,8 +176,10 @@ test.group('ConnectionManager', (group) => {
connection: connections[1],
},
})
await manager.removeAllListeners().closeAll()
done()
} catch (error) {
await manager.removeAllListeners().closeAll()
done(error)
}
})
Expand Down
10 changes: 9 additions & 1 deletion test/connection/connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ test.group('Connection | setup', (group) => {

test('instantiate knex when connect is invoked', async (assert, done) => {
const connection = new Connection('primary', getConfig(), getLogger())
connection.on('connect', () => {
connection.on('connect', async () => {
assert.isDefined(connection.client!)
assert.equal(connection.pool!.numUsed(), 0)
await connection.disconnect()
done()
})

Expand Down Expand Up @@ -156,6 +157,7 @@ test.group('Connection | setup', (group) => {
assert.equal(connection.client!['_context'].client.constructor.name, 'Client_MySQL')
assert.equal(connection.client!['_context'].client.config.connection.charset, 'utf-8')
assert.equal(connection.client!['_context'].client.config.connection.typeCast, false)
await connection.disconnect()
})
})
}
Expand All @@ -180,6 +182,8 @@ test.group('Health Checks', (group) => {
message: 'Connection is healthy',
error: null,
})

await connection.disconnect()
})

if (process.env.DB !== 'sqlite') {
Expand All @@ -194,6 +198,8 @@ test.group('Health Checks', (group) => {
const report = await connection.getReport()
assert.equal(report.message, 'Unable to reach the database server')
assert.equal(report.error!.errno, 'ENOTFOUND')

await connection.disconnect()
}).timeout(0)

test('get healthcheck report for un-healthy read host', async (assert) => {
Expand All @@ -215,6 +221,8 @@ test.group('Health Checks', (group) => {
const report = await connection.getReport()
assert.equal(report.message, 'Unable to reach one of the read hosts')
assert.equal(report.error!.errno, 'ENOTFOUND')

await connection.disconnect()
}).timeout(0)
}
})
11 changes: 8 additions & 3 deletions test/database/insert-query-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test.group('Query Builder | insert', (group) => {
await cleanup()
})

test('perform insert', (assert) => {
test('perform insert', async (assert) => {
const connection = new Connection('primary', getConfig(), getLogger())
connection.connect()

Expand All @@ -37,9 +37,11 @@ test.group('Query Builder | insert', (group) => {

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

await connection.disconnect()
})

test('perform multi insert', (assert) => {
test('perform multi insert', async (assert) => {
const connection = new Connection('primary', getConfig(), getLogger())
connection.connect()

Expand All @@ -56,9 +58,11 @@ test.group('Query Builder | insert', (group) => {

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

await connection.disconnect()
})

test('define returning columns', (assert) => {
test('define returning columns', async (assert) => {
const connection = new Connection('primary', getConfig(), getLogger())
connection.connect()

Expand All @@ -77,5 +81,6 @@ test.group('Query Builder | insert', (group) => {

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)
await connection.disconnect()
})
})

0 comments on commit f6f0691

Please sign in to comment.