vona-module-test-vona 5.0.38 → 5.0.41
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/.metadata/index.d.ts +22 -1
- package/dist/bean/cacheRedis.post.d.ts +7 -0
- package/dist/index.js +499 -268
- package/dist/service/order.d.ts +4 -0
- package/dist/service/post.d.ts +20 -0
- package/package.json +4 -2
- package/src/.metadata/index.ts +1741 -0
- package/src/.metadata/this.ts +2 -0
- package/src/bean/aop.regExp.ts +30 -0
- package/src/bean/aop.simple.ts +58 -0
- package/src/bean/aopMethod.test.ts +42 -0
- package/src/bean/bean.testCtx.ts +55 -0
- package/src/bean/broadcast.test.ts +22 -0
- package/src/bean/cacheMem.test.ts +7 -0
- package/src/bean/cacheRedis.post.ts +10 -0
- package/src/bean/cacheRedis.test.ts +7 -0
- package/src/bean/event.helloEcho.ts +8 -0
- package/src/bean/eventListener.helloEcho.ts +21 -0
- package/src/bean/meta.version.ts +81 -0
- package/src/bean/queue.test.ts +18 -0
- package/src/bean/schedule.test.ts +12 -0
- package/src/bean/schedule.test3.ts +12 -0
- package/src/bean/summerCache.test.ts +29 -0
- package/src/config/config.ts +5 -0
- package/src/config/locale/en-us.ts +19 -0
- package/src/config/locale/zh-cn.ts +17 -0
- package/src/controller/bean.ts +95 -0
- package/src/controller/cacheMem.ts +63 -0
- package/src/controller/cacheRedis.ts +55 -0
- package/src/controller/dtoTest.ts +63 -0
- package/src/controller/guardPassport.ts +25 -0
- package/src/controller/onion.ts +92 -0
- package/src/controller/passport.ts +48 -0
- package/src/controller/performAction.ts +18 -0
- package/src/controller/queue.ts +25 -0
- package/src/controller/summer.ts +125 -0
- package/src/controller/tail.ts +32 -0
- package/src/controller/transaction.ts +29 -0
- package/src/controller/upload.ts +57 -0
- package/src/dto/categoryTree.ts +8 -0
- package/src/dto/orderCreate.ts +10 -0
- package/src/dto/orderResult.ts +9 -0
- package/src/dto/orderUpdate.ts +9 -0
- package/src/dto/postCreate.ts +9 -0
- package/src/dto/profile.ts +14 -0
- package/src/dto/roleLazy.ts +15 -0
- package/src/dto/user.ts +19 -0
- package/src/dto/userCreate.ts +8 -0
- package/src/dto/userLazy.ts +18 -0
- package/src/dto/userUpdate.ts +8 -0
- package/src/entity/category.ts +14 -0
- package/src/entity/order.ts +18 -0
- package/src/entity/post.ts +17 -0
- package/src/entity/postContent.ts +14 -0
- package/src/entity/product.ts +24 -0
- package/src/entity/role.ts +11 -0
- package/src/entity/roleUser.ts +14 -0
- package/src/entity/test.ts +18 -0
- package/src/entity/user.ts +17 -0
- package/src/index.ts +1 -0
- package/src/model/category.ts +17 -0
- package/src/model/categoryChain.ts +16 -0
- package/src/model/order.ts +26 -0
- package/src/model/orderStats.ts +27 -0
- package/src/model/post.ts +16 -0
- package/src/model/postContent.ts +17 -0
- package/src/model/product.ts +8 -0
- package/src/model/role.ts +15 -0
- package/src/model/roleUser.ts +8 -0
- package/src/model/test.ts +8 -0
- package/src/model/testDynamicTable.ts +21 -0
- package/src/model/user.ts +21 -0
- package/src/model/userStats.ts +15 -0
- package/src/model/userStatsGroup.ts +15 -0
- package/src/service/aopMethod.ts +38 -0
- package/src/service/caching.ts +75 -0
- package/src/service/order.ts +65 -0
- package/src/service/post.ts +219 -0
- package/src/service/test.ts +9 -0
- package/src/service/testApp.ts +13 -0
- package/src/service/testClass.ts +15 -0
- package/src/service/testData.ts +67 -0
- package/src/service/transaction.ts +20 -0
- package/test/aopMethod.test.ts +21 -0
- package/test/authSimple.test.ts +45 -0
- package/test/bean.test.ts +16 -0
- package/test/broadcast.test.ts +17 -0
- package/test/cache/cacheMem.test.ts +10 -0
- package/test/cache/cacheRedis.test.ts +10 -0
- package/test/cache/caching.test.ts +58 -0
- package/test/cache/summer.test.ts +10 -0
- package/test/database/database.test.ts +127 -0
- package/test/database/dtoAggregate.test.ts +44 -0
- package/test/database/dtoGet.test.ts +61 -0
- package/test/database/dtoGroup.test.ts +44 -0
- package/test/database/dtoLazy.test.ts +45 -0
- package/test/database/dtoMutate.test.ts +56 -0
- package/test/database/modelAggregate.test.ts +197 -0
- package/test/database/modelCache.test.ts +37 -0
- package/test/database/modelGeneral.test.ts +18 -0
- package/test/database/modelGroup.test.ts +211 -0
- package/test/database/modelRelations.test.ts +203 -0
- package/test/database/modelRelationsJoins.test.ts +39 -0
- package/test/database/modelRelationsMutate.test.ts +354 -0
- package/test/database/modelWhere.test.ts +138 -0
- package/test/database/orm.test.ts +22 -0
- package/test/database/transaction.test.ts +142 -0
- package/test/event.test.ts +14 -0
- package/test/guardPassport.test.ts +44 -0
- package/test/jwt.test.ts +27 -0
- package/test/locale.test.ts +51 -0
- package/test/passport.test.ts +57 -0
- package/test/queue.test.ts +11 -0
- package/test/tail.test.ts +21 -0
- package/test/upload.test.ts +58 -0
- package/test/utils/cabloyUtils.test.ts +22 -0
- package/test/utils/celjs.test.ts +26 -0
- package/test/utils/mappedTypes.test.ts +65 -0
- package/test/utils/performAction.test.ts +28 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { app } from 'vona-mock';
|
|
4
|
+
import { $relationMutate } from 'vona-module-a-orm';
|
|
5
|
+
import { ModelPost, ModelRole, ModelRoleUser } from 'vona-module-test-vona';
|
|
6
|
+
|
|
7
|
+
describe('modelRelationsMutate.test.ts', () => {
|
|
8
|
+
it('action:modelRelationsMutate', async () => {
|
|
9
|
+
await app.bean.executor.mockCtx(async () => {
|
|
10
|
+
const prefix = 'action:modelRelationsMutate';
|
|
11
|
+
// scope
|
|
12
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
13
|
+
// insert: roles
|
|
14
|
+
const roles = await scopeTest.model.role.insertBulk([
|
|
15
|
+
{ name: `${prefix}:family` },
|
|
16
|
+
{ name: `${prefix}:friend` },
|
|
17
|
+
]);
|
|
18
|
+
assert.equal(roles.length, 2);
|
|
19
|
+
assert.equal(roles[0].id !== undefined, true);
|
|
20
|
+
// insert: users
|
|
21
|
+
const users = await scopeTest.model.user.insertBulk([
|
|
22
|
+
{
|
|
23
|
+
name: `${prefix}:tom`,
|
|
24
|
+
posts: [{
|
|
25
|
+
title: `${prefix}:postApple`,
|
|
26
|
+
postContent: {
|
|
27
|
+
content: `${prefix}:postContentApple`,
|
|
28
|
+
},
|
|
29
|
+
}],
|
|
30
|
+
roles: [{
|
|
31
|
+
id: roles[0].id,
|
|
32
|
+
}],
|
|
33
|
+
},
|
|
34
|
+
], {
|
|
35
|
+
include: {
|
|
36
|
+
posts: { include: { postContent: true } },
|
|
37
|
+
roles: true,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
assert.equal(users.length, 1);
|
|
41
|
+
// check
|
|
42
|
+
const post = await scopeTest.model.post.get({ id: users[0].posts[0].id }, { include: { postContent: true } });
|
|
43
|
+
assert.equal(post?.postContent?.content, `${prefix}:postContentApple`);
|
|
44
|
+
// update: users
|
|
45
|
+
const _usersUpdate = await scopeTest.model.user.update({
|
|
46
|
+
id: users[0].id,
|
|
47
|
+
posts: [
|
|
48
|
+
// update
|
|
49
|
+
{
|
|
50
|
+
id: users[0].posts[0].id,
|
|
51
|
+
title: `${prefix}:postApple-update`,
|
|
52
|
+
// update
|
|
53
|
+
postContent: {
|
|
54
|
+
content: `${prefix}:postContentApple-update`,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
// insert
|
|
58
|
+
{
|
|
59
|
+
title: `${prefix}:postPear`,
|
|
60
|
+
postContent: {
|
|
61
|
+
content: `${prefix}:postContentPear`,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
roles: [
|
|
66
|
+
// delete
|
|
67
|
+
{ id: users[0].roles[0].id, deleted: true },
|
|
68
|
+
// insert
|
|
69
|
+
{ id: roles[1].id },
|
|
70
|
+
],
|
|
71
|
+
}, { include: {
|
|
72
|
+
posts: { include: { postContent: true } },
|
|
73
|
+
roles: true,
|
|
74
|
+
} });
|
|
75
|
+
// check
|
|
76
|
+
const usersUpdateCheck = await scopeTest.model.user.get({
|
|
77
|
+
id: users[0].id,
|
|
78
|
+
}, { include: {
|
|
79
|
+
posts: {
|
|
80
|
+
include: { postContent: true },
|
|
81
|
+
orders: [['id', 'asc']],
|
|
82
|
+
},
|
|
83
|
+
roles: true,
|
|
84
|
+
} });
|
|
85
|
+
assert.equal(usersUpdateCheck?.posts.length, 2);
|
|
86
|
+
assert.equal(usersUpdateCheck?.posts[0].title, `${prefix}:postApple-update`);
|
|
87
|
+
assert.equal(usersUpdateCheck?.posts[0].postContent?.content, `${prefix}:postContentApple-update`);
|
|
88
|
+
assert.equal(usersUpdateCheck?.posts[1].postContent?.content, `${prefix}:postContentPear`);
|
|
89
|
+
assert.equal(usersUpdateCheck?.roles.length, 1);
|
|
90
|
+
assert.equal(usersUpdateCheck?.roles[0].id, roles[1].id);
|
|
91
|
+
// mutate: users
|
|
92
|
+
const _usersMutate = await scopeTest.model.user.mutate({
|
|
93
|
+
id: users[0].id,
|
|
94
|
+
posts: [
|
|
95
|
+
// update
|
|
96
|
+
{
|
|
97
|
+
id: users[0].posts[0].id,
|
|
98
|
+
title: `${prefix}:postApple-mutate`,
|
|
99
|
+
// update
|
|
100
|
+
postContent: {
|
|
101
|
+
id: users[0].posts[0].postContent?.id,
|
|
102
|
+
content: `${prefix}:postContentApple-mutate`,
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
roles: [
|
|
107
|
+
// delete
|
|
108
|
+
{ id: roles[1].id, deleted: true },
|
|
109
|
+
],
|
|
110
|
+
}, {
|
|
111
|
+
include: {
|
|
112
|
+
posts: { include: { postContent: true } },
|
|
113
|
+
roles: true,
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
// check
|
|
117
|
+
const usersMutateCheck = await scopeTest.model.user.get({
|
|
118
|
+
id: users[0].id,
|
|
119
|
+
}, { include: {
|
|
120
|
+
posts: {
|
|
121
|
+
include: { postContent: true },
|
|
122
|
+
orders: [['id', 'asc']],
|
|
123
|
+
},
|
|
124
|
+
roles: { columns: '*' as any },
|
|
125
|
+
} });
|
|
126
|
+
assert.equal(usersMutateCheck?.posts.length, 2);
|
|
127
|
+
assert.equal(usersMutateCheck?.posts[0].title, `${prefix}:postApple-mutate`);
|
|
128
|
+
assert.equal(usersMutateCheck?.posts[0].postContent?.content, `${prefix}:postContentApple-mutate`);
|
|
129
|
+
assert.equal(usersMutateCheck?.posts[1].postContent?.content, `${prefix}:postContentPear`);
|
|
130
|
+
assert.equal(usersMutateCheck?.roles.length, 0);
|
|
131
|
+
// delete: users
|
|
132
|
+
await scopeTest.model.user.deleteBulk(users.map(item => item.id), {
|
|
133
|
+
include: {
|
|
134
|
+
posts: { include: { postContent: true } },
|
|
135
|
+
roles: true,
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
const roleUsers = await scopeTest.model.roleUser.select({ where: { userId: users.map(item => item.id) } });
|
|
139
|
+
assert.equal(roleUsers.length, 0);
|
|
140
|
+
// delete: roles
|
|
141
|
+
await scopeTest.model.role.deleteBulk(roles.map(item => item.id));
|
|
142
|
+
const roles2 = await scopeTest.model.role.select({
|
|
143
|
+
where: {
|
|
144
|
+
id: roles.map(item => item.id!),
|
|
145
|
+
},
|
|
146
|
+
}, { disableDeleted: true });
|
|
147
|
+
assert.equal(roles2.length, 2);
|
|
148
|
+
assert.equal(roles2[0].id !== undefined, true);
|
|
149
|
+
assert.equal(roles2[0].deleted, true);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('action:modelRelationsMutateWith', async () => {
|
|
154
|
+
await app.bean.executor.mockCtx(async () => {
|
|
155
|
+
const prefix = 'action:modelRelationsMutateWith';
|
|
156
|
+
// scope
|
|
157
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
158
|
+
// insert: roles
|
|
159
|
+
const roles = await scopeTest.model.role.insertBulk([
|
|
160
|
+
{ name: `${prefix}:family` },
|
|
161
|
+
{ name: `${prefix}:friend` },
|
|
162
|
+
]);
|
|
163
|
+
assert.equal(roles.length, 2);
|
|
164
|
+
assert.equal(roles[0].id !== undefined, true);
|
|
165
|
+
// insert: users
|
|
166
|
+
const users = await scopeTest.model.user.insertBulk([
|
|
167
|
+
{
|
|
168
|
+
name: `${prefix}:tom`,
|
|
169
|
+
posts: [{
|
|
170
|
+
title: `${prefix}:postApple`,
|
|
171
|
+
postContent: {
|
|
172
|
+
content: `${prefix}:postContentApple`,
|
|
173
|
+
},
|
|
174
|
+
}],
|
|
175
|
+
roles: [{
|
|
176
|
+
id: roles[0].id,
|
|
177
|
+
}],
|
|
178
|
+
},
|
|
179
|
+
], {
|
|
180
|
+
with: {
|
|
181
|
+
posts: $relationMutate.hasMany(ModelPost, 'userId', { include: { postContent: true } }),
|
|
182
|
+
roles: $relationMutate.belongsToMany(ModelRoleUser, ModelRole, 'userId', 'roleId'),
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
assert.equal(users.length, 1);
|
|
186
|
+
// check
|
|
187
|
+
const post = await scopeTest.model.post.get({ id: users[0].posts[0].id }, { include: { postContent: true } });
|
|
188
|
+
assert.equal(post?.postContent?.content, `${prefix}:postContentApple`);
|
|
189
|
+
// update: users
|
|
190
|
+
const _usersUpdate = await scopeTest.model.user.update({
|
|
191
|
+
id: users[0].id,
|
|
192
|
+
posts: [
|
|
193
|
+
// update
|
|
194
|
+
{
|
|
195
|
+
id: users[0].posts[0].id,
|
|
196
|
+
title: `${prefix}:postApple-update`,
|
|
197
|
+
// update
|
|
198
|
+
postContent: {
|
|
199
|
+
content: `${prefix}:postContentApple-update`,
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
// insert
|
|
203
|
+
{
|
|
204
|
+
title: `${prefix}:postPear`,
|
|
205
|
+
postContent: {
|
|
206
|
+
content: `${prefix}:postContentPear`,
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
roles: [
|
|
211
|
+
// delete
|
|
212
|
+
{ id: users[0].roles[0].id, deleted: true },
|
|
213
|
+
// insert
|
|
214
|
+
{ id: roles[1].id },
|
|
215
|
+
],
|
|
216
|
+
}, {
|
|
217
|
+
with: {
|
|
218
|
+
posts: $relationMutate.hasMany(ModelPost, 'userId', { include: { postContent: true } }),
|
|
219
|
+
roles: $relationMutate.belongsToMany(ModelRoleUser, ModelRole, 'userId', 'roleId'),
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
// check
|
|
223
|
+
const usersUpdateCheck = await scopeTest.model.user.get({
|
|
224
|
+
id: users[0].id,
|
|
225
|
+
}, { include: {
|
|
226
|
+
posts: {
|
|
227
|
+
include: { postContent: true },
|
|
228
|
+
orders: [['id', 'asc']],
|
|
229
|
+
},
|
|
230
|
+
roles: true,
|
|
231
|
+
} });
|
|
232
|
+
assert.equal(usersUpdateCheck?.posts.length, 2);
|
|
233
|
+
assert.equal(usersUpdateCheck?.posts[0].title, `${prefix}:postApple-update`);
|
|
234
|
+
assert.equal(usersUpdateCheck?.posts[0].postContent?.content, `${prefix}:postContentApple-update`);
|
|
235
|
+
assert.equal(usersUpdateCheck?.posts[1].postContent?.content, `${prefix}:postContentPear`);
|
|
236
|
+
assert.equal(usersUpdateCheck?.roles.length, 1);
|
|
237
|
+
assert.equal(usersUpdateCheck?.roles[0].id, roles[1].id);
|
|
238
|
+
// mutate: users
|
|
239
|
+
const _usersMutate = await scopeTest.model.user.mutate({
|
|
240
|
+
id: users[0].id,
|
|
241
|
+
posts: [
|
|
242
|
+
// update
|
|
243
|
+
{
|
|
244
|
+
id: users[0].posts[0].id,
|
|
245
|
+
title: `${prefix}:postApple-mutate`,
|
|
246
|
+
// update
|
|
247
|
+
postContent: {
|
|
248
|
+
id: users[0].posts[0].postContent?.id,
|
|
249
|
+
content: `${prefix}:postContentApple-mutate`,
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
],
|
|
253
|
+
roles: [
|
|
254
|
+
// delete
|
|
255
|
+
{ id: roles[1].id, deleted: true },
|
|
256
|
+
],
|
|
257
|
+
}, {
|
|
258
|
+
with: {
|
|
259
|
+
posts: $relationMutate.hasMany(ModelPost, 'userId', { include: { postContent: true } }),
|
|
260
|
+
roles: $relationMutate.belongsToMany(ModelRoleUser, ModelRole, 'userId', 'roleId'),
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
// check
|
|
264
|
+
const usersMutateCheck = await scopeTest.model.user.get({
|
|
265
|
+
id: users[0].id,
|
|
266
|
+
}, { include: {
|
|
267
|
+
posts: {
|
|
268
|
+
include: { postContent: true },
|
|
269
|
+
orders: [['id', 'asc']],
|
|
270
|
+
},
|
|
271
|
+
roles: { columns: '*' as any },
|
|
272
|
+
} });
|
|
273
|
+
assert.equal(usersMutateCheck?.posts.length, 2);
|
|
274
|
+
assert.equal(usersMutateCheck?.posts[0].title, `${prefix}:postApple-mutate`);
|
|
275
|
+
assert.equal(usersMutateCheck?.posts[0].postContent?.content, `${prefix}:postContentApple-mutate`);
|
|
276
|
+
assert.equal(usersMutateCheck?.posts[1].postContent?.content, `${prefix}:postContentPear`);
|
|
277
|
+
assert.equal(usersMutateCheck?.roles.length, 0);
|
|
278
|
+
// delete: users
|
|
279
|
+
await scopeTest.model.user.deleteBulk(users.map(item => item.id), {
|
|
280
|
+
with: {
|
|
281
|
+
posts: $relationMutate.hasMany(ModelPost, 'userId', { include: { postContent: true } }),
|
|
282
|
+
roles: $relationMutate.belongsToMany(ModelRoleUser, ModelRole, 'userId', 'roleId'),
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
const roleUsers = await scopeTest.model.roleUser.select({ where: { userId: users.map(item => item.id) } });
|
|
286
|
+
assert.equal(roleUsers.length, 0);
|
|
287
|
+
// delete: roles
|
|
288
|
+
await scopeTest.model.role.deleteBulk(roles.map(item => item.id));
|
|
289
|
+
const roles2 = await scopeTest.model.role.select({
|
|
290
|
+
where: {
|
|
291
|
+
id: roles.map(item => item.id!),
|
|
292
|
+
},
|
|
293
|
+
}, { disableDeleted: true });
|
|
294
|
+
assert.equal(roles2.length, 2);
|
|
295
|
+
assert.equal(roles2[0].id !== undefined, true);
|
|
296
|
+
assert.equal(roles2[0].deleted, true);
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('action:modelRelationsMutateTree', async () => {
|
|
301
|
+
await app.bean.executor.mockCtx(async () => {
|
|
302
|
+
const prefix = 'action:modelRelationsMutateTree';
|
|
303
|
+
// scope
|
|
304
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
305
|
+
// create: categoryTree
|
|
306
|
+
const categoryTree = await scopeTest.model.category.insert({
|
|
307
|
+
name: `${prefix}:1`,
|
|
308
|
+
children: [
|
|
309
|
+
{
|
|
310
|
+
name: `${prefix}:1-1`,
|
|
311
|
+
children: [
|
|
312
|
+
{ name: `${prefix}:1-1-1` },
|
|
313
|
+
{ name: `${prefix}:1-1-2` },
|
|
314
|
+
],
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
name: `${prefix}:1-2`,
|
|
318
|
+
},
|
|
319
|
+
],
|
|
320
|
+
});
|
|
321
|
+
const categoryTreeCheck = await scopeTest.model.category.get({ id: categoryTree.id });
|
|
322
|
+
assert.equal(categoryTree.id, categoryTreeCheck?.id);
|
|
323
|
+
const children = categoryTree.children.sort((a, b) => Number(a.id) - Number(b.id));
|
|
324
|
+
const childrenCheck = categoryTreeCheck!.children.sort((a, b) => Number(a.id) - Number(b.id));
|
|
325
|
+
assert.equal(children[0].name, childrenCheck[0].name);
|
|
326
|
+
assert.equal(children[0].name, `${prefix}:1-1`);
|
|
327
|
+
assert.equal(children[0].children?.length, childrenCheck[0].children.length);
|
|
328
|
+
assert.equal(children[0].children?.length, 2);
|
|
329
|
+
// get: categoryChain
|
|
330
|
+
const categoryChain = await scopeTest.model.categoryChain.get({
|
|
331
|
+
id: children[0].children![0].id,
|
|
332
|
+
});
|
|
333
|
+
assert.equal(categoryChain?.parent?.id, children[0].id);
|
|
334
|
+
assert.equal(categoryChain?.parent?.parent?.id, categoryTree.id);
|
|
335
|
+
assert.equal(categoryChain?.parent?.parent?.parent, undefined);
|
|
336
|
+
// delete: categoryTree
|
|
337
|
+
const items = await scopeTest.model.category.select({
|
|
338
|
+
where: {
|
|
339
|
+
name: { _startsWith_: `${prefix}:` },
|
|
340
|
+
},
|
|
341
|
+
include: { children: false },
|
|
342
|
+
});
|
|
343
|
+
assert.equal(items.length, 5);
|
|
344
|
+
await scopeTest.model.category.delete({ id: categoryTree.id }, { include: { children: { meta: { client: '_auto_' } } } });
|
|
345
|
+
const items2 = await scopeTest.model.category.select({
|
|
346
|
+
where: {
|
|
347
|
+
name: { _startsWith_: `${prefix}:` },
|
|
348
|
+
},
|
|
349
|
+
include: { children: false },
|
|
350
|
+
});
|
|
351
|
+
assert.equal(items2.length, 0);
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import { describe, it } from 'node:test';
|
|
4
|
+
import { app } from 'vona-mock';
|
|
5
|
+
|
|
6
|
+
describe('modelWhere.test.ts', () => {
|
|
7
|
+
it('action:modelWhere', async () => {
|
|
8
|
+
await app.bean.executor.mockCtx(async () => {
|
|
9
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
10
|
+
// ref
|
|
11
|
+
let builder = scopeTest.model.post.builder();
|
|
12
|
+
scopeTest.model.post.buildWhere(builder, { id: scopeTest.model.post.ref('id') as any });
|
|
13
|
+
let sql = builder.toQuery();
|
|
14
|
+
assert.equal(sql, 'select * from "testVonaPost" where "id" = "id"');
|
|
15
|
+
builder = scopeTest.model.post.builder();
|
|
16
|
+
scopeTest.model.post.buildWhere(builder, { id: scopeTest.model.post.ref('testVonaPost.id') as any });
|
|
17
|
+
sql = builder.toQuery();
|
|
18
|
+
assert.equal(sql, 'select * from "testVonaPost" where "id" = "testVonaPost"."id"');
|
|
19
|
+
// raw
|
|
20
|
+
builder = scopeTest.model.post.builder();
|
|
21
|
+
scopeTest.model.post.buildWhere(builder, scopeTest.model.post.raw('?? = ?', ['id', 1]) as any);
|
|
22
|
+
sql = builder.toQuery();
|
|
23
|
+
assert.equal(sql, 'select * from "testVonaPost" where "id" = 1');
|
|
24
|
+
// op: normal
|
|
25
|
+
builder = scopeTest.model.post.builder();
|
|
26
|
+
scopeTest.model.post.buildWhere(builder, { id: 1 });
|
|
27
|
+
sql = builder.toQuery();
|
|
28
|
+
assert.equal(sql, 'select * from "testVonaPost" where "id" = 1');
|
|
29
|
+
// op: normal: joint
|
|
30
|
+
builder = scopeTest.model.post.builder();
|
|
31
|
+
scopeTest.model.post.buildWhere(builder, { id: { _or_: { _eq_: 3, _gt_: 4 } } });
|
|
32
|
+
sql = builder.toQuery();
|
|
33
|
+
assert.equal(sql, 'select * from "testVonaPost" where ((("id" = 3) or ("id" > 4)))');
|
|
34
|
+
// op: and
|
|
35
|
+
builder = scopeTest.model.post.builder();
|
|
36
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
37
|
+
_and_: { iid: 1, id: 2 },
|
|
38
|
+
});
|
|
39
|
+
sql = builder.toQuery();
|
|
40
|
+
assert.equal(sql, 'select * from "testVonaPost" where (("iid" = 1) and ("id" = 2))');
|
|
41
|
+
// op: and: empty
|
|
42
|
+
builder = scopeTest.model.post.builder();
|
|
43
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
44
|
+
_and_: { _and_: {} },
|
|
45
|
+
});
|
|
46
|
+
sql = builder.toQuery();
|
|
47
|
+
assert.equal(sql, 'select * from "testVonaPost"');
|
|
48
|
+
// op: or
|
|
49
|
+
builder = scopeTest.model.post.builder();
|
|
50
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
51
|
+
_or_: { iid: 1, id: 2 },
|
|
52
|
+
});
|
|
53
|
+
sql = builder.toQuery();
|
|
54
|
+
assert.equal(sql, 'select * from "testVonaPost" where (("iid" = 1) or ("id" = 2))');
|
|
55
|
+
// op: or: more
|
|
56
|
+
builder = scopeTest.model.post.builder();
|
|
57
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
58
|
+
_or_: {
|
|
59
|
+
_and_: { iid: 1, id: 2 },
|
|
60
|
+
title: 'test',
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
sql = builder.toQuery();
|
|
64
|
+
assert.equal(sql, 'select * from "testVonaPost" where (((("iid" = 1) and ("id" = 2))) or ("title" = \'test\'))');
|
|
65
|
+
// op: not
|
|
66
|
+
builder = scopeTest.model.post.builder();
|
|
67
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
68
|
+
_not_: { iid: 1, id: 2 },
|
|
69
|
+
});
|
|
70
|
+
sql = builder.toQuery();
|
|
71
|
+
assert.equal(sql, 'select * from "testVonaPost" where not ("iid" = 1 and "id" = 2)');
|
|
72
|
+
// op: exists
|
|
73
|
+
builder = scopeTest.model.post.builder();
|
|
74
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
75
|
+
_exists_: function (this: Knex.QueryBuilder) {
|
|
76
|
+
this.select('id').from('testVonaUser').where({ 'testVonaUser.id': app.bean.model.ref('userId') });
|
|
77
|
+
} as any,
|
|
78
|
+
});
|
|
79
|
+
sql = builder.toQuery();
|
|
80
|
+
assert.equal(sql, 'select * from "testVonaPost" where exists (select "id" from "testVonaUser" where "testVonaUser"."id" = "userId")');
|
|
81
|
+
// op: notExists
|
|
82
|
+
builder = scopeTest.model.post.builder();
|
|
83
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
84
|
+
_notExists_: function (this: Knex.QueryBuilder) {
|
|
85
|
+
this.select('id').from('testVonaUser').where({ 'testVonaUser.id': app.bean.model.ref('userId') });
|
|
86
|
+
} as any,
|
|
87
|
+
});
|
|
88
|
+
sql = builder.toQuery();
|
|
89
|
+
assert.equal(sql, 'select * from "testVonaPost" where not exists (select "id" from "testVonaUser" where "testVonaUser"."id" = "userId")');
|
|
90
|
+
// op: array direct/in/notIn
|
|
91
|
+
builder = scopeTest.model.post.builder();
|
|
92
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
93
|
+
id: [1, 2],
|
|
94
|
+
iid: [],
|
|
95
|
+
_or_: { id: { _in_: [2, 3] }, iid: { _notIn_: [3, 4] } },
|
|
96
|
+
});
|
|
97
|
+
sql = builder.toQuery();
|
|
98
|
+
assert.equal(sql, 'select * from "testVonaPost" where "id" in (1, 2) and 1 = 0 and ((("id" in (2, 3))) or (("iid" not in (3, 4))))');
|
|
99
|
+
// op: null/notNull
|
|
100
|
+
builder = scopeTest.model.post.builder();
|
|
101
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
102
|
+
id: { _is_: null },
|
|
103
|
+
iid: { _isNot_: undefined },
|
|
104
|
+
});
|
|
105
|
+
sql = builder.toQuery();
|
|
106
|
+
assert.equal(sql, 'select * from "testVonaPost" where ("id" is null) and ("iid" is not null)');
|
|
107
|
+
// op: between/notBetween
|
|
108
|
+
builder = scopeTest.model.post.builder();
|
|
109
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
110
|
+
id: { _between_: [1, 3] },
|
|
111
|
+
iid: { _notBetween_: [2, 4] },
|
|
112
|
+
});
|
|
113
|
+
sql = builder.toQuery();
|
|
114
|
+
assert.equal(sql, 'select * from "testVonaPost" where ("id" between 1 and 3) and ("iid" not between 2 and 4)');
|
|
115
|
+
// op: ref
|
|
116
|
+
builder = scopeTest.model.post.builder();
|
|
117
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
118
|
+
iid: { _ref_: 'id' },
|
|
119
|
+
});
|
|
120
|
+
sql = builder.toQuery();
|
|
121
|
+
assert.equal(sql, 'select * from "testVonaPost" where ("iid" = "id")');
|
|
122
|
+
// op: gt/gte/lt/lte
|
|
123
|
+
builder = scopeTest.model.post.builder();
|
|
124
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
125
|
+
id: { _gt_: 1, _gte_: 2, _lt_: 3, _lte_: 4 },
|
|
126
|
+
});
|
|
127
|
+
sql = builder.toQuery();
|
|
128
|
+
assert.equal(sql, 'select * from "testVonaPost" where ("id" > 1 and "id" >= 2 and "id" < 3 and "id" <= 4)');
|
|
129
|
+
// op: startsWith/endsWith/includes
|
|
130
|
+
builder = scopeTest.model.post.builder();
|
|
131
|
+
scopeTest.model.post.buildWhere(builder, {
|
|
132
|
+
title: { _startsWith_: 'a', _endsWith_: 'b', _includes_: 'c' },
|
|
133
|
+
});
|
|
134
|
+
sql = builder.toQuery();
|
|
135
|
+
assert.equal(sql, 'select * from "testVonaPost" where ("title" like \'a%\' and "title" like \'%b\' and "title" like \'%c%\')');
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import { app } from 'vona-mock';
|
|
3
|
+
|
|
4
|
+
describe('orm.test.ts', () => {
|
|
5
|
+
it('action:orm:hasOne', async () => {
|
|
6
|
+
await app.bean.executor.mockCtx(async () => {
|
|
7
|
+
// scope
|
|
8
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
9
|
+
// create: post/postContent
|
|
10
|
+
const post1 = await scopeTest.model.post.insert({
|
|
11
|
+
title: 'action:orm:hasOne:post1',
|
|
12
|
+
});
|
|
13
|
+
const postContent1 = await scopeTest.model.postContent.insert({
|
|
14
|
+
content: 'action:orm:hasOne:content',
|
|
15
|
+
postId: post1.id,
|
|
16
|
+
});
|
|
17
|
+
// delete
|
|
18
|
+
await scopeTest.model.post.delete({ id: post1.id });
|
|
19
|
+
await scopeTest.model.postContent.delete({ id: postContent1.id });
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { app } from 'vona-mock';
|
|
4
|
+
|
|
5
|
+
describe('transaction.test.ts', () => {
|
|
6
|
+
const tableNameFail = '__tempTransactionFail';
|
|
7
|
+
const tableNameSuccess = '__tempTransactionSuccess';
|
|
8
|
+
|
|
9
|
+
it('action:transaction:fail', async () => {
|
|
10
|
+
// transaction
|
|
11
|
+
await app.bean.executor.mockCtx(async () => {
|
|
12
|
+
// create table
|
|
13
|
+
await app.bean.model.createTable(tableNameFail, table => {
|
|
14
|
+
table.basicFields();
|
|
15
|
+
table.string('name');
|
|
16
|
+
});
|
|
17
|
+
// create a new item
|
|
18
|
+
const res = await app.bean.model.insert(tableNameFail as any, {
|
|
19
|
+
name: 'hello',
|
|
20
|
+
});
|
|
21
|
+
const id = res.id;
|
|
22
|
+
|
|
23
|
+
// try to change name
|
|
24
|
+
const itemNew = {
|
|
25
|
+
id,
|
|
26
|
+
name: 'hello!!',
|
|
27
|
+
};
|
|
28
|
+
try {
|
|
29
|
+
await app.bean.executor.performAction('post', '/test/vona/transaction/fail', {
|
|
30
|
+
body: itemNew,
|
|
31
|
+
});
|
|
32
|
+
} catch (_err) {}
|
|
33
|
+
|
|
34
|
+
// check name
|
|
35
|
+
const item = await app.bean.model.get(tableNameFail as any, {
|
|
36
|
+
id,
|
|
37
|
+
});
|
|
38
|
+
assert.notEqual(item.name, itemNew.name);
|
|
39
|
+
|
|
40
|
+
// drop table
|
|
41
|
+
await app.bean.model.dropTable(tableNameFail);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// aop method
|
|
45
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
46
|
+
await app.bean.executor.mockCtx(async () => {
|
|
47
|
+
// create table
|
|
48
|
+
await app.bean.model.createTable(tableNameFail, table => {
|
|
49
|
+
table.basicFields();
|
|
50
|
+
table.string('name');
|
|
51
|
+
});
|
|
52
|
+
// create a new item
|
|
53
|
+
const res = await app.bean.model.insert(tableNameFail as any, {
|
|
54
|
+
name: 'hello',
|
|
55
|
+
});
|
|
56
|
+
const id = res.id;
|
|
57
|
+
|
|
58
|
+
// try to change name
|
|
59
|
+
const itemNew = {
|
|
60
|
+
id,
|
|
61
|
+
name: 'hello!!',
|
|
62
|
+
};
|
|
63
|
+
try {
|
|
64
|
+
await scopeTest.service.transaction.fail(itemNew);
|
|
65
|
+
} catch (_err) {}
|
|
66
|
+
|
|
67
|
+
// check name
|
|
68
|
+
const item = await app.bean.model.get(tableNameFail as any, {
|
|
69
|
+
id,
|
|
70
|
+
});
|
|
71
|
+
assert.notEqual(item.name, itemNew.name);
|
|
72
|
+
|
|
73
|
+
// drop table
|
|
74
|
+
await app.bean.model.dropTable(tableNameFail);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('action:transaction:success', async () => {
|
|
79
|
+
// transaction
|
|
80
|
+
await app.bean.executor.mockCtx(async () => {
|
|
81
|
+
// create table
|
|
82
|
+
await app.bean.model.createTable(tableNameSuccess, table => {
|
|
83
|
+
table.basicFields();
|
|
84
|
+
table.string('name');
|
|
85
|
+
});
|
|
86
|
+
// create a new item
|
|
87
|
+
const res = await app.bean.model.insert(tableNameSuccess as any, {
|
|
88
|
+
name: 'hello',
|
|
89
|
+
});
|
|
90
|
+
const id = res.id;
|
|
91
|
+
|
|
92
|
+
// try to change name
|
|
93
|
+
const itemNew = {
|
|
94
|
+
id,
|
|
95
|
+
name: 'hello!!',
|
|
96
|
+
};
|
|
97
|
+
await app.bean.executor.performAction('post', '/test/vona/transaction/success', {
|
|
98
|
+
body: itemNew,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// check name
|
|
102
|
+
const item = await app.bean.model.get(tableNameSuccess as any, {
|
|
103
|
+
id,
|
|
104
|
+
});
|
|
105
|
+
assert.equal(item.name, itemNew.name);
|
|
106
|
+
|
|
107
|
+
// drop table
|
|
108
|
+
await app.bean.model.dropTable(tableNameSuccess);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// aop method
|
|
112
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
113
|
+
await app.bean.executor.mockCtx(async () => {
|
|
114
|
+
// create table
|
|
115
|
+
await app.bean.model.createTable(tableNameSuccess, table => {
|
|
116
|
+
table.basicFields();
|
|
117
|
+
table.string('name');
|
|
118
|
+
});
|
|
119
|
+
// create a new item
|
|
120
|
+
const res = await app.bean.model.insert(tableNameSuccess as any, {
|
|
121
|
+
name: 'hello',
|
|
122
|
+
});
|
|
123
|
+
const id = res.id;
|
|
124
|
+
|
|
125
|
+
// try to change name
|
|
126
|
+
const itemNew = {
|
|
127
|
+
id,
|
|
128
|
+
name: 'hello!!',
|
|
129
|
+
};
|
|
130
|
+
await scopeTest.service.transaction.success(itemNew);
|
|
131
|
+
|
|
132
|
+
// check name
|
|
133
|
+
const item = await app.bean.model.get(tableNameSuccess as any, {
|
|
134
|
+
id,
|
|
135
|
+
});
|
|
136
|
+
assert.equal(item.name, itemNew.name);
|
|
137
|
+
|
|
138
|
+
// drop table
|
|
139
|
+
await app.bean.model.dropTable(tableNameSuccess);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { app } from 'vona-mock';
|
|
4
|
+
|
|
5
|
+
describe('event.test.ts', () => {
|
|
6
|
+
it('action:event', async () => {
|
|
7
|
+
await app.bean.executor.mockCtx(async () => {
|
|
8
|
+
// scope
|
|
9
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
10
|
+
const result = await scopeTest.event.helloEcho.emit({ text: 'hello' }, 'world');
|
|
11
|
+
assert.equal(result, 'hello world');
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
});
|