@geekmidas/testkit 0.0.4 → 0.0.6
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/dist/Factory.mjs +1 -1
- package/dist/{KyselyFactory-BGvSMLtd.cjs → KyselyFactory-BX7Kv2uP.cjs} +1 -1
- package/dist/{KyselyFactory-ionH4gvk.mjs → KyselyFactory-pOMOFQWE.mjs} +2 -2
- package/dist/KyselyFactory.cjs +2 -2
- package/dist/KyselyFactory.mjs +3 -3
- package/dist/{ObjectionFactory-CFrtXe7i.mjs → ObjectionFactory-ChuX8sZN.mjs} +1 -1
- package/dist/ObjectionFactory.mjs +2 -2
- package/dist/{PostgresKyselyMigrator-CbtiZgfI.mjs → PostgresKyselyMigrator-D8fm35-s.mjs} +1 -1
- package/dist/{PostgresKyselyMigrator-Cxf2Dp9y.cjs → PostgresKyselyMigrator-JTY2LfwD.cjs} +2 -2
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/{PostgresMigrator-eqyAFSf-.cjs → PostgresMigrator-Bz-tnjB6.cjs} +1 -1
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/{VitestKyselyTransactionIsolator-DXjWQtDN.mjs → VitestKyselyTransactionIsolator-D-qpeVKO.mjs} +1 -1
- package/dist/{VitestKyselyTransactionIsolator-Dh2AgJDd.cjs → VitestKyselyTransactionIsolator-jF6Ohyu_.cjs} +1 -1
- package/dist/VitestKyselyTransactionIsolator.cjs +2 -3
- package/dist/VitestKyselyTransactionIsolator.mjs +2 -3
- package/dist/{VitestTransactionIsolator-zK5NJ7DQ.cjs → VitestTransactionIsolator-BK9UsrKt.cjs} +4 -2
- package/dist/{VitestTransactionIsolator-pLwsDo_A.mjs → VitestTransactionIsolator-e-R3p_X8.mjs} +4 -3
- package/dist/VitestTransactionIsolator.cjs +1 -2
- package/dist/VitestTransactionIsolator.mjs +1 -2
- package/dist/__tests__/Factory.spec.cjs +42 -42
- package/dist/__tests__/Factory.spec.mjs +23 -24
- package/dist/__tests__/KyselyFactory.spec.cjs +80 -80
- package/dist/__tests__/KyselyFactory.spec.mjs +74 -74
- package/dist/__tests__/ObjectionFactory.spec.cjs +80 -81
- package/dist/__tests__/ObjectionFactory.spec.mjs +52 -53
- package/dist/__tests__/PostgresMigrator.spec.cjs +59 -60
- package/dist/__tests__/PostgresMigrator.spec.mjs +31 -32
- package/dist/__tests__/faker.spec.cjs +60 -60
- package/dist/__tests__/faker.spec.mjs +35 -36
- package/dist/__tests__/integration.spec.cjs +44 -41
- package/dist/__tests__/integration.spec.mjs +45 -43
- package/dist/{chunk-DWy1uDak.cjs → chunk-CUT6urMc.cjs} +0 -9
- package/dist/example.cjs +2 -2
- package/dist/example.mjs +3 -3
- package/dist/{faker-h6CkRloU.cjs → faker-caz-8zt8.cjs} +1 -1
- package/dist/faker.cjs +1 -1
- package/dist/faker.mjs +1 -1
- package/dist/{helpers-C2NH7xcz.cjs → helpers-B2CfbaTC.cjs} +1 -1
- package/dist/helpers-DN4sJO4i.mjs +13 -0
- package/dist/helpers-DOtYCEvZ.cjs +19 -0
- package/dist/helpers.cjs +2 -6
- package/dist/helpers.mjs +2 -5
- package/dist/kysely-C1-aHdnU.mjs +11 -0
- package/dist/kysely-DL3C2eM4.cjs +16 -0
- package/dist/kysely.cjs +8 -15
- package/dist/kysely.mjs +8 -15
- package/dist/objection.mjs +2 -2
- package/package.json +2 -1
- package/src/VitestTransactionIsolator.ts +4 -2
- package/src/__tests__/KyselyFactory.spec.ts +4 -6
- package/src/__tests__/integration.spec.ts +8 -3
- package/src/helpers.ts +1 -18
- package/src/kysely.ts +3 -1
- package/dist/dist-BM2KvLG1.mjs +0 -5618
- package/dist/dist-DE3gAxQI.cjs +0 -5736
- package/dist/helpers-BnARb5Ap.mjs +0 -19
- package/dist/helpers-C_RZk04R.cjs +0 -31
- package/dist/magic-string.es-C6yzoryu.mjs +0 -1014
- package/dist/magic-string.es-jdtJrR0A.cjs +0 -1015
- package/dist/vi.bdSIJ99Y-BgRxGeO2.mjs +0 -9382
- package/dist/vi.bdSIJ99Y-CFuzUeY6.cjs +0 -9393
- /package/dist/{Factory-D52Lsc6Z.mjs → Factory-DlzMkMzb.mjs} +0 -0
- /package/dist/{PostgresMigrator-DqeuPy-e.mjs → PostgresMigrator-CEoRKTdq.mjs} +0 -0
- /package/dist/{faker-cGCFcrj2.mjs → faker-BwaXA_RF.mjs} +0 -0
- /package/dist/{helpers-CukcFAU9.mjs → helpers-Rf5F71r9.mjs} +0 -0
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
const require_chunk = require('../chunk-
|
|
1
|
+
const require_chunk = require('../chunk-CUT6urMc.cjs');
|
|
2
2
|
require('../Factory-DREHoms3.cjs');
|
|
3
|
-
require('../faker-
|
|
4
|
-
const require_KyselyFactory = require('../KyselyFactory-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
require('../VitestTransactionIsolator-
|
|
8
|
-
require('../VitestKyselyTransactionIsolator-
|
|
9
|
-
const require_helpers = require('../helpers-
|
|
10
|
-
const
|
|
3
|
+
require('../faker-caz-8zt8.cjs');
|
|
4
|
+
const require_KyselyFactory = require('../KyselyFactory-BX7Kv2uP.cjs');
|
|
5
|
+
require('../PostgresMigrator-Bz-tnjB6.cjs');
|
|
6
|
+
require('../PostgresKyselyMigrator-JTY2LfwD.cjs');
|
|
7
|
+
require('../VitestTransactionIsolator-BK9UsrKt.cjs');
|
|
8
|
+
require('../VitestKyselyTransactionIsolator-jF6Ohyu_.cjs');
|
|
9
|
+
const require_helpers = require('../helpers-DOtYCEvZ.cjs');
|
|
10
|
+
const require_kysely = require('../kysely-DL3C2eM4.cjs');
|
|
11
|
+
const require_helpers$1 = require('../helpers-B2CfbaTC.cjs');
|
|
11
12
|
const pg = require_chunk.__toESM(require("pg"));
|
|
13
|
+
const vitest = require_chunk.__toESM(require("vitest"));
|
|
12
14
|
|
|
13
15
|
//#region src/__tests__/KyselyFactory.spec.ts
|
|
14
16
|
const db = require_helpers.createKyselyDb(require_helpers$1.TEST_DATABASE_CONFIG);
|
|
15
|
-
const itWithTransaction =
|
|
17
|
+
const itWithTransaction = require_kysely.wrapVitestKyselyTransaction(vitest.it, db, require_helpers$1.createTestTables);
|
|
16
18
|
const int8TypeId = 20;
|
|
17
19
|
pg.default.types.setTypeParser(int8TypeId, (val) => {
|
|
18
20
|
return parseInt(val, 10);
|
|
19
21
|
});
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
require_dist.describe("KyselyFactory.insert", () => {
|
|
24
|
-
itWithTransaction("should insert a record with defaults", async ({ trx: trx$1 }) => {
|
|
22
|
+
(0, vitest.describe)("KyselyFactory", () => {
|
|
23
|
+
(0, vitest.describe)("KyselyFactory.insert", () => {
|
|
24
|
+
itWithTransaction("should insert a record with defaults", async ({ trx }) => {
|
|
25
25
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
26
26
|
name: "John Doe",
|
|
27
27
|
email: `user${Date.now()}@example.com`,
|
|
28
28
|
createdAt: /* @__PURE__ */ new Date()
|
|
29
29
|
}));
|
|
30
30
|
const builders = { user: userBuilder };
|
|
31
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
31
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
32
32
|
const user = await factory.insert("user");
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
(0, vitest.expect)(user).toBeDefined();
|
|
34
|
+
(0, vitest.expect)(user.id).toBeDefined();
|
|
35
|
+
(0, vitest.expect)(user.name).toBe("John Doe");
|
|
36
|
+
(0, vitest.expect)(user.email).toContain("user");
|
|
37
|
+
(0, vitest.expect)(user.email).toContain("@example.com");
|
|
38
|
+
(0, vitest.expect)(user.createdAt).toBeInstanceOf(Date);
|
|
39
39
|
});
|
|
40
|
-
itWithTransaction("should override defaults with provided attributes", async ({ trx
|
|
40
|
+
itWithTransaction("should override defaults with provided attributes", async ({ trx }) => {
|
|
41
41
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
42
42
|
name: "John Doe",
|
|
43
43
|
email: `user${Date.now()}@example.com`,
|
|
44
44
|
createdAt: /* @__PURE__ */ new Date()
|
|
45
45
|
}));
|
|
46
46
|
const builders = { user: userBuilder };
|
|
47
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
47
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
48
48
|
const customEmail = "custom@test.com";
|
|
49
49
|
const customName = "Jane Smith";
|
|
50
50
|
const user = await factory.insert("user", {
|
|
51
51
|
email: customEmail,
|
|
52
52
|
name: customName
|
|
53
53
|
});
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
(0, vitest.expect)(user.name).toBe(customName);
|
|
55
|
+
(0, vitest.expect)(user.email).toBe(customEmail);
|
|
56
56
|
});
|
|
57
|
-
itWithTransaction("should handle relations", async ({ trx
|
|
57
|
+
itWithTransaction("should handle relations", async ({ trx }) => {
|
|
58
58
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
59
59
|
name: "John Doe",
|
|
60
60
|
email: `user${Date.now()}@example.com`,
|
|
@@ -80,55 +80,55 @@ require_dist.describe("KyselyFactory", () => {
|
|
|
80
80
|
user: userBuilder,
|
|
81
81
|
post: postBuilder
|
|
82
82
|
};
|
|
83
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
83
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
84
84
|
const post = await factory.insert("post", { title: "Test Post" });
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
85
|
+
(0, vitest.expect)(post).toBeDefined();
|
|
86
|
+
(0, vitest.expect)(post.title).toBe("Test Post");
|
|
87
|
+
(0, vitest.expect)(post.userId).toBeDefined();
|
|
88
|
+
(0, vitest.expect)(typeof post.userId).toBe("number");
|
|
89
89
|
});
|
|
90
|
-
itWithTransaction("should throw error for non-existent builder", async ({ trx
|
|
91
|
-
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx
|
|
92
|
-
await
|
|
90
|
+
itWithTransaction("should throw error for non-existent builder", async ({ trx }) => {
|
|
91
|
+
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx);
|
|
92
|
+
await (0, vitest.expect)(factory.insert("nonExistent")).rejects.toThrow("Factory \"nonExistent\" does not exist");
|
|
93
93
|
});
|
|
94
94
|
});
|
|
95
|
-
|
|
96
|
-
itWithTransaction("should insert multiple records with same attributes", async ({ trx
|
|
95
|
+
(0, vitest.describe)("KyselyFactory.insertMany", () => {
|
|
96
|
+
itWithTransaction("should insert multiple records with same attributes", async ({ trx }) => {
|
|
97
97
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
98
98
|
name: "John Doe",
|
|
99
99
|
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
100
100
|
createdAt: /* @__PURE__ */ new Date()
|
|
101
101
|
}));
|
|
102
102
|
const builders = { user: userBuilder };
|
|
103
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
103
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
104
104
|
const users = await factory.insertMany(3, "user");
|
|
105
|
-
|
|
105
|
+
(0, vitest.expect)(users).toHaveLength(3);
|
|
106
106
|
users.forEach((user, index) => {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
107
|
+
(0, vitest.expect)(user.id).toBeDefined();
|
|
108
|
+
(0, vitest.expect)(user.name).toBe("John Doe");
|
|
109
|
+
(0, vitest.expect)(user.email).toContain("@example.com");
|
|
110
110
|
});
|
|
111
111
|
});
|
|
112
|
-
itWithTransaction("should insert multiple records with dynamic attributes", async ({ trx
|
|
112
|
+
itWithTransaction("should insert multiple records with dynamic attributes", async ({ trx }) => {
|
|
113
113
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
114
114
|
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
115
115
|
createdAt: /* @__PURE__ */ new Date()
|
|
116
116
|
}));
|
|
117
117
|
const builders = { user: userBuilder };
|
|
118
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
118
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
119
119
|
const users = await factory.insertMany(3, "user", (idx) => ({ name: `User ${idx}` }));
|
|
120
|
-
|
|
120
|
+
(0, vitest.expect)(users).toHaveLength(3);
|
|
121
121
|
users.forEach((user, index) => {
|
|
122
|
-
|
|
122
|
+
(0, vitest.expect)(user.name).toBe(`User ${index}`);
|
|
123
123
|
});
|
|
124
124
|
});
|
|
125
|
-
itWithTransaction("should throw error for non-existent builder", async ({ trx
|
|
126
|
-
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx
|
|
127
|
-
await
|
|
125
|
+
itWithTransaction("should throw error for non-existent builder", async ({ trx }) => {
|
|
126
|
+
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx);
|
|
127
|
+
await (0, vitest.expect)(factory.insertMany(2, "nonExistent")).rejects.toThrow("Builder \"nonExistent\" is not registered");
|
|
128
128
|
});
|
|
129
129
|
});
|
|
130
|
-
|
|
131
|
-
itWithTransaction("should work with async defaults", async ({ trx
|
|
130
|
+
(0, vitest.describe)("KyselyFactory.createBuilder", () => {
|
|
131
|
+
itWithTransaction("should work with async defaults", async ({ trx }) => {
|
|
132
132
|
let counter = 0;
|
|
133
133
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => {
|
|
134
134
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
@@ -140,71 +140,71 @@ require_dist.describe("KyselyFactory", () => {
|
|
|
140
140
|
};
|
|
141
141
|
});
|
|
142
142
|
const builders = { user: userBuilder };
|
|
143
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
143
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
144
144
|
const user1 = await factory.insert("user");
|
|
145
145
|
const user2 = await factory.insert("user");
|
|
146
|
-
|
|
147
|
-
|
|
146
|
+
(0, vitest.expect)(user1.name).toBe("Async User 1");
|
|
147
|
+
(0, vitest.expect)(user2.name).toBe("Async User 2");
|
|
148
148
|
});
|
|
149
149
|
});
|
|
150
|
-
|
|
151
|
-
itWithTransaction("should execute seed functions", async ({ trx
|
|
150
|
+
(0, vitest.describe)("KyselyFactory.seed", () => {
|
|
151
|
+
itWithTransaction("should execute seed functions", async ({ trx }) => {
|
|
152
152
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
153
153
|
name: "John Doe",
|
|
154
154
|
email: `user${Date.now()}@example.com`,
|
|
155
155
|
createdAt: /* @__PURE__ */ new Date()
|
|
156
156
|
}));
|
|
157
157
|
const builders = { user: userBuilder };
|
|
158
|
-
const seeds = { createAdminUser: require_KyselyFactory.KyselyFactory.createSeed(async (attrs, factory$1, db$
|
|
158
|
+
const seeds = { createAdminUser: require_KyselyFactory.KyselyFactory.createSeed(async (attrs, factory$1, db$1) => {
|
|
159
159
|
return await factory$1.insert("user", {
|
|
160
160
|
name: attrs.name || "Admin User",
|
|
161
161
|
email: "admin@example.com"
|
|
162
162
|
});
|
|
163
163
|
}) };
|
|
164
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, seeds, trx
|
|
164
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, seeds, trx);
|
|
165
165
|
const adminUser = await factory.seed("createAdminUser");
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
166
|
+
(0, vitest.expect)(adminUser).toBeDefined();
|
|
167
|
+
(0, vitest.expect)(adminUser.name).toBe("Admin User");
|
|
168
|
+
(0, vitest.expect)(adminUser.email).toBe("admin@example.com");
|
|
169
169
|
});
|
|
170
|
-
itWithTransaction("should pass attributes to seed functions", async ({ trx
|
|
170
|
+
itWithTransaction("should pass attributes to seed functions", async ({ trx }) => {
|
|
171
171
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
172
172
|
name: "John Doe",
|
|
173
173
|
email: `user${Date.now()}@example.com`,
|
|
174
174
|
createdAt: /* @__PURE__ */ new Date()
|
|
175
175
|
}));
|
|
176
176
|
const builders = { user: userBuilder };
|
|
177
|
-
const seeds = { createCustomUser: require_KyselyFactory.KyselyFactory.createSeed(async (attrs, factory$1, db$
|
|
177
|
+
const seeds = { createCustomUser: require_KyselyFactory.KyselyFactory.createSeed(async (attrs, factory$1, db$1) => {
|
|
178
178
|
return await factory$1.insert("user", attrs);
|
|
179
179
|
}) };
|
|
180
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, seeds, trx
|
|
180
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, seeds, trx);
|
|
181
181
|
const customUser = await factory.seed("createCustomUser", {
|
|
182
182
|
name: "Custom User",
|
|
183
183
|
email: "custom@test.com"
|
|
184
184
|
});
|
|
185
|
-
|
|
186
|
-
|
|
185
|
+
(0, vitest.expect)(customUser.name).toBe("Custom User");
|
|
186
|
+
(0, vitest.expect)(customUser.email).toBe("custom@test.com");
|
|
187
187
|
});
|
|
188
|
-
itWithTransaction("should throw error for non-existent seed", async ({ trx
|
|
189
|
-
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx
|
|
190
|
-
|
|
188
|
+
itWithTransaction("should throw error for non-existent seed", async ({ trx }) => {
|
|
189
|
+
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx);
|
|
190
|
+
(0, vitest.expect)(() => factory.seed("nonExistent")).toThrow("Seed \"nonExistent\" is not registered");
|
|
191
191
|
});
|
|
192
192
|
});
|
|
193
|
-
|
|
194
|
-
itWithTransaction("should work with controlled transactions", async ({ trx
|
|
193
|
+
(0, vitest.describe)("Factory integration", () => {
|
|
194
|
+
itWithTransaction("should work with controlled transactions", async ({ trx }) => {
|
|
195
195
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
196
196
|
name: "John Doe",
|
|
197
197
|
email: `user${Date.now()}@example.com`,
|
|
198
198
|
createdAt: /* @__PURE__ */ new Date()
|
|
199
199
|
}));
|
|
200
200
|
const builders = { user: userBuilder };
|
|
201
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
201
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
202
202
|
const user = await factory.insert("user");
|
|
203
|
-
const foundUser = await trx
|
|
204
|
-
|
|
205
|
-
|
|
203
|
+
const foundUser = await trx.selectFrom("users").selectAll().where("id", "=", user.id).executeTakeFirst();
|
|
204
|
+
(0, vitest.expect)(foundUser).toBeDefined();
|
|
205
|
+
(0, vitest.expect)(foundUser?.id).toBe(user.id);
|
|
206
206
|
});
|
|
207
|
-
itWithTransaction("should work with factory passed to defaults", async ({ trx
|
|
207
|
+
itWithTransaction("should work with factory passed to defaults", async ({ trx }) => {
|
|
208
208
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
209
209
|
name: "John Doe",
|
|
210
210
|
email: `user${Date.now()}@example.com`,
|
|
@@ -223,11 +223,11 @@ require_dist.describe("KyselyFactory", () => {
|
|
|
223
223
|
user: userBuilder,
|
|
224
224
|
post: postBuilder
|
|
225
225
|
};
|
|
226
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
226
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
227
227
|
const post = await factory.insert("post");
|
|
228
|
-
|
|
229
|
-
const relatedUser = await trx
|
|
230
|
-
|
|
228
|
+
(0, vitest.expect)(post.userId).toBeDefined();
|
|
229
|
+
const relatedUser = await trx.selectFrom("users").selectAll().where("id", "=", post.userId).executeTakeFirst();
|
|
230
|
+
(0, vitest.expect)(relatedUser).toBeDefined();
|
|
231
231
|
});
|
|
232
232
|
});
|
|
233
233
|
});
|
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import
|
|
2
|
-
import "../
|
|
3
|
-
import "../
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import "../VitestTransactionIsolator-
|
|
7
|
-
import "../VitestKyselyTransactionIsolator-
|
|
8
|
-
import { createKyselyDb
|
|
9
|
-
import {
|
|
1
|
+
import "../Factory-DlzMkMzb.mjs";
|
|
2
|
+
import "../faker-BwaXA_RF.mjs";
|
|
3
|
+
import { KyselyFactory } from "../KyselyFactory-pOMOFQWE.mjs";
|
|
4
|
+
import "../PostgresMigrator-CEoRKTdq.mjs";
|
|
5
|
+
import "../PostgresKyselyMigrator-D8fm35-s.mjs";
|
|
6
|
+
import "../VitestTransactionIsolator-e-R3p_X8.mjs";
|
|
7
|
+
import "../VitestKyselyTransactionIsolator-D-qpeVKO.mjs";
|
|
8
|
+
import { createKyselyDb } from "../helpers-DN4sJO4i.mjs";
|
|
9
|
+
import { wrapVitestKyselyTransaction } from "../kysely-C1-aHdnU.mjs";
|
|
10
|
+
import { TEST_DATABASE_CONFIG, createTestTables } from "../helpers-Rf5F71r9.mjs";
|
|
10
11
|
import pg from "pg";
|
|
12
|
+
import { describe, expect, it } from "vitest";
|
|
11
13
|
|
|
12
14
|
//#region src/__tests__/KyselyFactory.spec.ts
|
|
13
15
|
const db = createKyselyDb(TEST_DATABASE_CONFIG);
|
|
14
|
-
const itWithTransaction = wrapVitestKyselyTransaction(db, createTestTables);
|
|
16
|
+
const itWithTransaction = wrapVitestKyselyTransaction(it, db, createTestTables);
|
|
15
17
|
const int8TypeId = 20;
|
|
16
18
|
pg.types.setTypeParser(int8TypeId, (val) => {
|
|
17
19
|
return parseInt(val, 10);
|
|
18
20
|
});
|
|
19
21
|
describe("KyselyFactory", () => {
|
|
20
|
-
let db$1;
|
|
21
|
-
let trx;
|
|
22
22
|
describe("KyselyFactory.insert", () => {
|
|
23
|
-
itWithTransaction("should insert a record with defaults", async ({ trx
|
|
23
|
+
itWithTransaction("should insert a record with defaults", async ({ trx }) => {
|
|
24
24
|
const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
25
25
|
name: "John Doe",
|
|
26
26
|
email: `user${Date.now()}@example.com`,
|
|
27
27
|
createdAt: /* @__PURE__ */ new Date()
|
|
28
28
|
}));
|
|
29
29
|
const builders = { user: userBuilder };
|
|
30
|
-
const factory = new KyselyFactory(builders, {}, trx
|
|
30
|
+
const factory = new KyselyFactory(builders, {}, trx);
|
|
31
31
|
const user = await factory.insert("user");
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
expect(user).toBeDefined();
|
|
33
|
+
expect(user.id).toBeDefined();
|
|
34
|
+
expect(user.name).toBe("John Doe");
|
|
35
|
+
expect(user.email).toContain("user");
|
|
36
|
+
expect(user.email).toContain("@example.com");
|
|
37
|
+
expect(user.createdAt).toBeInstanceOf(Date);
|
|
38
38
|
});
|
|
39
|
-
itWithTransaction("should override defaults with provided attributes", async ({ trx
|
|
39
|
+
itWithTransaction("should override defaults with provided attributes", async ({ trx }) => {
|
|
40
40
|
const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
41
41
|
name: "John Doe",
|
|
42
42
|
email: `user${Date.now()}@example.com`,
|
|
43
43
|
createdAt: /* @__PURE__ */ new Date()
|
|
44
44
|
}));
|
|
45
45
|
const builders = { user: userBuilder };
|
|
46
|
-
const factory = new KyselyFactory(builders, {}, trx
|
|
46
|
+
const factory = new KyselyFactory(builders, {}, trx);
|
|
47
47
|
const customEmail = "custom@test.com";
|
|
48
48
|
const customName = "Jane Smith";
|
|
49
49
|
const user = await factory.insert("user", {
|
|
50
50
|
email: customEmail,
|
|
51
51
|
name: customName
|
|
52
52
|
});
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
expect(user.name).toBe(customName);
|
|
54
|
+
expect(user.email).toBe(customEmail);
|
|
55
55
|
});
|
|
56
|
-
itWithTransaction("should handle relations", async ({ trx
|
|
56
|
+
itWithTransaction("should handle relations", async ({ trx }) => {
|
|
57
57
|
const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
58
58
|
name: "John Doe",
|
|
59
59
|
email: `user${Date.now()}@example.com`,
|
|
@@ -79,55 +79,55 @@ describe("KyselyFactory", () => {
|
|
|
79
79
|
user: userBuilder,
|
|
80
80
|
post: postBuilder
|
|
81
81
|
};
|
|
82
|
-
const factory = new KyselyFactory(builders, {}, trx
|
|
82
|
+
const factory = new KyselyFactory(builders, {}, trx);
|
|
83
83
|
const post = await factory.insert("post", { title: "Test Post" });
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
expect(post).toBeDefined();
|
|
85
|
+
expect(post.title).toBe("Test Post");
|
|
86
|
+
expect(post.userId).toBeDefined();
|
|
87
|
+
expect(typeof post.userId).toBe("number");
|
|
88
88
|
});
|
|
89
|
-
itWithTransaction("should throw error for non-existent builder", async ({ trx
|
|
90
|
-
const factory = new KyselyFactory({}, {}, trx
|
|
91
|
-
await
|
|
89
|
+
itWithTransaction("should throw error for non-existent builder", async ({ trx }) => {
|
|
90
|
+
const factory = new KyselyFactory({}, {}, trx);
|
|
91
|
+
await expect(factory.insert("nonExistent")).rejects.toThrow("Factory \"nonExistent\" does not exist");
|
|
92
92
|
});
|
|
93
93
|
});
|
|
94
94
|
describe("KyselyFactory.insertMany", () => {
|
|
95
|
-
itWithTransaction("should insert multiple records with same attributes", async ({ trx
|
|
95
|
+
itWithTransaction("should insert multiple records with same attributes", async ({ trx }) => {
|
|
96
96
|
const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
97
97
|
name: "John Doe",
|
|
98
98
|
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
99
99
|
createdAt: /* @__PURE__ */ new Date()
|
|
100
100
|
}));
|
|
101
101
|
const builders = { user: userBuilder };
|
|
102
|
-
const factory = new KyselyFactory(builders, {}, trx
|
|
102
|
+
const factory = new KyselyFactory(builders, {}, trx);
|
|
103
103
|
const users = await factory.insertMany(3, "user");
|
|
104
|
-
|
|
104
|
+
expect(users).toHaveLength(3);
|
|
105
105
|
users.forEach((user, index) => {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
expect(user.id).toBeDefined();
|
|
107
|
+
expect(user.name).toBe("John Doe");
|
|
108
|
+
expect(user.email).toContain("@example.com");
|
|
109
109
|
});
|
|
110
110
|
});
|
|
111
|
-
itWithTransaction("should insert multiple records with dynamic attributes", async ({ trx
|
|
111
|
+
itWithTransaction("should insert multiple records with dynamic attributes", async ({ trx }) => {
|
|
112
112
|
const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
113
113
|
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
114
114
|
createdAt: /* @__PURE__ */ new Date()
|
|
115
115
|
}));
|
|
116
116
|
const builders = { user: userBuilder };
|
|
117
|
-
const factory = new KyselyFactory(builders, {}, trx
|
|
117
|
+
const factory = new KyselyFactory(builders, {}, trx);
|
|
118
118
|
const users = await factory.insertMany(3, "user", (idx) => ({ name: `User ${idx}` }));
|
|
119
|
-
|
|
119
|
+
expect(users).toHaveLength(3);
|
|
120
120
|
users.forEach((user, index) => {
|
|
121
|
-
|
|
121
|
+
expect(user.name).toBe(`User ${index}`);
|
|
122
122
|
});
|
|
123
123
|
});
|
|
124
|
-
itWithTransaction("should throw error for non-existent builder", async ({ trx
|
|
125
|
-
const factory = new KyselyFactory({}, {}, trx
|
|
126
|
-
await
|
|
124
|
+
itWithTransaction("should throw error for non-existent builder", async ({ trx }) => {
|
|
125
|
+
const factory = new KyselyFactory({}, {}, trx);
|
|
126
|
+
await expect(factory.insertMany(2, "nonExistent")).rejects.toThrow("Builder \"nonExistent\" is not registered");
|
|
127
127
|
});
|
|
128
128
|
});
|
|
129
129
|
describe("KyselyFactory.createBuilder", () => {
|
|
130
|
-
itWithTransaction("should work with async defaults", async ({ trx
|
|
130
|
+
itWithTransaction("should work with async defaults", async ({ trx }) => {
|
|
131
131
|
let counter = 0;
|
|
132
132
|
const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => {
|
|
133
133
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
@@ -139,71 +139,71 @@ describe("KyselyFactory", () => {
|
|
|
139
139
|
};
|
|
140
140
|
});
|
|
141
141
|
const builders = { user: userBuilder };
|
|
142
|
-
const factory = new KyselyFactory(builders, {}, trx
|
|
142
|
+
const factory = new KyselyFactory(builders, {}, trx);
|
|
143
143
|
const user1 = await factory.insert("user");
|
|
144
144
|
const user2 = await factory.insert("user");
|
|
145
|
-
|
|
146
|
-
|
|
145
|
+
expect(user1.name).toBe("Async User 1");
|
|
146
|
+
expect(user2.name).toBe("Async User 2");
|
|
147
147
|
});
|
|
148
148
|
});
|
|
149
149
|
describe("KyselyFactory.seed", () => {
|
|
150
|
-
itWithTransaction("should execute seed functions", async ({ trx
|
|
150
|
+
itWithTransaction("should execute seed functions", async ({ trx }) => {
|
|
151
151
|
const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
152
152
|
name: "John Doe",
|
|
153
153
|
email: `user${Date.now()}@example.com`,
|
|
154
154
|
createdAt: /* @__PURE__ */ new Date()
|
|
155
155
|
}));
|
|
156
156
|
const builders = { user: userBuilder };
|
|
157
|
-
const seeds = { createAdminUser: KyselyFactory.createSeed(async (attrs, factory$1, db$
|
|
157
|
+
const seeds = { createAdminUser: KyselyFactory.createSeed(async (attrs, factory$1, db$1) => {
|
|
158
158
|
return await factory$1.insert("user", {
|
|
159
159
|
name: attrs.name || "Admin User",
|
|
160
160
|
email: "admin@example.com"
|
|
161
161
|
});
|
|
162
162
|
}) };
|
|
163
|
-
const factory = new KyselyFactory(builders, seeds, trx
|
|
163
|
+
const factory = new KyselyFactory(builders, seeds, trx);
|
|
164
164
|
const adminUser = await factory.seed("createAdminUser");
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
165
|
+
expect(adminUser).toBeDefined();
|
|
166
|
+
expect(adminUser.name).toBe("Admin User");
|
|
167
|
+
expect(adminUser.email).toBe("admin@example.com");
|
|
168
168
|
});
|
|
169
|
-
itWithTransaction("should pass attributes to seed functions", async ({ trx
|
|
169
|
+
itWithTransaction("should pass attributes to seed functions", async ({ trx }) => {
|
|
170
170
|
const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
171
171
|
name: "John Doe",
|
|
172
172
|
email: `user${Date.now()}@example.com`,
|
|
173
173
|
createdAt: /* @__PURE__ */ new Date()
|
|
174
174
|
}));
|
|
175
175
|
const builders = { user: userBuilder };
|
|
176
|
-
const seeds = { createCustomUser: KyselyFactory.createSeed(async (attrs, factory$1, db$
|
|
176
|
+
const seeds = { createCustomUser: KyselyFactory.createSeed(async (attrs, factory$1, db$1) => {
|
|
177
177
|
return await factory$1.insert("user", attrs);
|
|
178
178
|
}) };
|
|
179
|
-
const factory = new KyselyFactory(builders, seeds, trx
|
|
179
|
+
const factory = new KyselyFactory(builders, seeds, trx);
|
|
180
180
|
const customUser = await factory.seed("createCustomUser", {
|
|
181
181
|
name: "Custom User",
|
|
182
182
|
email: "custom@test.com"
|
|
183
183
|
});
|
|
184
|
-
|
|
185
|
-
|
|
184
|
+
expect(customUser.name).toBe("Custom User");
|
|
185
|
+
expect(customUser.email).toBe("custom@test.com");
|
|
186
186
|
});
|
|
187
|
-
itWithTransaction("should throw error for non-existent seed", async ({ trx
|
|
188
|
-
const factory = new KyselyFactory({}, {}, trx
|
|
189
|
-
|
|
187
|
+
itWithTransaction("should throw error for non-existent seed", async ({ trx }) => {
|
|
188
|
+
const factory = new KyselyFactory({}, {}, trx);
|
|
189
|
+
expect(() => factory.seed("nonExistent")).toThrow("Seed \"nonExistent\" is not registered");
|
|
190
190
|
});
|
|
191
191
|
});
|
|
192
192
|
describe("Factory integration", () => {
|
|
193
|
-
itWithTransaction("should work with controlled transactions", async ({ trx
|
|
193
|
+
itWithTransaction("should work with controlled transactions", async ({ trx }) => {
|
|
194
194
|
const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
195
195
|
name: "John Doe",
|
|
196
196
|
email: `user${Date.now()}@example.com`,
|
|
197
197
|
createdAt: /* @__PURE__ */ new Date()
|
|
198
198
|
}));
|
|
199
199
|
const builders = { user: userBuilder };
|
|
200
|
-
const factory = new KyselyFactory(builders, {}, trx
|
|
200
|
+
const factory = new KyselyFactory(builders, {}, trx);
|
|
201
201
|
const user = await factory.insert("user");
|
|
202
|
-
const foundUser = await trx
|
|
203
|
-
|
|
204
|
-
|
|
202
|
+
const foundUser = await trx.selectFrom("users").selectAll().where("id", "=", user.id).executeTakeFirst();
|
|
203
|
+
expect(foundUser).toBeDefined();
|
|
204
|
+
expect(foundUser?.id).toBe(user.id);
|
|
205
205
|
});
|
|
206
|
-
itWithTransaction("should work with factory passed to defaults", async ({ trx
|
|
206
|
+
itWithTransaction("should work with factory passed to defaults", async ({ trx }) => {
|
|
207
207
|
const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
208
208
|
name: "John Doe",
|
|
209
209
|
email: `user${Date.now()}@example.com`,
|
|
@@ -222,11 +222,11 @@ describe("KyselyFactory", () => {
|
|
|
222
222
|
user: userBuilder,
|
|
223
223
|
post: postBuilder
|
|
224
224
|
};
|
|
225
|
-
const factory = new KyselyFactory(builders, {}, trx
|
|
225
|
+
const factory = new KyselyFactory(builders, {}, trx);
|
|
226
226
|
const post = await factory.insert("post");
|
|
227
|
-
|
|
228
|
-
const relatedUser = await trx
|
|
229
|
-
|
|
227
|
+
expect(post.userId).toBeDefined();
|
|
228
|
+
const relatedUser = await trx.selectFrom("users").selectAll().where("id", "=", post.userId).executeTakeFirst();
|
|
229
|
+
expect(relatedUser).toBeDefined();
|
|
230
230
|
});
|
|
231
231
|
});
|
|
232
232
|
});
|