From a40925bb75b138a6b79d379d830d81bb6547e84e Mon Sep 17 00:00:00 2001 From: cuixiaorui Date: Mon, 1 Jul 2024 18:32:51 +0800 Subject: [PATCH] test: add findAll test case --- .../src/course-pack/course-pack.controller.ts | 6 +-- .../src/course-pack/course-pack.service.ts | 13 ++++-- .../tests/course-pack.service.spec.ts | 43 ++++++++++++++++--- apps/api/test/fixture/db.ts | 2 + 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/apps/api/src/course-pack/course-pack.controller.ts b/apps/api/src/course-pack/course-pack.controller.ts index 64cb95409..bd1164fb1 100644 --- a/apps/api/src/course-pack/course-pack.controller.ts +++ b/apps/api/src/course-pack/course-pack.controller.ts @@ -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() diff --git a/apps/api/src/course-pack/course-pack.service.ts b/apps/api/src/course-pack/course-pack.service.ts index 01d8f378f..a7fbc7e24 100644 --- a/apps/api/src/course-pack/course-pack.service.ts +++ b/apps/api/src/course-pack/course-pack.service.ts @@ -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() { diff --git a/apps/api/src/course-pack/tests/course-pack.service.spec.ts b/apps/api/src/course-pack/tests/course-pack.service.spec.ts index eaad5c32d..af962eda6 100644 --- a/apps/api/src/course-pack/tests/course-pack.service.spec.ts +++ b/apps/api/src/course-pack/tests/course-pack.service.spec.ts @@ -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); diff --git a/apps/api/test/fixture/db.ts b/apps/api/test/fixture/db.ts index d386172ab..212b83b34 100644 --- a/apps/api/test/fixture/db.ts +++ b/apps/api/test/fixture/db.ts @@ -11,6 +11,8 @@ export async function insertCoursePack(db: DbType, values?: Partial