vona-module-test-vona 5.0.39 → 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 +12 -1
- package/dist/index.js +239 -128
- package/dist/service/order.d.ts +4 -0
- package/dist/service/post.d.ts +2 -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,219 @@
|
|
|
1
|
+
import type { TableIdentity } from 'table-identity';
|
|
2
|
+
import { BeanBase } from 'vona';
|
|
3
|
+
import { Service } from 'vona-module-a-bean';
|
|
4
|
+
import { Database } from 'vona-module-a-orm';
|
|
5
|
+
|
|
6
|
+
@Service()
|
|
7
|
+
export class ServicePost extends BeanBase {
|
|
8
|
+
async relationBelongsTo() {
|
|
9
|
+
const postContent = await this.scope.model.postContent.select({
|
|
10
|
+
include: {
|
|
11
|
+
post: true,
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
console.log(postContent[0]?.post?.title);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async relationHasOne() {
|
|
18
|
+
// insert
|
|
19
|
+
const postCreate = await this.scope.model.post.insert(
|
|
20
|
+
{
|
|
21
|
+
title: 'Post001',
|
|
22
|
+
postContent: {
|
|
23
|
+
content: 'This is a post',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
include: {
|
|
28
|
+
postContent: true,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
// get
|
|
33
|
+
const post = await this.scope.model.post.get(
|
|
34
|
+
{
|
|
35
|
+
id: postCreate.id,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
include: {
|
|
39
|
+
postContent: true,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
// update
|
|
44
|
+
await this.scope.model.post.update(
|
|
45
|
+
{
|
|
46
|
+
id: postCreate.id,
|
|
47
|
+
title: 'Post001-Update',
|
|
48
|
+
postContent: {
|
|
49
|
+
content: 'This is a post-changed',
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
include: {
|
|
54
|
+
postContent: true,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
// delete
|
|
59
|
+
await this.scope.model.post.delete(
|
|
60
|
+
{
|
|
61
|
+
id: postCreate.id,
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
include: {
|
|
65
|
+
postContent: true,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
console.log(post?.postContent?.id);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@Database.transaction()
|
|
73
|
+
async transaction() {
|
|
74
|
+
// insert
|
|
75
|
+
const post = await this.scope.model.post.insert({
|
|
76
|
+
title: 'Post001',
|
|
77
|
+
});
|
|
78
|
+
// cache
|
|
79
|
+
await this.scope.cacheRedis.post.set(post, post.id);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async transactionManually() {
|
|
83
|
+
const db = this.bean.database.getDb({ clientName: 'default' });
|
|
84
|
+
await db.transaction.begin(async () => {
|
|
85
|
+
const modelPost = this.scope.model.post.newInstance(db);
|
|
86
|
+
const post = await modelPost.insert({ title: 'Post001' });
|
|
87
|
+
await this.scope.cacheRedis.post.set(post, post.id, { db });
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async create() {
|
|
92
|
+
const post = await this.scope.model.post.insert({
|
|
93
|
+
title: 'Post001',
|
|
94
|
+
});
|
|
95
|
+
// console.log(post.id);
|
|
96
|
+
return post;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async createBulk() {
|
|
100
|
+
const posts = await this.scope.model.post.insertBulk([
|
|
101
|
+
{ title: 'Post001' },
|
|
102
|
+
{ title: 'Post002' },
|
|
103
|
+
]);
|
|
104
|
+
// console.log(posts[0].id, posts[1].id);
|
|
105
|
+
return posts;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async update() {
|
|
109
|
+
const post = await this.scope.model.post.update({
|
|
110
|
+
id: 1,
|
|
111
|
+
title: 'Post001-Update',
|
|
112
|
+
});
|
|
113
|
+
return post;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async update2() {
|
|
117
|
+
const post = await this.scope.model.post.update(
|
|
118
|
+
{
|
|
119
|
+
title: 'Post001-Update',
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
where: {
|
|
123
|
+
title: { _startsWith_: 'Post001' },
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
);
|
|
127
|
+
return post;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async updateBulk() {
|
|
131
|
+
const posts = await this.scope.model.post.updateBulk([
|
|
132
|
+
{ id: 1, title: 'Post001-Update' },
|
|
133
|
+
{ id: 2, title: 'Post002-Update' },
|
|
134
|
+
]);
|
|
135
|
+
return posts;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async delete() {
|
|
139
|
+
await this.scope.model.post.delete({
|
|
140
|
+
id: 1,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async delete2() {
|
|
145
|
+
await this.scope.model.post.delete({
|
|
146
|
+
title: {
|
|
147
|
+
_startsWith_: 'Post',
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async deleteBulk() {
|
|
153
|
+
await this.scope.model.post.deleteBulk([1, 2]);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async mutate() {
|
|
157
|
+
// insert
|
|
158
|
+
const post = await this.scope.model.post.mutate({
|
|
159
|
+
title: 'Post001',
|
|
160
|
+
});
|
|
161
|
+
// update
|
|
162
|
+
await this.scope.model.post.mutate({
|
|
163
|
+
id: post.id,
|
|
164
|
+
title: 'Post001-Update',
|
|
165
|
+
});
|
|
166
|
+
// delete
|
|
167
|
+
await this.scope.model.post.mutate({
|
|
168
|
+
id: post.id,
|
|
169
|
+
deleted: true,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async mutateBulk() {
|
|
174
|
+
await this.scope.model.post.mutateBulk([
|
|
175
|
+
// insert
|
|
176
|
+
{ title: 'Post003' },
|
|
177
|
+
// update
|
|
178
|
+
{ id: 1, title: 'Post001-Update' },
|
|
179
|
+
// delete
|
|
180
|
+
{ id: 2, deleted: true },
|
|
181
|
+
]);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async select() {
|
|
185
|
+
return await this.scope.model.post.select({
|
|
186
|
+
where: {
|
|
187
|
+
stars: '_skip_',
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async select2() {
|
|
193
|
+
return await this.scope.model.post.select({
|
|
194
|
+
columns: ['id', 'title', 'userId'],
|
|
195
|
+
where: {
|
|
196
|
+
'id': { _gt_: 1 },
|
|
197
|
+
'testVonaUser.id': 1,
|
|
198
|
+
},
|
|
199
|
+
joins: [['innerJoin', 'testVonaUser', ['userId', 'testVonaUser.id']]],
|
|
200
|
+
offset: 0,
|
|
201
|
+
limit: 20,
|
|
202
|
+
orders: [['createdAt', 'desc']],
|
|
203
|
+
}, {
|
|
204
|
+
disableDeleted: false,
|
|
205
|
+
}, 'test-vona:user');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async get(id: TableIdentity) {
|
|
209
|
+
return await this.scope.model.post.get({ id });
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async mget(ids: TableIdentity[]) {
|
|
213
|
+
return await this.scope.model.post.mget(ids);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async count() {
|
|
217
|
+
return await this.scope.model.post.count();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BeanBase } from 'vona';
|
|
2
|
+
import { Service } from 'vona-module-a-bean';
|
|
3
|
+
|
|
4
|
+
@Service()
|
|
5
|
+
export class ServiceTestApp extends BeanBase {
|
|
6
|
+
actionSync({ a, b }: any) {
|
|
7
|
+
return a + b;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async actionAsync({ a, b }: any) {
|
|
11
|
+
return Promise.resolve(a + b);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BeanBase } from 'vona';
|
|
2
|
+
import { Service } from 'vona-module-a-bean';
|
|
3
|
+
|
|
4
|
+
class ClassBeanBase extends BeanBase {
|
|
5
|
+
actionSync({ a, b }: any) {
|
|
6
|
+
return a + b;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
@Service()
|
|
11
|
+
export class ServiceTestClass extends ClassBeanBase {
|
|
12
|
+
async actionAsync({ a, b }: any) {
|
|
13
|
+
return Promise.resolve(a + b);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { EntityRole } from 'vona-module-home-user';
|
|
2
|
+
import type { EntityPost } from '../entity/post.ts';
|
|
3
|
+
import type { EntityPostContent } from '../entity/postContent.ts';
|
|
4
|
+
import type { EntityUser } from '../entity/user.ts';
|
|
5
|
+
import { BeanBase } from 'vona';
|
|
6
|
+
import { Service } from 'vona-module-a-bean';
|
|
7
|
+
|
|
8
|
+
export interface ITestData {
|
|
9
|
+
userTom: EntityUser;
|
|
10
|
+
userJimmy: EntityUser;
|
|
11
|
+
roleFamily: EntityRole;
|
|
12
|
+
roleFriend: EntityRole;
|
|
13
|
+
postApple: EntityPost;
|
|
14
|
+
postPear: EntityPost;
|
|
15
|
+
postContentApple: EntityPostContent;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@Service()
|
|
19
|
+
export class ServiceTestData extends BeanBase {
|
|
20
|
+
async create(prefix: string): Promise<ITestData> {
|
|
21
|
+
const scopeTest = this.scope;
|
|
22
|
+
const userTom = await scopeTest.model.user.insert({ name: `${prefix}:tom` });
|
|
23
|
+
const userJimmy = await scopeTest.model.user.insert({ name: `${prefix}:jimmy` });
|
|
24
|
+
const roleFamily = await scopeTest.model.role.insert({ name: `${prefix}:family` });
|
|
25
|
+
const roleFriend = await scopeTest.model.role.insert({ name: `${prefix}:friend` });
|
|
26
|
+
await scopeTest.model.roleUser.insertBulk([
|
|
27
|
+
{ userId: userTom.id, roleId: roleFamily.id },
|
|
28
|
+
{ userId: userTom.id, roleId: roleFriend.id },
|
|
29
|
+
{ userId: userJimmy.id, roleId: roleFamily.id },
|
|
30
|
+
]);
|
|
31
|
+
const postApple = await scopeTest.model.post.insert({ title: `${prefix}:postApple`, userId: userTom.id });
|
|
32
|
+
const postPear = await scopeTest.model.post.insert({ title: `${prefix}:postPear`, userId: userTom.id });
|
|
33
|
+
const postContentApple = await scopeTest.model.postContent.insert({ content: `${prefix}:postContentApple`, postId: postApple.id });
|
|
34
|
+
// ok
|
|
35
|
+
return {
|
|
36
|
+
userTom,
|
|
37
|
+
userJimmy,
|
|
38
|
+
roleFamily,
|
|
39
|
+
roleFriend,
|
|
40
|
+
postApple,
|
|
41
|
+
postPear,
|
|
42
|
+
postContentApple,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async drop(data: ITestData) {
|
|
47
|
+
const {
|
|
48
|
+
userTom,
|
|
49
|
+
userJimmy,
|
|
50
|
+
roleFamily,
|
|
51
|
+
roleFriend,
|
|
52
|
+
postApple,
|
|
53
|
+
postPear,
|
|
54
|
+
postContentApple,
|
|
55
|
+
} = data;
|
|
56
|
+
const scopeTest = this.scope;
|
|
57
|
+
await scopeTest.model.postContent.delete({ id: postContentApple.id });
|
|
58
|
+
await scopeTest.model.post.delete({ id: postApple.id });
|
|
59
|
+
await scopeTest.model.post.delete({ id: postPear.id });
|
|
60
|
+
await scopeTest.model.roleUser.delete({ userId: userTom.id });
|
|
61
|
+
await scopeTest.model.roleUser.delete({ userId: userJimmy.id });
|
|
62
|
+
await scopeTest.model.role.delete({ id: roleFamily.id });
|
|
63
|
+
await scopeTest.model.role.delete({ id: roleFriend.id });
|
|
64
|
+
await scopeTest.model.user.delete({ id: userTom.id });
|
|
65
|
+
await scopeTest.model.user.delete({ id: userJimmy.id });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { BeanBase } from 'vona';
|
|
2
|
+
import { Service } from 'vona-module-a-bean';
|
|
3
|
+
import { Database } from 'vona-module-a-orm';
|
|
4
|
+
|
|
5
|
+
const tableNameFail = '__tempTransactionFail';
|
|
6
|
+
const tableNameSuccess = '__tempTransactionSuccess';
|
|
7
|
+
|
|
8
|
+
@Service()
|
|
9
|
+
export class ServiceTransaction extends BeanBase {
|
|
10
|
+
@Database.transaction()
|
|
11
|
+
async fail(item: object) {
|
|
12
|
+
await this.bean.model.update(`${tableNameFail}` as any, item);
|
|
13
|
+
await this.bean.model.update(`${tableNameFail}error` as any, item);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Database.transaction()
|
|
17
|
+
async success(item: object) {
|
|
18
|
+
await this.bean.model.update(tableNameSuccess as any, item);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { app } from 'vona-mock';
|
|
4
|
+
|
|
5
|
+
describe('aopMethod.test.ts', () => {
|
|
6
|
+
it('action:aopMethod', async () => {
|
|
7
|
+
await app.bean.executor.mockCtx(async () => {
|
|
8
|
+
// scope
|
|
9
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
10
|
+
let result = scopeTest.service.aopMethod.testSyncBase();
|
|
11
|
+
assert.equal(result, '-+hello+-');
|
|
12
|
+
result = scopeTest.service.aopMethod.testSync();
|
|
13
|
+
assert.equal(result, '-+hello+-');
|
|
14
|
+
result = await scopeTest.service.aopMethod.testAsync();
|
|
15
|
+
assert.equal(result, '-+hello+-');
|
|
16
|
+
scopeTest.service.aopMethod.name = '-+vona+-';
|
|
17
|
+
result = scopeTest.service.aopMethod.name;
|
|
18
|
+
assert.equal(result, '-+vona+-');
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { catchError } from '@cabloy/utils';
|
|
4
|
+
import { app } from 'vona-mock';
|
|
5
|
+
|
|
6
|
+
describe('authSimple.test.ts', () => {
|
|
7
|
+
it('action:passwordHash', async () => {
|
|
8
|
+
const scope = app.bean.scope('a-authsimple');
|
|
9
|
+
const password = '12344##1xxaasDFQ,.$';
|
|
10
|
+
const hash = await scope.service.authSimple.calcPasswordHash(password);
|
|
11
|
+
let verified = await scope.service.authSimple.verifyPasswordHash(password, hash);
|
|
12
|
+
assert.equal(verified, true);
|
|
13
|
+
verified = await scope.service.authSimple.verifyPasswordHash(`${password}failed`, hash);
|
|
14
|
+
assert.equal(verified, false);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('action:authSimple', async () => {
|
|
18
|
+
await app.bean.executor.mockCtx(async () => {
|
|
19
|
+
// login
|
|
20
|
+
const jwt = await app.bean.auth.authenticate('a-authsimple:simple', {
|
|
21
|
+
clientName: 'default',
|
|
22
|
+
clientOptions: { username: 'admin', password: '123456' },
|
|
23
|
+
});
|
|
24
|
+
assert.equal(!!jwt?.accessToken, true);
|
|
25
|
+
// isAuthenticated: isolate
|
|
26
|
+
const [isAuthenticated2, _err] = await catchError(async () => {
|
|
27
|
+
return await app.bean.executor.newCtxIsolate(async () => {
|
|
28
|
+
return await app.bean.executor.performAction('get', '/test/vona/passport/isAuthenticated');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
assert.equal(isAuthenticated2, undefined);
|
|
32
|
+
// isAuthenticated: isolate + header
|
|
33
|
+
const isAuthenticated = await app.bean.executor.newCtxIsolate(async () => {
|
|
34
|
+
return await app.bean.executor.performAction('get', '/test/vona/passport/isAuthenticated', { authToken: jwt?.accessToken });
|
|
35
|
+
});
|
|
36
|
+
assert.equal(isAuthenticated, true);
|
|
37
|
+
// login again
|
|
38
|
+
const jwt2 = await app.bean.authSimple.authenticate(
|
|
39
|
+
{ username: 'admin', password: '123456' },
|
|
40
|
+
'default',
|
|
41
|
+
);
|
|
42
|
+
assert.equal(!!jwt2?.accessToken, true);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import { app } from 'vona-mock';
|
|
3
|
+
|
|
4
|
+
describe('bean.test.ts', () => {
|
|
5
|
+
it('action:bean', async () => {
|
|
6
|
+
await app.bean.executor.mockCtx(async () => {
|
|
7
|
+
await app.bean.executor.performAction('get', '/test/vona/bean/test');
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('action:service', async () => {
|
|
12
|
+
await app.bean.executor.mockCtx(async () => {
|
|
13
|
+
await app.bean.executor.performAction('get', '/test/vona/bean/service');
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { app } from 'vona-mock';
|
|
4
|
+
|
|
5
|
+
describe('broadcast.test.ts', () => {
|
|
6
|
+
it('action:broadcast', async () => {
|
|
7
|
+
await app.bean.executor.mockCtx(async () => {
|
|
8
|
+
app.bean.scope('test-vona').broadcast.test.emit(
|
|
9
|
+
{ message: 'hello' },
|
|
10
|
+
{
|
|
11
|
+
locale: 'zh-cn',
|
|
12
|
+
},
|
|
13
|
+
);
|
|
14
|
+
assert.equal(app.ctx.instanceName, '');
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import { app } from 'vona-mock';
|
|
3
|
+
|
|
4
|
+
describe('cacheMem.test.ts', () => {
|
|
5
|
+
it('action:cacheMem', async () => {
|
|
6
|
+
await app.bean.executor.mockCtx(async () => {
|
|
7
|
+
await app.bean.executor.performAction('post', '/test/vona/cacheMem');
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import { app } from 'vona-mock';
|
|
3
|
+
|
|
4
|
+
describe('cacheRedis.test.ts', () => {
|
|
5
|
+
it('action:cacheRedis', async () => {
|
|
6
|
+
await app.bean.executor.mockCtx(async () => {
|
|
7
|
+
await app.bean.executor.performAction('post', '/test/vona/cacheRedis');
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { app } from 'vona-mock';
|
|
4
|
+
|
|
5
|
+
describe('caching.test.ts', () => {
|
|
6
|
+
it('action:caching', async () => {
|
|
7
|
+
await app.bean.executor.mockCtx(async () => {
|
|
8
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
9
|
+
// get: cacheKeyFn(string)
|
|
10
|
+
const res = await scopeTest.service.caching.get(2);
|
|
11
|
+
assert.equal(res.id, 2);
|
|
12
|
+
// get: cacheKeyFn(function)
|
|
13
|
+
const res2 = await scopeTest.service.caching.get2(2);
|
|
14
|
+
assert.equal(res.id, res2.id);
|
|
15
|
+
// get: cacheKey(celjs expression)
|
|
16
|
+
const res3 = await scopeTest.service.caching.get3(2);
|
|
17
|
+
assert.equal(res.id, res3.id);
|
|
18
|
+
// get: cacheKey(default)
|
|
19
|
+
const res4 = await scopeTest.service.caching.get4(2);
|
|
20
|
+
assert.equal(res.id, res4.id);
|
|
21
|
+
// get: cacheKey(default) not hit cache
|
|
22
|
+
const res5 = await scopeTest.service.caching.get4(5);
|
|
23
|
+
assert.equal(res5, undefined);
|
|
24
|
+
// set: cacheKeyFn/cacheValueFn
|
|
25
|
+
await scopeTest.service.caching.set(2, { ...res, name: `${res.name}!` });
|
|
26
|
+
// get again
|
|
27
|
+
const res6 = await scopeTest.service.caching.get4(2);
|
|
28
|
+
assert.equal(res.id, res6.id);
|
|
29
|
+
assert.equal(`${res.name}!`, res6.name);
|
|
30
|
+
// set: cacheKey/cacheValue
|
|
31
|
+
await scopeTest.service.caching.set2(2, { ...res, name: `${res.name}!` });
|
|
32
|
+
// get again
|
|
33
|
+
const res7 = await scopeTest.service.caching.get4(2);
|
|
34
|
+
assert.equal(res.id, res7.id);
|
|
35
|
+
assert.equal(`${res.name}!`, res7.name);
|
|
36
|
+
// set: no cacheValue/cacheValueFn
|
|
37
|
+
await scopeTest.service.caching.set3(2, { ...res, name: `${res.name}!` });
|
|
38
|
+
// get again
|
|
39
|
+
const res8 = await scopeTest.service.caching.get4(2);
|
|
40
|
+
assert.equal(res.id, res8.id);
|
|
41
|
+
assert.equal(`${res.name}!`, res8.name);
|
|
42
|
+
// del
|
|
43
|
+
await scopeTest.service.caching.del(2);
|
|
44
|
+
// get again
|
|
45
|
+
const res9 = await scopeTest.service.caching.get4(2);
|
|
46
|
+
assert.equal(res9, undefined);
|
|
47
|
+
// cache again
|
|
48
|
+
const res10 = await scopeTest.service.caching.get(2);
|
|
49
|
+
assert.equal(res10.id, 2);
|
|
50
|
+
const res11 = await scopeTest.service.caching.get4(2);
|
|
51
|
+
assert.equal(res11.id, 2);
|
|
52
|
+
// clear
|
|
53
|
+
await scopeTest.service.caching.clear();
|
|
54
|
+
const res12 = await scopeTest.service.caching.get4(2);
|
|
55
|
+
assert.equal(res12, undefined);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import { app } from 'vona-mock';
|
|
3
|
+
|
|
4
|
+
describe('summer.test.ts', () => {
|
|
5
|
+
it('action:summer', async () => {
|
|
6
|
+
await app.bean.executor.mockCtx(async () => {
|
|
7
|
+
await app.bean.executor.performAction('post', '/test/vona/summer');
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
});
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { catchError } from '@cabloy/utils';
|
|
4
|
+
import moment from 'moment';
|
|
5
|
+
import { app } from 'vona-mock';
|
|
6
|
+
|
|
7
|
+
describe('database.test.ts', () => {
|
|
8
|
+
it('action:database:switchClient', async () => {
|
|
9
|
+
await app.bean.executor.mockCtx(async () => {
|
|
10
|
+
const scopeDatabase = app.bean.scope('a-orm');
|
|
11
|
+
const defaultClientName = scopeDatabase.service.database.getDefaultClientName();
|
|
12
|
+
// current
|
|
13
|
+
assert.equal(app.ctx.db.clientName, defaultClientName);
|
|
14
|
+
// switch
|
|
15
|
+
const clientNames = Object.keys(app.config.database.clients);
|
|
16
|
+
const clientName2 = clientNames.find(item => item !== defaultClientName);
|
|
17
|
+
await app.bean.database.switchDb(async () => {
|
|
18
|
+
assert.equal(app.ctx.db.clientName, clientName2);
|
|
19
|
+
}, { clientName: clientName2 as any });
|
|
20
|
+
// restore
|
|
21
|
+
assert.equal(app.ctx.db.clientName, defaultClientName);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
it('action:model:clientName', async () => {
|
|
25
|
+
await app.bean.executor.mockCtx(async () => {
|
|
26
|
+
// scope
|
|
27
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
28
|
+
const modelTest = scopeTest.model.test;
|
|
29
|
+
assert.equal(modelTest.options.client, 'default');
|
|
30
|
+
const modelTest2 = scopeTest.model.test;
|
|
31
|
+
assert.equal(modelTest, modelTest2);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
it('action:model:clientNameDynamic:transaction:fail', async () => {
|
|
35
|
+
await app.bean.executor.mockCtx(async () => {
|
|
36
|
+
// scope
|
|
37
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
38
|
+
const entityTest = await scopeTest.model.test.insert({ title: 'clientNameDynamic:fail' });
|
|
39
|
+
assert.equal(entityTest.title, 'clientNameDynamic:fail');
|
|
40
|
+
await catchError(async () => {
|
|
41
|
+
const db = app.bean.database.getDb({ clientName: 'default' });
|
|
42
|
+
await db.transaction.begin(async () => {
|
|
43
|
+
const modelTest = scopeTest.model.test.newInstance(db);
|
|
44
|
+
assert.equal(modelTest.options.client, 'default');
|
|
45
|
+
await modelTest.update({ id: entityTest.id, title: 'clientNameDynamic:fail_1' });
|
|
46
|
+
throw new Error('rollback');
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
const entityTest2 = await scopeTest.model.test.get({ id: entityTest.id });
|
|
50
|
+
assert.equal(entityTest2?.title, 'clientNameDynamic:fail');
|
|
51
|
+
// delete
|
|
52
|
+
await scopeTest.model.test.delete({ id: entityTest.id });
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
it('action:model:clientNameDynamic:transaction:success', async () => {
|
|
56
|
+
await app.bean.executor.mockCtx(async () => {
|
|
57
|
+
// scope
|
|
58
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
59
|
+
const entityTest = await scopeTest.model.test.insert({ title: 'clientNameDynamic:success' });
|
|
60
|
+
assert.equal(entityTest.title, 'clientNameDynamic:success');
|
|
61
|
+
const db = app.bean.database.getDb({ clientName: 'default' });
|
|
62
|
+
await db.transaction.begin(async () => {
|
|
63
|
+
const modelTest = scopeTest.model.test.newInstance(db);
|
|
64
|
+
assert.equal(modelTest.options.client, 'default');
|
|
65
|
+
await modelTest.update({ id: entityTest.id, title: 'clientNameDynamic:success_1' });
|
|
66
|
+
});
|
|
67
|
+
const entityTest2 = await scopeTest.model.test.get({ id: entityTest.id });
|
|
68
|
+
assert.equal(entityTest2?.title, 'clientNameDynamic:success_1');
|
|
69
|
+
// delete
|
|
70
|
+
await scopeTest.model.test.delete({ id: entityTest.id });
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
it('action:database:transaction:compensate', async () => {
|
|
74
|
+
await app.bean.executor.mockCtx(async () => {
|
|
75
|
+
// scope
|
|
76
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
77
|
+
const entityTest = await scopeTest.model.test.insert({ title: 'transaction:compensate:fail' });
|
|
78
|
+
assert.equal(entityTest.title, 'transaction:compensate:fail');
|
|
79
|
+
await catchError(async () => {
|
|
80
|
+
const db = app.bean.database.getDb({ clientName: 'default' });
|
|
81
|
+
await db.transaction.begin(async () => {
|
|
82
|
+
const modelTest = scopeTest.model.test.newInstance(db);
|
|
83
|
+
assert.equal(modelTest.options.client, 'default');
|
|
84
|
+
await modelTest.update({ id: entityTest.id, title: 'transaction:compensate:fail_1' });
|
|
85
|
+
// let cache take effect
|
|
86
|
+
await modelTest.get({ id: entityTest.id });
|
|
87
|
+
throw new Error('rollback');
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
const entityTest2 = await scopeTest.model.test.get({ id: entityTest.id });
|
|
91
|
+
assert.equal(entityTest2?.title, 'transaction:compensate:fail');
|
|
92
|
+
// delete
|
|
93
|
+
await scopeTest.model.test.delete({ id: entityTest.id });
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
it('action:model:dynamicTableName', async () => {
|
|
97
|
+
await app.bean.executor.mockCtx(async () => {
|
|
98
|
+
// scope
|
|
99
|
+
const scopeTest = app.bean.scope('test-vona');
|
|
100
|
+
const entityTest = scopeTest.entity.test;
|
|
101
|
+
const modelTest = scopeTest.model.testDynamicTable;
|
|
102
|
+
// tableName
|
|
103
|
+
const tableName = `${entityTest.$table}_${moment().format('YYYYMMDD')}`;
|
|
104
|
+
// create table
|
|
105
|
+
await app.bean.model.createTable(tableName, table => {
|
|
106
|
+
table.basicFields();
|
|
107
|
+
table.string(entityTest.title, 255);
|
|
108
|
+
table.string(entityTest.description, 255);
|
|
109
|
+
});
|
|
110
|
+
// insert
|
|
111
|
+
const item = await modelTest.insert({ title: 'title', description: 'description' });
|
|
112
|
+
// get
|
|
113
|
+
const item2 = await modelTest.get({ id: item.id });
|
|
114
|
+
assert.equal(item2?.id, item.id);
|
|
115
|
+
// get by tableName
|
|
116
|
+
const item3 = await app.bean.model.get(tableName as any, { id: item.id });
|
|
117
|
+
assert.equal(item3.id, item.id);
|
|
118
|
+
// delete
|
|
119
|
+
await modelTest.delete({ id: item.id });
|
|
120
|
+
// get by tableName
|
|
121
|
+
const item4 = await app.bean.model.get(tableName as any, { id: item.id });
|
|
122
|
+
assert.equal(item4, undefined);
|
|
123
|
+
// drop table
|
|
124
|
+
await app.bean.model.dropTable(tableName);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
});
|