|
| 1 | +/* |
| 2 | + * @adonisjs/lucid |
| 3 | + * |
| 4 | + * (c) AdonisJS |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import { AssertionError } from 'node:assert' |
| 11 | +import { type ApplicationService } from '@adonisjs/core/types' |
| 12 | +import { type LucidRow, type LucidModel } from '../types/model.js' |
| 13 | + |
| 14 | +/** |
| 15 | + * Provides database assertion methods for use inside test bodies. |
| 16 | + * Exposed on Japa's TestContext as `db`. |
| 17 | + */ |
| 18 | +export class DatabaseTestAssertions { |
| 19 | + constructor( |
| 20 | + protected app: ApplicationService, |
| 21 | + protected connectionName?: string |
| 22 | + ) {} |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns a new instance of assertions configured for the given connection |
| 26 | + */ |
| 27 | + connection(connectionName: string) { |
| 28 | + return new DatabaseTestAssertions(this.app, connectionName) |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Returns the Database instance from the container |
| 33 | + */ |
| 34 | + async #getDb() { |
| 35 | + return this.app.container.make('lucid.db') |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Returns a query client for the configured connection |
| 40 | + */ |
| 41 | + async #getConnection() { |
| 42 | + const db = await this.#getDb() |
| 43 | + return db.connection(this.connectionName) |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Assert that the given table has rows matching the provided data. |
| 48 | + * When `count` is provided, asserts that exactly that many matching rows exist. |
| 49 | + */ |
| 50 | + async assertHas(table: string, data: Record<string, any>, count?: number) { |
| 51 | + const connection = await this.#getConnection() |
| 52 | + const result: any = await connection.query().from(table).where(data).count('* as count') |
| 53 | + const actualCount = Number(result[0].count) |
| 54 | + |
| 55 | + if (count !== undefined && actualCount !== count) { |
| 56 | + throw new AssertionError({ |
| 57 | + message: `Expected table '${table}' to have ${count} rows matching ${JSON.stringify(data)}, but found ${actualCount}`, |
| 58 | + actual: actualCount, |
| 59 | + expected: count, |
| 60 | + operator: 'assertHas', |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + if (count === undefined && actualCount === 0) { |
| 65 | + throw new AssertionError({ |
| 66 | + message: `Expected table '${table}' to have rows matching ${JSON.stringify(data)}, but none were found`, |
| 67 | + actual: 0, |
| 68 | + expected: '>= 1', |
| 69 | + operator: 'assertHas', |
| 70 | + }) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Assert that the given table has no rows matching the provided data. |
| 76 | + */ |
| 77 | + async assertMissing(table: string, data: Record<string, any>) { |
| 78 | + const connection = await this.#getConnection() |
| 79 | + const result: any = await connection.query().from(table).where(data).count('* as count') |
| 80 | + const actualCount = Number(result[0].count) |
| 81 | + |
| 82 | + if (actualCount > 0) { |
| 83 | + throw new AssertionError({ |
| 84 | + message: `Expected table '${table}' to have no rows matching ${JSON.stringify(data)}, but found ${actualCount}`, |
| 85 | + actual: actualCount, |
| 86 | + expected: 0, |
| 87 | + operator: 'assertMissing', |
| 88 | + }) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Assert that the given table has exactly the expected number of rows. |
| 94 | + */ |
| 95 | + async assertCount(table: string, expectedCount: number) { |
| 96 | + const connection = await this.#getConnection() |
| 97 | + const result: any = await connection.query().from(table).count('* as count') |
| 98 | + const actualCount = Number(result[0].count) |
| 99 | + |
| 100 | + if (actualCount !== expectedCount) { |
| 101 | + throw new AssertionError({ |
| 102 | + message: `Expected table '${table}' to have ${expectedCount} rows, but found ${actualCount}`, |
| 103 | + actual: actualCount, |
| 104 | + expected: expectedCount, |
| 105 | + operator: 'assertCount', |
| 106 | + }) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Assert that the given table is empty (has no rows). |
| 112 | + */ |
| 113 | + async assertEmpty(table: string) { |
| 114 | + return this.assertCount(table, 0) |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Assert that a model instance exists in the database. |
| 119 | + */ |
| 120 | + async assertModelExists(model: LucidRow) { |
| 121 | + const Model = model.constructor as LucidModel |
| 122 | + const primaryKeyValue = model.$primaryKeyValue |
| 123 | + |
| 124 | + if (primaryKeyValue === undefined) { |
| 125 | + throw new Error(`Cannot assert model existence: primary key value is undefined`) |
| 126 | + } |
| 127 | + |
| 128 | + const result = await Model.find(primaryKeyValue, { connection: this.connectionName }) |
| 129 | + |
| 130 | + if (!result) { |
| 131 | + throw new AssertionError({ |
| 132 | + message: `Expected '${Model.name}' model with primary key ${primaryKeyValue} to exist, but it was not found`, |
| 133 | + actual: 'missing', |
| 134 | + expected: 'exists', |
| 135 | + operator: 'assertModelExists', |
| 136 | + }) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Assert that a model instance does not exist in the database. |
| 142 | + */ |
| 143 | + async assertModelMissing(model: LucidRow) { |
| 144 | + const Model = model.constructor as LucidModel |
| 145 | + const primaryKeyValue = model.$primaryKeyValue |
| 146 | + |
| 147 | + if (primaryKeyValue === undefined) { |
| 148 | + throw new Error(`Cannot assert model absence: primary key value is undefined`) |
| 149 | + } |
| 150 | + |
| 151 | + const result = await Model.find(primaryKeyValue, { connection: this.connectionName }) |
| 152 | + |
| 153 | + if (result) { |
| 154 | + throw new AssertionError({ |
| 155 | + message: `Expected '${Model.name}' model with primary key ${primaryKeyValue} to not exist, but it was found`, |
| 156 | + actual: 'exists', |
| 157 | + expected: 'missing', |
| 158 | + operator: 'assertModelMissing', |
| 159 | + }) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments