Skip to content

Commit

Permalink
feat: add test utils for migrations and database seeders
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 4, 2022
1 parent bfa3aa0 commit 1e52ea6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 53 deletions.
6 changes: 3 additions & 3 deletions providers/DatabaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ export default class DatabaseServiceProvider {
*/
private defineTestUtils() {
this.app.container.withBindings(
['Adonis/Core/TestUtils', 'Adonis/Lucid/Database'],
(testUtils, Db) => {
['Adonis/Core/TestUtils', 'Adonis/Core/Ace'],
(testUtils, ace) => {
const { defineTestUtils } = require('../src/Bindings/TestUtils')
return new defineTestUtils(testUtils, Db, this.app)
return new defineTestUtils(testUtils, ace)
}
)
}
Expand Down
16 changes: 6 additions & 10 deletions src/Bindings/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,24 @@
* file that was distributed with this source code.
*/

import type { DatabaseContract } from '@ioc:Adonis/Lucid/Database'
import type { TestUtilsContract } from '@ioc:Adonis/Core/TestUtils'
import type { ApplicationContract } from '@ioc:Adonis/Core/Application'

import { TestsMigrator } from '../TestUtils/Migration'
import type Ace from '@ioc:Adonis/Core/Ace'

import { TestsSeeder } from '../TestUtils/Seeder'
import { TestsMigrator } from '../TestUtils/Migration'

/**
* Define database testing utilities
*/
export function defineTestUtils(
testUtils: TestUtilsContract,
db: DatabaseContract,
application: ApplicationContract
) {
export function defineTestUtils(testUtils: TestUtilsContract, ace: typeof Ace) {
testUtils.constructor.macro('db', (connectionName?: string) => {
return {
migrate() {
return new TestsMigrator(db, connectionName || db.primaryConnectionName, application).run()
return new TestsMigrator(ace, connectionName).run()
},
seed() {
return new TestsSeeder(db, connectionName || db.primaryConnectionName, application).seed()
return new TestsSeeder(ace, connectionName).run()
},
}
})
Expand Down
41 changes: 17 additions & 24 deletions src/TestUtils/Migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,32 @@
* file that was distributed with this source code.
*/

import type { DatabaseContract } from '@ioc:Adonis/Lucid/Database'
import type { ApplicationContract } from '@ioc:Adonis/Core/Application'

import { Migrator } from '../Migrator'
import type Ace from '@ioc:Adonis/Core/Ace'

/**
* Migrator class to be used for testing.
*/
export class TestsMigrator {
constructor(
private Db: DatabaseContract,
private connectionName: string,
private application: ApplicationContract
) {}
constructor(private ace: typeof Ace, private connectionName?: string) {}

private async rollback() {
const migrator = new Migrator(this.Db, this.application, {
direction: 'down',
connectionName: this.connectionName,
dryRun: false,
})
private async runCommand(commandName: string) {
const args: string[] = []
if (this.connectionName) {
args.push(`--connection="${this.connectionName}"`)
}

migrator.run()
const command = await this.ace.exec(commandName, args)
if (command.exitCode) {
if (command.error) {
throw command.error
} else {
throw new Error(`"${commandName}" failed`)
}
}
}

public async run() {
const migrator = new Migrator(this.Db, this.application, {
direction: 'up',
connectionName: this.connectionName,
dryRun: false,
})

await migrator.run()
return () => this.rollback()
await this.runCommand('migration:run')
return () => this.runCommand('migration:rollback')
}
}
35 changes: 21 additions & 14 deletions src/TestUtils/Seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@
* file that was distributed with this source code.
*/

import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import { DatabaseContract } from '@ioc:Adonis/Lucid/Database'

import { SeedsRunner } from '../SeedsRunner'
import type Ace from '@ioc:Adonis/Core/Ace'

/**
* Seeder class to be used for testing
*/
export class TestsSeeder {
constructor(
private Db: DatabaseContract,
private connectionName: string,
private application: ApplicationContract
) {}
constructor(private ace: typeof Ace, private connectionName?: string) {}

public async seed() {
const runner = new SeedsRunner(this.Db, this.application, this.connectionName)
const seeders = await runner.getList()
private async runCommand(commandName: string) {
const args: string[] = []
if (this.connectionName) {
args.push(`--connection="${this.connectionName}"`)
}

for (let seeder of seeders) {
await runner.run(seeder)
const command = await this.ace.exec(commandName, args)
if (command.exitCode) {
if (command.error) {
throw command.error
} else {
throw new Error(`"${commandName}" failed`)
}
}
}

public async run() {
await this.runCommand('db:seed')
}
}
4 changes: 2 additions & 2 deletions test/database-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ test.group('Database Provider', (group) => {
)

const TestUtils = app.container.use('Adonis/Core/TestUtils')
assert.properties(TestUtils.db, ['seed', 'migrate'])
}).skip(true)
assert.properties(TestUtils.db(), ['seed', 'migrate'])
})
})

0 comments on commit 1e52ea6

Please sign in to comment.