@geekmidas/testkit 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PostgresKyselyMigrator.spec +471 -0
- package/dist/Factory.mjs +1 -1
- package/dist/{KyselyFactory-DiiWtMYe.cjs → KyselyFactory-BGvSMLtd.cjs} +11 -12
- package/dist/{KyselyFactory-DZewtWtJ.mjs → KyselyFactory-ionH4gvk.mjs} +12 -13
- package/dist/KyselyFactory.cjs +2 -1
- package/dist/KyselyFactory.mjs +3 -2
- package/dist/{ObjectionFactory-MAf2m8LI.mjs → ObjectionFactory-CFrtXe7i.mjs} +1 -1
- package/dist/ObjectionFactory.cjs +1 -1
- package/dist/ObjectionFactory.mjs +2 -2
- package/dist/{PostgresKyselyMigrator-ChMJpPrQ.mjs → PostgresKyselyMigrator-CbtiZgfI.mjs} +1 -1
- package/dist/{PostgresKyselyMigrator-rY3hO_-1.cjs → PostgresKyselyMigrator-Cxf2Dp9y.cjs} +3 -2
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/{PostgresMigrator-BJ2-5A_b.cjs → PostgresMigrator-eqyAFSf-.cjs} +2 -39
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/VitestKyselyTransactionIsolator-DXjWQtDN.mjs +12 -0
- package/dist/VitestKyselyTransactionIsolator-Dh2AgJDd.cjs +17 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +5 -0
- package/dist/VitestKyselyTransactionIsolator.mjs +5 -0
- package/dist/VitestTransactionIsolator-pLwsDo_A.mjs +40 -0
- package/dist/VitestTransactionIsolator-zK5NJ7DQ.cjs +51 -0
- package/dist/VitestTransactionIsolator.cjs +5 -0
- package/dist/VitestTransactionIsolator.mjs +4 -0
- package/dist/__tests__/Factory.spec.cjs +139 -0
- package/dist/__tests__/Factory.spec.mjs +139 -0
- package/dist/__tests__/KyselyFactory.spec.cjs +221 -15008
- package/dist/__tests__/KyselyFactory.spec.mjs +220 -15034
- package/dist/__tests__/ObjectionFactory.spec.cjs +387 -0
- package/dist/__tests__/ObjectionFactory.spec.mjs +386 -0
- package/dist/__tests__/PostgresMigrator.spec.cjs +257 -0
- package/dist/__tests__/PostgresMigrator.spec.mjs +256 -0
- package/dist/__tests__/faker.spec.cjs +115 -0
- package/dist/__tests__/faker.spec.mjs +115 -0
- package/dist/__tests__/integration.spec.cjs +279 -0
- package/dist/__tests__/integration.spec.mjs +279 -0
- package/dist/chunk-DWy1uDak.cjs +39 -0
- package/dist/dist-BM2KvLG1.mjs +5618 -0
- package/dist/dist-DE3gAxQI.cjs +5736 -0
- package/dist/example.cjs +2 -1
- package/dist/example.mjs +3 -2
- package/dist/faker-cGCFcrj2.mjs +85 -0
- package/dist/faker-h6CkRloU.cjs +121 -0
- package/dist/faker.cjs +8 -0
- package/dist/faker.mjs +3 -0
- package/dist/helpers-BnARb5Ap.mjs +19 -0
- package/dist/helpers-C2NH7xcz.cjs +135 -0
- package/dist/helpers-C_RZk04R.cjs +31 -0
- package/dist/helpers-CukcFAU9.mjs +111 -0
- package/dist/helpers.cjs +7 -0
- package/dist/helpers.mjs +6 -0
- package/dist/kysely.cjs +16 -4
- package/dist/kysely.mjs +16 -5
- package/dist/objection.cjs +1 -1
- package/dist/objection.mjs +2 -2
- package/dist/vi.bdSIJ99Y-BgRxGeO2.mjs +9382 -0
- package/dist/vi.bdSIJ99Y-CFuzUeY6.cjs +9393 -0
- package/package.json +11 -6
- package/src/Factory.ts +3 -1
- package/src/KyselyFactory.ts +30 -36
- package/src/VitestKyselyTransactionIsolator.ts +23 -0
- package/src/VitestTransactionIsolator.ts +70 -0
- package/src/__tests__/Factory.spec.ts +164 -0
- package/src/__tests__/KyselyFactory.spec.ts +432 -64
- package/src/__tests__/ObjectionFactory.spec.ts +532 -0
- package/src/__tests__/PostgresMigrator.spec.ts +366 -0
- package/src/__tests__/faker.spec.ts +142 -0
- package/src/__tests__/integration.spec.ts +442 -0
- package/src/faker.ts +112 -0
- package/src/helpers.ts +28 -0
- package/src/kysely.ts +14 -0
- package/test/globalSetup.ts +41 -40
- package/test/helpers.ts +273 -0
- /package/dist/{Factory-DlzMkMzb.mjs → Factory-D52Lsc6Z.mjs} +0 -0
- /package/dist/{ObjectionFactory-DeFYWbzt.cjs → ObjectionFactory-BlkzSEqo.cjs} +0 -0
- /package/dist/{PostgresMigrator-BKaNTth5.mjs → PostgresMigrator-DqeuPy-e.mjs} +0 -0
- /package/dist/{magic-string.es-CxbtJGk_.mjs → magic-string.es-C6yzoryu.mjs} +0 -0
- /package/dist/{magic-string.es-KiPEzMtt.cjs → magic-string.es-jdtJrR0A.cjs} +0 -0
package/test/helpers.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
import knex from 'knex';
|
|
3
|
+
import {
|
|
4
|
+
CamelCasePlugin,
|
|
5
|
+
type ControlledTransaction,
|
|
6
|
+
Kysely,
|
|
7
|
+
type Migrator,
|
|
8
|
+
PostgresDialect,
|
|
9
|
+
sql,
|
|
10
|
+
} from 'kysely';
|
|
11
|
+
import pg from 'pg';
|
|
12
|
+
import { TEST_DATABASE_CONFIG } from './globalSetup';
|
|
13
|
+
|
|
14
|
+
export interface TestDatabase {
|
|
15
|
+
users: {
|
|
16
|
+
id: number;
|
|
17
|
+
name: string;
|
|
18
|
+
email: string;
|
|
19
|
+
role?: 'admin' | 'user';
|
|
20
|
+
createdAt: Date;
|
|
21
|
+
updatedAt?: Date;
|
|
22
|
+
};
|
|
23
|
+
posts: {
|
|
24
|
+
id: number;
|
|
25
|
+
title: string;
|
|
26
|
+
content: string;
|
|
27
|
+
userId: number;
|
|
28
|
+
published?: boolean;
|
|
29
|
+
createdAt: Date;
|
|
30
|
+
updatedAt?: Date;
|
|
31
|
+
};
|
|
32
|
+
comments: {
|
|
33
|
+
id: number;
|
|
34
|
+
content: string;
|
|
35
|
+
postId: number;
|
|
36
|
+
userId: number;
|
|
37
|
+
createdAt: Date;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates a Kysely database instance for testing
|
|
43
|
+
*/
|
|
44
|
+
export function createKyselyDb(): Kysely<TestDatabase> {
|
|
45
|
+
return new Kysely({
|
|
46
|
+
dialect: new PostgresDialect({
|
|
47
|
+
pool: new pg.Pool(TEST_DATABASE_CONFIG),
|
|
48
|
+
}),
|
|
49
|
+
plugins: [new CamelCasePlugin()],
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Creates a Knex database instance for testing
|
|
55
|
+
*/
|
|
56
|
+
export function createKnexDb(): Knex {
|
|
57
|
+
return knex({
|
|
58
|
+
client: 'pg',
|
|
59
|
+
connection: TEST_DATABASE_CONFIG,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Test setup helper that creates tables within a transaction and returns cleanup
|
|
65
|
+
*/
|
|
66
|
+
export async function setupKyselyTest(db: Kysely<TestDatabase>): Promise<{
|
|
67
|
+
db: Kysely<TestDatabase>;
|
|
68
|
+
trx: ControlledTransaction<TestDatabase, []>;
|
|
69
|
+
cleanup: () => Promise<void>;
|
|
70
|
+
}> {
|
|
71
|
+
const trx = await db.startTransaction().execute();
|
|
72
|
+
|
|
73
|
+
// Create tables within the transaction
|
|
74
|
+
await createTestTables(db);
|
|
75
|
+
|
|
76
|
+
const cleanup = async () => {
|
|
77
|
+
await trx.rollback().execute();
|
|
78
|
+
await db.destroy();
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return { db, trx, cleanup };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Test setup helper for Knex/Objection that creates tables within a transaction
|
|
86
|
+
*/
|
|
87
|
+
export async function setupKnexTest(): Promise<{
|
|
88
|
+
db: Knex;
|
|
89
|
+
trx: Knex.Transaction;
|
|
90
|
+
cleanup: () => Promise<void>;
|
|
91
|
+
}> {
|
|
92
|
+
const db = createKnexDb();
|
|
93
|
+
const trx = await db.transaction();
|
|
94
|
+
|
|
95
|
+
// Create tables within the transaction
|
|
96
|
+
await createTestTablesKnex(trx);
|
|
97
|
+
|
|
98
|
+
const cleanup = async () => {
|
|
99
|
+
await trx.rollback();
|
|
100
|
+
await db.destroy();
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return { db, trx, cleanup };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Creates test tables using Kysely
|
|
108
|
+
*/
|
|
109
|
+
export async function createTestTables(
|
|
110
|
+
db: Kysely<TestDatabase> | ControlledTransaction<TestDatabase, []>,
|
|
111
|
+
): Promise<void> {
|
|
112
|
+
// Create users table
|
|
113
|
+
await db.schema
|
|
114
|
+
.createTable('users')
|
|
115
|
+
.addColumn('id', 'bigserial', (col) => col.primaryKey())
|
|
116
|
+
.addColumn('name', 'varchar', (col) => col.notNull())
|
|
117
|
+
.addColumn('email', 'varchar', (col) => col.notNull().unique())
|
|
118
|
+
.addColumn('role', 'varchar', (col) => col.defaultTo('user'))
|
|
119
|
+
.addColumn('created_at', 'timestamp', (col) =>
|
|
120
|
+
col.defaultTo(sql`now()`).notNull(),
|
|
121
|
+
)
|
|
122
|
+
.addColumn('updated_at', 'timestamp', (col) =>
|
|
123
|
+
col.defaultTo(sql`now()`).notNull(),
|
|
124
|
+
)
|
|
125
|
+
.execute();
|
|
126
|
+
|
|
127
|
+
// Create posts table
|
|
128
|
+
await db.schema
|
|
129
|
+
.createTable('posts')
|
|
130
|
+
.addColumn('id', 'bigserial', (col) => col.primaryKey())
|
|
131
|
+
.addColumn('title', 'varchar', (col) => col.notNull())
|
|
132
|
+
.addColumn('content', 'text', (col) => col.notNull())
|
|
133
|
+
.addColumn('user_id', 'bigint', (col) =>
|
|
134
|
+
col.notNull().references('users.id').onDelete('cascade'),
|
|
135
|
+
)
|
|
136
|
+
.addColumn('published', 'boolean', (col) => col.defaultTo(false))
|
|
137
|
+
.addColumn('created_at', 'timestamp', (col) =>
|
|
138
|
+
col.defaultTo(sql`now()`).notNull(),
|
|
139
|
+
)
|
|
140
|
+
.addColumn('updated_at', 'timestamp', (col) =>
|
|
141
|
+
col.defaultTo(sql`now()`).notNull(),
|
|
142
|
+
)
|
|
143
|
+
.execute();
|
|
144
|
+
|
|
145
|
+
// Create comments table
|
|
146
|
+
await db.schema
|
|
147
|
+
.createTable('comments')
|
|
148
|
+
.addColumn('id', 'bigserial', (col) => col.primaryKey())
|
|
149
|
+
.addColumn('content', 'text', (col) => col.notNull())
|
|
150
|
+
.addColumn('post_id', 'bigint', (col) =>
|
|
151
|
+
col.notNull().references('posts.id').onDelete('cascade'),
|
|
152
|
+
)
|
|
153
|
+
.addColumn('user_id', 'bigint', (col) =>
|
|
154
|
+
col.notNull().references('users.id').onDelete('cascade'),
|
|
155
|
+
)
|
|
156
|
+
.addColumn('created_at', 'timestamp', (col) =>
|
|
157
|
+
col.defaultTo(sql`now()`).notNull(),
|
|
158
|
+
)
|
|
159
|
+
.execute();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Creates test tables using Knex
|
|
164
|
+
*/
|
|
165
|
+
async function createTestTablesKnex(trx: Knex.Transaction): Promise<void> {
|
|
166
|
+
// Create users table
|
|
167
|
+
await trx.schema.createTable('users', (table) => {
|
|
168
|
+
table.bigIncrements('id').primary();
|
|
169
|
+
table.string('name').notNullable();
|
|
170
|
+
table.string('email').notNullable().unique();
|
|
171
|
+
table.string('role').defaultTo('user');
|
|
172
|
+
table.timestamp('created_at').defaultTo(trx.fn.now()).notNullable();
|
|
173
|
+
table.timestamp('updated_at').defaultTo(trx.fn.now()).notNullable();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// Create posts table
|
|
177
|
+
await trx.schema.createTable('posts', (table) => {
|
|
178
|
+
table.bigIncrements('id').primary();
|
|
179
|
+
table.string('title').notNullable();
|
|
180
|
+
table.text('content').notNullable();
|
|
181
|
+
table
|
|
182
|
+
.bigInteger('user_id')
|
|
183
|
+
.notNullable()
|
|
184
|
+
.references('id')
|
|
185
|
+
.inTable('users')
|
|
186
|
+
.onDelete('cascade');
|
|
187
|
+
table.boolean('published').defaultTo(false);
|
|
188
|
+
table.timestamp('created_at').defaultTo(trx.fn.now()).notNullable();
|
|
189
|
+
table.timestamp('updated_at').defaultTo(trx.fn.now()).notNullable();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Create comments table
|
|
193
|
+
await trx.schema.createTable('comments', (table) => {
|
|
194
|
+
table.bigIncrements('id').primary();
|
|
195
|
+
table.text('content').notNullable();
|
|
196
|
+
table
|
|
197
|
+
.bigInteger('post_id')
|
|
198
|
+
.notNullable()
|
|
199
|
+
.references('id')
|
|
200
|
+
.inTable('posts')
|
|
201
|
+
.onDelete('cascade');
|
|
202
|
+
table
|
|
203
|
+
.bigInteger('user_id')
|
|
204
|
+
.notNullable()
|
|
205
|
+
.references('id')
|
|
206
|
+
.inTable('users')
|
|
207
|
+
.onDelete('cascade');
|
|
208
|
+
table.timestamp('created_at').defaultTo(trx.fn.now()).notNullable();
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Helper for PostgresMigrator tests - creates a separate test database
|
|
214
|
+
*/
|
|
215
|
+
export async function createTestDatabase(
|
|
216
|
+
dbName: string,
|
|
217
|
+
): Promise<() => Promise<void>> {
|
|
218
|
+
const adminConfig = {
|
|
219
|
+
host: TEST_DATABASE_CONFIG.host,
|
|
220
|
+
port: TEST_DATABASE_CONFIG.port,
|
|
221
|
+
user: TEST_DATABASE_CONFIG.user,
|
|
222
|
+
password: TEST_DATABASE_CONFIG.password,
|
|
223
|
+
database: 'postgres',
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const client = new pg.Client(adminConfig);
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
await client.connect();
|
|
230
|
+
|
|
231
|
+
// Drop database if it exists, then create it
|
|
232
|
+
await client.query(`DROP DATABASE IF EXISTS "${dbName}"`);
|
|
233
|
+
await client.query(`CREATE DATABASE "${dbName}"`);
|
|
234
|
+
} finally {
|
|
235
|
+
await client.end();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Return cleanup function
|
|
239
|
+
return async () => {
|
|
240
|
+
const cleanupClient = new pg.Client(adminConfig);
|
|
241
|
+
try {
|
|
242
|
+
await cleanupClient.connect();
|
|
243
|
+
await cleanupClient.query(`DROP DATABASE IF EXISTS "${dbName}"`);
|
|
244
|
+
} finally {
|
|
245
|
+
await cleanupClient.end();
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Creates a Kysely migrator for testing
|
|
252
|
+
*/
|
|
253
|
+
export function createTestMigrator(
|
|
254
|
+
db: Kysely<any>,
|
|
255
|
+
migrations: Record<
|
|
256
|
+
string,
|
|
257
|
+
{
|
|
258
|
+
up: (db: Kysely<any>) => Promise<void>;
|
|
259
|
+
down: (db: Kysely<any>) => Promise<void>;
|
|
260
|
+
}
|
|
261
|
+
>,
|
|
262
|
+
): Migrator {
|
|
263
|
+
const { Migrator } = require('kysely');
|
|
264
|
+
|
|
265
|
+
return new Migrator({
|
|
266
|
+
db,
|
|
267
|
+
provider: {
|
|
268
|
+
async getMigrations() {
|
|
269
|
+
return migrations;
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
});
|
|
273
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|