@geekmidas/testkit 0.0.11 → 0.0.13
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/README.md +1 -1
- package/dist/{Factory-B9LP1L18.d.cts → Factory-D7P3bKKb.d.mts} +2 -2
- package/dist/{Factory-DiZSNxC0.d.mts → Factory-pNV7ZQ7-.d.cts} +2 -2
- package/dist/Factory.d.cts +2 -2
- package/dist/Factory.d.mts +2 -2
- package/dist/{KyselyFactory-ELiHgHVv.mjs → KyselyFactory-BcYkC0t2.mjs} +2 -2
- package/dist/{KyselyFactory-B-zlLbov.d.mts → KyselyFactory-BrWADI77.d.mts} +3 -3
- package/dist/{KyselyFactory-Bdq1s1Go.cjs → KyselyFactory-Cf0o2YxO.cjs} +2 -2
- package/dist/{KyselyFactory-CrLWxJW4.d.cts → KyselyFactory-DM2dnUXU.d.cts} +3 -3
- package/dist/KyselyFactory.cjs +2 -2
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +3 -3
- package/dist/KyselyFactory.mjs +2 -2
- package/dist/{ObjectionFactory-aqM0dDW7.mjs → ObjectionFactory-8hebmnai.mjs} +21 -5
- package/dist/{ObjectionFactory-CJCpvwts.d.mts → ObjectionFactory-B40NQWSe.d.mts} +2 -2
- package/dist/{ObjectionFactory-Wq80ypMM.cjs → ObjectionFactory-CDriunkS.cjs} +21 -5
- package/dist/{ObjectionFactory-vME-wNlq.d.cts → ObjectionFactory-D3l1VuyX.d.cts} +2 -2
- package/dist/ObjectionFactory.cjs +2 -2
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/ObjectionFactory.d.mts +3 -3
- package/dist/ObjectionFactory.mjs +2 -2
- package/dist/__tests__/KyselyFactory.spec.cjs +3 -3
- package/dist/__tests__/KyselyFactory.spec.mjs +3 -3
- package/dist/__tests__/ObjectionFactory.spec.cjs +289 -451
- package/dist/__tests__/ObjectionFactory.spec.mjs +290 -452
- package/dist/__tests__/PostgresKyselyMigrator.spec.cjs +397 -0
- package/dist/__tests__/PostgresKyselyMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresKyselyMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresKyselyMigrator.spec.mjs +396 -0
- package/dist/__tests__/PostgresMigrator.spec.cjs +1 -1
- package/dist/__tests__/PostgresMigrator.spec.mjs +1 -1
- package/dist/__tests__/PostgresObjectionMigrator.spec.cjs +1 -1
- package/dist/__tests__/PostgresObjectionMigrator.spec.mjs +1 -1
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +4 -12
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +4 -12
- package/dist/__tests__/faker.spec.cjs +1 -1
- package/dist/__tests__/faker.spec.mjs +1 -1
- package/dist/__tests__/integration.spec.cjs +3 -3
- package/dist/__tests__/integration.spec.mjs +3 -3
- package/dist/{faker-SMN4ira4.cjs → faker-B14IEMIN.cjs} +41 -1
- package/dist/{faker-CxKkEeYi.mjs → faker-BGKYFoCT.mjs} +36 -2
- package/dist/{faker-DAiFK3T3.d.cts → faker-BSH1EMtg.d.cts} +13 -3
- package/dist/{faker-nN9Ki6fn.d.mts → faker-C-Iuk_R1.d.mts} +13 -3
- package/dist/faker.cjs +2 -1
- package/dist/faker.d.cts +2 -2
- package/dist/faker.d.mts +2 -2
- package/dist/faker.mjs +2 -2
- package/dist/{helpers-BEmjyUVE.mjs → helpers-B4TXg3Wp.mjs} +11 -36
- package/dist/{helpers-CNMBePuj.cjs → helpers-Bf0nXhbu.cjs} +10 -41
- package/dist/kysely.cjs +2 -2
- package/dist/kysely.d.cts +3 -3
- package/dist/kysely.d.mts +3 -3
- package/dist/kysely.mjs +2 -2
- package/dist/objection.cjs +2 -2
- package/dist/objection.d.cts +3 -3
- package/dist/objection.d.mts +3 -3
- package/dist/objection.mjs +2 -2
- package/package.json +2 -2
- package/src/Factory.ts +4 -1
- package/src/KyselyFactory.ts +6 -2
- package/src/ObjectionFactory.ts +31 -4
- package/src/__tests__/ObjectionFactory.spec.ts +423 -542
- package/src/__tests__/PostgresKyselyMigrator.spec.ts +690 -0
- package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +0 -8
- package/src/faker.ts +86 -0
- package/test/helpers.ts +13 -21
- package/dist/example.cjs +0 -22
- package/dist/example.d.cts +0 -26
- package/dist/example.d.mts +0 -26
- package/dist/example.mjs +0 -22
- package/src/example.ts +0 -45
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import "../Factory-z2m01hMj.mjs";
|
|
2
|
-
import { faker } from "../faker-
|
|
3
|
-
import { ObjectionFactory } from "../ObjectionFactory-
|
|
4
|
-
import
|
|
5
|
-
import
|
|
2
|
+
import { faker } from "../faker-BGKYFoCT.mjs";
|
|
3
|
+
import { ObjectionFactory } from "../ObjectionFactory-8hebmnai.mjs";
|
|
4
|
+
import "../PostgresMigrator-DxPC_gGu.mjs";
|
|
5
|
+
import "../PostgresObjectionMigrator-G4h5FLvU.mjs";
|
|
6
|
+
import "../VitestTransactionIsolator-BWwK-ca6.mjs";
|
|
7
|
+
import "../VitestObjectionTransactionIsolator-BPoLUFop.mjs";
|
|
8
|
+
import { wrapVitestObjectionTransaction } from "../objection-lsMgM5gP.mjs";
|
|
9
|
+
import { createKnexDb, createTestTablesKnex } from "../helpers-B4TXg3Wp.mjs";
|
|
10
|
+
import { describe, expect, test } from "vitest";
|
|
6
11
|
import { Model } from "objection";
|
|
7
12
|
|
|
8
13
|
//#region src/__tests__/ObjectionFactory.spec.ts
|
|
@@ -12,10 +17,6 @@ var User = class extends Model {
|
|
|
12
17
|
}
|
|
13
18
|
id;
|
|
14
19
|
name;
|
|
15
|
-
email;
|
|
16
|
-
role;
|
|
17
|
-
createdAt;
|
|
18
|
-
updatedAt;
|
|
19
20
|
};
|
|
20
21
|
var Post = class extends Model {
|
|
21
22
|
static get tableName() {
|
|
@@ -23,478 +24,315 @@ var Post = class extends Model {
|
|
|
23
24
|
}
|
|
24
25
|
id;
|
|
25
26
|
title;
|
|
26
|
-
|
|
27
|
-
userId;
|
|
28
|
-
published;
|
|
29
|
-
createdAt;
|
|
30
|
-
updatedAt;
|
|
27
|
+
user_id;
|
|
31
28
|
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
userId;
|
|
40
|
-
createdAt;
|
|
41
|
-
};
|
|
42
|
-
describe.skip("ObjectionFactory", () => {
|
|
43
|
-
let factory;
|
|
44
|
-
let db;
|
|
45
|
-
let trx;
|
|
46
|
-
let cleanup;
|
|
47
|
-
beforeAll(async () => {
|
|
48
|
-
const setup = await setupKnexTest();
|
|
49
|
-
db = setup.db;
|
|
50
|
-
trx = setup.trx;
|
|
51
|
-
cleanup = setup.cleanup;
|
|
52
|
-
User.knex(trx);
|
|
53
|
-
Post.knex(trx);
|
|
54
|
-
Comment.knex(trx);
|
|
29
|
+
const it$1 = wrapVitestObjectionTransaction(test, createKnexDb(), createTestTablesKnex);
|
|
30
|
+
describe("ObjectionFactory", () => {
|
|
31
|
+
it$1("should create an ObjectionFactory instance", ({ trx }) => {
|
|
32
|
+
const builders = {};
|
|
33
|
+
const seeds = {};
|
|
34
|
+
const factory = new ObjectionFactory(builders, seeds, trx);
|
|
35
|
+
expect(factory).toBeInstanceOf(ObjectionFactory);
|
|
55
36
|
});
|
|
56
|
-
|
|
57
|
-
|
|
37
|
+
it$1("should call builder and insert the record", async ({ trx }) => {
|
|
38
|
+
const userBuilder = async (attrs$1, factory$1, db) => {
|
|
39
|
+
return User.fromJson({ name: attrs$1.name || "Default Name" });
|
|
40
|
+
};
|
|
41
|
+
const builders = { user: userBuilder };
|
|
42
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
43
|
+
const attrs = {
|
|
44
|
+
name: "John Doe",
|
|
45
|
+
email: "john@example.com"
|
|
46
|
+
};
|
|
47
|
+
const result = await factory.insert("user", attrs);
|
|
48
|
+
expect(result).toBeInstanceOf(User);
|
|
49
|
+
expect(result.name).toBe("John Doe");
|
|
50
|
+
expect(result.id).toBeDefined();
|
|
58
51
|
});
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
52
|
+
it$1("should use empty object as default attributes", async ({ trx }) => {
|
|
53
|
+
const userBuilder = async (attrs, factory$1, db) => {
|
|
54
|
+
return User.fromJson({ name: "Default Name" });
|
|
55
|
+
};
|
|
56
|
+
const builders = { user: userBuilder };
|
|
57
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
58
|
+
const result = await factory.insert("user");
|
|
59
|
+
expect(result).toBeInstanceOf(User);
|
|
60
|
+
expect(result.name).toBe("Default Name");
|
|
61
|
+
expect(result.id).toBeDefined();
|
|
66
62
|
});
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
63
|
+
it$1("should throw error for non-existent factory", async ({ trx }) => {
|
|
64
|
+
const factory = new ObjectionFactory({}, {}, trx);
|
|
65
|
+
await expect(factory.insert("nonExistent")).rejects.toThrow("Factory \"nonExistent\" does not exist");
|
|
66
|
+
});
|
|
67
|
+
it$1("should handle builder that returns a promise", async ({ trx }) => {
|
|
68
|
+
const userBuilder = async (attrs, factory$1, db) => {
|
|
69
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
70
|
+
return User.fromJson({ name: attrs.name || "Default Name" });
|
|
71
|
+
};
|
|
72
|
+
const builders = { user: userBuilder };
|
|
73
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
74
|
+
const result = await factory.insert("user", { name: "Jane" });
|
|
75
|
+
expect(result).toBeInstanceOf(User);
|
|
76
|
+
expect(result.name).toBe("Jane");
|
|
77
|
+
});
|
|
78
|
+
it$1("should insert multiple records with same attributes", async ({ trx }) => {
|
|
79
|
+
const userBuilder = async (attrs$1, factory$1, db) => {
|
|
80
|
+
return User.fromJson({ name: attrs$1.name || "Default Name" });
|
|
81
|
+
};
|
|
82
|
+
const builders = { user: userBuilder };
|
|
83
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
84
|
+
const attrs = { name: "User" };
|
|
85
|
+
const results = await factory.insertMany(3, "user", attrs);
|
|
86
|
+
expect(results).toHaveLength(3);
|
|
87
|
+
results.forEach((result) => {
|
|
85
88
|
expect(result).toBeInstanceOf(User);
|
|
86
|
-
expect(result.name).toBe("
|
|
87
|
-
expect(result.email).toBe("john@example.com");
|
|
89
|
+
expect(result.name).toBe("User");
|
|
88
90
|
expect(result.id).toBeDefined();
|
|
89
91
|
});
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
92
|
+
});
|
|
93
|
+
it$1("should insert multiple records with dynamic attributes", async ({ trx }) => {
|
|
94
|
+
const userBuilder = async (attrs, factory$1, db) => {
|
|
95
|
+
return User.fromJson({ name: attrs.name || "Default Name" });
|
|
96
|
+
};
|
|
97
|
+
const builders = { user: userBuilder };
|
|
98
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
99
|
+
const attrsFn = (idx) => ({ name: `User ${idx}` });
|
|
100
|
+
const results = await factory.insertMany(2, "user", attrsFn);
|
|
101
|
+
expect(results).toHaveLength(2);
|
|
102
|
+
expect(results[0].name).toBe("User 0");
|
|
103
|
+
expect(results[1].name).toBe("User 1");
|
|
104
|
+
results.forEach((result) => {
|
|
103
105
|
expect(result).toBeInstanceOf(User);
|
|
104
|
-
expect(result.name).toBe("Default Name");
|
|
105
106
|
expect(result.id).toBeDefined();
|
|
106
107
|
});
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
role: "user",
|
|
118
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
119
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
120
|
-
});
|
|
121
|
-
};
|
|
122
|
-
const builders = { user: userBuilder };
|
|
123
|
-
factory = new ObjectionFactory(builders, {}, trx);
|
|
124
|
-
const result = await factory.insert("user", { name: "Jane" });
|
|
108
|
+
});
|
|
109
|
+
it$1("should use empty object as default attributes for insertMany", async ({ trx }) => {
|
|
110
|
+
const userBuilder = async (attrs, factory$1, db) => {
|
|
111
|
+
return User.fromJson({ name: "Default Name" });
|
|
112
|
+
};
|
|
113
|
+
const builders = { user: userBuilder };
|
|
114
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
115
|
+
const results = await factory.insertMany(2, "user");
|
|
116
|
+
expect(results).toHaveLength(2);
|
|
117
|
+
results.forEach((result) => {
|
|
125
118
|
expect(result).toBeInstanceOf(User);
|
|
126
|
-
expect(result.name).toBe("
|
|
119
|
+
expect(result.name).toBe("Default Name");
|
|
120
|
+
expect(result.id).toBeDefined();
|
|
127
121
|
});
|
|
128
122
|
});
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
159
|
-
});
|
|
160
|
-
};
|
|
161
|
-
const builders = { user: userBuilder };
|
|
162
|
-
factory = new ObjectionFactory(builders, {}, trx);
|
|
163
|
-
const attrsFn = (idx) => ({ name: `User ${idx}` });
|
|
164
|
-
const results = await factory.insertMany(2, "user", attrsFn);
|
|
165
|
-
expect(results).toHaveLength(2);
|
|
166
|
-
expect(results[0].name).toBe("User 0");
|
|
167
|
-
expect(results[1].name).toBe("User 1");
|
|
168
|
-
results.forEach((result) => {
|
|
169
|
-
expect(result).toBeInstanceOf(User);
|
|
170
|
-
expect(result.id).toBeDefined();
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
it("should use empty object as default attributes for insertMany", async () => {
|
|
174
|
-
const userBuilder = async (attrs, factory$1, db$1) => {
|
|
175
|
-
return User.fromJson({
|
|
176
|
-
name: "Default Name",
|
|
177
|
-
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
178
|
-
role: "user",
|
|
179
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
180
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
181
|
-
});
|
|
182
|
-
};
|
|
183
|
-
const builders = { user: userBuilder };
|
|
184
|
-
factory = new ObjectionFactory(builders, {}, trx);
|
|
185
|
-
const results = await factory.insertMany(2, "user");
|
|
186
|
-
expect(results).toHaveLength(2);
|
|
187
|
-
results.forEach((result) => {
|
|
188
|
-
expect(result).toBeInstanceOf(User);
|
|
189
|
-
expect(result.name).toBe("Default Name");
|
|
190
|
-
expect(result.id).toBeDefined();
|
|
123
|
+
it$1("should throw error for non-existent builder in insertMany", async ({ trx }) => {
|
|
124
|
+
const factory = new ObjectionFactory({}, {}, trx);
|
|
125
|
+
await expect(factory.insertMany(2, "nonExistent")).rejects.toThrow("Builder \"nonExistent\" is not registered");
|
|
126
|
+
});
|
|
127
|
+
it$1("should execute seed function", async ({ trx }) => {
|
|
128
|
+
const userBuilder = async (attrs$1, factory$1, db) => {
|
|
129
|
+
return User.fromJson({ name: attrs$1.name || "Default Name" });
|
|
130
|
+
};
|
|
131
|
+
const builders = { user: userBuilder };
|
|
132
|
+
const createAdminSeed = async (attrs$1, factory$1, db) => {
|
|
133
|
+
return await factory$1.insert("user", { name: attrs$1.name || "Admin User" });
|
|
134
|
+
};
|
|
135
|
+
const seeds = { createAdmin: createAdminSeed };
|
|
136
|
+
const factory = new ObjectionFactory(builders, seeds, trx);
|
|
137
|
+
const attrs = { name: "Super Admin" };
|
|
138
|
+
const result = await factory.seed("createAdmin", attrs);
|
|
139
|
+
expect(result).toBeInstanceOf(User);
|
|
140
|
+
expect(result.name).toBe("Super Admin");
|
|
141
|
+
expect(result.id).toBeDefined();
|
|
142
|
+
});
|
|
143
|
+
it$1("should use empty object as default attributes for seed", async ({ trx }) => {
|
|
144
|
+
const userBuilder = async (attrs, factory$1, db) => {
|
|
145
|
+
return User.fromJson({ name: "Default Admin" });
|
|
146
|
+
};
|
|
147
|
+
const builders = { user: userBuilder };
|
|
148
|
+
const createAdminSeed = async (attrs, factory$1, db) => {
|
|
149
|
+
return await factory$1.insert("user", {
|
|
150
|
+
name: "Default Admin",
|
|
151
|
+
role: "admin"
|
|
191
152
|
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
153
|
+
};
|
|
154
|
+
const seeds = { createAdmin: createAdminSeed };
|
|
155
|
+
const factory = new ObjectionFactory(builders, seeds, trx);
|
|
156
|
+
const result = await factory.seed("createAdmin");
|
|
157
|
+
expect(result).toBeInstanceOf(User);
|
|
158
|
+
expect(result.name).toBe("Default Admin");
|
|
197
159
|
});
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
return User.fromJson({
|
|
202
|
-
name: attrs$1.name || "Default Name",
|
|
203
|
-
email: attrs$1.email || `admin${Date.now()}@example.com`,
|
|
204
|
-
role: "admin",
|
|
205
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
206
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
207
|
-
});
|
|
208
|
-
};
|
|
209
|
-
const builders = { user: userBuilder };
|
|
210
|
-
const createAdminSeed = async (attrs$1, factory$1, db$1) => {
|
|
211
|
-
return await factory$1.insert("user", {
|
|
212
|
-
name: attrs$1.name || "Admin User",
|
|
213
|
-
email: "admin@example.com",
|
|
214
|
-
role: "admin"
|
|
215
|
-
});
|
|
216
|
-
};
|
|
217
|
-
const seeds = { createAdmin: createAdminSeed };
|
|
218
|
-
factory = new ObjectionFactory(builders, seeds, trx);
|
|
219
|
-
const attrs = { name: "Super Admin" };
|
|
220
|
-
const result = await factory.seed("createAdmin", attrs);
|
|
221
|
-
expect(result).toBeInstanceOf(User);
|
|
222
|
-
expect(result.name).toBe("Super Admin");
|
|
223
|
-
expect(result.role).toBe("admin");
|
|
224
|
-
expect(result.id).toBeDefined();
|
|
225
|
-
});
|
|
226
|
-
it("should use empty object as default attributes for seed", async () => {
|
|
227
|
-
const userBuilder = async (attrs, factory$1, db$1) => {
|
|
228
|
-
return User.fromJson({
|
|
229
|
-
name: "Default Admin",
|
|
230
|
-
email: `admin${Date.now()}@example.com`,
|
|
231
|
-
role: "admin",
|
|
232
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
233
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
234
|
-
});
|
|
235
|
-
};
|
|
236
|
-
const builders = { user: userBuilder };
|
|
237
|
-
const createAdminSeed = async (attrs, factory$1, db$1) => {
|
|
238
|
-
return await factory$1.insert("user", {
|
|
239
|
-
name: "Default Admin",
|
|
240
|
-
role: "admin"
|
|
241
|
-
});
|
|
242
|
-
};
|
|
243
|
-
const seeds = { createAdmin: createAdminSeed };
|
|
244
|
-
factory = new ObjectionFactory(builders, seeds, trx);
|
|
245
|
-
const result = await factory.seed("createAdmin");
|
|
246
|
-
expect(result).toBeInstanceOf(User);
|
|
247
|
-
expect(result.name).toBe("Default Admin");
|
|
248
|
-
expect(result.role).toBe("admin");
|
|
249
|
-
});
|
|
250
|
-
it("should throw error for non-existent seed", () => {
|
|
251
|
-
factory = new ObjectionFactory({}, {}, trx);
|
|
252
|
-
expect(() => factory.seed("nonExistent")).toThrow("Seed \"nonExistent\" is not registered");
|
|
253
|
-
});
|
|
254
|
-
it("should pass factory and db to seed function", async () => {
|
|
255
|
-
const userBuilder = async (attrs, factory$1, db$1) => {
|
|
256
|
-
return User.fromJson({
|
|
257
|
-
name: attrs.name || "Test User",
|
|
258
|
-
email: `test${Date.now()}@example.com`,
|
|
259
|
-
role: "user",
|
|
260
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
261
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
262
|
-
});
|
|
263
|
-
};
|
|
264
|
-
const builders = { user: userBuilder };
|
|
265
|
-
const complexSeed = async (attrs, passedFactory, passedDb) => {
|
|
266
|
-
expect(passedFactory).toBe(factory);
|
|
267
|
-
expect(passedDb).toBe(trx);
|
|
268
|
-
return await passedFactory.insert("user", { name: `Complex ${attrs.data}` });
|
|
269
|
-
};
|
|
270
|
-
const seeds = { complexSeed };
|
|
271
|
-
factory = new ObjectionFactory(builders, seeds, trx);
|
|
272
|
-
const result = await factory.seed("complexSeed", { data: "test" });
|
|
273
|
-
expect(result).toBeInstanceOf(User);
|
|
274
|
-
expect(result.name).toBe("Complex test");
|
|
275
|
-
});
|
|
160
|
+
it$1("should throw error for non-existent seed", ({ trx }) => {
|
|
161
|
+
const factory = new ObjectionFactory({}, {}, trx);
|
|
162
|
+
expect(() => factory.seed("nonExistent")).toThrow("Seed \"nonExistent\" is not registered");
|
|
276
163
|
});
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
164
|
+
it$1("should pass factory and db to seed function", async ({ trx }) => {
|
|
165
|
+
const userBuilder = async (attrs, factory$1, db) => {
|
|
166
|
+
return User.fromJson({ name: attrs.name || "Test User" });
|
|
167
|
+
};
|
|
168
|
+
const builders = { user: userBuilder };
|
|
169
|
+
const complexSeed = async (attrs, passedFactory, passedDb) => {
|
|
170
|
+
expect(passedFactory).toBe(factory);
|
|
171
|
+
expect(passedDb).toBe(trx);
|
|
172
|
+
return await passedFactory.insert("user", { name: `Complex ${attrs.data}` });
|
|
173
|
+
};
|
|
174
|
+
const seeds = { complexSeed };
|
|
175
|
+
const factory = new ObjectionFactory(builders, seeds, trx);
|
|
176
|
+
const result = await factory.seed("complexSeed", { data: "test" });
|
|
177
|
+
expect(result).toBeInstanceOf(User);
|
|
178
|
+
expect(result.name).toBe("Complex test");
|
|
179
|
+
});
|
|
180
|
+
it$1("should return the seed function unchanged", () => {
|
|
181
|
+
const seedFn = async (attrs, factory, db) => {
|
|
182
|
+
return {
|
|
183
|
+
id: 1,
|
|
184
|
+
name: "test"
|
|
284
185
|
};
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
186
|
+
};
|
|
187
|
+
const result = ObjectionFactory.createSeed(seedFn);
|
|
188
|
+
expect(result).toBe(seedFn);
|
|
288
189
|
});
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
190
|
+
it$1("should create a builder function with auto-insert", async ({ trx }) => {
|
|
191
|
+
const userBuilder = ObjectionFactory.createBuilder(User, (attrs, factory$1, db, faker$1) => ({
|
|
192
|
+
name: faker$1.person.fullName(),
|
|
193
|
+
...attrs
|
|
194
|
+
}));
|
|
195
|
+
const builders = { user: userBuilder };
|
|
196
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
197
|
+
const result = await factory.insert("user", { name: "Test User" });
|
|
198
|
+
expect(result).toBeInstanceOf(User);
|
|
199
|
+
expect(result.name).toBe("Test User");
|
|
200
|
+
expect(result.id).toBeDefined();
|
|
201
|
+
});
|
|
202
|
+
it$1("should create a builder function without auto-insert", async ({ trx }) => {
|
|
203
|
+
const userBuilder = ObjectionFactory.createBuilder(User, (attrs) => ({
|
|
204
|
+
name: "No Insert User",
|
|
205
|
+
...attrs
|
|
206
|
+
}), false);
|
|
207
|
+
const builders = { user: userBuilder };
|
|
208
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
209
|
+
const result = await factory.insert("user");
|
|
210
|
+
expect(result).toBeInstanceOf(User);
|
|
211
|
+
expect(result.name).toBe("No Insert User");
|
|
212
|
+
expect(result.id).toBeDefined();
|
|
213
|
+
});
|
|
214
|
+
it$1("should pass all parameters to the item function", async ({ trx }) => {
|
|
215
|
+
let capturedFactory;
|
|
216
|
+
let capturedDb;
|
|
217
|
+
let capturedFaker;
|
|
218
|
+
const userBuilder = ObjectionFactory.createBuilder(User, (attrs, factory$1, db, fakerInstance) => {
|
|
219
|
+
capturedFactory = factory$1;
|
|
220
|
+
capturedDb = db;
|
|
221
|
+
capturedFaker = fakerInstance;
|
|
222
|
+
return {
|
|
223
|
+
name: "Test User",
|
|
314
224
|
...attrs
|
|
315
|
-
}), false);
|
|
316
|
-
const builders = { user: userBuilder };
|
|
317
|
-
factory = new ObjectionFactory(builders, {}, trx);
|
|
318
|
-
const result = await factory.insert("user");
|
|
319
|
-
expect(result).toBeInstanceOf(User);
|
|
320
|
-
expect(result.name).toBe("No Insert User");
|
|
321
|
-
expect(result.id).toBeDefined();
|
|
322
|
-
});
|
|
323
|
-
it("should pass all parameters to the item function", async () => {
|
|
324
|
-
let capturedFactory;
|
|
325
|
-
let capturedDb;
|
|
326
|
-
let capturedFaker;
|
|
327
|
-
const userBuilder = ObjectionFactory.createBuilder(User, (attrs, factory$1, db$1, fakerInstance) => {
|
|
328
|
-
capturedFactory = factory$1;
|
|
329
|
-
capturedDb = db$1;
|
|
330
|
-
capturedFaker = fakerInstance;
|
|
331
|
-
return {
|
|
332
|
-
name: "Test User",
|
|
333
|
-
email: "test@example.com",
|
|
334
|
-
role: "user",
|
|
335
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
336
|
-
updatedAt: /* @__PURE__ */ new Date(),
|
|
337
|
-
...attrs
|
|
338
|
-
};
|
|
339
|
-
});
|
|
340
|
-
const builders = { user: userBuilder };
|
|
341
|
-
factory = new ObjectionFactory(builders, {}, trx);
|
|
342
|
-
await factory.insert("user");
|
|
343
|
-
expect(capturedFactory).toBe(factory);
|
|
344
|
-
expect(capturedDb).toBe(trx);
|
|
345
|
-
expect(capturedFaker).toBe(faker);
|
|
346
|
-
});
|
|
347
|
-
it("should handle async item functions", async () => {
|
|
348
|
-
const userBuilder = ObjectionFactory.createBuilder(User, async (attrs, factory$1, db$1, faker$1) => {
|
|
349
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
350
|
-
return {
|
|
351
|
-
name: "Async User",
|
|
352
|
-
email: faker$1.internet.email(),
|
|
353
|
-
role: "user",
|
|
354
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
355
|
-
updatedAt: /* @__PURE__ */ new Date(),
|
|
356
|
-
...attrs
|
|
357
|
-
};
|
|
358
|
-
});
|
|
359
|
-
const builders = { user: userBuilder };
|
|
360
|
-
factory = new ObjectionFactory(builders, {}, trx);
|
|
361
|
-
const result = await factory.insert("user");
|
|
362
|
-
expect(result).toBeInstanceOf(User);
|
|
363
|
-
expect(result.name).toBe("Async User");
|
|
364
|
-
expect(result.id).toBeDefined();
|
|
365
|
-
});
|
|
366
|
-
it("should work without item function", async () => {
|
|
367
|
-
const userBuilder = ObjectionFactory.createBuilder(User);
|
|
368
|
-
const builders = { user: userBuilder };
|
|
369
|
-
factory = new ObjectionFactory(builders, {}, trx);
|
|
370
|
-
const attrs = {
|
|
371
|
-
name: "Manual User",
|
|
372
|
-
email: "manual@example.com",
|
|
373
|
-
role: "user",
|
|
374
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
375
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
376
225
|
};
|
|
377
|
-
const result = await factory.insert("user", attrs);
|
|
378
|
-
expect(result).toBeInstanceOf(User);
|
|
379
|
-
expect(result.name).toBe("Manual User");
|
|
380
|
-
expect(result.email).toBe("manual@example.com");
|
|
381
|
-
expect(result.id).toBeDefined();
|
|
382
|
-
});
|
|
383
|
-
it("should allow overriding default values", async () => {
|
|
384
|
-
const userBuilder = ObjectionFactory.createBuilder(User, (attrs, factory$1, db$1, faker$1) => ({
|
|
385
|
-
name: "Default Name",
|
|
386
|
-
email: "default@example.com",
|
|
387
|
-
role: "user",
|
|
388
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
389
|
-
updatedAt: /* @__PURE__ */ new Date(),
|
|
390
|
-
...attrs
|
|
391
|
-
}));
|
|
392
|
-
const builders = { user: userBuilder };
|
|
393
|
-
factory = new ObjectionFactory(builders, {}, trx);
|
|
394
|
-
const result = await factory.insert("user", {
|
|
395
|
-
name: "Override Name",
|
|
396
|
-
email: "override@example.com"
|
|
397
|
-
});
|
|
398
|
-
expect(result).toBeInstanceOf(User);
|
|
399
|
-
expect(result.name).toBe("Override Name");
|
|
400
|
-
expect(result.email).toBe("override@example.com");
|
|
401
|
-
expect(result.role).toBe("user");
|
|
402
226
|
});
|
|
227
|
+
const builders = { user: userBuilder };
|
|
228
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
229
|
+
await factory.insert("user");
|
|
230
|
+
expect(capturedFactory).toBe(factory);
|
|
231
|
+
expect(capturedDb).toBe(trx);
|
|
232
|
+
expect(capturedFaker).toBe(faker);
|
|
403
233
|
});
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
factory = new ObjectionFactory(builders, {}, trx);
|
|
411
|
-
await expect(factory.insert("user")).rejects.toThrow("Builder failed");
|
|
412
|
-
});
|
|
413
|
-
it("should handle invalid model data gracefully", async () => {
|
|
414
|
-
const userBuilder = async (attrs, factory$1, db$1) => {
|
|
415
|
-
return User.fromJson({ invalidField: "invalid" });
|
|
416
|
-
};
|
|
417
|
-
const builders = { user: userBuilder };
|
|
418
|
-
factory = new ObjectionFactory(builders, {}, trx);
|
|
419
|
-
await expect(factory.insert("user")).rejects.toThrow();
|
|
420
|
-
});
|
|
421
|
-
it("should handle seed function errors gracefully", async () => {
|
|
422
|
-
const failingSeed = async (attrs, factory$1, db$1) => {
|
|
423
|
-
throw new Error("Seed failed");
|
|
234
|
+
it$1("should handle async item functions", async ({ trx }) => {
|
|
235
|
+
const userBuilder = ObjectionFactory.createBuilder(User, async (attrs, factory$1, db, faker$1) => {
|
|
236
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
237
|
+
return {
|
|
238
|
+
name: "Async User",
|
|
239
|
+
...attrs
|
|
424
240
|
};
|
|
425
|
-
const seeds = { failingSeed };
|
|
426
|
-
factory = new ObjectionFactory({}, seeds, trx);
|
|
427
|
-
await expect(factory.seed("failingSeed")).rejects.toThrow("Seed failed");
|
|
428
241
|
});
|
|
242
|
+
const builders = { user: userBuilder };
|
|
243
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
244
|
+
const result = await factory.insert("user");
|
|
245
|
+
expect(result).toBeInstanceOf(User);
|
|
246
|
+
expect(result.name).toBe("Async User");
|
|
247
|
+
expect(result.id).toBeDefined();
|
|
429
248
|
});
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
249
|
+
it$1("should work without item function", async ({ trx }) => {
|
|
250
|
+
const userBuilder = ObjectionFactory.createBuilder(User);
|
|
251
|
+
const builders = { user: userBuilder };
|
|
252
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
253
|
+
const attrs = { name: "Manual User" };
|
|
254
|
+
const result = await factory.insert("user", attrs);
|
|
255
|
+
expect(result).toBeInstanceOf(User);
|
|
256
|
+
expect(result.name).toBe("Manual User");
|
|
257
|
+
expect(result.id).toBeDefined();
|
|
258
|
+
});
|
|
259
|
+
it$1("should allow overriding default values", async ({ trx }) => {
|
|
260
|
+
const userBuilder = ObjectionFactory.createBuilder(User, (attrs, factory$1, db, faker$1) => ({
|
|
261
|
+
name: "Default Name",
|
|
262
|
+
...attrs
|
|
263
|
+
}));
|
|
264
|
+
const builders = { user: userBuilder };
|
|
265
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
266
|
+
const result = await factory.insert("user", { name: "Override Name" });
|
|
267
|
+
expect(result).toBeInstanceOf(User);
|
|
268
|
+
expect(result.name).toBe("Override Name");
|
|
269
|
+
});
|
|
270
|
+
it$1("should handle builder errors gracefully", async ({ trx }) => {
|
|
271
|
+
const userBuilder = async (attrs, factory$1, db) => {
|
|
272
|
+
throw new Error("Builder failed");
|
|
273
|
+
};
|
|
274
|
+
const builders = { user: userBuilder };
|
|
275
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
276
|
+
await expect(factory.insert("user")).rejects.toThrow("Builder failed");
|
|
277
|
+
});
|
|
278
|
+
it$1("should handle invalid model data gracefully", async ({ trx }) => {
|
|
279
|
+
const userBuilder = async (attrs, factory$1, db) => {
|
|
280
|
+
return User.fromJson({ invalidField: "invalid" });
|
|
281
|
+
};
|
|
282
|
+
const builders = { user: userBuilder };
|
|
283
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
284
|
+
await expect(factory.insert("user")).rejects.toThrow();
|
|
285
|
+
});
|
|
286
|
+
it$1("should handle seed function errors gracefully", async ({ trx }) => {
|
|
287
|
+
const failingSeed = async (attrs, factory$1, db) => {
|
|
288
|
+
throw new Error("Seed failed");
|
|
289
|
+
};
|
|
290
|
+
const seeds = { failingSeed };
|
|
291
|
+
const factory = new ObjectionFactory({}, seeds, trx);
|
|
292
|
+
await expect(factory.seed("failingSeed")).rejects.toThrow("Seed failed");
|
|
293
|
+
});
|
|
294
|
+
it$1("should work with typed builders and seeds", async ({ trx }) => {
|
|
295
|
+
const userBuilder = async (attrs, factory$1, db) => {
|
|
296
|
+
return User.fromJson({ name: attrs.name || "Default User" });
|
|
297
|
+
};
|
|
298
|
+
const adminSeed = async (attrs, factory$1, db) => {
|
|
299
|
+
return factory$1.insert("user", { name: "Admin" });
|
|
300
|
+
};
|
|
301
|
+
const builders = { user: userBuilder };
|
|
302
|
+
const seeds = { admin: adminSeed };
|
|
303
|
+
const factory = new ObjectionFactory(builders, seeds, trx);
|
|
304
|
+
expect(factory).toBeInstanceOf(ObjectionFactory);
|
|
305
|
+
const admin = await factory.seed("admin", { isSuper: true });
|
|
306
|
+
expect(admin).toBeInstanceOf(User);
|
|
307
|
+
expect(admin.name).toBe("Admin");
|
|
308
|
+
});
|
|
309
|
+
it$1("should handle complex builder scenarios", async ({ trx }) => {
|
|
310
|
+
const userBuilder = async (attrs, factory$1, db) => {
|
|
311
|
+
return User.fromJson({ name: attrs.name || "Default User" });
|
|
312
|
+
};
|
|
313
|
+
const postBuilder = async (attrs, factory$1, db) => {
|
|
314
|
+
if (!attrs.user_id) {
|
|
315
|
+
const user = await factory$1.insert("user");
|
|
478
316
|
return Post.fromJson({
|
|
479
317
|
title: attrs.title || "Default Post",
|
|
480
|
-
|
|
481
|
-
userId: attrs.userId,
|
|
482
|
-
published: attrs.published || false,
|
|
483
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
484
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
318
|
+
user_id: user.id
|
|
485
319
|
});
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
};
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
});
|
|
320
|
+
}
|
|
321
|
+
return Post.fromJson({
|
|
322
|
+
title: attrs.title || "Default Post",
|
|
323
|
+
user_id: attrs.user_id
|
|
324
|
+
});
|
|
325
|
+
};
|
|
326
|
+
const builders = {
|
|
327
|
+
user: userBuilder,
|
|
328
|
+
post: postBuilder
|
|
329
|
+
};
|
|
330
|
+
const factory = new ObjectionFactory(builders, {}, trx);
|
|
331
|
+
const post = await factory.insert("post", { title: "Test Post" });
|
|
332
|
+
expect(post).toBeInstanceOf(Post);
|
|
333
|
+
expect(post.title).toBe("Test Post");
|
|
334
|
+
expect(post.user_id).toBeDefined();
|
|
335
|
+
expect(typeof post.user_id).toBe("string");
|
|
498
336
|
});
|
|
499
337
|
});
|
|
500
338
|
|