@geekmidas/testkit 0.3.1 → 0.6.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 (209) hide show
  1. package/dist/Factory-BFVnMMCC.mjs.map +1 -1
  2. package/dist/Factory-BhjUOBWN.cjs.map +1 -1
  3. package/dist/{Factory-BK06XuDA.d.cts → Factory-Bx0AJXZB.d.cts} +3 -2
  4. package/dist/Factory-Bx0AJXZB.d.cts.map +1 -0
  5. package/dist/{Factory-BcGJjLc8.d.mts → Factory-SFupxRC2.d.mts} +2 -1
  6. package/dist/Factory-SFupxRC2.d.mts.map +1 -0
  7. package/dist/Factory.d.cts +2 -2
  8. package/dist/Factory.d.mts +1 -1
  9. package/dist/KyselyFactory-BFqVIn_0.cjs.map +1 -1
  10. package/dist/KyselyFactory-DMswpwji.mjs.map +1 -1
  11. package/dist/{KyselyFactory-Bu9ssWZP.d.cts → KyselyFactory-KLeKH43i.d.cts} +4 -3
  12. package/dist/KyselyFactory-KLeKH43i.d.cts.map +1 -0
  13. package/dist/{KyselyFactory-Cj-EultY.d.mts → KyselyFactory-vAxYodck.d.mts} +3 -2
  14. package/dist/KyselyFactory-vAxYodck.d.mts.map +1 -0
  15. package/dist/KyselyFactory.d.cts +3 -3
  16. package/dist/KyselyFactory.d.mts +2 -2
  17. package/dist/{ObjectionFactory-DL4qkuF1.d.mts → ObjectionFactory-BWjB49-i.d.mts} +3 -2
  18. package/dist/ObjectionFactory-BWjB49-i.d.mts.map +1 -0
  19. package/dist/ObjectionFactory-BeFBYcan.cjs.map +1 -1
  20. package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +1 -1
  21. package/dist/{ObjectionFactory-tKWZOiYO.d.cts → ObjectionFactory-aMGvAKt9.d.cts} +4 -3
  22. package/dist/ObjectionFactory-aMGvAKt9.d.cts.map +1 -0
  23. package/dist/ObjectionFactory.d.cts +3 -3
  24. package/dist/ObjectionFactory.d.mts +2 -2
  25. package/dist/{PostgresKyselyMigrator-upT-hmrz.mjs → PostgresKyselyMigrator-6sE1KOni.mjs} +2 -2
  26. package/dist/PostgresKyselyMigrator-6sE1KOni.mjs.map +1 -0
  27. package/dist/{PostgresKyselyMigrator-CIx3AFSR.d.mts → PostgresKyselyMigrator-CBltSOq5.d.cts} +3 -2
  28. package/dist/PostgresKyselyMigrator-CBltSOq5.d.cts.map +1 -0
  29. package/dist/{PostgresKyselyMigrator-CfytARcA.cjs → PostgresKyselyMigrator-D6IbPq8t.cjs} +2 -2
  30. package/dist/PostgresKyselyMigrator-D6IbPq8t.cjs.map +1 -0
  31. package/dist/{PostgresKyselyMigrator-CQ3aUoy_.d.cts → PostgresKyselyMigrator-DrVWncqd.d.mts} +3 -2
  32. package/dist/PostgresKyselyMigrator-DrVWncqd.d.mts.map +1 -0
  33. package/dist/PostgresKyselyMigrator.cjs +2 -2
  34. package/dist/PostgresKyselyMigrator.d.cts +2 -2
  35. package/dist/PostgresKyselyMigrator.d.mts +2 -2
  36. package/dist/PostgresKyselyMigrator.mjs +2 -2
  37. package/dist/{PostgresMigrator-DbuJGAVy.mjs → PostgresMigrator-BjjenqSd.mjs} +2 -2
  38. package/dist/PostgresMigrator-BjjenqSd.mjs.map +1 -0
  39. package/dist/{PostgresMigrator-D5UkK1_K.d.cts → PostgresMigrator-Bres0U6E.d.cts} +2 -1
  40. package/dist/PostgresMigrator-Bres0U6E.d.cts.map +1 -0
  41. package/dist/{PostgresMigrator-DFcNdCvD.cjs → PostgresMigrator-D6dQn0x2.cjs} +2 -2
  42. package/dist/PostgresMigrator-D6dQn0x2.cjs.map +1 -0
  43. package/dist/{PostgresMigrator-DQaRxoaY.d.mts → PostgresMigrator-S-YYosAC.d.mts} +2 -1
  44. package/dist/PostgresMigrator-S-YYosAC.d.mts.map +1 -0
  45. package/dist/PostgresMigrator.cjs +1 -1
  46. package/dist/PostgresMigrator.d.cts +1 -1
  47. package/dist/PostgresMigrator.d.mts +1 -1
  48. package/dist/PostgresMigrator.mjs +1 -1
  49. package/dist/{PostgresObjectionMigrator-CZHHcCOv.d.cts → PostgresObjectionMigrator-CPfBAP7r.d.cts} +3 -2
  50. package/dist/PostgresObjectionMigrator-CPfBAP7r.d.cts.map +1 -0
  51. package/dist/{PostgresObjectionMigrator-BG6ymgnt.cjs → PostgresObjectionMigrator-DK8ODIHQ.cjs} +2 -2
  52. package/dist/PostgresObjectionMigrator-DK8ODIHQ.cjs.map +1 -0
  53. package/dist/{PostgresObjectionMigrator-D_hCcrQu.d.mts → PostgresObjectionMigrator-DVEqB5tp.d.mts} +3 -2
  54. package/dist/PostgresObjectionMigrator-DVEqB5tp.d.mts.map +1 -0
  55. package/dist/{PostgresObjectionMigrator-DPj2pOpX.mjs → PostgresObjectionMigrator-D_QxXbIN.mjs} +2 -2
  56. package/dist/PostgresObjectionMigrator-D_QxXbIN.mjs.map +1 -0
  57. package/dist/PostgresObjectionMigrator.cjs +2 -2
  58. package/dist/PostgresObjectionMigrator.d.cts +2 -2
  59. package/dist/PostgresObjectionMigrator.d.mts +2 -2
  60. package/dist/PostgresObjectionMigrator.mjs +2 -2
  61. package/dist/{VitestKyselyTransactionIsolator-D3EZZhjZ.d.cts → VitestKyselyTransactionIsolator-CduJlHoT.d.cts} +4 -3
  62. package/dist/VitestKyselyTransactionIsolator-CduJlHoT.d.cts.map +1 -0
  63. package/dist/{VitestKyselyTransactionIsolator-Dxlp1u0f.d.mts → VitestKyselyTransactionIsolator-Cswnnj0k.d.mts} +4 -3
  64. package/dist/VitestKyselyTransactionIsolator-Cswnnj0k.d.mts.map +1 -0
  65. package/dist/{VitestKyselyTransactionIsolator-EvDLk5zg.cjs → VitestKyselyTransactionIsolator-D7RRXOBa.cjs} +2 -2
  66. package/dist/VitestKyselyTransactionIsolator-D7RRXOBa.cjs.map +1 -0
  67. package/dist/{VitestKyselyTransactionIsolator-CNURW8y6.mjs → VitestKyselyTransactionIsolator-DceyIqr4.mjs} +2 -2
  68. package/dist/VitestKyselyTransactionIsolator-DceyIqr4.mjs.map +1 -0
  69. package/dist/VitestKyselyTransactionIsolator.cjs +1 -1
  70. package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
  71. package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
  72. package/dist/VitestKyselyTransactionIsolator.mjs +1 -1
  73. package/dist/{VitestObjectionTransactionIsolator-1TpsPqfG.d.cts → VitestObjectionTransactionIsolator-BXoR6xdG.d.cts} +4 -3
  74. package/dist/VitestObjectionTransactionIsolator-BXoR6xdG.d.cts.map +1 -0
  75. package/dist/{VitestObjectionTransactionIsolator-CM5KTAFA.cjs → VitestObjectionTransactionIsolator-CdLRrzNf.cjs} +2 -2
  76. package/dist/VitestObjectionTransactionIsolator-CdLRrzNf.cjs.map +1 -0
  77. package/dist/{VitestObjectionTransactionIsolator-jQFaCz0u.mjs → VitestObjectionTransactionIsolator-OF2osYY5.mjs} +2 -2
  78. package/dist/VitestObjectionTransactionIsolator-OF2osYY5.mjs.map +1 -0
  79. package/dist/{VitestObjectionTransactionIsolator-i9jIgU8Q.d.mts → VitestObjectionTransactionIsolator-x6hY5j4u.d.mts} +4 -3
  80. package/dist/VitestObjectionTransactionIsolator-x6hY5j4u.d.mts.map +1 -0
  81. package/dist/VitestObjectionTransactionIsolator.cjs +1 -1
  82. package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
  83. package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
  84. package/dist/VitestObjectionTransactionIsolator.mjs +1 -1
  85. package/dist/{VitestTransactionIsolator-BvR19bYn.d.mts → VitestTransactionIsolator-BNWJqh9f.d.mts} +3 -2
  86. package/dist/VitestTransactionIsolator-BNWJqh9f.d.mts.map +1 -0
  87. package/dist/VitestTransactionIsolator-CMfJXZP8.cjs.map +1 -1
  88. package/dist/{VitestTransactionIsolator-CwQaxZLP.d.cts → VitestTransactionIsolator-CSroc7Df.d.cts} +3 -2
  89. package/dist/VitestTransactionIsolator-CSroc7Df.d.cts.map +1 -0
  90. package/dist/VitestTransactionIsolator-DQ7tLqgV.mjs.map +1 -1
  91. package/dist/VitestTransactionIsolator.d.cts +1 -1
  92. package/dist/VitestTransactionIsolator.d.mts +1 -1
  93. package/dist/aws.cjs.map +1 -1
  94. package/dist/aws.d.cts +2 -0
  95. package/dist/aws.d.cts.map +1 -0
  96. package/dist/aws.d.mts +2 -0
  97. package/dist/aws.d.mts.map +1 -0
  98. package/dist/aws.mjs.map +1 -1
  99. package/dist/benchmark.cjs.map +1 -1
  100. package/dist/benchmark.d.cts +1 -0
  101. package/dist/benchmark.d.cts.map +1 -0
  102. package/dist/benchmark.d.mts +1 -0
  103. package/dist/benchmark.d.mts.map +1 -0
  104. package/dist/benchmark.mjs.map +1 -1
  105. package/dist/better-auth.cjs +29 -30
  106. package/dist/better-auth.cjs.map +1 -1
  107. package/dist/better-auth.d.cts +2 -2
  108. package/dist/better-auth.d.cts.map +1 -0
  109. package/dist/better-auth.d.mts.map +1 -0
  110. package/dist/better-auth.mjs +29 -30
  111. package/dist/better-auth.mjs.map +1 -1
  112. package/dist/directory-B-Ozljzk.mjs.map +1 -1
  113. package/dist/directory-BVC8g7cX.cjs.map +1 -1
  114. package/dist/{directory-DlkPEzL4.d.cts → directory-CVrfTq1I.d.mts} +2 -1
  115. package/dist/directory-CVrfTq1I.d.mts.map +1 -0
  116. package/dist/directory-Cys9g76X.d.cts +13 -0
  117. package/dist/directory-Cys9g76X.d.cts.map +1 -0
  118. package/dist/faker-B14IEMIN.cjs.map +1 -1
  119. package/dist/faker-BGKYFoCT.mjs.map +1 -1
  120. package/dist/{faker-Cg76aFNO.d.cts → faker-BSH1EMtg.d.cts} +3 -3
  121. package/dist/faker-BSH1EMtg.d.cts.map +1 -0
  122. package/dist/faker-DHh7xs4u.d.mts.map +1 -0
  123. package/dist/faker.d.cts +1 -1
  124. package/dist/helpers.cjs.map +1 -1
  125. package/dist/helpers.d.cts +1 -0
  126. package/dist/helpers.d.cts.map +1 -0
  127. package/dist/helpers.d.mts +1 -0
  128. package/dist/helpers.d.mts.map +1 -0
  129. package/dist/helpers.mjs.map +1 -1
  130. package/dist/kysely.cjs +3 -3
  131. package/dist/kysely.cjs.map +1 -1
  132. package/dist/kysely.d.cts +8 -7
  133. package/dist/kysely.d.cts.map +1 -0
  134. package/dist/kysely.d.mts +7 -6
  135. package/dist/kysely.d.mts.map +1 -0
  136. package/dist/kysely.mjs +3 -3
  137. package/dist/kysely.mjs.map +1 -1
  138. package/dist/logger.cjs.map +1 -1
  139. package/dist/logger.d.cts +1 -0
  140. package/dist/logger.d.cts.map +1 -0
  141. package/dist/logger.d.mts +1 -0
  142. package/dist/logger.d.mts.map +1 -0
  143. package/dist/logger.mjs.map +1 -1
  144. package/dist/objection.cjs +3 -3
  145. package/dist/objection.cjs.map +1 -1
  146. package/dist/objection.d.cts +8 -7
  147. package/dist/objection.d.cts.map +1 -0
  148. package/dist/objection.d.mts +7 -6
  149. package/dist/objection.d.mts.map +1 -0
  150. package/dist/objection.mjs +3 -3
  151. package/dist/objection.mjs.map +1 -1
  152. package/dist/os/directory.d.cts +1 -1
  153. package/dist/os/directory.d.mts +1 -1
  154. package/dist/os/index.d.cts +1 -1
  155. package/dist/os/index.d.mts +1 -1
  156. package/dist/timer.cjs.map +1 -1
  157. package/dist/timer.d.cts +2 -0
  158. package/dist/timer.d.cts.map +1 -0
  159. package/dist/timer.d.mts +2 -0
  160. package/dist/timer.d.mts.map +1 -0
  161. package/dist/timer.mjs.map +1 -1
  162. package/package.json +39 -5
  163. package/src/Factory.ts +72 -72
  164. package/src/KyselyFactory.ts +330 -330
  165. package/src/ObjectionFactory.ts +354 -355
  166. package/src/PostgresKyselyMigrator.ts +37 -37
  167. package/src/PostgresMigrator.ts +107 -107
  168. package/src/PostgresObjectionMigrator.ts +91 -91
  169. package/src/VitestKyselyTransactionIsolator.ts +27 -27
  170. package/src/VitestObjectionTransactionIsolator.ts +39 -39
  171. package/src/VitestTransactionIsolator.ts +196 -195
  172. package/src/__tests__/Factory.spec.ts +163 -155
  173. package/src/__tests__/KyselyFactory.spec.ts +443 -439
  174. package/src/__tests__/ObjectionFactory.spec.ts +563 -557
  175. package/src/__tests__/PostgresKyselyMigrator.spec.ts +641 -641
  176. package/src/__tests__/PostgresMigrator.spec.ts +341 -341
  177. package/src/__tests__/PostgresObjectionMigrator.spec.ts +578 -578
  178. package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +114 -114
  179. package/src/__tests__/benchmark.spec.ts +140 -0
  180. package/src/__tests__/better-auth.spec.ts +15 -15
  181. package/src/__tests__/faker.spec.ts +226 -137
  182. package/src/__tests__/integration.spec.ts +597 -597
  183. package/src/__tests__/utilities.spec.ts +211 -0
  184. package/src/aws.ts +104 -104
  185. package/src/benchmark.ts +12 -12
  186. package/src/better-auth.ts +286 -301
  187. package/src/faker.ts +153 -153
  188. package/src/helpers.ts +6 -6
  189. package/src/kysely.ts +33 -33
  190. package/src/logger.ts +10 -10
  191. package/src/objection.ts +31 -31
  192. package/src/os/directory.ts +11 -10
  193. package/src/timer.ts +1 -1
  194. package/test/globalSetup.ts +45 -45
  195. package/test/helpers.ts +189 -189
  196. package/test/migrations/1749664623372_user.ts +13 -13
  197. package/tsconfig.json +9 -0
  198. package/vitest.config.ts +4 -4
  199. package/dist/PostgresKyselyMigrator-CfytARcA.cjs.map +0 -1
  200. package/dist/PostgresKyselyMigrator-upT-hmrz.mjs.map +0 -1
  201. package/dist/PostgresMigrator-DFcNdCvD.cjs.map +0 -1
  202. package/dist/PostgresMigrator-DbuJGAVy.mjs.map +0 -1
  203. package/dist/PostgresObjectionMigrator-BG6ymgnt.cjs.map +0 -1
  204. package/dist/PostgresObjectionMigrator-DPj2pOpX.mjs.map +0 -1
  205. package/dist/VitestKyselyTransactionIsolator-CNURW8y6.mjs.map +0 -1
  206. package/dist/VitestKyselyTransactionIsolator-EvDLk5zg.cjs.map +0 -1
  207. package/dist/VitestObjectionTransactionIsolator-CM5KTAFA.cjs.map +0 -1
  208. package/dist/VitestObjectionTransactionIsolator-jQFaCz0u.mjs.map +0 -1
  209. package/dist/directory-BPf1LgNX.d.mts +0 -12
