@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.
Files changed (71) hide show
  1. package/README.md +1 -1
  2. package/dist/{Factory-B9LP1L18.d.cts → Factory-D7P3bKKb.d.mts} +2 -2
  3. package/dist/{Factory-DiZSNxC0.d.mts → Factory-pNV7ZQ7-.d.cts} +2 -2
  4. package/dist/Factory.d.cts +2 -2
  5. package/dist/Factory.d.mts +2 -2
  6. package/dist/{KyselyFactory-ELiHgHVv.mjs → KyselyFactory-BcYkC0t2.mjs} +2 -2
  7. package/dist/{KyselyFactory-B-zlLbov.d.mts → KyselyFactory-BrWADI77.d.mts} +3 -3
  8. package/dist/{KyselyFactory-Bdq1s1Go.cjs → KyselyFactory-Cf0o2YxO.cjs} +2 -2
  9. package/dist/{KyselyFactory-CrLWxJW4.d.cts → KyselyFactory-DM2dnUXU.d.cts} +3 -3
  10. package/dist/KyselyFactory.cjs +2 -2
  11. package/dist/KyselyFactory.d.cts +3 -3
  12. package/dist/KyselyFactory.d.mts +3 -3
  13. package/dist/KyselyFactory.mjs +2 -2
  14. package/dist/{ObjectionFactory-aqM0dDW7.mjs → ObjectionFactory-8hebmnai.mjs} +21 -5
  15. package/dist/{ObjectionFactory-CJCpvwts.d.mts → ObjectionFactory-B40NQWSe.d.mts} +2 -2
  16. package/dist/{ObjectionFactory-Wq80ypMM.cjs → ObjectionFactory-CDriunkS.cjs} +21 -5
  17. package/dist/{ObjectionFactory-vME-wNlq.d.cts → ObjectionFactory-D3l1VuyX.d.cts} +2 -2
  18. package/dist/ObjectionFactory.cjs +2 -2
  19. package/dist/ObjectionFactory.d.cts +3 -3
  20. package/dist/ObjectionFactory.d.mts +3 -3
  21. package/dist/ObjectionFactory.mjs +2 -2
  22. package/dist/__tests__/KyselyFactory.spec.cjs +3 -3
  23. package/dist/__tests__/KyselyFactory.spec.mjs +3 -3
  24. package/dist/__tests__/ObjectionFactory.spec.cjs +289 -451
  25. package/dist/__tests__/ObjectionFactory.spec.mjs +290 -452
  26. package/dist/__tests__/PostgresKyselyMigrator.spec.cjs +397 -0
  27. package/dist/__tests__/PostgresKyselyMigrator.spec.d.cts +1 -0
  28. package/dist/__tests__/PostgresKyselyMigrator.spec.d.mts +1 -0
  29. package/dist/__tests__/PostgresKyselyMigrator.spec.mjs +396 -0
  30. package/dist/__tests__/PostgresMigrator.spec.cjs +1 -1
  31. package/dist/__tests__/PostgresMigrator.spec.mjs +1 -1
  32. package/dist/__tests__/PostgresObjectionMigrator.spec.cjs +1 -1
  33. package/dist/__tests__/PostgresObjectionMigrator.spec.mjs +1 -1
  34. package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +4 -12
  35. package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +4 -12
  36. package/dist/__tests__/faker.spec.cjs +1 -1
  37. package/dist/__tests__/faker.spec.mjs +1 -1
  38. package/dist/__tests__/integration.spec.cjs +3 -3
  39. package/dist/__tests__/integration.spec.mjs +3 -3
  40. package/dist/{faker-SMN4ira4.cjs → faker-B14IEMIN.cjs} +41 -1
  41. package/dist/{faker-CxKkEeYi.mjs → faker-BGKYFoCT.mjs} +36 -2
  42. package/dist/{faker-DAiFK3T3.d.cts → faker-BSH1EMtg.d.cts} +13 -3
  43. package/dist/{faker-nN9Ki6fn.d.mts → faker-C-Iuk_R1.d.mts} +13 -3
  44. package/dist/faker.cjs +2 -1
  45. package/dist/faker.d.cts +2 -2
  46. package/dist/faker.d.mts +2 -2
  47. package/dist/faker.mjs +2 -2
  48. package/dist/{helpers-BEmjyUVE.mjs → helpers-B4TXg3Wp.mjs} +11 -36
  49. package/dist/{helpers-CNMBePuj.cjs → helpers-Bf0nXhbu.cjs} +10 -41
  50. package/dist/kysely.cjs +2 -2
  51. package/dist/kysely.d.cts +3 -3
  52. package/dist/kysely.d.mts +3 -3
  53. package/dist/kysely.mjs +2 -2
  54. package/dist/objection.cjs +2 -2
  55. package/dist/objection.d.cts +3 -3
  56. package/dist/objection.d.mts +3 -3
  57. package/dist/objection.mjs +2 -2
  58. package/package.json +2 -2
  59. package/src/Factory.ts +4 -1
  60. package/src/KyselyFactory.ts +6 -2
  61. package/src/ObjectionFactory.ts +31 -4
  62. package/src/__tests__/ObjectionFactory.spec.ts +423 -542
  63. package/src/__tests__/PostgresKyselyMigrator.spec.ts +690 -0
  64. package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +0 -8
  65. package/src/faker.ts +86 -0
  66. package/test/helpers.ts +13 -21
  67. package/dist/example.cjs +0 -22
  68. package/dist/example.d.cts +0 -26
  69. package/dist/example.d.mts +0 -26
  70. package/dist/example.mjs +0 -22
  71. package/src/example.ts +0 -45
