From 76593ec0636871b0902b7da8fabea6c0b47fb656 Mon Sep 17 00:00:00 2001 From: WOOSERK <55542546+WOOSERK@users.noreply.github.com> Date: Thu, 15 Dec 2022 20:09:21 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor(BE):=20waste=EC=97=90=EC=84=9C=20w?= =?UTF-8?q?eight=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20(#421)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scheduler-server/src/ranking/ranking.service.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scheduler-server/src/ranking/ranking.service.ts b/scheduler-server/src/ranking/ranking.service.ts index e3e86a5f..33d9198f 100644 --- a/scheduler-server/src/ranking/ranking.service.ts +++ b/scheduler-server/src/ranking/ranking.service.ts @@ -75,28 +75,28 @@ export class RankingService { return; } - const timeWaste = this.calcTimeWaste(idx); + const timeWeight = this.calcTimeWeight(idx); for (const tag in tagCounts) { if (totalTagCounts[tag]) { - totalTagCounts[tag] += tagCounts[tag] * timeWaste; + totalTagCounts[tag] += tagCounts[tag] * timeWeight; continue; } - totalTagCounts[tag] = tagCounts[tag] * timeWaste; + totalTagCounts[tag] = tagCounts[tag] * timeWeight; } }); return totalTagCounts; } - /** + /* * 시간 가중치는 현재 큐의 인덱스와 배열의 크기에 따라 정해진다 */ - private calcTimeWaste(idx: number): number { + private calcTimeWeight(idx: number): number { const offset = idx - this.index + ARRAY_SIZE - 1; - const timeWaste = ((offset % ARRAY_SIZE) + 1) / ARRAY_SIZE; + const timeWeight = ((offset % ARRAY_SIZE) + 1) / ARRAY_SIZE; - return timeWaste; + return timeWeight; } private addPrevInfo(newRanking: any[]) { From a0609fde707fbd23ae771e8209a709322701a016 Mon Sep 17 00:00:00 2001 From: WOOSERK <55542546+WOOSERK@users.noreply.github.com> Date: Thu, 15 Dec 2022 20:09:42 +0900 Subject: [PATCH 2/4] =?UTF-8?q?test(BE):=20=EB=A6=AC=EB=B7=B0=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=EB=8B=A8=EC=9C=84=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9E=91=EC=84=B1=20(#421)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/domain/review/review.service.spec.ts | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 server/src/domain/review/review.service.spec.ts diff --git a/server/src/domain/review/review.service.spec.ts b/server/src/domain/review/review.service.spec.ts new file mode 100644 index 00000000..b597c80c --- /dev/null +++ b/server/src/domain/review/review.service.spec.ts @@ -0,0 +1,174 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PostNotFoundException } from 'src/exception/post-not-found.exception'; +import { UserNotFoundException } from 'src/exception/user-not-found.exception'; +import { Post } from '../post/post.entity'; +import { PostRepository } from '../post/post.repository'; +import { User } from '../user/user.entity'; +import { UserRepository } from '../user/user.repository'; +import { + ReviewGetAllRequestDto, + ReviewWriteRequestDto, +} from './dto/controller-request.dto'; +import { Review } from './review.entity'; +import { ReviewRepository } from './review.repository'; +import { ReviewService } from './review.service'; + +describe('리뷰 서비스', () => { + let testingModule: TestingModule; + let reviewService: ReviewService; + let reviewRepository: ReviewRepository; + let postRepository: PostRepository; + let userRepository: UserRepository; + + beforeAll(async () => { + testingModule = await Test.createTestingModule({ + providers: [ + ReviewService, + { + provide: ReviewRepository, + useValue: {}, + }, + { + provide: PostRepository, + useValue: {}, + }, + { + provide: UserRepository, + useValue: {}, + }, + ], + }).compile(); + }); + + beforeEach(() => { + reviewService = testingModule.get(ReviewService); + reviewRepository = testingModule.get(ReviewRepository); + postRepository = testingModule.get(PostRepository); + userRepository = testingModule.get(UserRepository); + }); + + describe('리뷰 작성', () => { + it('존재하지 않는 유저가 리뷰를 작성하면 UserNotFoundException이 발생한다.', async () => { + // given + const dto: ReviewWriteRequestDto = { + postId: 1, + content: '내용', + }; + const userId = 1; + + userRepository.findOneBy = jest.fn(() => null); + postRepository.findOneBy = jest.fn(); + + // when + try { + await reviewService.write(userId, dto); + throw new Error(); + } catch (err) { + // then + expect(userRepository.findOneBy).toHaveBeenCalledTimes(1); + expect(err).toBeInstanceOf(UserNotFoundException); + } + }); + + it('존재하지 않는 포스트에 리뷰를 작성하면 PostNotFoundException이 발생한다.', async () => { + // given + const dto: ReviewWriteRequestDto = { + postId: 1, + content: '내용', + }; + const userId = 1; + + userRepository.findOneBy = jest.fn(async () => new User()); + postRepository.findOneBy = jest.fn(() => null); + + // when + try { + await reviewService.write(userId, dto); + throw new Error(); + } catch (err) { + expect(postRepository.findOneBy).toHaveBeenCalledTimes(1); + expect(err).toBeInstanceOf(PostNotFoundException); + } + }); + + it('유저 정보와 포스트 정보로 리뷰 작성에 성공한다.', async () => { + // given + const dto: ReviewWriteRequestDto = { + postId: 1, + content: '내용', + }; + const userId = 1; + + userRepository.findOneBy = jest.fn(async () => new User()); + postRepository.findOneBy = jest.fn(async () => new Post()); + postRepository.increaseReviewCount = jest.fn(async () => new Post()); + reviewRepository.insert = jest.fn(); + + // when + await reviewService.write(userId, dto); + + // then + expect(reviewRepository.insert).toHaveBeenCalledTimes(1); + }); + }); + + describe('리뷰 목록 조회', () => { + const REQUEST_CNT = 3; + it('리뷰를 한 번에 최대 REQUEST_CNT개까지 반환한다.', async () => { + // given + const reviews = Array.from({ length: REQUEST_CNT + 1 }, (v, i) => { + const review = new Review(); + review.id = i + 1; + review.user = new User(); + return review; + }); + const postId = 1; + const requestDto = new ReviewGetAllRequestDto(); + + reviewRepository.find = jest.fn(async () => reviews); + + // when + const dtos = await reviewService.getReviewsOfPost(postId, requestDto); + + // then + expect(dtos.reviews.length).toEqual(REQUEST_CNT); + expect(dtos.isLast).toEqual(false); + }); + + it('리뷰가 요청한 개수보다 적거나 같으면 isLast가 true다.', async () => { + // given + const reviews = Array.from({ length: REQUEST_CNT }, (v, i) => { + const review = new Review(); + review.id = i + 1; + review.user = new User(); + return review; + }); + const postId = 1; + const requestDto = new ReviewGetAllRequestDto(); + + reviewRepository.find = jest.fn(async () => reviews); + + // when + const dtos = await reviewService.getReviewsOfPost(postId, requestDto); + + // then + expect(dtos.lastId).toEqual(dtos.reviews[dtos.reviews.length - 1].id); + expect(dtos.isLast).toEqual(true); + }); + + it('리뷰가 없으면 isLast가 true다.', async () => { + // given + const postId = 1; + const requestDto = new ReviewGetAllRequestDto(); + + reviewRepository.find = jest.fn(); + + // when + const dtos = await reviewService.getReviewsOfPost(postId, requestDto); + + // then + expect(dtos.reviews.length).toEqual(0); + expect(dtos.isLast).toEqual(true); + }); + }); +}); From 786a1209bcd9aa7298b589f3b94fb80b011df048 Mon Sep 17 00:00:00 2001 From: WOOSERK <55542546+WOOSERK@users.noreply.github.com> Date: Thu, 15 Dec 2022 20:38:19 +0900 Subject: [PATCH 3/4] =?UTF-8?q?test(BE):=20=EC=8B=A0=EA=B3=A0=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=EB=8B=A8=EC=9C=84=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EA=B5=AC=ED=98=84=20(#421)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/domain/report/report.service.spec.ts | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 server/src/domain/report/report.service.spec.ts diff --git a/server/src/domain/report/report.service.spec.ts b/server/src/domain/report/report.service.spec.ts new file mode 100644 index 00000000..94a5f8b2 --- /dev/null +++ b/server/src/domain/report/report.service.spec.ts @@ -0,0 +1,111 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PostAlreadyReportedException } from 'src/exception/post-already-reported.exception'; +import { PostNotFoundException } from 'src/exception/post-not-found.exception'; +import { UserNotFoundException } from 'src/exception/user-not-found.exception'; +import { Post } from '../post/post.entity'; +import { PostRepository } from '../post/post.repository'; +import { User } from '../user/user.entity'; +import { UserRepository } from '../user/user.repository'; +import { ReportCreateRequestDto } from './dto/controller-request.dto'; +import { Report } from './report.entity'; +import { ReportRepository } from './report.repository'; +import { ReportService } from './report.service'; + +describe('신고 서비스', () => { + let testingModule: TestingModule; + let reportService: ReportService; + let reportRepository: ReportRepository; + let userRepository: UserRepository; + let postRepository: PostRepository; + + beforeAll(async () => { + testingModule = await Test.createTestingModule({ + providers: [ + ReportService, + { + provide: ReportRepository, + useValue: {}, + }, + { + provide: PostRepository, + useValue: {}, + }, + { + provide: UserRepository, + useValue: {}, + }, + ], + }).compile(); + }); + + beforeEach(() => { + reportService = testingModule.get(ReportService); + reportRepository = testingModule.get(ReportRepository); + postRepository = testingModule.get(PostRepository); + userRepository = testingModule.get(UserRepository); + }); + + describe('신고하기', () => { + it('존재하지 않는 유저가 신고를 하면 UserNotFoundException이 발생한다.', async () => { + // given + const userId = 1; + const postId = 1; + const requestDto = new ReportCreateRequestDto(); + + userRepository.findOneBy = jest.fn(); + postRepository.findOneBy = jest.fn(); + + // when + try { + await reportService.report(userId, postId, requestDto); + throw new Error(); + } catch (err) { + // then + expect(err).toBeInstanceOf(UserNotFoundException); + } + }); + + it('존재하지 않는 포스트에 신고를 하면 PostNotFoundException이 발생한다.', async () => { + // given + const userId = 1; + const postId = 1; + const requestDto = new ReportCreateRequestDto(); + + userRepository.findOneBy = jest.fn(async () => new User()); + postRepository.findOneBy = jest.fn(); + + // when + try { + await reportService.report(userId, postId, requestDto); + throw new Error(); + } catch (err) { + // then + expect(err).toBeInstanceOf(PostNotFoundException); + } + }); + + it('신고한 포스트를 다시 신고하면 PostAlreadyReportedException이 발생한다.', async () => { + // given + const userId = 1; + const postId = 1; + const requestDto = new ReportCreateRequestDto(); + + userRepository.findOneBy = jest.fn(async () => new User()); + postRepository.findOneBy = jest.fn(async () => new Post()); + reportRepository.findOneBy = jest.fn(); + reportRepository.insert = jest.fn(); + + // when + try { + await reportService.report(userId, postId, requestDto); + reportRepository.findOneBy = jest.fn(async () => new Report()); + await reportService.report(userId, postId, requestDto); + throw new Error(); + } catch (err) { + // then + expect(err).toBeInstanceOf(PostAlreadyReportedException); + expect(reportRepository.insert).toHaveBeenCalledTimes(1); + } + }); + }); +}); From fac95588d07410691c583c6a6b96a6367a884db8 Mon Sep 17 00:00:00 2001 From: WOOSERK <55542546+WOOSERK@users.noreply.github.com> Date: Thu, 15 Dec 2022 20:43:14 +0900 Subject: [PATCH 4/4] =?UTF-8?q?setting(BE):=20=EC=BB=A4=EB=B2=84=EB=A6=AC?= =?UTF-8?q?=EC=A7=80=20=EB=8C=80=EC=83=81=EC=97=90=EC=84=9C=20dist=20?= =?UTF-8?q?=EC=A0=9C=EC=99=B8=20(#421)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/package.json b/server/package.json index ba63f3b1..fce99a14 100644 --- a/server/package.json +++ b/server/package.json @@ -90,7 +90,7 @@ "^.+\\.(t|j)s$": "ts-jest" }, "collectCoverageFrom": [ - "**/*.service.(t|j)s" + "src/**/*.service.(t|j)s" ], "coverageDirectory": "./coverage", "testEnvironment": "node"