@@ -1,366 +1,366 @@
1
1
  import { Client } from 'pg';
2
2
  import {
3
- afterAll,
4
- afterEach,
5
- beforeAll,
6
- beforeEach,
7
- describe,
8
- expect,
9
- it,
10
- vi,
3
+ afterAll,
4
+ afterEach,
5
+ beforeAll,
6
+ beforeEach,
7
+ describe,
8
+ expect,
9
+ it,
10
+ vi,
11
11
  } from 'vitest';
12
12
  import { createTestDatabase } from '../../test/helpers';
13
13
  import { PostgresMigrator } from '../PostgresMigrator';
14
14
 
15
15
  // Create a concrete implementation for testing
16
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
- }
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
36
  }
37
37
 
38
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('abstract method', () => {
289
- it('should require concrete implementation of migrate method', () => {
290
- // TypeScript ensures abstract methods are implemented
291
- // This test verifies the TestPostgresMigrator implements migrate
292
- const migrator = new TestPostgresMigrator(
293
- `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`,
294
- );
295
- expect(typeof migrator.migrate).toBe('function');
296
- });
297
- });
298
-
299
- describe('integration scenarios', () => {
300
- it('should handle complete workflow', async () => {
301
- const integrationDbName = `test_integration_${Date.now()}`;
302
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${integrationDbName}`;
303
- const migrator = new TestPostgresMigrator(uri);
304
-
305
- // Add a migration that creates a table
306
- migrator.addMigration(async () => {
307
- const client = new Client({
308
- host: 'localhost',
309
- port: 5432,
310
- user: 'geekmidas',
311
- password: 'geekmidas',
312
- database: integrationDbName,
313
- });
314
- await client.connect();
315
- await client.query(`
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('abstract method', () => {
289
+ it('should require concrete implementation of migrate method', () => {
290
+ // TypeScript ensures abstract methods are implemented
291
+ // This test verifies the TestPostgresMigrator implements migrate
292
+ const migrator = new TestPostgresMigrator(
293
+ `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`,
294
+ );
295
+ expect(typeof migrator.migrate).toBe('function');
296
+ });
297
+ });
298
+
299
+ describe('integration scenarios', () => {
300
+ it('should handle complete workflow', async () => {
301
+ const integrationDbName = `test_integration_${Date.now()}`;
302
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${integrationDbName}`;
303
+ const migrator = new TestPostgresMigrator(uri);
304
+
305
+ // Add a migration that creates a table
306
+ migrator.addMigration(async () => {
307
+ const client = new Client({
308
+ host: 'localhost',
309
+ port: 5432,
310
+ user: 'geekmidas',
311
+ password: 'geekmidas',
312
+ database: integrationDbName,
313
+ });
314
+ await client.connect();
315
+ await client.query(`
316
316
  CREATE TABLE IF NOT EXISTS test_table (
317
317
  id SERIAL PRIMARY KEY,
318
318
  name VARCHAR(255) NOT NULL
319
319
  )
320
320
  `);
321
- await client.end();
322
- });
323
-
324
- // Start migration
325
- const cleanup = await migrator.start();
326
-
327
- expect(migrator.migrateCalled).toBe(true);
328
- expect(consoleSpy).toHaveBeenCalledWith(
329
- `Migrating database: ${integrationDbName}`,
330
- );
331
-
332
- // Verify the table was created
333
- const verifyClient = new Client({
334
- host: 'localhost',
335
- port: 5432,
336
- user: 'geekmidas',
337
- password: 'geekmidas',
338
- database: integrationDbName,
339
- });
340
- await verifyClient.connect();
341
- const tableResult = await verifyClient.query(`
321
+ await client.end();
322
+ });
323
+
324
+ // Start migration
325
+ const cleanup = await migrator.start();
326
+
327
+ expect(migrator.migrateCalled).toBe(true);
328
+ expect(consoleSpy).toHaveBeenCalledWith(
329
+ `Migrating database: ${integrationDbName}`,
330
+ );
331
+
332
+ // Verify the table was created
333
+ const verifyClient = new Client({
334
+ host: 'localhost',
335
+ port: 5432,
336
+ user: 'geekmidas',
337
+ password: 'geekmidas',
338
+ database: integrationDbName,
339
+ });
340
+ await verifyClient.connect();
341
+ const tableResult = await verifyClient.query(`
342
342
  SELECT table_name FROM information_schema.tables
343
343
  WHERE table_schema = 'public' AND table_name = 'test_table'
344
344
  `);
345
- expect(tableResult.rowCount).toBe(1);
346
- await verifyClient.end();
345
+ expect(tableResult.rowCount).toBe(1);
346
+ await verifyClient.end();
347
347
 
348
- // Cleanup
349
- await cleanup();
350
- });
348
+ // Cleanup
349
+ await cleanup();
350
+ });
351
351
 
352
- it('should handle database that already exists and cleanup', async () => {
353
- // Use the existing test database
354
- const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`;
355
- const migrator = new TestPostgresMigrator(uri);
352
+ it('should handle database that already exists and cleanup', async () => {
353
+ // Use the existing test database
354
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`;
355
+ const migrator = new TestPostgresMigrator(uri);
356
356
 
357
- // Start migration (database already exists)
358
- const cleanup = await migrator.start();
357
+ // Start migration (database already exists)
358
+ const cleanup = await migrator.start();
359
359
 
360
- expect(migrator.migrateCalled).toBe(true);
360
+ expect(migrator.migrateCalled).toBe(true);
361
361
 
362
- // Don't call cleanup as we need the database for other tests
363
- expect(typeof cleanup).toBe('function');
364
- });
365
- });
362
+ // Don't call cleanup as we need the database for other tests
363
+ expect(typeof cleanup).toBe('function');
364
+ });
365
+ });
366
366
  });