befly 3.9.38 → 3.9.40
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 +37 -38
- package/befly.config.ts +62 -40
- package/checks/checkApi.ts +16 -16
- package/checks/checkApp.ts +19 -25
- package/checks/checkTable.ts +42 -42
- package/docs/README.md +42 -35
- package/docs/{api.md → api/api.md} +223 -231
- package/docs/cipher.md +71 -69
- package/docs/database.md +143 -141
- package/docs/{examples.md → guide/examples.md} +181 -181
- package/docs/guide/quickstart.md +331 -0
- package/docs/hooks/auth.md +38 -0
- package/docs/hooks/cors.md +28 -0
- package/docs/{hook.md → hooks/hook.md} +140 -57
- package/docs/hooks/parser.md +19 -0
- package/docs/hooks/rateLimit.md +47 -0
- package/docs/{redis.md → infra/redis.md} +84 -93
- package/docs/plugins/cipher.md +61 -0
- package/docs/plugins/database.md +128 -0
- package/docs/{plugin.md → plugins/plugin.md} +83 -81
- package/docs/quickstart.md +26 -26
- package/docs/{addon.md → reference/addon.md} +46 -46
- package/docs/{config.md → reference/config.md} +32 -80
- package/docs/{logger.md → reference/logger.md} +52 -52
- package/docs/{sync.md → reference/sync.md} +32 -35
- package/docs/{table.md → reference/table.md} +1 -1
- package/docs/{validator.md → reference/validator.md} +57 -57
- package/hooks/auth.ts +8 -4
- package/hooks/cors.ts +13 -13
- package/hooks/parser.ts +37 -17
- package/hooks/permission.ts +26 -14
- package/hooks/rateLimit.ts +276 -0
- package/hooks/validator.ts +8 -8
- package/lib/asyncContext.ts +43 -0
- package/lib/cacheHelper.ts +212 -77
- package/lib/cacheKeys.ts +38 -0
- package/lib/cipher.ts +30 -30
- package/lib/connect.ts +28 -28
- package/lib/dbHelper.ts +183 -102
- package/lib/jwt.ts +16 -16
- package/lib/logger.ts +610 -19
- package/lib/redisHelper.ts +185 -44
- package/lib/sqlBuilder.ts +90 -91
- package/lib/validator.ts +59 -39
- package/loader/loadApis.ts +48 -44
- package/loader/loadHooks.ts +40 -14
- package/loader/loadPlugins.ts +16 -17
- package/main.ts +57 -47
- package/package.json +47 -45
- package/paths.ts +15 -14
- package/plugins/cache.ts +5 -4
- package/plugins/cipher.ts +3 -3
- package/plugins/config.ts +2 -2
- package/plugins/db.ts +9 -9
- package/plugins/jwt.ts +3 -3
- package/plugins/logger.ts +8 -12
- package/plugins/redis.ts +8 -8
- package/plugins/tool.ts +6 -6
- package/router/api.ts +85 -56
- package/router/static.ts +12 -12
- package/sync/syncAll.ts +12 -12
- package/sync/syncApi.ts +55 -52
- package/sync/syncDb/apply.ts +20 -19
- package/sync/syncDb/constants.ts +25 -23
- package/sync/syncDb/ddl.ts +35 -36
- package/sync/syncDb/helpers.ts +6 -9
- package/sync/syncDb/schema.ts +10 -9
- package/sync/syncDb/sqlite.ts +7 -8
- package/sync/syncDb/table.ts +37 -35
- package/sync/syncDb/tableCreate.ts +21 -20
- package/sync/syncDb/types.ts +23 -20
- package/sync/syncDb/version.ts +10 -10
- package/sync/syncDb.ts +43 -36
- package/sync/syncDev.ts +74 -65
- package/sync/syncMenu.ts +190 -55
- package/tests/api-integration-array-number.test.ts +282 -0
- package/tests/befly-config-env.test.ts +78 -0
- package/tests/cacheHelper.test.ts +135 -104
- package/tests/cacheKeys.test.ts +41 -0
- package/tests/cipher.test.ts +90 -89
- package/tests/dbHelper-advanced.test.ts +140 -134
- package/tests/dbHelper-all-array-types.test.ts +316 -0
- package/tests/dbHelper-array-serialization.test.ts +258 -0
- package/tests/dbHelper-columns.test.ts +56 -55
- package/tests/dbHelper-execute.test.ts +45 -44
- package/tests/dbHelper-joins.test.ts +124 -119
- package/tests/fields-redis-cache.test.ts +29 -27
- package/tests/fields-validate.test.ts +38 -38
- package/tests/getClientIp.test.ts +54 -0
- package/tests/integration.test.ts +69 -67
- package/tests/jwt.test.ts +27 -26
- package/tests/logger.test.ts +267 -34
- package/tests/rateLimit-hook.test.ts +477 -0
- package/tests/redisHelper.test.ts +187 -188
- package/tests/redisKeys.test.ts +6 -73
- package/tests/scanConfig.test.ts +144 -0
- package/tests/sqlBuilder-advanced.test.ts +217 -215
- package/tests/sqlBuilder.test.ts +92 -91
- package/tests/sync-connection.test.ts +29 -29
- package/tests/syncDb-apply.test.ts +97 -96
- package/tests/syncDb-array-number.test.ts +160 -0
- package/tests/syncDb-constants.test.ts +48 -47
- package/tests/syncDb-ddl.test.ts +99 -98
- package/tests/syncDb-helpers.test.ts +29 -28
- package/tests/syncDb-schema.test.ts +61 -60
- package/tests/syncDb-types.test.ts +60 -59
- package/tests/syncMenu-paths.test.ts +68 -0
- package/tests/util.test.ts +42 -41
- package/tests/validator-array-number.test.ts +310 -0
- package/tests/validator-default.test.ts +373 -0
- package/tests/validator.test.ts +271 -266
- package/tsconfig.json +4 -5
- package/types/api.d.ts +7 -12
- package/types/befly.d.ts +60 -13
- package/types/cache.d.ts +8 -4
- package/types/common.d.ts +17 -9
- package/types/context.d.ts +2 -2
- package/types/crypto.d.ts +23 -0
- package/types/database.d.ts +19 -19
- package/types/hook.d.ts +2 -2
- package/types/jwt.d.ts +118 -0
- package/types/logger.d.ts +30 -0
- package/types/plugin.d.ts +4 -4
- package/types/redis.d.ts +7 -3
- package/types/roleApisCache.ts +23 -0
- package/types/sync.d.ts +10 -10
- package/types/table.d.ts +50 -9
- package/types/validate.d.ts +69 -0
- package/utils/addonHelper.ts +90 -0
- package/utils/arrayKeysToCamel.ts +18 -0
- package/utils/calcPerfTime.ts +13 -0
- package/utils/configTypes.ts +3 -0
- package/utils/cors.ts +19 -0
- package/utils/fieldClear.ts +75 -0
- package/utils/genShortId.ts +12 -0
- package/utils/getClientIp.ts +45 -0
- package/utils/keysToCamel.ts +22 -0
- package/utils/keysToSnake.ts +22 -0
- package/utils/modules.ts +98 -0
- package/utils/pickFields.ts +19 -0
- package/utils/process.ts +56 -0
- package/utils/regex.ts +225 -0
- package/utils/response.ts +115 -0
- package/utils/route.ts +23 -0
- package/utils/scanConfig.ts +142 -0
- package/utils/scanFiles.ts +48 -0
- package/.prettierignore +0 -2
- package/.prettierrc +0 -12
- package/docs/1-/345/237/272/346/234/254/344/273/213/347/273/215.md +0 -35
- package/docs/2-/345/210/235/346/255/245/344/275/223/351/252/214.md +0 -64
- package/docs/3-/347/254/254/344/270/200/344/270/252/346/216/245/345/217/243.md +0 -46
- package/docs/4-/346/223/215/344/275/234/346/225/260/346/215/256/345/272/223.md +0 -172
- package/hooks/requestLogger.ts +0 -84
- package/types/index.ts +0 -24
- package/util.ts +0 -283
package/tests/sqlBuilder.test.ts
CHANGED
|
@@ -1,184 +1,185 @@
|
|
|
1
|
-
|
|
2
|
-
import { SqlBuilder } from '../lib/sqlBuilder';
|
|
1
|
+
import { describe, test, expect } from "bun:test";
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import { SqlBuilder } from "../lib/sqlBuilder";
|
|
4
|
+
|
|
5
|
+
describe("SqlBuilder - SELECT 查询", () => {
|
|
6
|
+
test("简单查询", () => {
|
|
6
7
|
const builder = new SqlBuilder();
|
|
7
|
-
const result = builder.select([
|
|
8
|
-
expect(result.sql).toContain(
|
|
8
|
+
const result = builder.select(["id", "name"]).from("users").toSelectSql();
|
|
9
|
+
expect(result.sql).toContain("SELECT `id`, `name` FROM `users`");
|
|
9
10
|
expect(result.params).toEqual([]);
|
|
10
11
|
});
|
|
11
12
|
|
|
12
|
-
test(
|
|
13
|
+
test("查询所有字段", () => {
|
|
13
14
|
const builder = new SqlBuilder();
|
|
14
|
-
const result = builder.select([
|
|
15
|
-
expect(result.sql).toContain(
|
|
15
|
+
const result = builder.select(["*"]).from("users").toSelectSql();
|
|
16
|
+
expect(result.sql).toContain("SELECT * FROM `users`");
|
|
16
17
|
});
|
|
17
18
|
|
|
18
|
-
test(
|
|
19
|
+
test("带 WHERE 条件", () => {
|
|
19
20
|
const builder = new SqlBuilder();
|
|
20
|
-
const result = builder.select([
|
|
21
|
-
expect(result.sql).toContain(
|
|
21
|
+
const result = builder.select(["*"]).from("users").where({ id: 1 }).toSelectSql();
|
|
22
|
+
expect(result.sql).toContain("WHERE `id` = ?");
|
|
22
23
|
expect(result.params).toEqual([1]);
|
|
23
24
|
});
|
|
24
25
|
|
|
25
|
-
test(
|
|
26
|
+
test("多个 WHERE 条件", () => {
|
|
26
27
|
const builder = new SqlBuilder();
|
|
27
|
-
const result = builder.select([
|
|
28
|
-
expect(result.sql).toContain(
|
|
29
|
-
expect(result.sql).toContain(
|
|
30
|
-
expect(result.params).toEqual([1,
|
|
28
|
+
const result = builder.select(["*"]).from("users").where({ id: 1, status: "active" }).toSelectSql();
|
|
29
|
+
expect(result.sql).toContain("WHERE");
|
|
30
|
+
expect(result.sql).toContain("AND");
|
|
31
|
+
expect(result.params).toEqual([1, "active"]);
|
|
31
32
|
});
|
|
32
33
|
|
|
33
|
-
test(
|
|
34
|
+
test("ORDER BY", () => {
|
|
34
35
|
const builder = new SqlBuilder();
|
|
35
|
-
const result = builder.select([
|
|
36
|
-
expect(result.sql).toContain(
|
|
36
|
+
const result = builder.select(["*"]).from("users").orderBy(["created_at#DESC"]).toSelectSql();
|
|
37
|
+
expect(result.sql).toContain("ORDER BY `created_at` DESC");
|
|
37
38
|
});
|
|
38
39
|
|
|
39
|
-
test(
|
|
40
|
+
test("LIMIT 和 OFFSET", () => {
|
|
40
41
|
const builder = new SqlBuilder();
|
|
41
|
-
const result = builder.select([
|
|
42
|
-
expect(result.sql).toContain(
|
|
42
|
+
const result = builder.select(["*"]).from("users").limit(10).offset(20).toSelectSql();
|
|
43
|
+
expect(result.sql).toContain("LIMIT 10 OFFSET 20");
|
|
43
44
|
});
|
|
44
45
|
});
|
|
45
46
|
|
|
46
|
-
describe(
|
|
47
|
-
test(
|
|
47
|
+
describe("SqlBuilder - WHERE 操作符", () => {
|
|
48
|
+
test("$ne 不等于", () => {
|
|
48
49
|
const builder = new SqlBuilder();
|
|
49
50
|
const result = builder
|
|
50
|
-
.select([
|
|
51
|
-
.from(
|
|
52
|
-
.where({ status: { $ne:
|
|
51
|
+
.select(["*"])
|
|
52
|
+
.from("users")
|
|
53
|
+
.where({ status: { $ne: "deleted" } })
|
|
53
54
|
.toSelectSql();
|
|
54
|
-
expect(result.sql).toContain(
|
|
55
|
-
expect(result.params).toEqual([
|
|
55
|
+
expect(result.sql).toContain("`status` != ?");
|
|
56
|
+
expect(result.params).toEqual(["deleted"]);
|
|
56
57
|
});
|
|
57
58
|
|
|
58
|
-
test(
|
|
59
|
+
test("$in 包含", () => {
|
|
59
60
|
const builder = new SqlBuilder();
|
|
60
61
|
const result = builder
|
|
61
|
-
.select([
|
|
62
|
-
.from(
|
|
62
|
+
.select(["*"])
|
|
63
|
+
.from("users")
|
|
63
64
|
.where({ id: { $in: [1, 2, 3] } })
|
|
64
65
|
.toSelectSql();
|
|
65
|
-
expect(result.sql).toContain(
|
|
66
|
+
expect(result.sql).toContain("`id` IN (?,?,?)");
|
|
66
67
|
expect(result.params).toEqual([1, 2, 3]);
|
|
67
68
|
});
|
|
68
69
|
|
|
69
|
-
test(
|
|
70
|
+
test("$gt 大于", () => {
|
|
70
71
|
const builder = new SqlBuilder();
|
|
71
72
|
const result = builder
|
|
72
|
-
.select([
|
|
73
|
-
.from(
|
|
73
|
+
.select(["*"])
|
|
74
|
+
.from("users")
|
|
74
75
|
.where({ age: { $gt: 18 } })
|
|
75
76
|
.toSelectSql();
|
|
76
|
-
expect(result.sql).toContain(
|
|
77
|
+
expect(result.sql).toContain("`age` > ?");
|
|
77
78
|
expect(result.params).toEqual([18]);
|
|
78
79
|
});
|
|
79
80
|
|
|
80
|
-
test(
|
|
81
|
+
test("$gte 大于等于", () => {
|
|
81
82
|
const builder = new SqlBuilder();
|
|
82
83
|
const result = builder
|
|
83
|
-
.select([
|
|
84
|
-
.from(
|
|
84
|
+
.select(["*"])
|
|
85
|
+
.from("users")
|
|
85
86
|
.where({ age: { $gte: 18 } })
|
|
86
87
|
.toSelectSql();
|
|
87
|
-
expect(result.sql).toContain(
|
|
88
|
+
expect(result.sql).toContain("`age` >= ?");
|
|
88
89
|
expect(result.params).toEqual([18]);
|
|
89
90
|
});
|
|
90
91
|
|
|
91
|
-
test(
|
|
92
|
+
test("$lt 小于", () => {
|
|
92
93
|
const builder = new SqlBuilder();
|
|
93
94
|
const result = builder
|
|
94
|
-
.select([
|
|
95
|
-
.from(
|
|
95
|
+
.select(["*"])
|
|
96
|
+
.from("users")
|
|
96
97
|
.where({ age: { $lt: 60 } })
|
|
97
98
|
.toSelectSql();
|
|
98
|
-
expect(result.sql).toContain(
|
|
99
|
+
expect(result.sql).toContain("`age` < ?");
|
|
99
100
|
expect(result.params).toEqual([60]);
|
|
100
101
|
});
|
|
101
102
|
|
|
102
|
-
test(
|
|
103
|
+
test("$lte 小于等于", () => {
|
|
103
104
|
const builder = new SqlBuilder();
|
|
104
105
|
const result = builder
|
|
105
|
-
.select([
|
|
106
|
-
.from(
|
|
106
|
+
.select(["*"])
|
|
107
|
+
.from("users")
|
|
107
108
|
.where({ age: { $lte: 60 } })
|
|
108
109
|
.toSelectSql();
|
|
109
|
-
expect(result.sql).toContain(
|
|
110
|
+
expect(result.sql).toContain("`age` <= ?");
|
|
110
111
|
expect(result.params).toEqual([60]);
|
|
111
112
|
});
|
|
112
113
|
|
|
113
|
-
test(
|
|
114
|
+
test("$like 模糊匹配", () => {
|
|
114
115
|
const builder = new SqlBuilder();
|
|
115
116
|
const result = builder
|
|
116
|
-
.select([
|
|
117
|
-
.from(
|
|
118
|
-
.where({ name: { $like:
|
|
117
|
+
.select(["*"])
|
|
118
|
+
.from("users")
|
|
119
|
+
.where({ name: { $like: "%john%" } })
|
|
119
120
|
.toSelectSql();
|
|
120
|
-
expect(result.sql).toContain(
|
|
121
|
-
expect(result.params).toEqual([
|
|
121
|
+
expect(result.sql).toContain("`name` LIKE ?");
|
|
122
|
+
expect(result.params).toEqual(["%john%"]);
|
|
122
123
|
});
|
|
123
124
|
});
|
|
124
125
|
|
|
125
|
-
describe(
|
|
126
|
-
test(
|
|
126
|
+
describe("SqlBuilder - INSERT", () => {
|
|
127
|
+
test("插入单条数据", () => {
|
|
127
128
|
const builder = new SqlBuilder();
|
|
128
|
-
const result = builder.toInsertSql(
|
|
129
|
-
expect(result.sql).toContain(
|
|
130
|
-
expect(result.sql).toContain(
|
|
131
|
-
expect(result.sql).toContain(
|
|
132
|
-
expect(result.params).toEqual([
|
|
129
|
+
const result = builder.toInsertSql("users", { name: "John", age: 25 });
|
|
130
|
+
expect(result.sql).toContain("INSERT INTO `users`");
|
|
131
|
+
expect(result.sql).toContain("(`name`, `age`)");
|
|
132
|
+
expect(result.sql).toContain("VALUES (?, ?)");
|
|
133
|
+
expect(result.params).toEqual(["John", 25]);
|
|
133
134
|
});
|
|
134
135
|
});
|
|
135
136
|
|
|
136
|
-
describe(
|
|
137
|
-
test(
|
|
137
|
+
describe("SqlBuilder - UPDATE", () => {
|
|
138
|
+
test("更新数据", () => {
|
|
138
139
|
const builder = new SqlBuilder();
|
|
139
|
-
const result = builder.where({ id: 1 }).toUpdateSql(
|
|
140
|
-
expect(result.sql).toContain(
|
|
141
|
-
expect(result.sql).toContain(
|
|
142
|
-
expect(result.sql).toContain(
|
|
143
|
-
expect(result.params).toEqual([
|
|
140
|
+
const result = builder.where({ id: 1 }).toUpdateSql("users", { name: "Jane" });
|
|
141
|
+
expect(result.sql).toContain("UPDATE `users`");
|
|
142
|
+
expect(result.sql).toContain("SET `name` = ?");
|
|
143
|
+
expect(result.sql).toContain("WHERE `id` = ?");
|
|
144
|
+
expect(result.params).toEqual(["Jane", 1]);
|
|
144
145
|
});
|
|
145
146
|
});
|
|
146
147
|
|
|
147
|
-
describe(
|
|
148
|
-
test(
|
|
148
|
+
describe("SqlBuilder - DELETE", () => {
|
|
149
|
+
test("删除数据", () => {
|
|
149
150
|
const builder = new SqlBuilder();
|
|
150
|
-
const result = builder.where({ id: 1 }).toDeleteSql(
|
|
151
|
-
expect(result.sql).toContain(
|
|
152
|
-
expect(result.sql).toContain(
|
|
151
|
+
const result = builder.where({ id: 1 }).toDeleteSql("users");
|
|
152
|
+
expect(result.sql).toContain("DELETE FROM `users`");
|
|
153
|
+
expect(result.sql).toContain("WHERE `id` = ?");
|
|
153
154
|
expect(result.params).toEqual([1]);
|
|
154
155
|
});
|
|
155
156
|
});
|
|
156
157
|
|
|
157
|
-
describe(
|
|
158
|
-
test(
|
|
158
|
+
describe("SqlBuilder - 链式调用", () => {
|
|
159
|
+
test("复杂查询", () => {
|
|
159
160
|
const builder = new SqlBuilder();
|
|
160
161
|
const result = builder
|
|
161
|
-
.select([
|
|
162
|
-
.from(
|
|
163
|
-
.where({ status:
|
|
164
|
-
.orderBy([
|
|
162
|
+
.select(["id", "name", "email"])
|
|
163
|
+
.from("users")
|
|
164
|
+
.where({ status: "active", age: { $gte: 18 } })
|
|
165
|
+
.orderBy(["created_at#DESC"])
|
|
165
166
|
.limit(10)
|
|
166
167
|
.toSelectSql();
|
|
167
168
|
|
|
168
|
-
expect(result.sql).toContain(
|
|
169
|
-
expect(result.sql).toContain(
|
|
170
|
-
expect(result.sql).toContain(
|
|
171
|
-
expect(result.sql).toContain(
|
|
172
|
-
expect(result.sql).toContain(
|
|
169
|
+
expect(result.sql).toContain("SELECT");
|
|
170
|
+
expect(result.sql).toContain("FROM `users`");
|
|
171
|
+
expect(result.sql).toContain("WHERE");
|
|
172
|
+
expect(result.sql).toContain("ORDER BY");
|
|
173
|
+
expect(result.sql).toContain("LIMIT 10");
|
|
173
174
|
expect(result.params.length).toBeGreaterThan(0);
|
|
174
175
|
});
|
|
175
176
|
|
|
176
|
-
test(
|
|
177
|
+
test("reset 重置", () => {
|
|
177
178
|
const builder = new SqlBuilder();
|
|
178
|
-
builder.select([
|
|
179
|
+
builder.select(["*"]).from("users").where({ id: 1 });
|
|
179
180
|
builder.reset();
|
|
180
|
-
const result = builder.select([
|
|
181
|
-
expect(result.sql).toContain(
|
|
182
|
-
expect(result.sql).not.toContain(
|
|
181
|
+
const result = builder.select(["*"]).from("posts").toSelectSql();
|
|
182
|
+
expect(result.sql).toContain("FROM `posts`");
|
|
183
|
+
expect(result.sql).not.toContain("users");
|
|
183
184
|
});
|
|
184
185
|
});
|
|
@@ -2,53 +2,53 @@
|
|
|
2
2
|
* sync 模块连接管理集成测试
|
|
3
3
|
* 验证数据库连接的正确关闭
|
|
4
4
|
*/
|
|
5
|
-
import { describe, test, expect, afterEach } from
|
|
5
|
+
import { describe, test, expect, afterEach } from "bun:test";
|
|
6
6
|
|
|
7
|
-
import { Connect } from
|
|
7
|
+
import { Connect } from "../lib/connect.js";
|
|
8
8
|
|
|
9
|
-
describe(
|
|
9
|
+
describe("sync 模块连接管理", () => {
|
|
10
10
|
afterEach(() => {
|
|
11
11
|
// 每个测试后重置连接状态
|
|
12
12
|
Connect.__reset();
|
|
13
13
|
});
|
|
14
14
|
|
|
15
|
-
describe(
|
|
16
|
-
test(
|
|
15
|
+
describe("Connect.isConnected", () => {
|
|
16
|
+
test("初始状态应该都是未连接", () => {
|
|
17
17
|
const status = Connect.isConnected();
|
|
18
18
|
expect(status.sql).toBe(false);
|
|
19
19
|
expect(status.redis).toBe(false);
|
|
20
20
|
});
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
-
describe(
|
|
24
|
-
test(
|
|
23
|
+
describe("Connect.disconnect", () => {
|
|
24
|
+
test("disconnect 应该能安全地关闭未连接的状态", async () => {
|
|
25
25
|
// 即使没有连接,disconnect 也不应该抛出错误
|
|
26
26
|
await expect(Connect.disconnect()).resolves.toBeUndefined();
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
-
test(
|
|
29
|
+
test("disconnectSql 应该能安全地关闭未连接的状态", async () => {
|
|
30
30
|
await expect(Connect.disconnectSql()).resolves.toBeUndefined();
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
-
test(
|
|
33
|
+
test("disconnectRedis 应该能安全地关闭未连接的状态", async () => {
|
|
34
34
|
await expect(Connect.disconnectRedis()).resolves.toBeUndefined();
|
|
35
35
|
});
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
-
describe(
|
|
39
|
-
test(
|
|
40
|
-
expect(() => Connect.getSql()).toThrow(
|
|
38
|
+
describe("Connect.getSql", () => {
|
|
39
|
+
test("未连接时应该抛出错误", () => {
|
|
40
|
+
expect(() => Connect.getSql()).toThrow("SQL 客户端未连接");
|
|
41
41
|
});
|
|
42
42
|
});
|
|
43
43
|
|
|
44
|
-
describe(
|
|
45
|
-
test(
|
|
46
|
-
expect(() => Connect.getRedis()).toThrow(
|
|
44
|
+
describe("Connect.getRedis", () => {
|
|
45
|
+
test("未连接时应该抛出错误", () => {
|
|
46
|
+
expect(() => Connect.getRedis()).toThrow("Redis 客户端未连接");
|
|
47
47
|
});
|
|
48
48
|
});
|
|
49
49
|
|
|
50
|
-
describe(
|
|
51
|
-
test(
|
|
50
|
+
describe("Mock 连接测试", () => {
|
|
51
|
+
test("__setMockSql 应该设置 mock SQL 客户端", () => {
|
|
52
52
|
const mockSql = { close: async () => {} } as any;
|
|
53
53
|
Connect.__setMockSql(mockSql);
|
|
54
54
|
|
|
@@ -57,7 +57,7 @@ describe('sync 模块连接管理', () => {
|
|
|
57
57
|
expect(status.redis).toBe(false);
|
|
58
58
|
});
|
|
59
59
|
|
|
60
|
-
test(
|
|
60
|
+
test("__setMockRedis 应该设置 mock Redis 客户端", () => {
|
|
61
61
|
const mockRedis = { close: () => {} } as any;
|
|
62
62
|
Connect.__setMockRedis(mockRedis);
|
|
63
63
|
|
|
@@ -66,7 +66,7 @@ describe('sync 模块连接管理', () => {
|
|
|
66
66
|
expect(status.redis).toBe(true);
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
-
test(
|
|
69
|
+
test("__reset 应该重置所有连接", () => {
|
|
70
70
|
const mockSql = { close: async () => {} } as any;
|
|
71
71
|
const mockRedis = { close: () => {} } as any;
|
|
72
72
|
Connect.__setMockSql(mockSql);
|
|
@@ -81,7 +81,7 @@ describe('sync 模块连接管理', () => {
|
|
|
81
81
|
expect(Connect.isConnected().redis).toBe(false);
|
|
82
82
|
});
|
|
83
83
|
|
|
84
|
-
test(
|
|
84
|
+
test("disconnect 应该正确关闭 mock 连接", async () => {
|
|
85
85
|
let sqlClosed = false;
|
|
86
86
|
let redisClosed = false;
|
|
87
87
|
|
|
@@ -108,11 +108,11 @@ describe('sync 模块连接管理', () => {
|
|
|
108
108
|
});
|
|
109
109
|
});
|
|
110
110
|
|
|
111
|
-
describe(
|
|
112
|
-
test(
|
|
111
|
+
describe("连接异常处理", () => {
|
|
112
|
+
test("disconnectSql 应该处理关闭时的错误", async () => {
|
|
113
113
|
const mockSql = {
|
|
114
114
|
close: async () => {
|
|
115
|
-
throw new Error(
|
|
115
|
+
throw new Error("Close error");
|
|
116
116
|
}
|
|
117
117
|
} as any;
|
|
118
118
|
|
|
@@ -123,10 +123,10 @@ describe('sync 模块连接管理', () => {
|
|
|
123
123
|
expect(Connect.isConnected().sql).toBe(false);
|
|
124
124
|
});
|
|
125
125
|
|
|
126
|
-
test(
|
|
126
|
+
test("disconnectRedis 应该处理关闭时的错误", async () => {
|
|
127
127
|
const mockRedis = {
|
|
128
128
|
close: () => {
|
|
129
|
-
throw new Error(
|
|
129
|
+
throw new Error("Close error");
|
|
130
130
|
}
|
|
131
131
|
} as any;
|
|
132
132
|
|
|
@@ -138,8 +138,8 @@ describe('sync 模块连接管理', () => {
|
|
|
138
138
|
});
|
|
139
139
|
});
|
|
140
140
|
|
|
141
|
-
describe(
|
|
142
|
-
test(
|
|
141
|
+
describe("Connect.getStatus", () => {
|
|
142
|
+
test("未连接时返回正确的状态", () => {
|
|
143
143
|
const status = Connect.getStatus();
|
|
144
144
|
|
|
145
145
|
expect(status.sql.connected).toBe(false);
|
|
@@ -152,7 +152,7 @@ describe('sync 模块连接管理', () => {
|
|
|
152
152
|
expect(status.redis.uptime).toBeNull();
|
|
153
153
|
});
|
|
154
154
|
|
|
155
|
-
test(
|
|
155
|
+
test("Mock 连接后返回正确的状态", () => {
|
|
156
156
|
const mockSql = { close: async () => {} } as any;
|
|
157
157
|
const mockRedis = { close: () => {} } as any;
|
|
158
158
|
|
|
@@ -166,7 +166,7 @@ describe('sync 模块连接管理', () => {
|
|
|
166
166
|
expect(status.redis.connected).toBe(true);
|
|
167
167
|
});
|
|
168
168
|
|
|
169
|
-
test(
|
|
169
|
+
test("__reset 应该重置所有状态包括连接时间", () => {
|
|
170
170
|
const mockSql = { close: async () => {} } as any;
|
|
171
171
|
Connect.__setMockSql(mockSql);
|
|
172
172
|
|