@geekmidas/testkit 9.0.2 → 10.0.0-alpha.1

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 (129) hide show
  1. package/dist/{Factory-CUM2767q.d.cts → Factory-Cwzho3c8.d.cts} +2 -2
  2. package/dist/{Factory-ClyZLRsO.d.mts.map → Factory-Cwzho3c8.d.cts.map} +1 -1
  3. package/dist/{Factory-ClyZLRsO.d.mts → Factory-SFupxRC2.d.mts} +2 -2
  4. package/dist/{Factory-CUM2767q.d.cts.map → Factory-SFupxRC2.d.mts.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-hMcVtvJq.d.cts} +3 -3
  8. package/dist/{KyselyFactory-CTZmiGI9.d.mts.map → KyselyFactory-hMcVtvJq.d.cts.map} +1 -1
  9. package/dist/{KyselyFactory-CTZmiGI9.d.mts → KyselyFactory-vAxYodck.d.mts} +3 -3
  10. package/dist/{KyselyFactory-OMwcuAX_.d.cts.map → KyselyFactory-vAxYodck.d.mts.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-BWjB49-i.d.mts} +3 -3
  14. package/dist/{ObjectionFactory-DDrKwAoO.d.mts.map → ObjectionFactory-BWjB49-i.d.mts.map} +1 -1
  15. package/dist/{ObjectionFactory-DvmZVHhe.d.cts → ObjectionFactory-DW-qwnqO.d.cts} +3 -3
  16. package/dist/{ObjectionFactory-DvmZVHhe.d.cts.map → ObjectionFactory-DW-qwnqO.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/{directory-DGOcVlKD.d.cts → directory-Bpz5c5pj.d.cts} +3 -3
  58. package/dist/{directory-DGOcVlKD.d.cts.map → directory-Bpz5c5pj.d.cts.map} +1 -1
  59. package/dist/{faker-BcjUfHxx.d.mts → faker-DHh7xs4u.d.mts} +3 -3
  60. package/dist/{faker-BcjUfHxx.d.mts.map → faker-DHh7xs4u.d.mts.map} +1 -1
  61. package/dist/{faker-zVCm31nU.d.cts → faker-Dg3trU4a.d.cts} +3 -3
  62. package/dist/{faker-zVCm31nU.d.cts.map → faker-Dg3trU4a.d.cts.map} +1 -1
  63. package/dist/faker.d.cts +1 -1
  64. package/dist/faker.d.mts +1 -1
  65. package/dist/kysely.cjs +2 -2
  66. package/dist/kysely.d.cts +5 -5
  67. package/dist/kysely.d.mts +5 -5
  68. package/dist/kysely.mjs +2 -2
  69. package/dist/objection.cjs +2 -2
  70. package/dist/objection.d.cts +5 -5
  71. package/dist/objection.d.mts +5 -5
  72. package/dist/objection.mjs +2 -2
  73. package/dist/os/directory.d.cts +1 -1
  74. package/dist/os/index.d.cts +1 -1
  75. package/dist/requestContext.cjs +1 -1
  76. package/dist/requestContext.d.cts +1 -1
  77. package/dist/requestContext.d.mts +1 -1
  78. package/dist/requestContext.mjs +1 -1
  79. package/package.json +10 -7
  80. package/CHANGELOG.md +0 -151
  81. package/dist/VitestKyselyTransactionIsolator-0CqOsuc9.cjs.map +0 -1
  82. package/dist/VitestKyselyTransactionIsolator-CduJlHoT.d.cts.map +0 -1
  83. package/dist/VitestKyselyTransactionIsolator-CfSMOFlN.mjs.map +0 -1
  84. package/dist/VitestKyselyTransactionIsolator-Cswnnj0k.d.mts.map +0 -1
  85. package/dist/VitestTransactionIsolator-BNWJqh9f.d.mts.map +0 -1
  86. package/dist/VitestTransactionIsolator-CSroc7Df.d.cts.map +0 -1
  87. package/dist/VitestTransactionIsolator-CtZaOUjH.cjs.map +0 -1
  88. package/dist/VitestTransactionIsolator-D6ZxLWys.mjs.map +0 -1
  89. package/src/Factory.ts +0 -174
  90. package/src/KyselyFactory.ts +0 -391
  91. package/src/ObjectionFactory.ts +0 -412
  92. package/src/PostgresKyselyMigrator.ts +0 -104
  93. package/src/PostgresMigrator.ts +0 -193
  94. package/src/PostgresObjectionMigrator.ts +0 -138
  95. package/src/VitestKyselyTransactionIsolator.ts +0 -72
  96. package/src/VitestObjectionTransactionIsolator.ts +0 -75
  97. package/src/VitestTransactionIsolator.ts +0 -348
  98. package/src/__tests__/Factory.spec.ts +0 -178
  99. package/src/__tests__/KyselyFactory.spec.ts +0 -458
  100. package/src/__tests__/ObjectionFactory.spec.ts +0 -586
  101. package/src/__tests__/PostgresKyselyMigrator.spec.ts +0 -794
  102. package/src/__tests__/PostgresMigrator.spec.ts +0 -471
  103. package/src/__tests__/PostgresObjectionMigrator.spec.ts +0 -634
  104. package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +0 -138
  105. package/src/__tests__/benchmark.spec.ts +0 -140
  106. package/src/__tests__/better-auth.spec.ts +0 -21
  107. package/src/__tests__/faker.spec.ts +0 -231
  108. package/src/__tests__/initScript.spec.ts +0 -332
  109. package/src/__tests__/integration.spec.ts +0 -610
  110. package/src/__tests__/requestContext.spec.ts +0 -113
  111. package/src/__tests__/utilities.spec.ts +0 -211
  112. package/src/aws.ts +0 -131
  113. package/src/benchmark.ts +0 -48
  114. package/src/better-auth.ts +0 -310
  115. package/src/faker.ts +0 -349
  116. package/src/helpers.ts +0 -45
  117. package/src/initScript.ts +0 -122
  118. package/src/kysely.ts +0 -166
  119. package/src/logger.ts +0 -18
  120. package/src/objection.ts +0 -157
  121. package/src/os/directory.ts +0 -26
  122. package/src/os/index.ts +0 -1
  123. package/src/requestContext.ts +0 -119
  124. package/src/timer.ts +0 -3
  125. package/test/globalSetup.ts +0 -54
  126. package/test/helpers.ts +0 -268
  127. package/test/migrations/1749664623372_user.ts +0 -22
  128. package/tsconfig.json +0 -9
  129. package/vitest.config.ts +0 -8
@@ -1,178 +0,0 @@
1
- import { beforeEach, describe, expect, it } from 'vitest';
2
- import {
3
- Factory,
4
- type FactorySeed,
5
- type MixedFactoryBuilder,
6
- } from '../Factory';
7
-
8
- // Create a concrete implementation for testing
9
- class TestFactory extends Factory<
10
- { testBuilder: (attrs: any) => any },
11
- { testSeed: (attrs: any, factory: any, db: any) => any }
12
- > {
13
- async insert(_builderName: any, attrs?: any) {
14
- return Promise.resolve({ id: 1, ...attrs });
15
- }
16
-
17
- async insertMany(count: number, builderName: any, attrs?: any) {
18
- const results: any[] = [];
19
- for (let i = 0; i < count; i++) {
20
- const newAttrs = typeof attrs === 'function' ? attrs(i) : attrs;
21
- results.push(await this.insert(builderName, newAttrs));
22
- }
23
- return results;
24
- }
25
-
26
- seed(_seedName: any, attrs?: any): any {
27
- return Promise.resolve({ seedResult: true, ...attrs });
28
- }
29
- }
30
-
31
- describe('Factory', () => {
32
- describe('abstract class functionality', () => {
33
- it('should be instantiable through concrete implementation', () => {
34
- const factory = new TestFactory();
35
- expect(factory).toBeInstanceOf(Factory);
36
- expect(factory).toBeInstanceOf(TestFactory);
37
- });
38
-
39
- it('should have abstract methods defined', () => {
40
- const factory = new TestFactory();
41
- expect(typeof factory.insert).toBe('function');
42
- expect(typeof factory.insertMany).toBe('function');
43
- expect(typeof factory.seed).toBe('function');
44
- });
45
- });
46
-
47
- describe('createSeed static method', () => {
48
- it('should return the seed function unchanged', () => {
49
- const seedFn = async ({
50
- attrs,
51
- factory,
52
- db,
53
- }: {
54
- attrs: any;
55
- factory: any;
56
- db: any;
57
- }) => {
58
- return { id: 1, name: 'test' };
59
- };
60
-
61
- const result = Factory.createSeed(seedFn);
62
-
63
- expect(result).toBe(seedFn);
64
- expect(typeof result).toBe('function');
65
- });
66
-
67
- it('should work with different seed function signatures', () => {
68
- const simpleSeed = async () => ({ simple: true });
69
- const complexSeed = async ({
70
- attrs,
71
- }: {
72
- attrs: { name: string };
73
- factory: any;
74
- db: any;
75
- }) => {
76
- return { name: attrs.name, created: true };
77
- };
78
-
79
- const result1 = Factory.createSeed(simpleSeed);
80
- const result2 = Factory.createSeed(complexSeed);
81
-
82
- expect(result1).toBe(simpleSeed);
83
- expect(result2).toBe(complexSeed);
84
- });
85
- });
86
-
87
- describe('concrete implementation behavior', () => {
88
- let factory: TestFactory;
89
-
90
- beforeEach(() => {
91
- factory = new TestFactory();
92
- });
93
-
94
- it('should implement insert method', async () => {
95
- const result = await factory.insert('testBuilder', { name: 'test' });
96
- expect(result).toEqual({ id: 1, name: 'test' });
97
- });
98
-
99
- it('should implement insertMany method', async () => {
100
- const results = await factory.insertMany(3, 'testBuilder', {
101
- name: 'test',
102
- });
103
- expect(results).toHaveLength(3);
104
- expect(results[0]).toEqual({ id: 1, name: 'test' });
105
- });
106
-
107
- it('should implement insertMany with function attributes', async () => {
108
- const results = await factory.insertMany(2, 'testBuilder', (idx) => ({
109
- name: `test${idx}`,
110
- }));
111
- expect(results).toHaveLength(2);
112
- expect(results[0]).toEqual({ id: 1, name: 'test0' });
113
- expect(results[1]).toEqual({ id: 1, name: 'test1' });
114
- });
115
-
116
- it('should implement seed method', async () => {
117
- const result = await factory.seed('testSeed', { custom: 'value' });
118
- expect(result).toEqual({ seedResult: true, custom: 'value' });
119
- });
120
- });
121
-
122
- describe('type definitions', () => {
123
- it('should properly type MixedFactoryBuilder', () => {
124
- // Test that the type allows both sync and async returns
125
- const syncBuilder: MixedFactoryBuilder = (_attrs, _factory, _db) => ({
126
- sync: true,
127
- });
128
- const asyncBuilder: MixedFactoryBuilder = async (
129
- _attrs,
130
- _factory,
131
- _db,
132
- ) => ({
133
- async: true,
134
- });
135
-
136
- expect(typeof syncBuilder).toBe('function');
137
- expect(typeof asyncBuilder).toBe('function');
138
- });
139
-
140
- it('should properly type FactorySeed', () => {
141
- // Test that FactorySeed requires async return with object parameter
142
- const seed: FactorySeed = async ({ attrs, factory, db }) => ({
143
- seeded: true,
144
- });
145
-
146
- expect(typeof seed).toBe('function');
147
- });
148
- });
149
- });
150
-
151
- // Test the type exports
152
- describe('Factory types', () => {
153
- it('should export MixedFactoryBuilder type', () => {
154
- const builder: MixedFactoryBuilder<
155
- { name: string },
156
- TestFactory,
157
- { id: number; name: string },
158
- any
159
- > = (attrs, _factory, _db) => {
160
- return { id: 1, name: attrs.name };
161
- };
162
-
163
- expect(typeof builder).toBe('function');
164
- });
165
-
166
- it('should export FactorySeed type', () => {
167
- const seed: FactorySeed<
168
- { count: number },
169
- TestFactory,
170
- { created: number },
171
- any
172
- > = async ({ attrs }) => {
173
- return { created: attrs.count };
174
- };
175
-
176
- expect(typeof seed).toBe('function');
177
- });
178
- });
@@ -1,458 +0,0 @@
1
- import pg from 'pg';
2
- import { describe, expect, it } from 'vitest';
3
- import { TEST_DATABASE_CONFIG } from '../../test/globalSetup';
4
- import { createTestTables, type TestDatabase } from '../../test/helpers';
5
- import { createKyselyDb } from '../helpers';
6
- import { KyselyFactory } from '../KyselyFactory';
7
- import { wrapVitestKyselyTransaction } from '../kysely';
8
-
9
- const db = () => createKyselyDb<TestDatabase>(TEST_DATABASE_CONFIG);
10
- const itWithTransaction = wrapVitestKyselyTransaction<TestDatabase>(it, {
11
- connection: db,
12
- setup: createTestTables,
13
- });
14
-
15
- const int8TypeId = 20;
16
- pg.types.setTypeParser(int8TypeId, (val) => {
17
- return parseInt(val, 10);
18
- });
19
- describe('KyselyFactory', () => {
20
- describe('KyselyFactory.insert', () => {
21
- itWithTransaction(
22
- 'should insert a record with defaults',
23
- async ({ trx }) => {
24
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
25
- 'users',
26
- async ({ attrs }) => ({
27
- name: 'John Doe',
28
- email: `user${Date.now()}@example.com`,
29
- createdAt: new Date(),
30
- }),
31
- );
32
-
33
- const builders = {
34
- user: userBuilder,
35
- };
36
-
37
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
38
- builders,
39
- {},
40
- trx,
41
- );
42
-
43
- const user = await factory.insert('user');
44
-
45
- expect(user).toBeDefined();
46
- expect(user.id).toBeDefined();
47
- expect(user.name).toBe('John Doe');
48
- expect(user.email).toContain('user');
49
- expect(user.email).toContain('@example.com');
50
- expect(user.createdAt).toBeInstanceOf(Date);
51
- },
52
- );
53
-
54
- itWithTransaction(
55
- 'should override defaults with provided attributes',
56
- async ({ trx }) => {
57
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
58
- 'users',
59
- async ({ attrs }) => ({
60
- name: 'John Doe',
61
- email: `user${Date.now()}@example.com`,
62
- createdAt: new Date(),
63
- }),
64
- );
65
-
66
- const builders = {
67
- user: userBuilder,
68
- };
69
-
70
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
71
- builders,
72
- {},
73
- trx,
74
- );
75
-
76
- const customEmail = 'custom@test.com';
77
- const customName = 'Jane Smith';
78
- const user = await factory.insert('user', {
79
- email: customEmail,
80
- name: customName,
81
- });
82
-
83
- expect(user.name).toBe(customName);
84
- expect(user.email).toBe(customEmail);
85
- },
86
- );
87
-
88
- itWithTransaction('should handle relations', async ({ trx }) => {
89
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
90
- 'users',
91
- async (_attrs) => ({
92
- name: 'John Doe',
93
- email: `user${Date.now()}@example.com`,
94
- createdAt: new Date(),
95
- }),
96
- );
97
-
98
- const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
99
- 'posts',
100
- async ({ attrs, factory }) => {
101
- // Create a user if userId not provided
102
- if (!attrs.userId) {
103
- const user = await factory.insert('user');
104
- return {
105
- title: 'Default Post',
106
- content: 'Default content',
107
- userId: user.id,
108
- createdAt: new Date(),
109
- };
110
- }
111
- return {
112
- title: 'Default Post',
113
- content: 'Default content',
114
- createdAt: new Date(),
115
- };
116
- },
117
- );
118
-
119
- const builders = {
120
- user: userBuilder,
121
- post: postBuilder,
122
- };
123
-
124
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
125
- builders,
126
- {},
127
- trx,
128
- );
129
-
130
- const post = await factory.insert('post', {
131
- title: 'Test Post',
132
- });
133
-
134
- expect(post).toBeDefined();
135
- expect(post.title).toBe('Test Post');
136
- expect(post.userId).toBeDefined();
137
- expect(typeof post.userId).toBe('number');
138
- });
139
-
140
- itWithTransaction(
141
- 'should throw error for non-existent builder',
142
- async ({ trx }) => {
143
- const factory = new KyselyFactory<TestDatabase, any, {}>({}, {}, trx);
144
-
145
- await expect(factory.insert('nonExistent' as any)).rejects.toThrow(
146
- 'Factory "nonExistent" does not exist',
147
- );
148
- },
149
- );
150
- });
151
-
152
- describe('KyselyFactory.insertMany', () => {
153
- itWithTransaction(
154
- 'should insert multiple records with same attributes',
155
- async ({ trx }) => {
156
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
157
- 'users',
158
- async () => ({
159
- name: 'John Doe',
160
- email: `user${Date.now()}-${Math.random()}@example.com`,
161
- createdAt: new Date(),
162
- }),
163
- );
164
-
165
- const builders = {
166
- user: userBuilder,
167
- };
168
-
169
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
170
- builders,
171
- {},
172
- trx,
173
- );
174
-
175
- const users = await factory.insertMany(3, 'user');
176
-
177
- expect(users).toHaveLength(3);
178
- users.forEach((user, _index) => {
179
- expect(user.id).toBeDefined();
180
- expect(user.name).toBe('John Doe');
181
- expect(user.email).toContain('@example.com');
182
- });
183
- },
184
- );
185
-
186
- itWithTransaction(
187
- 'should insert multiple records with dynamic attributes',
188
- async ({ trx }) => {
189
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
190
- 'users',
191
- async () => ({
192
- email: `user${Date.now()}-${Math.random()}@example.com`,
193
- createdAt: new Date(),
194
- }),
195
- );
196
-
197
- const builders = {
198
- user: userBuilder,
199
- };
200
-
201
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
202
- builders,
203
- {},
204
- trx,
205
- );
206
-
207
- const users = await factory.insertMany(3, 'user', (idx) => ({
208
- name: `User ${idx}`,
209
- }));
210
-
211
- expect(users).toHaveLength(3);
212
- users.forEach((user, index) => {
213
- expect(user.name).toBe(`User ${index}`);
214
- });
215
- },
216
- );
217
-
218
- itWithTransaction(
219
- 'should throw error for non-existent builder',
220
- async ({ trx }) => {
221
- const factory = new KyselyFactory<TestDatabase, any, {}>({}, {}, trx);
222
-
223
- await expect(
224
- factory.insertMany(2, 'nonExistent' as any),
225
- ).rejects.toThrow('Builder "nonExistent" is not registered');
226
- },
227
- );
228
- });
229
-
230
- describe('KyselyFactory.createBuilder', () => {
231
- itWithTransaction('should work with async defaults', async ({ trx }) => {
232
- let counter = 0;
233
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
234
- 'users',
235
- async () => {
236
- // Simulate async operation
237
- await new Promise((resolve) => setTimeout(resolve, 10));
238
- counter++;
239
- return {
240
- name: `Async User ${counter}`,
241
- email: `user${counter}@example.com`,
242
- createdAt: new Date(),
243
- };
244
- },
245
- );
246
-
247
- const builders = {
248
- user: userBuilder,
249
- };
250
-
251
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
252
- builders,
253
- {},
254
- trx,
255
- );
256
-
257
- const user1 = await factory.insert('user');
258
- const user2 = await factory.insert('user');
259
-
260
- expect(user1.name).toBe('Async User 1');
261
- expect(user2.name).toBe('Async User 2');
262
- });
263
- });
264
-
265
- describe('KyselyFactory.seed', () => {
266
- itWithTransaction('should execute seed functions', async ({ trx }) => {
267
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
268
- 'users',
269
- async (_attrs) => ({
270
- name: 'John Doe',
271
- email: `user${Date.now()}@example.com`,
272
- createdAt: new Date(),
273
- }),
274
- );
275
-
276
- const builders = {
277
- user: userBuilder,
278
- };
279
-
280
- const seeds = {
281
- createAdminUser: KyselyFactory.createSeed(
282
- async ({
283
- attrs,
284
- factory,
285
- }: {
286
- attrs: { name?: string };
287
- factory: any;
288
- db: any;
289
- }) => {
290
- return await factory.insert('user', {
291
- name: attrs.name || 'Admin User',
292
- email: 'admin@example.com',
293
- });
294
- },
295
- ),
296
- };
297
-
298
- const factory = new KyselyFactory<
299
- TestDatabase,
300
- typeof builders,
301
- typeof seeds
302
- >(builders, seeds, trx);
303
-
304
- const adminUser = await factory.seed('createAdminUser');
305
-
306
- expect(adminUser).toBeDefined();
307
- expect(adminUser.name).toBe('Admin User');
308
- expect(adminUser.email).toBe('admin@example.com');
309
- });
310
-
311
- itWithTransaction(
312
- 'should pass attributes to seed functions',
313
- async ({ trx }) => {
314
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
315
- 'users',
316
- async ({ attrs }) => ({
317
- name: 'John Doe',
318
- email: `user${Date.now()}@example.com`,
319
- createdAt: new Date(),
320
- }),
321
- );
322
-
323
- const builders = {
324
- user: userBuilder,
325
- };
326
-
327
- const seeds = {
328
- createCustomUser: KyselyFactory.createSeed(
329
- async ({
330
- attrs,
331
- factory,
332
- }: {
333
- attrs: { name: string; email: string };
334
- factory: any;
335
- db: any;
336
- }) => {
337
- return await factory.insert('user', attrs);
338
- },
339
- ),
340
- };
341
-
342
- const factory = new KyselyFactory<
343
- TestDatabase,
344
- typeof builders,
345
- typeof seeds
346
- >(builders, seeds, trx);
347
-
348
- const customUser = await factory.seed('createCustomUser', {
349
- name: 'Custom User',
350
- email: 'custom@test.com',
351
- });
352
-
353
- expect(customUser.name).toBe('Custom User');
354
- expect(customUser.email).toBe('custom@test.com');
355
- },
356
- );
357
-
358
- itWithTransaction(
359
- 'should throw error for non-existent seed',
360
- async ({ trx }) => {
361
- const factory = new KyselyFactory<TestDatabase, any, any>({}, {}, trx);
362
-
363
- expect(() => factory.seed('nonExistent' as any)).toThrow(
364
- 'Seed "nonExistent" is not registered',
365
- );
366
- },
367
- );
368
- });
369
-
370
- describe('Factory integration', () => {
371
- itWithTransaction(
372
- 'should work with controlled transactions',
373
- async ({ trx }) => {
374
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
375
- 'users',
376
- async ({ attrs }) => ({
377
- name: 'John Doe',
378
- email: `user${Date.now()}@example.com`,
379
- createdAt: new Date(),
380
- }),
381
- );
382
-
383
- const builders = {
384
- user: userBuilder,
385
- };
386
-
387
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
388
- builders,
389
- {},
390
- trx,
391
- );
392
-
393
- const user = await factory.insert('user');
394
-
395
- // Verify the user exists in the transaction
396
- const foundUser = await trx
397
- .selectFrom('users')
398
- .selectAll()
399
- .where('id', '=', user.id)
400
- .executeTakeFirst();
401
-
402
- expect(foundUser).toBeDefined();
403
- expect(foundUser?.id).toBe(user.id);
404
- },
405
- );
406
-
407
- itWithTransaction(
408
- 'should work with factory passed to defaults',
409
- async ({ trx }) => {
410
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
411
- 'users',
412
- async ({ attrs }) => ({
413
- name: 'John Doe',
414
- email: `user${Date.now()}@example.com`,
415
- createdAt: new Date(),
416
- }),
417
- );
418
-
419
- const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
420
- 'posts',
421
- async ({ factory }) => {
422
- const user = await factory.insert('user');
423
- return {
424
- title: 'Default Post',
425
- content: 'Default content',
426
- userId: user.id,
427
- createdAt: new Date(),
428
- };
429
- },
430
- );
431
-
432
- const builders = {
433
- user: userBuilder,
434
- post: postBuilder,
435
- };
436
-
437
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
438
- builders,
439
- {},
440
- trx,
441
- );
442
-
443
- const post = await factory.insert('post');
444
-
445
- expect(post.userId).toBeDefined();
446
-
447
- // Verify the related user exists
448
- const relatedUser = await trx
449
- .selectFrom('users')
450
- .selectAll()
451
- .where('id', '=', post.userId)
452
- .executeTakeFirst();
453
-
454
- expect(relatedUser).toBeDefined();
455
- },
456
- );
457
- });
458
- });