Mock users and groups(模拟用户与用户组)¶
User mocks¶
You are able to create partial mock of a user using createUser, where all properties besides id and username are optional. You need to import { createUser } from "@foundry/functions-testing-lib".
import { MyFunctions } from ".."
import { verifyOntologyEditFunction, createGroup, createUser } from "@foundry/functions-testing-lib";
describe("example test suite", () => {
const myFunctions = new MyFunctions();
test("test users and groups", async () => {
const group = createGroup({
id: "groupId",
});
const user = createUser({
id: "userId",
username: "username",
});
await expect(myFunctions.searchUsers("userId", "groupId")).resolves.toEqual([user, group]);
});
});
This can be used to test the following function:
import { Function, OntologyEditFunction, Users, Group, Principal } from "@foundry/functions-api";
export class MyFunctions {
@Function()
public async searchUsers(userId: string, groupId: string): Promise<Principal[]> {
const existingPrincipals = await Promise.all([
Users.getUserByIdAsync(userId),
Users.getGroupByIdAsync(groupId),
]);
return existingPrincipals.filter(r => !!r).map(r => r!);
}
}
Group mocks¶
You are also able to create partial mock of a group using createGroup, where all properties besides id are optional. You need to import { createGroup } from "@foundry/functions-testing-lib".
import { MyFunctions } from ".."
import { verifyOntologyEditFunction, createGroup } from "@foundry/functions-testing-lib";
describe("example test suite", () => {
const myFunctions = new MyFunctions();
test("test groups", async () => {
const group = createGroup({
id: "groupId",
});
await expect(myFunctions.searchGroups("groupId")).resolves.toEqual([group]);
});
});
This can be used to test the following function:
import { Function, OntologyEditFunction, Users, Group, Principal } from "@foundry/functions-api";
export class MyFunctions {
@Function()
public async searchGroups(groupId: string): Promise<Principal[]> {
const existingPrincipals = await Promise.all([
Users.getGroupByIdAsync(groupId),
]);
return existingPrincipals.filter(r => !!r).map(r => r!);
}
}
中文翻译¶
模拟用户与用户组¶
用户模拟(User mocks)¶
您可以使用 createUser 创建部分模拟的用户,其中除 id 和 username 之外的所有属性均为可选。您需要从 "@foundry/functions-testing-lib" 导入 { createUser }。
import { MyFunctions } from ".."
import { verifyOntologyEditFunction, createGroup, createUser } from "@foundry/functions-testing-lib";
describe("示例测试套件", () => {
const myFunctions = new MyFunctions();
test("测试用户与用户组", async () => {
const group = createGroup({
id: "groupId",
});
const user = createUser({
id: "userId",
username: "username",
});
await expect(myFunctions.searchUsers("userId", "groupId")).resolves.toEqual([user, group]);
});
});
此代码可用于测试以下函数:
import { Function, OntologyEditFunction, Users, Group, Principal } from "@foundry/functions-api";
export class MyFunctions {
@Function()
public async searchUsers(userId: string, groupId: string): Promise<Principal[]> {
const existingPrincipals = await Promise.all([
Users.getUserByIdAsync(userId),
Users.getGroupByIdAsync(groupId),
]);
return existingPrincipals.filter(r => !!r).map(r => r!);
}
}
用户组模拟(Group mocks)¶
您还可以使用 createGroup 创建部分模拟的用户组,其中除 id 之外的所有属性均为可选。您需要从 "@foundry/functions-testing-lib" 导入 { createGroup }。
import { MyFunctions } from ".."
import { verifyOntologyEditFunction, createGroup } from "@foundry/functions-testing-lib";
describe("示例测试套件", () => {
const myFunctions = new MyFunctions();
test("测试用户组", async () => {
const group = createGroup({
id: "groupId",
});
await expect(myFunctions.searchGroups("groupId")).resolves.toEqual([group]);
});
});
此代码可用于测试以下函数:
import { Function, OntologyEditFunction, Users, Group, Principal } from "@foundry/functions-api";
export class MyFunctions {
@Function()
public async searchGroups(groupId: string): Promise<Principal[]> {
const existingPrincipals = await Promise.all([
Users.getGroupByIdAsync(groupId),
]);
return existingPrincipals.filter(r => !!r).map(r => r!);
}
}