@geekmidas/testkit 0.0.5 → 0.0.7
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-WMhTNZ9S.cjs +56 -0
- package/dist/Factory-z2m01hMj.mjs +50 -0
- package/dist/Factory.cjs +1 -1
- package/dist/Factory.mjs +1 -1
- package/dist/KyselyFactory-Bdq1s1Go.cjs +215 -0
- package/dist/KyselyFactory-ELiHgHVv.mjs +210 -0
- package/dist/KyselyFactory.cjs +3 -3
- package/dist/KyselyFactory.mjs +3 -3
- package/dist/ObjectionFactory-89p-FFEw.mjs +178 -0
- package/dist/ObjectionFactory-C47B03Ot.cjs +183 -0
- package/dist/ObjectionFactory.cjs +2 -2
- package/dist/ObjectionFactory.mjs +2 -2
- package/dist/PostgresKyselyMigrator-Bs31emFd.cjs +87 -0
- package/dist/PostgresKyselyMigrator-ChIpZFYB.mjs +81 -0
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/PostgresMigrator-BtAWdLss.cjs +151 -0
- package/dist/PostgresMigrator-BzqksJcW.mjs +145 -0
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/VitestKyselyTransactionIsolator-AfxPJEwR.mjs +58 -0
- package/dist/VitestKyselyTransactionIsolator-YWnSJiIH.cjs +63 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
- package/dist/VitestObjectionTransactionIsolator-0uX6DW5G.cjs +66 -0
- package/dist/VitestObjectionTransactionIsolator-BZRYy8iW.mjs +61 -0
- package/dist/VitestObjectionTransactionIsolator.cjs +4 -0
- package/dist/VitestObjectionTransactionIsolator.mjs +4 -0
- package/dist/VitestTransactionIsolator-DcOz0LZF.cjs +129 -0
- package/dist/VitestTransactionIsolator-kFL36T8x.mjs +117 -0
- package/dist/VitestTransactionIsolator.cjs +1 -1
- package/dist/VitestTransactionIsolator.mjs +1 -1
- package/dist/__tests__/Factory.spec.cjs +1 -1
- package/dist/__tests__/Factory.spec.mjs +1 -1
- package/dist/__tests__/KyselyFactory.spec.cjs +41 -40
- package/dist/__tests__/KyselyFactory.spec.mjs +42 -41
- package/dist/__tests__/ObjectionFactory.spec.cjs +3 -3
- package/dist/__tests__/ObjectionFactory.spec.mjs +3 -3
- package/dist/__tests__/PostgresMigrator.spec.cjs +2 -2
- package/dist/__tests__/PostgresMigrator.spec.mjs +2 -2
- package/dist/__tests__/faker.spec.cjs +1 -1
- package/dist/__tests__/faker.spec.mjs +1 -1
- package/dist/__tests__/integration.spec.cjs +11 -8
- package/dist/__tests__/integration.spec.mjs +12 -9
- package/dist/example.cjs +3 -3
- package/dist/example.mjs +3 -3
- package/dist/faker-CxKkEeYi.mjs +227 -0
- package/dist/faker-SMN4ira4.cjs +263 -0
- package/dist/faker.cjs +1 -1
- package/dist/faker.mjs +1 -1
- package/dist/helpers-CKMlwSYT.mjs +47 -0
- package/dist/helpers-H4hO5SZR.cjs +53 -0
- package/dist/helpers.cjs +2 -5
- package/dist/helpers.mjs +2 -4
- package/dist/kysely-B-GOhABm.cjs +72 -0
- package/dist/kysely-CqfoKVXs.mjs +67 -0
- package/dist/kysely.cjs +11 -15
- package/dist/kysely.mjs +9 -15
- package/dist/objection.cjs +86 -3
- package/dist/objection.mjs +83 -3
- package/package.json +2 -2
- package/src/Factory.ts +97 -0
- package/src/KyselyFactory.ts +180 -0
- package/src/ObjectionFactory.ts +145 -3
- package/src/PostgresKyselyMigrator.ts +54 -0
- package/src/PostgresMigrator.ts +90 -0
- package/src/VitestKyselyTransactionIsolator.ts +46 -0
- package/src/VitestObjectionTransactionIsolator.ts +73 -0
- package/src/VitestTransactionIsolator.ts +99 -2
- package/src/__tests__/KyselyFactory.spec.ts +4 -6
- package/src/__tests__/integration.spec.ts +8 -3
- package/src/faker.ts +158 -7
- package/src/helpers.ts +35 -18
- package/src/kysely.ts +66 -1
- package/src/objection.ts +95 -0
- package/dist/Factory-DREHoms3.cjs +0 -15
- package/dist/Factory-DlzMkMzb.mjs +0 -9
- package/dist/KyselyFactory-BX7Kv2uP.cjs +0 -65
- package/dist/KyselyFactory-pOMOFQWE.mjs +0 -60
- package/dist/ObjectionFactory-BlkzSEqo.cjs +0 -41
- package/dist/ObjectionFactory-ChuX8sZN.mjs +0 -36
- package/dist/PostgresKyselyMigrator-D8fm35-s.mjs +0 -27
- package/dist/PostgresKyselyMigrator-JTY2LfwD.cjs +0 -33
- package/dist/PostgresMigrator-Bz-tnjB6.cjs +0 -67
- package/dist/PostgresMigrator-CEoRKTdq.mjs +0 -61
- package/dist/VitestKyselyTransactionIsolator-BS3R-V0I.mjs +0 -12
- package/dist/VitestKyselyTransactionIsolator-DWSTKIe3.cjs +0 -17
- package/dist/VitestTransactionIsolator-BjVXqFs6.mjs +0 -40
- package/dist/VitestTransactionIsolator-Bx2c4OzK.cjs +0 -52
- package/dist/faker-BwaXA_RF.mjs +0 -85
- package/dist/faker-caz-8zt8.cjs +0 -121
- package/dist/helpers-B9Jdk_C7.cjs +0 -31
- package/dist/helpers-DOiGIkaU.mjs +0 -19
- /package/dist/{helpers-DKEBHABj.cjs → helpers-Bnm3Jy9X.cjs} +0 -0
- /package/dist/{helpers-BfuX-cjN.mjs → helpers-CukcFAU9.mjs} +0 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/VitestTransactionIsolator.ts
|
|
3
|
+
/**
|
|
4
|
+
* PostgreSQL transaction isolation levels.
|
|
5
|
+
* Controls the visibility of concurrent transactions.
|
|
6
|
+
*
|
|
7
|
+
* @see https://www.postgresql.org/docs/current/transaction-iso.html
|
|
8
|
+
*/
|
|
9
|
+
let IsolationLevel = /* @__PURE__ */ function(IsolationLevel$1) {
|
|
10
|
+
/**
|
|
11
|
+
* Lowest isolation level. Allows dirty reads.
|
|
12
|
+
* Not recommended for testing.
|
|
13
|
+
*/
|
|
14
|
+
IsolationLevel$1["READ_UNCOMMITTED"] = "READ UNCOMMITTED";
|
|
15
|
+
/**
|
|
16
|
+
* Default PostgreSQL isolation level.
|
|
17
|
+
* Prevents dirty reads but allows non-repeatable reads.
|
|
18
|
+
*/
|
|
19
|
+
IsolationLevel$1["READ_COMMITTED"] = "READ COMMITTED";
|
|
20
|
+
/**
|
|
21
|
+
* Prevents dirty reads and non-repeatable reads.
|
|
22
|
+
* Recommended for most test scenarios.
|
|
23
|
+
*/
|
|
24
|
+
IsolationLevel$1["REPEATABLE_READ"] = "REPEATABLE READ";
|
|
25
|
+
/**
|
|
26
|
+
* Highest isolation level. Prevents all phenomena.
|
|
27
|
+
* May cause performance overhead in tests.
|
|
28
|
+
*/
|
|
29
|
+
IsolationLevel$1["SERIALIZABLE"] = "SERIALIZABLE";
|
|
30
|
+
return IsolationLevel$1;
|
|
31
|
+
}({});
|
|
32
|
+
/**
|
|
33
|
+
* Abstract base class for implementing database transaction isolation in Vitest tests.
|
|
34
|
+
* Provides automatic transaction rollback after each test to maintain test isolation.
|
|
35
|
+
* Subclasses must implement the transact() method for their specific database driver.
|
|
36
|
+
*
|
|
37
|
+
* @template Connection - The database connection type
|
|
38
|
+
* @template Transaction - The transaction type
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* // Implement for your database driver
|
|
43
|
+
* class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {
|
|
44
|
+
* async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {
|
|
45
|
+
* await conn.transaction(level, fn);
|
|
46
|
+
* }
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* // Use in tests
|
|
50
|
+
* const isolator = new MyDatabaseIsolator(test);
|
|
51
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(db);
|
|
52
|
+
*
|
|
53
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
54
|
+
* await trx.insert('users', { name: 'Test' });
|
|
55
|
+
* // Data is automatically rolled back after test
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
var VitestPostgresTransactionIsolator = class {
|
|
60
|
+
/**
|
|
61
|
+
* Creates a new VitestPostgresTransactionIsolator instance.
|
|
62
|
+
*
|
|
63
|
+
* @param api - The Vitest test API (usually the `test` export from vitest)
|
|
64
|
+
*/
|
|
65
|
+
constructor(api) {
|
|
66
|
+
this.api = api;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Creates a wrapped version of Vitest's test API that provides transaction isolation.
|
|
70
|
+
* Each test will run within a database transaction that is automatically rolled back.
|
|
71
|
+
*
|
|
72
|
+
* @param conn - The database connection to use
|
|
73
|
+
* @param setup - Optional setup function to run within the transaction before each test
|
|
74
|
+
* @param level - The transaction isolation level (defaults to REPEATABLE_READ)
|
|
75
|
+
* @returns A wrapped test API with transaction support
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(db, async (trx) => {
|
|
80
|
+
* // Optional setup: create common test data
|
|
81
|
+
* await trx.insert('settings', { key: 'test', value: 'true' });
|
|
82
|
+
* });
|
|
83
|
+
*
|
|
84
|
+
* isolatedTest('test with transaction', async ({ trx }) => {
|
|
85
|
+
* const user = await trx.insert('users', { name: 'Test' });
|
|
86
|
+
* expect(user).toBeDefined();
|
|
87
|
+
* });
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
wrapVitestWithTransaction(conn, setup, level = IsolationLevel.REPEATABLE_READ) {
|
|
91
|
+
return this.api.extend({ trx: async ({}, use) => {
|
|
92
|
+
class TestRollback extends Error {
|
|
93
|
+
constructor() {
|
|
94
|
+
super("Test rollback");
|
|
95
|
+
this.name = "TestRollback";
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
let testError;
|
|
99
|
+
try {
|
|
100
|
+
await this.transact(conn, level, async (transaction) => {
|
|
101
|
+
try {
|
|
102
|
+
await setup?.(transaction);
|
|
103
|
+
await use(transaction);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
testError = error;
|
|
106
|
+
}
|
|
107
|
+
throw new TestRollback();
|
|
108
|
+
});
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (!(error instanceof TestRollback)) throw error;
|
|
111
|
+
if (testError) throw testError;
|
|
112
|
+
}
|
|
113
|
+
} });
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
//#endregion
|
|
118
|
+
Object.defineProperty(exports, 'IsolationLevel', {
|
|
119
|
+
enumerable: true,
|
|
120
|
+
get: function () {
|
|
121
|
+
return IsolationLevel;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
Object.defineProperty(exports, 'VitestPostgresTransactionIsolator', {
|
|
125
|
+
enumerable: true,
|
|
126
|
+
get: function () {
|
|
127
|
+
return VitestPostgresTransactionIsolator;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
//#region src/VitestTransactionIsolator.ts
|
|
2
|
+
/**
|
|
3
|
+
* PostgreSQL transaction isolation levels.
|
|
4
|
+
* Controls the visibility of concurrent transactions.
|
|
5
|
+
*
|
|
6
|
+
* @see https://www.postgresql.org/docs/current/transaction-iso.html
|
|
7
|
+
*/
|
|
8
|
+
let IsolationLevel = /* @__PURE__ */ function(IsolationLevel$1) {
|
|
9
|
+
/**
|
|
10
|
+
* Lowest isolation level. Allows dirty reads.
|
|
11
|
+
* Not recommended for testing.
|
|
12
|
+
*/
|
|
13
|
+
IsolationLevel$1["READ_UNCOMMITTED"] = "READ UNCOMMITTED";
|
|
14
|
+
/**
|
|
15
|
+
* Default PostgreSQL isolation level.
|
|
16
|
+
* Prevents dirty reads but allows non-repeatable reads.
|
|
17
|
+
*/
|
|
18
|
+
IsolationLevel$1["READ_COMMITTED"] = "READ COMMITTED";
|
|
19
|
+
/**
|
|
20
|
+
* Prevents dirty reads and non-repeatable reads.
|
|
21
|
+
* Recommended for most test scenarios.
|
|
22
|
+
*/
|
|
23
|
+
IsolationLevel$1["REPEATABLE_READ"] = "REPEATABLE READ";
|
|
24
|
+
/**
|
|
25
|
+
* Highest isolation level. Prevents all phenomena.
|
|
26
|
+
* May cause performance overhead in tests.
|
|
27
|
+
*/
|
|
28
|
+
IsolationLevel$1["SERIALIZABLE"] = "SERIALIZABLE";
|
|
29
|
+
return IsolationLevel$1;
|
|
30
|
+
}({});
|
|
31
|
+
/**
|
|
32
|
+
* Abstract base class for implementing database transaction isolation in Vitest tests.
|
|
33
|
+
* Provides automatic transaction rollback after each test to maintain test isolation.
|
|
34
|
+
* Subclasses must implement the transact() method for their specific database driver.
|
|
35
|
+
*
|
|
36
|
+
* @template Connection - The database connection type
|
|
37
|
+
* @template Transaction - The transaction type
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* // Implement for your database driver
|
|
42
|
+
* class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {
|
|
43
|
+
* async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {
|
|
44
|
+
* await conn.transaction(level, fn);
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* // Use in tests
|
|
49
|
+
* const isolator = new MyDatabaseIsolator(test);
|
|
50
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(db);
|
|
51
|
+
*
|
|
52
|
+
* isolatedTest('should create user', async ({ trx }) => {
|
|
53
|
+
* await trx.insert('users', { name: 'Test' });
|
|
54
|
+
* // Data is automatically rolled back after test
|
|
55
|
+
* });
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
var VitestPostgresTransactionIsolator = class {
|
|
59
|
+
/**
|
|
60
|
+
* Creates a new VitestPostgresTransactionIsolator instance.
|
|
61
|
+
*
|
|
62
|
+
* @param api - The Vitest test API (usually the `test` export from vitest)
|
|
63
|
+
*/
|
|
64
|
+
constructor(api) {
|
|
65
|
+
this.api = api;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Creates a wrapped version of Vitest's test API that provides transaction isolation.
|
|
69
|
+
* Each test will run within a database transaction that is automatically rolled back.
|
|
70
|
+
*
|
|
71
|
+
* @param conn - The database connection to use
|
|
72
|
+
* @param setup - Optional setup function to run within the transaction before each test
|
|
73
|
+
* @param level - The transaction isolation level (defaults to REPEATABLE_READ)
|
|
74
|
+
* @returns A wrapped test API with transaction support
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const isolatedTest = isolator.wrapVitestWithTransaction(db, async (trx) => {
|
|
79
|
+
* // Optional setup: create common test data
|
|
80
|
+
* await trx.insert('settings', { key: 'test', value: 'true' });
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* isolatedTest('test with transaction', async ({ trx }) => {
|
|
84
|
+
* const user = await trx.insert('users', { name: 'Test' });
|
|
85
|
+
* expect(user).toBeDefined();
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
wrapVitestWithTransaction(conn, setup, level = IsolationLevel.REPEATABLE_READ) {
|
|
90
|
+
return this.api.extend({ trx: async ({}, use) => {
|
|
91
|
+
class TestRollback extends Error {
|
|
92
|
+
constructor() {
|
|
93
|
+
super("Test rollback");
|
|
94
|
+
this.name = "TestRollback";
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
let testError;
|
|
98
|
+
try {
|
|
99
|
+
await this.transact(conn, level, async (transaction) => {
|
|
100
|
+
try {
|
|
101
|
+
await setup?.(transaction);
|
|
102
|
+
await use(transaction);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
testError = error;
|
|
105
|
+
}
|
|
106
|
+
throw new TestRollback();
|
|
107
|
+
});
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if (!(error instanceof TestRollback)) throw error;
|
|
110
|
+
if (testError) throw testError;
|
|
111
|
+
}
|
|
112
|
+
} });
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
export { IsolationLevel, VitestPostgresTransactionIsolator };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-
|
|
1
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-DcOz0LZF.cjs');
|
|
2
2
|
|
|
3
3
|
exports.IsolationLevel = require_VitestTransactionIsolator.IsolationLevel;
|
|
4
4
|
exports.VitestPostgresTransactionIsolator = require_VitestTransactionIsolator.VitestPostgresTransactionIsolator;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-
|
|
1
|
+
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-kFL36T8x.mjs";
|
|
2
2
|
|
|
3
3
|
export { IsolationLevel, VitestPostgresTransactionIsolator };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const require_chunk = require('../chunk-CUT6urMc.cjs');
|
|
2
|
-
const require_Factory = require('../Factory-
|
|
2
|
+
const require_Factory = require('../Factory-WMhTNZ9S.cjs');
|
|
3
3
|
const vitest = require_chunk.__toESM(require("vitest"));
|
|
4
4
|
|
|
5
5
|
//#region src/__tests__/Factory.spec.ts
|
|
@@ -1,33 +1,34 @@
|
|
|
1
1
|
const require_chunk = require('../chunk-CUT6urMc.cjs');
|
|
2
|
-
require('../Factory-
|
|
3
|
-
require('../faker-
|
|
4
|
-
const require_KyselyFactory = require('../KyselyFactory-
|
|
5
|
-
require('../
|
|
6
|
-
require('../
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
require('../Factory-WMhTNZ9S.cjs');
|
|
3
|
+
require('../faker-SMN4ira4.cjs');
|
|
4
|
+
const require_KyselyFactory = require('../KyselyFactory-Bdq1s1Go.cjs');
|
|
5
|
+
require('../PostgresMigrator-BtAWdLss.cjs');
|
|
6
|
+
require('../PostgresKyselyMigrator-Bs31emFd.cjs');
|
|
7
|
+
require('../VitestTransactionIsolator-DcOz0LZF.cjs');
|
|
8
|
+
require('../VitestKyselyTransactionIsolator-YWnSJiIH.cjs');
|
|
9
|
+
const require_helpers = require('../helpers-H4hO5SZR.cjs');
|
|
10
|
+
const require_kysely = require('../kysely-B-GOhABm.cjs');
|
|
11
|
+
const require_helpers$1 = require('../helpers-Bnm3Jy9X.cjs');
|
|
9
12
|
const pg = require_chunk.__toESM(require("pg"));
|
|
10
13
|
const vitest = require_chunk.__toESM(require("vitest"));
|
|
11
14
|
|
|
12
15
|
//#region src/__tests__/KyselyFactory.spec.ts
|
|
13
16
|
const db = require_helpers.createKyselyDb(require_helpers$1.TEST_DATABASE_CONFIG);
|
|
14
|
-
const itWithTransaction =
|
|
17
|
+
const itWithTransaction = require_kysely.wrapVitestKyselyTransaction(vitest.it, db, require_helpers$1.createTestTables);
|
|
15
18
|
const int8TypeId = 20;
|
|
16
19
|
pg.default.types.setTypeParser(int8TypeId, (val) => {
|
|
17
20
|
return parseInt(val, 10);
|
|
18
21
|
});
|
|
19
22
|
(0, vitest.describe)("KyselyFactory", () => {
|
|
20
|
-
let db$1;
|
|
21
|
-
let trx;
|
|
22
23
|
(0, vitest.describe)("KyselyFactory.insert", () => {
|
|
23
|
-
itWithTransaction("should insert a record with defaults", async ({ trx
|
|
24
|
+
itWithTransaction("should insert a record with defaults", async ({ trx }) => {
|
|
24
25
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
25
26
|
name: "John Doe",
|
|
26
27
|
email: `user${Date.now()}@example.com`,
|
|
27
28
|
createdAt: /* @__PURE__ */ new Date()
|
|
28
29
|
}));
|
|
29
30
|
const builders = { user: userBuilder };
|
|
30
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
31
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
31
32
|
const user = await factory.insert("user");
|
|
32
33
|
(0, vitest.expect)(user).toBeDefined();
|
|
33
34
|
(0, vitest.expect)(user.id).toBeDefined();
|
|
@@ -36,14 +37,14 @@ pg.default.types.setTypeParser(int8TypeId, (val) => {
|
|
|
36
37
|
(0, vitest.expect)(user.email).toContain("@example.com");
|
|
37
38
|
(0, vitest.expect)(user.createdAt).toBeInstanceOf(Date);
|
|
38
39
|
});
|
|
39
|
-
itWithTransaction("should override defaults with provided attributes", async ({ trx
|
|
40
|
+
itWithTransaction("should override defaults with provided attributes", async ({ trx }) => {
|
|
40
41
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
41
42
|
name: "John Doe",
|
|
42
43
|
email: `user${Date.now()}@example.com`,
|
|
43
44
|
createdAt: /* @__PURE__ */ new Date()
|
|
44
45
|
}));
|
|
45
46
|
const builders = { user: userBuilder };
|
|
46
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
47
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
47
48
|
const customEmail = "custom@test.com";
|
|
48
49
|
const customName = "Jane Smith";
|
|
49
50
|
const user = await factory.insert("user", {
|
|
@@ -53,7 +54,7 @@ pg.default.types.setTypeParser(int8TypeId, (val) => {
|
|
|
53
54
|
(0, vitest.expect)(user.name).toBe(customName);
|
|
54
55
|
(0, vitest.expect)(user.email).toBe(customEmail);
|
|
55
56
|
});
|
|
56
|
-
itWithTransaction("should handle relations", async ({ trx
|
|
57
|
+
itWithTransaction("should handle relations", async ({ trx }) => {
|
|
57
58
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
58
59
|
name: "John Doe",
|
|
59
60
|
email: `user${Date.now()}@example.com`,
|
|
@@ -79,27 +80,27 @@ pg.default.types.setTypeParser(int8TypeId, (val) => {
|
|
|
79
80
|
user: userBuilder,
|
|
80
81
|
post: postBuilder
|
|
81
82
|
};
|
|
82
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
83
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
83
84
|
const post = await factory.insert("post", { title: "Test Post" });
|
|
84
85
|
(0, vitest.expect)(post).toBeDefined();
|
|
85
86
|
(0, vitest.expect)(post.title).toBe("Test Post");
|
|
86
87
|
(0, vitest.expect)(post.userId).toBeDefined();
|
|
87
88
|
(0, vitest.expect)(typeof post.userId).toBe("number");
|
|
88
89
|
});
|
|
89
|
-
itWithTransaction("should throw error for non-existent builder", async ({ trx
|
|
90
|
-
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx
|
|
90
|
+
itWithTransaction("should throw error for non-existent builder", async ({ trx }) => {
|
|
91
|
+
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx);
|
|
91
92
|
await (0, vitest.expect)(factory.insert("nonExistent")).rejects.toThrow("Factory \"nonExistent\" does not exist");
|
|
92
93
|
});
|
|
93
94
|
});
|
|
94
95
|
(0, vitest.describe)("KyselyFactory.insertMany", () => {
|
|
95
|
-
itWithTransaction("should insert multiple records with same attributes", async ({ trx
|
|
96
|
+
itWithTransaction("should insert multiple records with same attributes", async ({ trx }) => {
|
|
96
97
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
97
98
|
name: "John Doe",
|
|
98
99
|
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
99
100
|
createdAt: /* @__PURE__ */ new Date()
|
|
100
101
|
}));
|
|
101
102
|
const builders = { user: userBuilder };
|
|
102
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
103
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
103
104
|
const users = await factory.insertMany(3, "user");
|
|
104
105
|
(0, vitest.expect)(users).toHaveLength(3);
|
|
105
106
|
users.forEach((user, index) => {
|
|
@@ -108,26 +109,26 @@ pg.default.types.setTypeParser(int8TypeId, (val) => {
|
|
|
108
109
|
(0, vitest.expect)(user.email).toContain("@example.com");
|
|
109
110
|
});
|
|
110
111
|
});
|
|
111
|
-
itWithTransaction("should insert multiple records with dynamic attributes", async ({ trx
|
|
112
|
+
itWithTransaction("should insert multiple records with dynamic attributes", async ({ trx }) => {
|
|
112
113
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
113
114
|
email: `user${Date.now()}-${Math.random()}@example.com`,
|
|
114
115
|
createdAt: /* @__PURE__ */ new Date()
|
|
115
116
|
}));
|
|
116
117
|
const builders = { user: userBuilder };
|
|
117
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
118
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
118
119
|
const users = await factory.insertMany(3, "user", (idx) => ({ name: `User ${idx}` }));
|
|
119
120
|
(0, vitest.expect)(users).toHaveLength(3);
|
|
120
121
|
users.forEach((user, index) => {
|
|
121
122
|
(0, vitest.expect)(user.name).toBe(`User ${index}`);
|
|
122
123
|
});
|
|
123
124
|
});
|
|
124
|
-
itWithTransaction("should throw error for non-existent builder", async ({ trx
|
|
125
|
-
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx
|
|
125
|
+
itWithTransaction("should throw error for non-existent builder", async ({ trx }) => {
|
|
126
|
+
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx);
|
|
126
127
|
await (0, vitest.expect)(factory.insertMany(2, "nonExistent")).rejects.toThrow("Builder \"nonExistent\" is not registered");
|
|
127
128
|
});
|
|
128
129
|
});
|
|
129
130
|
(0, vitest.describe)("KyselyFactory.createBuilder", () => {
|
|
130
|
-
itWithTransaction("should work with async defaults", async ({ trx
|
|
131
|
+
itWithTransaction("should work with async defaults", async ({ trx }) => {
|
|
131
132
|
let counter = 0;
|
|
132
133
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => {
|
|
133
134
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
@@ -139,7 +140,7 @@ pg.default.types.setTypeParser(int8TypeId, (val) => {
|
|
|
139
140
|
};
|
|
140
141
|
});
|
|
141
142
|
const builders = { user: userBuilder };
|
|
142
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
143
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
143
144
|
const user1 = await factory.insert("user");
|
|
144
145
|
const user2 = await factory.insert("user");
|
|
145
146
|
(0, vitest.expect)(user1.name).toBe("Async User 1");
|
|
@@ -147,36 +148,36 @@ pg.default.types.setTypeParser(int8TypeId, (val) => {
|
|
|
147
148
|
});
|
|
148
149
|
});
|
|
149
150
|
(0, vitest.describe)("KyselyFactory.seed", () => {
|
|
150
|
-
itWithTransaction("should execute seed functions", async ({ trx
|
|
151
|
+
itWithTransaction("should execute seed functions", async ({ trx }) => {
|
|
151
152
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
152
153
|
name: "John Doe",
|
|
153
154
|
email: `user${Date.now()}@example.com`,
|
|
154
155
|
createdAt: /* @__PURE__ */ new Date()
|
|
155
156
|
}));
|
|
156
157
|
const builders = { user: userBuilder };
|
|
157
|
-
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) => {
|
|
158
159
|
return await factory$1.insert("user", {
|
|
159
160
|
name: attrs.name || "Admin User",
|
|
160
161
|
email: "admin@example.com"
|
|
161
162
|
});
|
|
162
163
|
}) };
|
|
163
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, seeds, trx
|
|
164
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, seeds, trx);
|
|
164
165
|
const adminUser = await factory.seed("createAdminUser");
|
|
165
166
|
(0, vitest.expect)(adminUser).toBeDefined();
|
|
166
167
|
(0, vitest.expect)(adminUser.name).toBe("Admin User");
|
|
167
168
|
(0, vitest.expect)(adminUser.email).toBe("admin@example.com");
|
|
168
169
|
});
|
|
169
|
-
itWithTransaction("should pass attributes to seed functions", async ({ trx
|
|
170
|
+
itWithTransaction("should pass attributes to seed functions", async ({ trx }) => {
|
|
170
171
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
171
172
|
name: "John Doe",
|
|
172
173
|
email: `user${Date.now()}@example.com`,
|
|
173
174
|
createdAt: /* @__PURE__ */ new Date()
|
|
174
175
|
}));
|
|
175
176
|
const builders = { user: userBuilder };
|
|
176
|
-
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) => {
|
|
177
178
|
return await factory$1.insert("user", attrs);
|
|
178
179
|
}) };
|
|
179
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, seeds, trx
|
|
180
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, seeds, trx);
|
|
180
181
|
const customUser = await factory.seed("createCustomUser", {
|
|
181
182
|
name: "Custom User",
|
|
182
183
|
email: "custom@test.com"
|
|
@@ -184,26 +185,26 @@ pg.default.types.setTypeParser(int8TypeId, (val) => {
|
|
|
184
185
|
(0, vitest.expect)(customUser.name).toBe("Custom User");
|
|
185
186
|
(0, vitest.expect)(customUser.email).toBe("custom@test.com");
|
|
186
187
|
});
|
|
187
|
-
itWithTransaction("should throw error for non-existent seed", async ({ trx
|
|
188
|
-
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx
|
|
188
|
+
itWithTransaction("should throw error for non-existent seed", async ({ trx }) => {
|
|
189
|
+
const factory = new require_KyselyFactory.KyselyFactory({}, {}, trx);
|
|
189
190
|
(0, vitest.expect)(() => factory.seed("nonExistent")).toThrow("Seed \"nonExistent\" is not registered");
|
|
190
191
|
});
|
|
191
192
|
});
|
|
192
193
|
(0, vitest.describe)("Factory integration", () => {
|
|
193
|
-
itWithTransaction("should work with controlled transactions", async ({ trx
|
|
194
|
+
itWithTransaction("should work with controlled transactions", async ({ trx }) => {
|
|
194
195
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
195
196
|
name: "John Doe",
|
|
196
197
|
email: `user${Date.now()}@example.com`,
|
|
197
198
|
createdAt: /* @__PURE__ */ new Date()
|
|
198
199
|
}));
|
|
199
200
|
const builders = { user: userBuilder };
|
|
200
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
201
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
201
202
|
const user = await factory.insert("user");
|
|
202
|
-
const foundUser = await trx
|
|
203
|
+
const foundUser = await trx.selectFrom("users").selectAll().where("id", "=", user.id).executeTakeFirst();
|
|
203
204
|
(0, vitest.expect)(foundUser).toBeDefined();
|
|
204
205
|
(0, vitest.expect)(foundUser?.id).toBe(user.id);
|
|
205
206
|
});
|
|
206
|
-
itWithTransaction("should work with factory passed to defaults", async ({ trx
|
|
207
|
+
itWithTransaction("should work with factory passed to defaults", async ({ trx }) => {
|
|
207
208
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
|
|
208
209
|
name: "John Doe",
|
|
209
210
|
email: `user${Date.now()}@example.com`,
|
|
@@ -222,10 +223,10 @@ pg.default.types.setTypeParser(int8TypeId, (val) => {
|
|
|
222
223
|
user: userBuilder,
|
|
223
224
|
post: postBuilder
|
|
224
225
|
};
|
|
225
|
-
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx
|
|
226
|
+
const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
|
|
226
227
|
const post = await factory.insert("post");
|
|
227
228
|
(0, vitest.expect)(post.userId).toBeDefined();
|
|
228
|
-
const relatedUser = await trx
|
|
229
|
+
const relatedUser = await trx.selectFrom("users").selectAll().where("id", "=", post.userId).executeTakeFirst();
|
|
229
230
|
(0, vitest.expect)(relatedUser).toBeDefined();
|
|
230
231
|
});
|
|
231
232
|
});
|