@geekmidas/testkit 9.0.2 → 10.0.0-alpha.0

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.
Files changed (125) hide show
  1. package/dist/{Factory-CUM2767q.d.cts → Factory-C6W78ulZ.d.mts} +2 -2
  2. package/dist/{Factory-ClyZLRsO.d.mts.map → Factory-C6W78ulZ.d.mts.map} +1 -1
  3. package/dist/{Factory-ClyZLRsO.d.mts → Factory-DSEADOaF.d.cts} +2 -2
  4. package/dist/{Factory-CUM2767q.d.cts.map → Factory-DSEADOaF.d.cts.map} +1 -1
  5. package/dist/Factory.d.cts +2 -2
  6. package/dist/Factory.d.mts +2 -2
  7. package/dist/{KyselyFactory-OMwcuAX_.d.cts → KyselyFactory-Cc2UmOJk.d.mts} +3 -3
  8. package/dist/{KyselyFactory-CTZmiGI9.d.mts.map → KyselyFactory-Cc2UmOJk.d.mts.map} +1 -1
  9. package/dist/{KyselyFactory-CTZmiGI9.d.mts → KyselyFactory-DzH0N1zr.d.cts} +3 -3
  10. package/dist/{KyselyFactory-OMwcuAX_.d.cts.map → KyselyFactory-DzH0N1zr.d.cts.map} +1 -1
  11. package/dist/KyselyFactory.d.cts +3 -3
  12. package/dist/KyselyFactory.d.mts +3 -3
  13. package/dist/{ObjectionFactory-DDrKwAoO.d.mts → ObjectionFactory-BYnPr9ZP.d.mts} +3 -3
  14. package/dist/{ObjectionFactory-DDrKwAoO.d.mts.map → ObjectionFactory-BYnPr9ZP.d.mts.map} +1 -1
  15. package/dist/{ObjectionFactory-DvmZVHhe.d.cts → ObjectionFactory-BmKdG0nT.d.cts} +3 -3
  16. package/dist/{ObjectionFactory-DvmZVHhe.d.cts.map → ObjectionFactory-BmKdG0nT.d.cts.map} +1 -1
  17. package/dist/ObjectionFactory.d.cts +3 -3
  18. package/dist/ObjectionFactory.d.mts +3 -3
  19. package/dist/{VitestKyselyTransactionIsolator-CfSMOFlN.mjs → VitestKyselyTransactionIsolator-1Saieke7.mjs} +10 -3
  20. package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs.map +1 -0
  21. package/dist/{VitestKyselyTransactionIsolator-0CqOsuc9.cjs → VitestKyselyTransactionIsolator-BjJSXryR.cjs} +10 -3
  22. package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs.map +1 -0
  23. package/dist/{VitestKyselyTransactionIsolator-CduJlHoT.d.cts → VitestKyselyTransactionIsolator-CwlTo6yf.d.mts} +8 -3
  24. package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts.map +1 -0
  25. package/dist/{VitestKyselyTransactionIsolator-Cswnnj0k.d.mts → VitestKyselyTransactionIsolator-DJasVuIZ.d.cts} +8 -3
  26. package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts.map +1 -0
  27. package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
  28. package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
  29. package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
  30. package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
  31. package/dist/{VitestObjectionTransactionIsolator-CNo40EdC.mjs → VitestObjectionTransactionIsolator-B_VSWFum.mjs} +2 -2
  32. package/dist/{VitestObjectionTransactionIsolator-CNo40EdC.mjs.map → VitestObjectionTransactionIsolator-B_VSWFum.mjs.map} +1 -1
  33. package/dist/{VitestObjectionTransactionIsolator-BXoR6xdG.d.cts → VitestObjectionTransactionIsolator-CTTjvTPO.d.cts} +2 -2
  34. package/dist/{VitestObjectionTransactionIsolator-BXoR6xdG.d.cts.map → VitestObjectionTransactionIsolator-CTTjvTPO.d.cts.map} +1 -1
  35. package/dist/{VitestObjectionTransactionIsolator-x6hY5j4u.d.mts → VitestObjectionTransactionIsolator-CyZG6nq4.d.mts} +2 -2
  36. package/dist/{VitestObjectionTransactionIsolator-x6hY5j4u.d.mts.map → VitestObjectionTransactionIsolator-CyZG6nq4.d.mts.map} +1 -1
  37. package/dist/{VitestObjectionTransactionIsolator-B5J9jjKq.cjs → VitestObjectionTransactionIsolator-DDoJTu7e.cjs} +2 -2
  38. package/dist/{VitestObjectionTransactionIsolator-B5J9jjKq.cjs.map → VitestObjectionTransactionIsolator-DDoJTu7e.cjs.map} +1 -1
  39. package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
  40. package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
  41. package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
  42. package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
  43. package/dist/{VitestTransactionIsolator-BNWJqh9f.d.mts → VitestTransactionIsolator-BLaw80cx.d.mts} +20 -4
  44. package/dist/VitestTransactionIsolator-BLaw80cx.d.mts.map +1 -0
  45. package/dist/{VitestTransactionIsolator-D6ZxLWys.mjs → VitestTransactionIsolator-CvwPecpl.mjs} +17 -4
  46. package/dist/VitestTransactionIsolator-CvwPecpl.mjs.map +1 -0
  47. package/dist/{VitestTransactionIsolator-CtZaOUjH.cjs → VitestTransactionIsolator-DyiX-QtK.cjs} +17 -4
  48. package/dist/VitestTransactionIsolator-DyiX-QtK.cjs.map +1 -0
  49. package/dist/{VitestTransactionIsolator-CSroc7Df.d.cts → VitestTransactionIsolator-glcxyS_R.d.cts} +20 -4
  50. package/dist/VitestTransactionIsolator-glcxyS_R.d.cts.map +1 -0
  51. package/dist/VitestTransactionIsolator.cjs +1 -1
  52. package/dist/VitestTransactionIsolator.d.cts +2 -2
  53. package/dist/VitestTransactionIsolator.d.mts +2 -2
  54. package/dist/VitestTransactionIsolator.mjs +1 -1
  55. package/dist/better-auth.d.cts +2 -2
  56. package/dist/better-auth.d.mts +2 -2
  57. package/dist/{faker-BcjUfHxx.d.mts → faker-D9gz7KjY.d.mts} +3 -3
  58. package/dist/{faker-BcjUfHxx.d.mts.map → faker-D9gz7KjY.d.mts.map} +1 -1
  59. package/dist/{faker-zVCm31nU.d.cts → faker-km9UhOS6.d.cts} +3 -3
  60. package/dist/{faker-zVCm31nU.d.cts.map → faker-km9UhOS6.d.cts.map} +1 -1
  61. package/dist/faker.d.cts +1 -1
  62. package/dist/faker.d.mts +1 -1
  63. package/dist/kysely.cjs +2 -2
  64. package/dist/kysely.d.cts +5 -5
  65. package/dist/kysely.d.mts +5 -5
  66. package/dist/kysely.mjs +2 -2
  67. package/dist/objection.cjs +2 -2
  68. package/dist/objection.d.cts +5 -5
  69. package/dist/objection.d.mts +5 -5
  70. package/dist/objection.mjs +2 -2
  71. package/dist/requestContext.cjs +1 -1
  72. package/dist/requestContext.d.cts +1 -1
  73. package/dist/requestContext.d.mts +1 -1
  74. package/dist/requestContext.mjs +1 -1
  75. package/package.json +10 -7
  76. package/CHANGELOG.md +0 -151
  77. package/dist/VitestKyselyTransactionIsolator-0CqOsuc9.cjs.map +0 -1
  78. package/dist/VitestKyselyTransactionIsolator-CduJlHoT.d.cts.map +0 -1
  79. package/dist/VitestKyselyTransactionIsolator-CfSMOFlN.mjs.map +0 -1
  80. package/dist/VitestKyselyTransactionIsolator-Cswnnj0k.d.mts.map +0 -1
  81. package/dist/VitestTransactionIsolator-BNWJqh9f.d.mts.map +0 -1
  82. package/dist/VitestTransactionIsolator-CSroc7Df.d.cts.map +0 -1
  83. package/dist/VitestTransactionIsolator-CtZaOUjH.cjs.map +0 -1
  84. package/dist/VitestTransactionIsolator-D6ZxLWys.mjs.map +0 -1
  85. package/src/Factory.ts +0 -174
  86. package/src/KyselyFactory.ts +0 -391
  87. package/src/ObjectionFactory.ts +0 -412
  88. package/src/PostgresKyselyMigrator.ts +0 -104
  89. package/src/PostgresMigrator.ts +0 -193
  90. package/src/PostgresObjectionMigrator.ts +0 -138
  91. package/src/VitestKyselyTransactionIsolator.ts +0 -72
  92. package/src/VitestObjectionTransactionIsolator.ts +0 -75
  93. package/src/VitestTransactionIsolator.ts +0 -348
  94. package/src/__tests__/Factory.spec.ts +0 -178
  95. package/src/__tests__/KyselyFactory.spec.ts +0 -458
  96. package/src/__tests__/ObjectionFactory.spec.ts +0 -586
  97. package/src/__tests__/PostgresKyselyMigrator.spec.ts +0 -794
  98. package/src/__tests__/PostgresMigrator.spec.ts +0 -471
  99. package/src/__tests__/PostgresObjectionMigrator.spec.ts +0 -634
  100. package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +0 -138
  101. package/src/__tests__/benchmark.spec.ts +0 -140
  102. package/src/__tests__/better-auth.spec.ts +0 -21
  103. package/src/__tests__/faker.spec.ts +0 -231
  104. package/src/__tests__/initScript.spec.ts +0 -332
  105. package/src/__tests__/integration.spec.ts +0 -610
  106. package/src/__tests__/requestContext.spec.ts +0 -113
  107. package/src/__tests__/utilities.spec.ts +0 -211
  108. package/src/aws.ts +0 -131
  109. package/src/benchmark.ts +0 -48
  110. package/src/better-auth.ts +0 -310
  111. package/src/faker.ts +0 -349
  112. package/src/helpers.ts +0 -45
  113. package/src/initScript.ts +0 -122
  114. package/src/kysely.ts +0 -166
  115. package/src/logger.ts +0 -18
  116. package/src/objection.ts +0 -157
  117. package/src/os/directory.ts +0 -26
  118. package/src/os/index.ts +0 -1
  119. package/src/requestContext.ts +0 -119
  120. package/src/timer.ts +0 -3
  121. package/test/globalSetup.ts +0 -54
  122. package/test/helpers.ts +0 -268
  123. package/test/migrations/1749664623372_user.ts +0 -22
  124. package/tsconfig.json +0 -9
  125. package/vitest.config.ts +0 -8
