@geekmidas/testkit 0.0.3 → 0.0.5
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/PostgresKyselyMigrator.spec +471 -0
- package/dist/{KyselyFactory-DiiWtMYe.cjs → KyselyFactory-BX7Kv2uP.cjs} +11 -12
- package/dist/{KyselyFactory-DZewtWtJ.mjs → KyselyFactory-pOMOFQWE.mjs} +11 -12
- package/dist/KyselyFactory.cjs +2 -1
- package/dist/KyselyFactory.mjs +2 -1
- package/dist/ObjectionFactory.cjs +1 -1
- package/dist/ObjectionFactory.mjs +1 -1
- package/dist/{PostgresKyselyMigrator-ChMJpPrQ.mjs → PostgresKyselyMigrator-D8fm35-s.mjs} +1 -1
- package/dist/{PostgresKyselyMigrator-rY3hO_-1.cjs → PostgresKyselyMigrator-JTY2LfwD.cjs} +3 -2
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/{PostgresMigrator-BJ2-5A_b.cjs → PostgresMigrator-Bz-tnjB6.cjs} +2 -39
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/VitestKyselyTransactionIsolator-BS3R-V0I.mjs +12 -0
- package/dist/VitestKyselyTransactionIsolator-DWSTKIe3.cjs +17 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +4 -0
- package/dist/VitestKyselyTransactionIsolator.mjs +4 -0
- package/dist/VitestTransactionIsolator-BjVXqFs6.mjs +40 -0
- package/dist/VitestTransactionIsolator-Bx2c4OzK.cjs +52 -0
- package/dist/VitestTransactionIsolator.cjs +4 -0
- package/dist/VitestTransactionIsolator.mjs +3 -0
- package/dist/__tests__/Factory.spec.cjs +139 -0
- package/dist/__tests__/Factory.spec.mjs +138 -0
- package/dist/__tests__/KyselyFactory.spec.cjs +220 -15008
- package/dist/__tests__/KyselyFactory.spec.mjs +218 -15033
- package/dist/__tests__/ObjectionFactory.spec.cjs +386 -0
- package/dist/__tests__/ObjectionFactory.spec.mjs +385 -0
- package/dist/__tests__/PostgresMigrator.spec.cjs +256 -0
- package/dist/__tests__/PostgresMigrator.spec.mjs +255 -0
- package/dist/__tests__/faker.spec.cjs +115 -0
- package/dist/__tests__/faker.spec.mjs +114 -0
- package/dist/__tests__/integration.spec.cjs +279 -0
- package/dist/__tests__/integration.spec.mjs +278 -0
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/example.cjs +2 -1
- package/dist/example.mjs +2 -1
- package/dist/faker-BwaXA_RF.mjs +85 -0
- package/dist/faker-caz-8zt8.cjs +121 -0
- package/dist/faker.cjs +8 -0
- package/dist/faker.mjs +3 -0
- package/dist/helpers-B9Jdk_C7.cjs +31 -0
- package/dist/helpers-BfuX-cjN.mjs +111 -0
- package/dist/helpers-DKEBHABj.cjs +135 -0
- package/dist/helpers-DOiGIkaU.mjs +19 -0
- package/dist/helpers.cjs +6 -0
- package/dist/helpers.mjs +5 -0
- package/dist/kysely.cjs +15 -4
- package/dist/kysely.mjs +14 -4
- package/dist/objection.cjs +1 -1
- package/dist/objection.mjs +1 -1
- package/package.json +8 -2
- package/src/Factory.ts +3 -1
- package/src/KyselyFactory.ts +30 -36
- package/src/VitestKyselyTransactionIsolator.ts +23 -0
- package/src/VitestTransactionIsolator.ts +70 -0
- package/src/__tests__/Factory.spec.ts +164 -0
- package/src/__tests__/KyselyFactory.spec.ts +432 -64
- package/src/__tests__/ObjectionFactory.spec.ts +532 -0
- package/src/__tests__/PostgresMigrator.spec.ts +366 -0
- package/src/__tests__/faker.spec.ts +142 -0
- package/src/__tests__/integration.spec.ts +442 -0
- package/src/faker.ts +112 -0
- package/src/helpers.ts +28 -0
- package/src/kysely.ts +14 -0
- package/test/globalSetup.ts +41 -40
- package/test/helpers.ts +273 -0
- package/dist/magic-string.es-CxbtJGk_.mjs +0 -1014
- package/dist/magic-string.es-KiPEzMtt.cjs +0 -1015
- /package/dist/{ObjectionFactory-DeFYWbzt.cjs → ObjectionFactory-BlkzSEqo.cjs} +0 -0
- /package/dist/{ObjectionFactory-MAf2m8LI.mjs → ObjectionFactory-ChuX8sZN.mjs} +0 -0
- /package/dist/{PostgresMigrator-BKaNTth5.mjs → PostgresMigrator-CEoRKTdq.mjs} +0 -0
|
@@ -1,30 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __commonJS = (cb, mod) => function() {
|
|
9
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
13
|
-
key = keys[i];
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
15
|
-
get: ((k) => from[k]).bind(null, key),
|
|
16
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
return to;
|
|
20
|
-
};
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
22
|
-
value: mod,
|
|
23
|
-
enumerable: true
|
|
24
|
-
}) : target, mod));
|
|
25
|
-
|
|
26
|
-
//#endregion
|
|
27
|
-
const pg = __toESM(require("pg"));
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const pg = require_chunk.__toESM(require("pg"));
|
|
28
3
|
|
|
29
4
|
//#region src/PostgresMigrator.ts
|
|
30
5
|
async function setupClient(uri) {
|
|
@@ -89,16 +64,4 @@ Object.defineProperty(exports, 'PostgresMigrator', {
|
|
|
89
64
|
get: function () {
|
|
90
65
|
return PostgresMigrator;
|
|
91
66
|
}
|
|
92
|
-
});
|
|
93
|
-
Object.defineProperty(exports, '__commonJS', {
|
|
94
|
-
enumerable: true,
|
|
95
|
-
get: function () {
|
|
96
|
-
return __commonJS;
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
Object.defineProperty(exports, '__toESM', {
|
|
100
|
-
enumerable: true,
|
|
101
|
-
get: function () {
|
|
102
|
-
return __toESM;
|
|
103
|
-
}
|
|
104
67
|
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-BjVXqFs6.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/VitestKyselyTransactionIsolator.ts
|
|
4
|
+
var VitestKyselyTransactionIsolator = class extends VitestPostgresTransactionIsolator {
|
|
5
|
+
async transact(conn, level, fn) {
|
|
6
|
+
const isolationLevel = level.toLocaleLowerCase();
|
|
7
|
+
await conn.transaction().setIsolationLevel(isolationLevel).execute(fn);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { VitestKyselyTransactionIsolator };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-Bx2c4OzK.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/VitestKyselyTransactionIsolator.ts
|
|
4
|
+
var VitestKyselyTransactionIsolator = class extends require_VitestTransactionIsolator.VitestPostgresTransactionIsolator {
|
|
5
|
+
async transact(conn, level, fn) {
|
|
6
|
+
const isolationLevel = level.toLocaleLowerCase();
|
|
7
|
+
await conn.transaction().setIsolationLevel(isolationLevel).execute(fn);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
Object.defineProperty(exports, 'VitestKyselyTransactionIsolator', {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return VitestKyselyTransactionIsolator;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
require('./VitestTransactionIsolator-Bx2c4OzK.cjs');
|
|
2
|
+
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-DWSTKIe3.cjs');
|
|
3
|
+
|
|
4
|
+
exports.VitestKyselyTransactionIsolator = require_VitestKyselyTransactionIsolator.VitestKyselyTransactionIsolator;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { test } from "vitest";
|
|
2
|
+
|
|
3
|
+
//#region src/VitestTransactionIsolator.ts
|
|
4
|
+
let IsolationLevel = /* @__PURE__ */ function(IsolationLevel$1) {
|
|
5
|
+
IsolationLevel$1["READ_UNCOMMITTED"] = "READ UNCOMMITTED";
|
|
6
|
+
IsolationLevel$1["READ_COMMITTED"] = "READ COMMITTED";
|
|
7
|
+
IsolationLevel$1["REPEATABLE_READ"] = "REPEATABLE READ";
|
|
8
|
+
IsolationLevel$1["SERIALIZABLE"] = "SERIALIZABLE";
|
|
9
|
+
return IsolationLevel$1;
|
|
10
|
+
}({});
|
|
11
|
+
var VitestPostgresTransactionIsolator = class {
|
|
12
|
+
wrapVitestWithTransaction(conn, setup, level = IsolationLevel.REPEATABLE_READ) {
|
|
13
|
+
return test.extend({ trx: async ({}, use) => {
|
|
14
|
+
class TestRollback extends Error {
|
|
15
|
+
constructor() {
|
|
16
|
+
super("Test rollback");
|
|
17
|
+
this.name = "TestRollback";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
let testError;
|
|
21
|
+
try {
|
|
22
|
+
await this.transact(conn, level, async (transaction) => {
|
|
23
|
+
try {
|
|
24
|
+
await setup?.(transaction);
|
|
25
|
+
await use(transaction);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
testError = error;
|
|
28
|
+
}
|
|
29
|
+
throw new TestRollback();
|
|
30
|
+
});
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (!(error instanceof TestRollback)) throw error;
|
|
33
|
+
if (testError) throw testError;
|
|
34
|
+
}
|
|
35
|
+
} });
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
export { IsolationLevel, VitestPostgresTransactionIsolator };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const vitest = require_chunk.__toESM(require("vitest"));
|
|
3
|
+
|
|
4
|
+
//#region src/VitestTransactionIsolator.ts
|
|
5
|
+
let IsolationLevel = /* @__PURE__ */ function(IsolationLevel$1) {
|
|
6
|
+
IsolationLevel$1["READ_UNCOMMITTED"] = "READ UNCOMMITTED";
|
|
7
|
+
IsolationLevel$1["READ_COMMITTED"] = "READ COMMITTED";
|
|
8
|
+
IsolationLevel$1["REPEATABLE_READ"] = "REPEATABLE READ";
|
|
9
|
+
IsolationLevel$1["SERIALIZABLE"] = "SERIALIZABLE";
|
|
10
|
+
return IsolationLevel$1;
|
|
11
|
+
}({});
|
|
12
|
+
var VitestPostgresTransactionIsolator = class {
|
|
13
|
+
wrapVitestWithTransaction(conn, setup, level = IsolationLevel.REPEATABLE_READ) {
|
|
14
|
+
return vitest.test.extend({ trx: async ({}, use) => {
|
|
15
|
+
class TestRollback extends Error {
|
|
16
|
+
constructor() {
|
|
17
|
+
super("Test rollback");
|
|
18
|
+
this.name = "TestRollback";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
let testError;
|
|
22
|
+
try {
|
|
23
|
+
await this.transact(conn, level, async (transaction) => {
|
|
24
|
+
try {
|
|
25
|
+
await setup?.(transaction);
|
|
26
|
+
await use(transaction);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
testError = error;
|
|
29
|
+
}
|
|
30
|
+
throw new TestRollback();
|
|
31
|
+
});
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (!(error instanceof TestRollback)) throw error;
|
|
34
|
+
if (testError) throw testError;
|
|
35
|
+
}
|
|
36
|
+
} });
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
//#endregion
|
|
41
|
+
Object.defineProperty(exports, 'IsolationLevel', {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
get: function () {
|
|
44
|
+
return IsolationLevel;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(exports, 'VitestPostgresTransactionIsolator', {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
get: function () {
|
|
50
|
+
return VitestPostgresTransactionIsolator;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-Bx2c4OzK.cjs');
|
|
2
|
+
|
|
3
|
+
exports.IsolationLevel = require_VitestTransactionIsolator.IsolationLevel;
|
|
4
|
+
exports.VitestPostgresTransactionIsolator = require_VitestTransactionIsolator.VitestPostgresTransactionIsolator;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
const require_chunk = require('../chunk-CUT6urMc.cjs');
|
|
2
|
+
const require_Factory = require('../Factory-DREHoms3.cjs');
|
|
3
|
+
const vitest = require_chunk.__toESM(require("vitest"));
|
|
4
|
+
|
|
5
|
+
//#region src/__tests__/Factory.spec.ts
|
|
6
|
+
var TestFactory = class extends require_Factory.Factory {
|
|
7
|
+
async insert(builderName, attrs) {
|
|
8
|
+
return Promise.resolve({
|
|
9
|
+
id: 1,
|
|
10
|
+
...attrs
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
async insertMany(count, builderName, attrs) {
|
|
14
|
+
const results = [];
|
|
15
|
+
for (let i = 0; i < count; i++) {
|
|
16
|
+
const newAttrs = typeof attrs === "function" ? attrs(i) : attrs;
|
|
17
|
+
results.push(await this.insert(builderName, newAttrs));
|
|
18
|
+
}
|
|
19
|
+
return results;
|
|
20
|
+
}
|
|
21
|
+
seed(seedName, attrs) {
|
|
22
|
+
return Promise.resolve({
|
|
23
|
+
seedResult: true,
|
|
24
|
+
...attrs
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
(0, vitest.describe)("Factory", () => {
|
|
29
|
+
(0, vitest.describe)("abstract class functionality", () => {
|
|
30
|
+
(0, vitest.it)("should be instantiable through concrete implementation", () => {
|
|
31
|
+
const factory = new TestFactory();
|
|
32
|
+
(0, vitest.expect)(factory).toBeInstanceOf(require_Factory.Factory);
|
|
33
|
+
(0, vitest.expect)(factory).toBeInstanceOf(TestFactory);
|
|
34
|
+
});
|
|
35
|
+
(0, vitest.it)("should have abstract methods defined", () => {
|
|
36
|
+
const factory = new TestFactory();
|
|
37
|
+
(0, vitest.expect)(typeof factory.insert).toBe("function");
|
|
38
|
+
(0, vitest.expect)(typeof factory.insertMany).toBe("function");
|
|
39
|
+
(0, vitest.expect)(typeof factory.seed).toBe("function");
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
(0, vitest.describe)("createSeed static method", () => {
|
|
43
|
+
(0, vitest.it)("should return the seed function unchanged", () => {
|
|
44
|
+
const seedFn = async (attrs, factory, db) => {
|
|
45
|
+
return {
|
|
46
|
+
id: 1,
|
|
47
|
+
name: "test"
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
const result = require_Factory.Factory.createSeed(seedFn);
|
|
51
|
+
(0, vitest.expect)(result).toBe(seedFn);
|
|
52
|
+
(0, vitest.expect)(typeof result).toBe("function");
|
|
53
|
+
});
|
|
54
|
+
(0, vitest.it)("should work with different seed function signatures", () => {
|
|
55
|
+
const simpleSeed = () => Promise.resolve({ simple: true });
|
|
56
|
+
const complexSeed = async (attrs, factory, db) => {
|
|
57
|
+
return {
|
|
58
|
+
name: attrs.name,
|
|
59
|
+
created: true
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const result1 = require_Factory.Factory.createSeed(simpleSeed);
|
|
63
|
+
const result2 = require_Factory.Factory.createSeed(complexSeed);
|
|
64
|
+
(0, vitest.expect)(result1).toBe(simpleSeed);
|
|
65
|
+
(0, vitest.expect)(result2).toBe(complexSeed);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
(0, vitest.describe)("concrete implementation behavior", () => {
|
|
69
|
+
let factory;
|
|
70
|
+
(0, vitest.beforeEach)(() => {
|
|
71
|
+
factory = new TestFactory();
|
|
72
|
+
});
|
|
73
|
+
(0, vitest.it)("should implement insert method", async () => {
|
|
74
|
+
const result = await factory.insert("testBuilder", { name: "test" });
|
|
75
|
+
(0, vitest.expect)(result).toEqual({
|
|
76
|
+
id: 1,
|
|
77
|
+
name: "test"
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
(0, vitest.it)("should implement insertMany method", async () => {
|
|
81
|
+
const results = await factory.insertMany(3, "testBuilder", { name: "test" });
|
|
82
|
+
(0, vitest.expect)(results).toHaveLength(3);
|
|
83
|
+
(0, vitest.expect)(results[0]).toEqual({
|
|
84
|
+
id: 1,
|
|
85
|
+
name: "test"
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
(0, vitest.it)("should implement insertMany with function attributes", async () => {
|
|
89
|
+
const results = await factory.insertMany(2, "testBuilder", (idx) => ({ name: `test${idx}` }));
|
|
90
|
+
(0, vitest.expect)(results).toHaveLength(2);
|
|
91
|
+
(0, vitest.expect)(results[0]).toEqual({
|
|
92
|
+
id: 1,
|
|
93
|
+
name: "test0"
|
|
94
|
+
});
|
|
95
|
+
(0, vitest.expect)(results[1]).toEqual({
|
|
96
|
+
id: 1,
|
|
97
|
+
name: "test1"
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
(0, vitest.it)("should implement seed method", async () => {
|
|
101
|
+
const result = await factory.seed("testSeed", { custom: "value" });
|
|
102
|
+
(0, vitest.expect)(result).toEqual({
|
|
103
|
+
seedResult: true,
|
|
104
|
+
custom: "value"
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
(0, vitest.describe)("type definitions", () => {
|
|
109
|
+
(0, vitest.it)("should properly type MixedFactoryBuilder", () => {
|
|
110
|
+
const syncBuilder = (attrs, factory, db) => ({ sync: true });
|
|
111
|
+
const asyncBuilder = async (attrs, factory, db) => ({ async: true });
|
|
112
|
+
(0, vitest.expect)(typeof syncBuilder).toBe("function");
|
|
113
|
+
(0, vitest.expect)(typeof asyncBuilder).toBe("function");
|
|
114
|
+
});
|
|
115
|
+
(0, vitest.it)("should properly type FactorySeed", () => {
|
|
116
|
+
const seed = async (attrs, factory, db) => ({ seeded: true });
|
|
117
|
+
(0, vitest.expect)(typeof seed).toBe("function");
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
(0, vitest.describe)("Factory types", () => {
|
|
122
|
+
(0, vitest.it)("should export MixedFactoryBuilder type", () => {
|
|
123
|
+
const builder = (attrs, factory, db) => {
|
|
124
|
+
return {
|
|
125
|
+
id: 1,
|
|
126
|
+
name: attrs.name
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
(0, vitest.expect)(typeof builder).toBe("function");
|
|
130
|
+
});
|
|
131
|
+
(0, vitest.it)("should export FactorySeed type", () => {
|
|
132
|
+
const seed = async (attrs, factory, db) => {
|
|
133
|
+
return { created: attrs.count };
|
|
134
|
+
};
|
|
135
|
+
(0, vitest.expect)(typeof seed).toBe("function");
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
//#endregion
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { Factory } from "../Factory-DlzMkMzb.mjs";
|
|
2
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
//#region src/__tests__/Factory.spec.ts
|
|
5
|
+
var TestFactory = class extends Factory {
|
|
6
|
+
async insert(builderName, attrs) {
|
|
7
|
+
return Promise.resolve({
|
|
8
|
+
id: 1,
|
|
9
|
+
...attrs
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
async insertMany(count, builderName, attrs) {
|
|
13
|
+
const results = [];
|
|
14
|
+
for (let i = 0; i < count; i++) {
|
|
15
|
+
const newAttrs = typeof attrs === "function" ? attrs(i) : attrs;
|
|
16
|
+
results.push(await this.insert(builderName, newAttrs));
|
|
17
|
+
}
|
|
18
|
+
return results;
|
|
19
|
+
}
|
|
20
|
+
seed(seedName, attrs) {
|
|
21
|
+
return Promise.resolve({
|
|
22
|
+
seedResult: true,
|
|
23
|
+
...attrs
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
describe("Factory", () => {
|
|
28
|
+
describe("abstract class functionality", () => {
|
|
29
|
+
it("should be instantiable through concrete implementation", () => {
|
|
30
|
+
const factory = new TestFactory();
|
|
31
|
+
expect(factory).toBeInstanceOf(Factory);
|
|
32
|
+
expect(factory).toBeInstanceOf(TestFactory);
|
|
33
|
+
});
|
|
34
|
+
it("should have abstract methods defined", () => {
|
|
35
|
+
const factory = new TestFactory();
|
|
36
|
+
expect(typeof factory.insert).toBe("function");
|
|
37
|
+
expect(typeof factory.insertMany).toBe("function");
|
|
38
|
+
expect(typeof factory.seed).toBe("function");
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe("createSeed static method", () => {
|
|
42
|
+
it("should return the seed function unchanged", () => {
|
|
43
|
+
const seedFn = async (attrs, factory, db) => {
|
|
44
|
+
return {
|
|
45
|
+
id: 1,
|
|
46
|
+
name: "test"
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
const result = Factory.createSeed(seedFn);
|
|
50
|
+
expect(result).toBe(seedFn);
|
|
51
|
+
expect(typeof result).toBe("function");
|
|
52
|
+
});
|
|
53
|
+
it("should work with different seed function signatures", () => {
|
|
54
|
+
const simpleSeed = () => Promise.resolve({ simple: true });
|
|
55
|
+
const complexSeed = async (attrs, factory, db) => {
|
|
56
|
+
return {
|
|
57
|
+
name: attrs.name,
|
|
58
|
+
created: true
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
const result1 = Factory.createSeed(simpleSeed);
|
|
62
|
+
const result2 = Factory.createSeed(complexSeed);
|
|
63
|
+
expect(result1).toBe(simpleSeed);
|
|
64
|
+
expect(result2).toBe(complexSeed);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
describe("concrete implementation behavior", () => {
|
|
68
|
+
let factory;
|
|
69
|
+
beforeEach(() => {
|
|
70
|
+
factory = new TestFactory();
|
|
71
|
+
});
|
|
72
|
+
it("should implement insert method", async () => {
|
|
73
|
+
const result = await factory.insert("testBuilder", { name: "test" });
|
|
74
|
+
expect(result).toEqual({
|
|
75
|
+
id: 1,
|
|
76
|
+
name: "test"
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
it("should implement insertMany method", async () => {
|
|
80
|
+
const results = await factory.insertMany(3, "testBuilder", { name: "test" });
|
|
81
|
+
expect(results).toHaveLength(3);
|
|
82
|
+
expect(results[0]).toEqual({
|
|
83
|
+
id: 1,
|
|
84
|
+
name: "test"
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
it("should implement insertMany with function attributes", async () => {
|
|
88
|
+
const results = await factory.insertMany(2, "testBuilder", (idx) => ({ name: `test${idx}` }));
|
|
89
|
+
expect(results).toHaveLength(2);
|
|
90
|
+
expect(results[0]).toEqual({
|
|
91
|
+
id: 1,
|
|
92
|
+
name: "test0"
|
|
93
|
+
});
|
|
94
|
+
expect(results[1]).toEqual({
|
|
95
|
+
id: 1,
|
|
96
|
+
name: "test1"
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
it("should implement seed method", async () => {
|
|
100
|
+
const result = await factory.seed("testSeed", { custom: "value" });
|
|
101
|
+
expect(result).toEqual({
|
|
102
|
+
seedResult: true,
|
|
103
|
+
custom: "value"
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
describe("type definitions", () => {
|
|
108
|
+
it("should properly type MixedFactoryBuilder", () => {
|
|
109
|
+
const syncBuilder = (attrs, factory, db) => ({ sync: true });
|
|
110
|
+
const asyncBuilder = async (attrs, factory, db) => ({ async: true });
|
|
111
|
+
expect(typeof syncBuilder).toBe("function");
|
|
112
|
+
expect(typeof asyncBuilder).toBe("function");
|
|
113
|
+
});
|
|
114
|
+
it("should properly type FactorySeed", () => {
|
|
115
|
+
const seed = async (attrs, factory, db) => ({ seeded: true });
|
|
116
|
+
expect(typeof seed).toBe("function");
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
describe("Factory types", () => {
|
|
121
|
+
it("should export MixedFactoryBuilder type", () => {
|
|
122
|
+
const builder = (attrs, factory, db) => {
|
|
123
|
+
return {
|
|
124
|
+
id: 1,
|
|
125
|
+
name: attrs.name
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
expect(typeof builder).toBe("function");
|
|
129
|
+
});
|
|
130
|
+
it("should export FactorySeed type", () => {
|
|
131
|
+
const seed = async (attrs, factory, db) => {
|
|
132
|
+
return { created: attrs.count };
|
|
133
|
+
};
|
|
134
|
+
expect(typeof seed).toBe("function");
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
//#endregion
|