Skip to content

Commit

Permalink
test: add findAll test case
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jul 1, 2024
1 parent faafdb0 commit a40925b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
6 changes: 1 addition & 5 deletions apps/api/src/course-pack/course-pack.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ export class CoursePackController {
@UseGuards(AuthGuard)
@Get()
async findAll(@User() user: UserEntity) {
if (user.userId) {
return await this.coursePackService.findAll(user.userId);
} else {
return await this.coursePackService.findAllPublicCoursePacks();
}
return await this.coursePackService.findAll(user.userId);
}

@UncheckAuth()
Expand Down
13 changes: 9 additions & 4 deletions apps/api/src/course-pack/course-pack.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ export class CoursePackService {
private readonly courseHistoryService: CourseHistoryService,
) {}

async findAll(userId: string) {
async findAll(userId?: string) {
const userIdOwnedCoursePacks = userId ? await this.findAllForUser(userId) : [];
const publicCoursePacks = await this.findAllPublicCoursePacks();

return [...userIdOwnedCoursePacks, ...publicCoursePacks];
}

async findAllForUser(userId: string) {
const userIdOwnedCoursePacks = await this.db.query.coursePack.findMany({
orderBy: asc(coursePack.order),
where: and(eq(coursePack.creatorId, userId), eq(coursePack.shareLevel, "private")),
});

const publicCoursePacks = await this.findAllPublicCoursePacks();

return [...userIdOwnedCoursePacks, ...publicCoursePacks];
return userIdOwnedCoursePacks;
}

async findAllPublicCoursePacks() {
Expand Down
43 changes: 38 additions & 5 deletions apps/api/src/course-pack/tests/course-pack.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,49 @@ describe("CoursePackService", () => {
});

describe("findAll", () => {
it("should return an array of course packs", async () => {
await insertCoursePack(db);
await insertCoursePack(db);
it("should return all course packs including private and public", async () => {
await insertCoursePack(db, { creatorId: "admin", shareLevel: "public" });
await insertCoursePack(db, { creatorId: "user1", shareLevel: "public" });

const result = await coursePackService.findAll();
const result = await coursePackService.findAll("user1");

expect(result.length).toBe(2);
expect(result.length).toBe(2); // user1's private and public packs
});

it("should return only public course packs", async () => {
await insertCoursePack(db, { creatorId: "admin", shareLevel: "public" });
await insertCoursePack(db, { creatorId: "admin", shareLevel: "public" });
await insertCoursePack(db, { creatorId: "user2", shareLevel: "private" });

const result = await coursePackService.findAllPublicCoursePacks();

expect(result.length).toBe(2); // all public packs
});

it("should return only private course packs and public course packs for a specific user", async () => {
await insertCoursePack(db, { creatorId: "user1", shareLevel: "private" });
await insertCoursePack(db, { creatorId: "admin", shareLevel: "public" });
await insertCoursePack(db, { creatorId: "user2", shareLevel: "private" });

const result = await coursePackService.findAll("user1");

expect(result.length).toBe(2); // user1's private pack
});
});

// describe("findAll", () => {
// // TODO
// // 这里需要测试 是否可以返回个人私有的课程包
// it("should return an array of course packs", async () => {
// await insertCoursePack(db);
// await insertCoursePack(db);

// const result = await coursePackService.findAllPublicCoursePacks();

// expect(result.length).toBe(2);
// });
// });

describe("findOne", () => {
it("should return a course pack for a valid ID", async () => {
const coursePackEntity = await insertCoursePack(db);
Expand Down
2 changes: 2 additions & 0 deletions apps/api/test/fixture/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export async function insertCoursePack(db: DbType, values?: Partial<CoursePackIn
title: "课程包",
description: "这是一个课程包",
isFree: true,
creatorId: "test",
shareLevel: "public",
} satisfies CoursePackInsert;

const [entity] = await db
Expand Down

0 comments on commit a40925b

Please sign in to comment.