@@ -1,471 +0,0 @@
1
- import { Client } from 'pg';
2
- import {
3
- afterAll,
4
- afterEach,
5
- beforeAll,
6
- beforeEach,
7
- describe,
8
- expect,
9
- it,
10
- vi,
11
- } from 'vitest';
12
- import { createTestDatabase } from '../../test/helpers';
13
- import { PostgresMigrator } from '../PostgresMigrator';
14
-
15
- // Create a concrete implementation for testing
16
- class TestPostgresMigrator extends PostgresMigrator {
17
- public migrateCalled = false;
18
- public migrateError?: Error;
19
- public customMigrations: Array<() => Promise<void>> = [];
20
-
21
- async migrate(): Promise<void> {
22
- this.migrateCalled = true;
23
- if (this.migrateError) {
24
- throw this.migrateError;
25
- }
26
-
27
- // Run any custom migrations
28
- for (const migration of this.customMigrations) {
29
- await migration();
30
- }
31
- }
32
-
33
- addMigration(migration: () => Promise<void>) {
34
- this.customMigrations.push(migration);
35
- }
36
- }
37
-
38
- describe('PostgresMigrator', () => {
39
- let testDbName: string;
40
- let cleanupDb: () => Promise<void>;
41
- let consoleSpy: any;
42
-
43
- beforeAll(async () => {
44
- // Create a unique test database for each test run
45
- testDbName = `test_postgres_migrator_${Date.now()}`;
46
- cleanupDb = await createTestDatabase(testDbName);
47
- });
48
-
49
- beforeEach(() => {
50
- consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
51
- });
52
-
53
- afterEach(() => {
54
- consoleSpy.mockRestore();
55
- });
56
-
57
- afterAll(async () => {
58
- await cleanupDb();
59
- });
60
-
61
- describe('constructor', () => {
62
- it('should create a PostgresMigrator instance', () => {
63
- const migrator = new TestPostgresMigrator(
64
- `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`,
65
- );
66
- expect(migrator).toBeInstanceOf(PostgresMigrator);
67
- });
68
- });
69
-
70
- describe('start method', () => {
71
- it('should create database, migrate, and return cleanup function', async () => {
72
- const newDbName = `test_start_${Date.now()}`;
73
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
74
- const migrator = new TestPostgresMigrator(uri);
75
-
76
- // Add a simple migration to verify it runs
77
- let migrationRan = false;
78
- migrator.addMigration(async () => {
79
- migrationRan = true;
80
- });
81
-
82
- const cleanup = await migrator.start();
83
-
84
- expect(migrator.migrateCalled).toBe(true);
85
- expect(migrationRan).toBe(true);
86
- expect(consoleSpy).toHaveBeenCalledWith(
87
- `Migrating database: ${newDbName}`,
88
- );
89
- expect(typeof cleanup).toBe('function');
90
-
91
- // Test cleanup
92
- await cleanup();
93
- });
94
-
95
- it('should handle existing database', async () => {
96
- // Use the already created test database
97
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`;
98
- const migrator = new TestPostgresMigrator(uri);
99
-
100
- let migrationRan = false;
101
- migrator.addMigration(async () => {
102
- migrationRan = true;
103
- });
104
-
105
- const cleanup = await migrator.start();
106
-
107
- expect(migrator.migrateCalled).toBe(true);
108
- expect(migrationRan).toBe(true);
109
- expect(typeof cleanup).toBe('function');
110
-
111
- // Test cleanup (but don't actually run it since we need the db for other tests)
112
- });
113
-
114
- it('should handle URI with query parameters', async () => {
115
- const queryDbName = `test_query_params_${Date.now()}`;
116
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${queryDbName}?ssl=false&timeout=30`;
117
- const migrator = new TestPostgresMigrator(uri);
118
-
119
- const cleanup = await migrator.start();
120
-
121
- expect(migrator.migrateCalled).toBe(true);
122
- expect(typeof cleanup).toBe('function');
123
-
124
- await cleanup();
125
- });
126
-
127
- it('should clean up connections even if migration fails', async () => {
128
- const failDbName = `test_fail_${Date.now()}`;
129
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${failDbName}`;
130
- const migrator = new TestPostgresMigrator(uri);
131
- migrator.migrateError = new Error('Migration failed');
132
-
133
- await expect(migrator.start()).rejects.toThrow('Migration failed');
134
-
135
- // Verify the database was still created but migration failed
136
- expect(migrator.migrateCalled).toBe(true);
137
-
138
- // Cleanup the failed database
139
- const cleanupClient = new Client({
140
- host: 'localhost',
141
- port: 5432,
142
- user: 'geekmidas',
143
- password: 'geekmidas',
144
- database: 'postgres',
145
- });
146
- try {
147
- await cleanupClient.connect();
148
- await cleanupClient.query(`DROP DATABASE IF EXISTS "${failDbName}"`);
149
- } finally {
150
- await cleanupClient.end();
151
- }
152
- });
153
-
154
- it('should return cleanup function that drops database', async () => {
155
- const cleanupDbName = `test_cleanup_${Date.now()}`;
156
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${cleanupDbName}`;
157
- const migrator = new TestPostgresMigrator(uri);
158
-
159
- const cleanup = await migrator.start();
160
-
161
- expect(migrator.migrateCalled).toBe(true);
162
- expect(typeof cleanup).toBe('function');
163
-
164
- // Verify database exists before cleanup
165
- const checkClient = new Client({
166
- host: 'localhost',
167
- port: 5432,
168
- user: 'geekmidas',
169
- password: 'geekmidas',
170
- database: 'postgres',
171
- });
172
- await checkClient.connect();
173
- const beforeResult = await checkClient.query(
174
- `SELECT * FROM pg_catalog.pg_database WHERE datname = $1`,
175
- [cleanupDbName],
176
- );
177
- expect(beforeResult.rowCount).toBe(1);
178
- await checkClient.end();
179
-
180
- // Call cleanup
181
- await cleanup();
182
-
183
- // Verify database was dropped
184
- const checkClient2 = new Client({
185
- host: 'localhost',
186
- port: 5432,
187
- user: 'geekmidas',
188
- password: 'geekmidas',
189
- database: 'postgres',
190
- });
191
- await checkClient2.connect();
192
- const afterResult = await checkClient2.query(
193
- `SELECT * FROM pg_catalog.pg_database WHERE datname = $1`,
194
- [cleanupDbName],
195
- );
196
- expect(afterResult.rowCount).toBe(0);
197
- await checkClient2.end();
198
- });
199
- });
200
-
201
- describe('database creation', () => {
202
- it('should handle connection errors gracefully', async () => {
203
- // Use invalid credentials to test connection error
204
- const badDbName = `test_bad_connection_${Date.now()}`;
205
- const uri = `postgresql://invalid_user:invalid_pass@localhost:5432/${badDbName}`;
206
- const migrator = new TestPostgresMigrator(uri);
207
-
208
- await expect(migrator.start()).rejects.toThrow();
209
- });
210
-
211
- it('should handle invalid database names', async () => {
212
- // Use a database name with invalid characters
213
- const invalidDbName = 'test-invalid-db-name!';
214
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${invalidDbName}`;
215
- const migrator = new TestPostgresMigrator(uri);
216
-
217
- await expect(migrator.start).rejects.toThrow();
218
- });
219
- });
220
-
221
- describe('URI parsing', () => {
222
- it('should parse different URI formats correctly', async () => {
223
- const testDbName = `test_uri_parsing_${Date.now()}`;
224
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`;
225
- const migrator = new TestPostgresMigrator(uri);
226
-
227
- const cleanup = await migrator.start();
228
-
229
- expect(migrator.migrateCalled).toBe(true);
230
- expect(typeof cleanup).toBe('function');
231
-
232
- await cleanup();
233
- });
234
- });
235
-
236
- describe('error handling', () => {
237
- it('should propagate migration errors', async () => {
238
- const errorDbName = `test_migration_error_${Date.now()}`;
239
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${errorDbName}`;
240
- const migrator = new TestPostgresMigrator(uri);
241
- const migrationError = new Error('Custom migration error');
242
- migrator.migrateError = migrationError;
243
-
244
- await expect(migrator.start()).rejects.toThrow('Custom migration error');
245
-
246
- // Cleanup the created database
247
- const cleanupClient = new Client({
248
- host: 'localhost',
249
- port: 5432,
250
- user: 'geekmidas',
251
- password: 'geekmidas',
252
- database: 'postgres',
253
- });
254
- try {
255
- await cleanupClient.connect();
256
- await cleanupClient.query(`DROP DATABASE IF EXISTS "${errorDbName}"`);
257
- } finally {
258
- await cleanupClient.end();
259
- }
260
- });
261
-
262
- it('should handle cleanup errors gracefully', async () => {
263
- const cleanupErrorDbName = `test_cleanup_error_${Date.now()}`;
264
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${cleanupErrorDbName}`;
265
- const migrator = new TestPostgresMigrator(uri);
266
-
267
- const cleanup = await migrator.start();
268
-
269
- // Manually drop the database to cause a cleanup error
270
- const adminClient = new Client({
271
- host: 'localhost',
272
- port: 5432,
273
- user: 'geekmidas',
274
- password: 'geekmidas',
275
- database: 'postgres',
276
- });
277
- await adminClient.connect();
278
- await adminClient.query(
279
- `DROP DATABASE IF EXISTS "${cleanupErrorDbName}"`,
280
- );
281
- await adminClient.end();
282
-
283
- // Now cleanup should fail because database doesn't exist
284
- await expect(cleanup()).rejects.toThrow();
285
- });
286
- });
287
-
288
- describe('afterCreate hook', () => {
289
- it('should call afterCreate after database creation and before migrate', async () => {
290
- const hookDbName = `test_after_create_${Date.now()}`;
291
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${hookDbName}`;
292
- const callOrder: string[] = [];
293
-
294
- const migrator = new TestPostgresMigrator(uri, async (receivedUri) => {
295
- callOrder.push('afterCreate');
296
- expect(receivedUri).toBe(uri);
297
- });
298
-
299
- migrator.addMigration(async () => {
300
- callOrder.push('migrate');
301
- });
302
-
303
- const cleanup = await migrator.start();
304
-
305
- expect(callOrder).toEqual(['afterCreate', 'migrate']);
306
-
307
- await cleanup();
308
- });
309
-
310
- it('should skip afterCreate when not provided', async () => {
311
- const noHookDbName = `test_no_hook_${Date.now()}`;
312
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${noHookDbName}`;
313
- const migrator = new TestPostgresMigrator(uri);
314
-
315
- const cleanup = await migrator.start();
316
-
317
- expect(migrator.migrateCalled).toBe(true);
318
-
319
- await cleanup();
320
- });
321
-
322
- it('should propagate afterCreate errors and not run migrate', async () => {
323
- const errorHookDbName = `test_hook_error_${Date.now()}`;
324
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${errorHookDbName}`;
325
-
326
- const migrator = new TestPostgresMigrator(uri, async () => {
327
- throw new Error('afterCreate failed');
328
- });
329
-
330
- await expect(migrator.start()).rejects.toThrow('afterCreate failed');
331
- expect(migrator.migrateCalled).toBe(false);
332
-
333
- // Cleanup the created database
334
- const cleanupClient = new Client({
335
- host: 'localhost',
336
- port: 5432,
337
- user: 'geekmidas',
338
- password: 'geekmidas',
339
- database: 'postgres',
340
- });
341
- try {
342
- await cleanupClient.connect();
343
- await cleanupClient.query(
344
- `DROP DATABASE IF EXISTS "${errorHookDbName}"`,
345
- );
346
- } finally {
347
- await cleanupClient.end();
348
- }
349
- });
350
-
351
- it('should allow afterCreate to set up database objects before migrations', async () => {
352
- const setupDbName = `test_hook_setup_${Date.now()}`;
353
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${setupDbName}`;
354
-
355
- const migrator = new TestPostgresMigrator(uri, async () => {
356
- const client = new Client({
357
- host: 'localhost',
358
- port: 5432,
359
- user: 'geekmidas',
360
- password: 'geekmidas',
361
- database: setupDbName,
362
- });
363
- await client.connect();
364
- await client.query(
365
- "CREATE SCHEMA IF NOT EXISTS app; COMMENT ON SCHEMA app IS 'Created by afterCreate hook';",
366
- );
367
- await client.end();
368
- });
369
-
370
- migrator.addMigration(async () => {
371
- // Verify schema exists during migration
372
- const client = new Client({
373
- host: 'localhost',
374
- port: 5432,
375
- user: 'geekmidas',
376
- password: 'geekmidas',
377
- database: setupDbName,
378
- });
379
- await client.connect();
380
- const result = await client.query(
381
- "SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'app'",
382
- );
383
- expect(result.rowCount).toBe(1);
384
- await client.end();
385
- });
386
-
387
- const cleanup = await migrator.start();
388
-
389
- await cleanup();
390
- });
391
- });
392
-
393
- describe('abstract method', () => {
394
- it('should require concrete implementation of migrate method', () => {
395
- // TypeScript ensures abstract methods are implemented
396
- // This test verifies the TestPostgresMigrator implements migrate
397
- const migrator = new TestPostgresMigrator(
398
- `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`,
399
- );
400
- expect(typeof migrator.migrate).toBe('function');
401
- });
402
- });
403
-
404
- describe('integration scenarios', () => {
405
- it('should handle complete workflow', async () => {
406
- const integrationDbName = `test_integration_${Date.now()}`;
407
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${integrationDbName}`;
408
- const migrator = new TestPostgresMigrator(uri);
409
-
410
- // Add a migration that creates a table
411
- migrator.addMigration(async () => {
412
- const client = new Client({
413
- host: 'localhost',
414
- port: 5432,
415
- user: 'geekmidas',
416
- password: 'geekmidas',
417
- database: integrationDbName,
418
- });
419
- await client.connect();
420
- await client.query(`
421
- CREATE TABLE IF NOT EXISTS test_table (
422
- id SERIAL PRIMARY KEY,
423
- name VARCHAR(255) NOT NULL
424
- )
425
- `);
426
- await client.end();
427
- });
428
-
429
- // Start migration
430
- const cleanup = await migrator.start();
431
-
432
- expect(migrator.migrateCalled).toBe(true);
433
- expect(consoleSpy).toHaveBeenCalledWith(
434
- `Migrating database: ${integrationDbName}`,
435
- );
436
-
437
- // Verify the table was created
438
- const verifyClient = new Client({
439
- host: 'localhost',
440
- port: 5432,
441
- user: 'geekmidas',
442
- password: 'geekmidas',
443
- database: integrationDbName,
444
- });
445
- await verifyClient.connect();
446
- const tableResult = await verifyClient.query(`
447
- SELECT table_name FROM information_schema.tables
448
- WHERE table_schema = 'public' AND table_name = 'test_table'
449
- `);
450
- expect(tableResult.rowCount).toBe(1);
451
- await verifyClient.end();
452
-
453
- // Cleanup
454
- await cleanup();
455
- });
456
-
457
- it('should handle database that already exists and cleanup', async () => {
458
- // Use the existing test database
459
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`;
460
- const migrator = new TestPostgresMigrator(uri);
461
-
462
- // Start migration (database already exists)
463
- const cleanup = await migrator.start();
464
-
465
- expect(migrator.migrateCalled).toBe(true);
466
-
467
- // Don't call cleanup as we need the database for other tests
468
- expect(typeof cleanup).toBe('function');
469
- });
470
- });
471
- });