@@ -12,8 +12,6 @@ class User extends Model {
12
12
 
13
13
  id!: number;
14
14
  name!: string;
15
- email!: string;
16
- role?: string;
17
15
  createdAt!: Date;
18
16
  updatedAt?: Date;
19
17
 
@@ -110,9 +108,6 @@ class Comment extends Model {
110
108
  // Create database connection
111
109
  const knex = createKnexDb();
112
110
 
113
- // Bind models to Knex instance
114
- Model.knex(knex);
115
-
116
111
  // Create wrapped test with transaction isolation
117
112
  const it = wrapVitestObjectionTransaction(base, knex, async (trx) => {
118
113
  // Create tables in the transaction
@@ -125,8 +120,6 @@ describe('VitestObjectionTransactionIsolator', () => {
125
120
  // Create a user within the transaction
126
121
  const user = await User.query(trx).insert({
127
122
  name: 'Test User',
128
- email: 'test@example.com',
129
- role: 'user',
130
123
  });
131
124
 
132
125
  expect(user).toBeDefined();
@@ -136,7 +129,6 @@ describe('VitestObjectionTransactionIsolator', () => {
136
129
  // Verify user exists in transaction
137
130
  const foundUser = await User.query(trx).findById(user.id);
138
131
  expect(foundUser).toBeDefined();
139
- expect(foundUser?.email).toBe(user.email);
140
132
 
141
133
  // Data will be rolled back after this test
142
134
  });
package/src/faker.ts CHANGED
@@ -215,6 +215,88 @@ function price(): number {
215
215
  return +faker.commerce.price();
216
216
  }
217
217
 
218
+ type Coordinate = {
219
+ lat: number;
220
+ lng: number;
221
+ };
222
+
223
+ export function coordinateInRadius(
224
+ center: Coordinate,
225
+ radius: number,
226
+ ): Coordinate {
227
+ // Earth's radius in meters
228
+ const earth = 6378137;
229
+ // Convert radius from meters to degrees
230
+ const d = radius / earth;
231
+
232
+ // Random bearing and distance
233
+ const theta = 2 * Math.PI * Math.random();
234
+ const r = d * Math.sqrt(Math.random());
235
+
236
+ const lat1 = (center.lat * Math.PI) / 180;
237
+ const lng1 = (center.lng * Math.PI) / 180;
238
+
239
+ const lat2 = Math.asin(
240
+ Math.sin(lat1) * Math.cos(r) +
241
+ Math.cos(lat1) * Math.sin(r) * Math.cos(theta),
242
+ );
243
+ const lng2 =
244
+ lng1 +
245
+ Math.atan2(
246
+ Math.sin(theta) * Math.sin(r) * Math.cos(lat1),
247
+ Math.cos(r) - Math.sin(lat1) * Math.sin(lat2),
248
+ );
249
+
250
+ return {
251
+ lat: (lat2 * 180) / Math.PI,
252
+ lng: (lng2 * 180) / Math.PI,
253
+ };
254
+ }
255
+
256
+ function coordinateOutsideRadius(
257
+ center: Coordinate,
258
+ minRadiusMeters: number,
259
+ maxRadiusMeters: number,
260
+ ): Coordinate {
261
+ // Earth's radius in meters
262
+ const earth = 6378137;
263
+
264
+ // Convert radii from meters to radians
265
+ const minD = minRadiusMeters / earth;
266
+ const maxD = maxRadiusMeters / earth;
267
+
268
+ // Random bearing
269
+ const theta = 2 * Math.PI * Math.random();
270
+
271
+ // Random distance in annular ring (uniform distribution by area)
272
+ // For uniform distribution in annulus: r = sqrt(r_min² + (r_max² - r_min²) * random)
273
+ const r = Math.sqrt(
274
+ minD * minD + (maxD * maxD - minD * minD) * Math.random(),
275
+ );
276
+
277
+ const lat1 = (center.lat * Math.PI) / 180;
278
+ const lng1 = (center.lng * Math.PI) / 180;
279
+
280
+ const lat2 = Math.asin(
281
+ Math.sin(lat1) * Math.cos(r) +
282
+ Math.cos(lat1) * Math.sin(r) * Math.cos(theta),
283
+ );
284
+ const lng2 =
285
+ lng1 +
286
+ Math.atan2(
287
+ Math.sin(theta) * Math.sin(r) * Math.cos(lat1),
288
+ Math.cos(r) - Math.sin(lat1) * Math.sin(lat2),
289
+ );
290
+
291
+ // Normalize longitude to [-180, 180]
292
+ const normalizedLng = (((lng2 * 180) / Math.PI + 540) % 360) - 180;
293
+
294
+ return {
295
+ lat: (lat2 * 180) / Math.PI,
296
+ lng: normalizedLng,
297
+ };
298
+ }
299
+
218
300
  /**
219
301
  * Enhanced faker instance with additional utility methods for testing.
220
302
  * Extends @faker-js/faker with custom methods for common test data generation patterns.
@@ -242,6 +324,10 @@ export const faker = Object.freeze(
242
324
  resetSequence,
243
325
  resetAllSequences,
244
326
  price,
327
+ coordinates: {
328
+ within: coordinateInRadius,
329
+ outside: coordinateOutsideRadius,
330
+ },
245
331
  }),
246
332
  );
247
333
 
package/test/helpers.ts CHANGED
@@ -165,49 +165,41 @@ export async function createTestTables(
165
165
  export async function createTestTablesKnex(
166
166
  trx: Knex.Transaction,
167
167
  ): Promise<void> {
168
- // Create users table
169
- await trx.schema.createTable('users', (table) => {
170
- table.bigIncrements('id').primary();
168
+ // Create simple users table for testing factory
169
+ await trx.raw(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
170
+ await trx.schema.createTableIfNotExists('users', (table) => {
171
+ table.uuid('id').primary().defaultTo(trx.raw('uuid_generate_v4()'));
171
172
  table.string('name').notNullable();
172
- table.string('email').notNullable().unique();
173
- table.string('role').defaultTo('user');
174
- table.timestamp('created_at').defaultTo(trx.fn.now()).notNullable();
175
- table.timestamp('updated_at').defaultTo(trx.fn.now()).notNullable();
176
173
  });
177
174
 
178
- // Create posts table
179
- await trx.schema.createTable('posts', (table) => {
180
- table.bigIncrements('id').primary();
175
+ // Create simple posts table for testing factory
176
+ await trx.schema.createTableIfNotExists('posts', (table) => {
177
+ table.uuid('id').primary().defaultTo(trx.raw('uuid_generate_v4()'));
181
178
  table.string('title').notNullable();
182
- table.text('content').notNullable();
183
179
  table
184
- .bigInteger('user_id')
180
+ .uuid('user_id')
185
181
  .notNullable()
186
182
  .references('id')
187
183
  .inTable('users')
188
184
  .onDelete('cascade');
189
- table.boolean('published').defaultTo(false);
190
- table.timestamp('created_at').defaultTo(trx.fn.now()).notNullable();
191
- table.timestamp('updated_at').defaultTo(trx.fn.now()).notNullable();
192
185
  });
193
186
 
194
- // Create comments table
195
- await trx.schema.createTable('comments', (table) => {
196
- table.bigIncrements('id').primary();
187
+ // Create simple comments table for testing factory
188
+ await trx.schema.createTableIfNotExists('comments', (table) => {
189
+ table.uuid('id').primary().defaultTo(trx.raw('uuid_generate_v4()'));
197
190
  table.text('content').notNullable();
198
191
  table
199
- .bigInteger('post_id')
192
+ .uuid('post_id')
200
193
  .notNullable()
201
194
  .references('id')
202
195
  .inTable('posts')
203
196
  .onDelete('cascade');
204
197
  table
205
- .bigInteger('user_id')
198
+ .uuid('user_id')
206
199
  .notNullable()
207
200
  .references('id')
208
201
  .inTable('users')
209
202
  .onDelete('cascade');
210
- table.timestamp('created_at').defaultTo(trx.fn.now()).notNullable();
211
203
  });
212
204
  }
213
205
 
package/dist/example.cjs DELETED
@@ -1,22 +0,0 @@
1
- require('./Factory-WMhTNZ9S.cjs');
2
- require('./faker-SMN4ira4.cjs');
3
- const require_KyselyFactory = require('./KyselyFactory-Bdq1s1Go.cjs');
4
-
5
- //#region src/example.ts
6
- const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder({
7
- table: "users",
8
- defaults: async (attrs) => ({
9
- name: "John Doe",
10
- email: `user${Date.now()}@example.com`,
11
- createdAt: /* @__PURE__ */ new Date()
12
- })
13
- });
14
- const builders = { user: userBuilder };
15
- const factory = new require_KyselyFactory.KyselyFactory(builders, {}, {});
16
- factory.insert("user", {
17
- name: "Jane Doe",
18
- email: `user${Date.now()}@example.com`,
19
- createdAt: /* @__PURE__ */ new Date()
20
- });
21
-
22
- //#endregion
@@ -1,26 +0,0 @@
1
- import { FakerFactory } from "./faker-DAiFK3T3.cjs";
2
- import * as kysely0 from "kysely";
3
-
4
- //#region src/example.d.ts
5
- interface Database {
6
- users: {
7
- id: number;
8
- name: string;
9
- email: string;
10
- createdAt: Date;
11
- };
12
- posts: {
13
- id: number;
14
- title: string;
15
- content: string;
16
- userId: number;
17
- createdAt: Date;
18
- };
19
- }
20
- declare const builders: {
21
- user: (attrs: Partial<kysely0.Insertable<DB[TableName]>>, factory: any, db: kysely0.Kysely<Database>, faker: FakerFactory) => Promise<kysely0.Selectable<DB[TableName]>>;
22
- };
23
- type Builders = typeof builders;
24
- type Seeds = Record<string, any>;
25
- //#endregion
26
- export { Builders, Seeds };
@@ -1,26 +0,0 @@
1
- import { FakerFactory } from "./faker-nN9Ki6fn.mjs";
2
- import * as kysely0 from "kysely";
3
-
4
- //#region src/example.d.ts
5
- interface Database {
6
- users: {
7
- id: number;
8
- name: string;
9
- email: string;
10
- createdAt: Date;
11
- };
12
- posts: {
13
- id: number;
14
- title: string;
15
- content: string;
16
- userId: number;
17
- createdAt: Date;
18
- };
19
- }
20
- declare const builders: {
21
- user: (attrs: Partial<kysely0.Insertable<DB[TableName]>>, factory: any, db: kysely0.Kysely<Database>, faker: FakerFactory) => Promise<kysely0.Selectable<DB[TableName]>>;
22
- };
23
- type Builders = typeof builders;
24
- type Seeds = Record<string, any>;
25
- //#endregion
26
- export { Builders, Seeds };
package/dist/example.mjs DELETED
@@ -1,22 +0,0 @@
1
- import "./Factory-z2m01hMj.mjs";
2
- import "./faker-CxKkEeYi.mjs";
3
- import { KyselyFactory } from "./KyselyFactory-ELiHgHVv.mjs";
4
-
5
- //#region src/example.ts
6
- const userBuilder = KyselyFactory.createBuilder({
7
- table: "users",
8
- defaults: async (attrs) => ({
9
- name: "John Doe",
10
- email: `user${Date.now()}@example.com`,
11
- createdAt: /* @__PURE__ */ new Date()
12
- })
13
- });
14
- const builders = { user: userBuilder };
15
- const factory = new KyselyFactory(builders, {}, {});
16
- factory.insert("user", {
17
- name: "Jane Doe",
18
- email: `user${Date.now()}@example.com`,
19
- createdAt: /* @__PURE__ */ new Date()
20
- });
21
-
22
- //#endregion
package/src/example.ts DELETED
@@ -1,45 +0,0 @@
1
- import { KyselyFactory } from './KyselyFactory';
2
-
3
- interface Database {
4
- users: {
5
- id: number;
6
- name: string;
7
- email: string;
8
- createdAt: Date;
9
- };
10
- posts: {
11
- id: number;
12
- title: string;
13
- content: string;
14
- userId: number;
15
- createdAt: Date;
16
- };
17
- }
18
-
19
- const userBuilder = KyselyFactory.createBuilder<Database, 'users'>({
20
- table: 'users',
21
- defaults: async (attrs) => ({
22
- name: 'John Doe',
23
- email: `user${Date.now()}@example.com`,
24
- createdAt: new Date(),
25
- }),
26
- });
27
-
28
- const builders = {
29
- user: userBuilder,
30
- };
31
-
32
- export type Builders = typeof builders;
33
- export type Seeds = Record<string, any>;
34
-
35
- const factory = new KyselyFactory<Database, Builders, Seeds>(
36
- builders,
37
- {},
38
- {} as any,
39
- );
40
-
41
- factory.insert('user', {
42
- name: 'Jane Doe',
43
- email: `user${Date.now()}@example.com`,
44
- createdAt: new Date(),
45
- });