From ffe39e2a83133ed02321f4767230df89b582c420 Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 2 Dec 2024 22:17:56 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=85=20test:=20=EC=A6=90=EA=B2=A8?= =?UTF-8?q?=EC=B0=BE=EA=B8=B0=20=EB=93=B1=EB=A1=9D=20API=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1=20?= =?UTF-8?q?#237?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookmark/stock-bookmark.service.spec.ts | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 BE/src/stock/bookmark/stock-bookmark.service.spec.ts diff --git a/BE/src/stock/bookmark/stock-bookmark.service.spec.ts b/BE/src/stock/bookmark/stock-bookmark.service.spec.ts new file mode 100644 index 00000000..f5371660 --- /dev/null +++ b/BE/src/stock/bookmark/stock-bookmark.service.spec.ts @@ -0,0 +1,66 @@ +import { Test } from '@nestjs/testing'; +import { BadRequestException } from '@nestjs/common'; +import { StockBookmarkRepository } from './stock-bookmark.repository'; +import { StockDetailService } from '../detail/stock-detail.service'; +import { StockBookmarkService } from './stock-bookmark.service'; + +describe('stock bookmark test', () => { + let stockBookmarkService: StockBookmarkService; + let stockBookmarkRepository: StockBookmarkRepository; + let stockDetailService: StockDetailService; + + beforeEach(async () => { + const mockStockBookmarkRepository = { + existsBy: jest.fn(), + create: jest.fn(), + insert: jest.fn(), + }; + const mockStockDetailService = { getInquirePrice: jest.fn() }; + + const module = await Test.createTestingModule({ + providers: [ + StockBookmarkService, + { + provide: StockBookmarkRepository, + useValue: mockStockBookmarkRepository, + }, + { + provide: StockDetailService, + useValue: mockStockDetailService, + }, + ], + }).compile(); + + stockBookmarkService = module.get(StockBookmarkService); + stockBookmarkRepository = module.get(StockBookmarkRepository); + stockDetailService = module.get(StockDetailService); + }); + + it('즐겨찾기에 등록되지 않은 종목에 대해 즐겨찾기 등록할 경우, DB에 즐겨찾기가 추가된다.', async () => { + jest.spyOn(stockBookmarkRepository, 'existsBy').mockResolvedValue(false); + + const createMock = jest.fn(); + jest + .spyOn(stockBookmarkRepository, 'create') + .mockImplementation(createMock); + + const saveMock = jest.fn(); + jest.spyOn(stockBookmarkRepository, 'insert').mockImplementation(saveMock); + + await stockBookmarkService.registerBookmark(1, '005930'); + + expect(createMock).toHaveBeenCalledWith({ + user_id: 1, + stock_code: '005930', + }); + expect(saveMock).toHaveBeenCalled(); + }); + + it('즐겨찾기에 이미 등록된 종목에 대해 즐겨찾기 등록할 경우, BadRequest 예외가 발생한다.', async () => { + jest.spyOn(stockBookmarkRepository, 'existsBy').mockResolvedValue(true); + + await expect( + stockBookmarkService.registerBookmark(1, '005930'), + ).rejects.toThrow(BadRequestException); + }); +}); From 80752a27b48099a794876e9c9ffb04d758859987 Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 2 Dec 2024 22:25:51 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=85=20test:=20=EC=A6=90=EA=B2=A8?= =?UTF-8?q?=EC=B0=BE=EA=B8=B0=20=EC=B7=A8=EC=86=8C=20API=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1=20?= =?UTF-8?q?#237?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookmark/stock-bookmark.service.spec.ts | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/BE/src/stock/bookmark/stock-bookmark.service.spec.ts b/BE/src/stock/bookmark/stock-bookmark.service.spec.ts index f5371660..22a4a2ff 100644 --- a/BE/src/stock/bookmark/stock-bookmark.service.spec.ts +++ b/BE/src/stock/bookmark/stock-bookmark.service.spec.ts @@ -1,5 +1,6 @@ import { Test } from '@nestjs/testing'; -import { BadRequestException } from '@nestjs/common'; +import { BadRequestException, NotFoundException } from '@nestjs/common'; +import { Column, PrimaryGeneratedColumn } from 'typeorm'; import { StockBookmarkRepository } from './stock-bookmark.repository'; import { StockDetailService } from '../detail/stock-detail.service'; import { StockBookmarkService } from './stock-bookmark.service'; @@ -14,6 +15,8 @@ describe('stock bookmark test', () => { existsBy: jest.fn(), create: jest.fn(), insert: jest.fn(), + findOneBy: jest.fn(), + remove: jest.fn(), }; const mockStockDetailService = { getInquirePrice: jest.fn() }; @@ -63,4 +66,31 @@ describe('stock bookmark test', () => { stockBookmarkService.registerBookmark(1, '005930'), ).rejects.toThrow(BadRequestException); }); + + it('존재하는 즐겨찾기를 취소할 경우, DB에서 해당 즐겨찾기가 삭제된다.', async () => { + jest.spyOn(stockBookmarkRepository, 'findOneBy').mockResolvedValue({ + id: 1, + stock_code: '005930', + user_id: 1, + }); + + const removeMock = jest.fn(); + jest + .spyOn(stockBookmarkRepository, 'remove') + .mockImplementation(removeMock); + + await stockBookmarkService.unregisterBookmark(1, '005930'); + + expect(removeMock).toHaveBeenCalled(); + }); + + it('존재하지 않는 즐겨찾기를 취소할 경우, NotFound 예외가 발생한다.', async () => { + jest + .spyOn(stockBookmarkRepository, 'findOneBy') + .mockResolvedValue(undefined); + + await expect( + stockBookmarkService.unregisterBookmark(1, '005930'), + ).rejects.toThrow(NotFoundException); + }); }); From 0e87ffeef98efe05ea040eec6497ca835e41a438 Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 2 Dec 2024 22:35:59 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=85=20test:=20=EC=A6=90=EA=B2=A8?= =?UTF-8?q?=EC=B0=BE=EA=B8=B0=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20API=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1=20#237?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookmark/stock-bookmark.service.spec.ts | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/BE/src/stock/bookmark/stock-bookmark.service.spec.ts b/BE/src/stock/bookmark/stock-bookmark.service.spec.ts index 22a4a2ff..d0bb4197 100644 --- a/BE/src/stock/bookmark/stock-bookmark.service.spec.ts +++ b/BE/src/stock/bookmark/stock-bookmark.service.spec.ts @@ -1,9 +1,9 @@ import { Test } from '@nestjs/testing'; import { BadRequestException, NotFoundException } from '@nestjs/common'; -import { Column, PrimaryGeneratedColumn } from 'typeorm'; import { StockBookmarkRepository } from './stock-bookmark.repository'; import { StockDetailService } from '../detail/stock-detail.service'; import { StockBookmarkService } from './stock-bookmark.service'; +import { StockBookmarkResponseDto } from './dto/stock-bookmark-response,dto'; describe('stock bookmark test', () => { let stockBookmarkService: StockBookmarkService; @@ -17,6 +17,7 @@ describe('stock bookmark test', () => { insert: jest.fn(), findOneBy: jest.fn(), remove: jest.fn(), + findBookmarkWithNameByUserId: jest.fn(), }; const mockStockDetailService = { getInquirePrice: jest.fn() }; @@ -93,4 +94,46 @@ describe('stock bookmark test', () => { stockBookmarkService.unregisterBookmark(1, '005930'), ).rejects.toThrow(NotFoundException); }); + + it('즐겨찾기를 조회할 경우, 해당 종목들의 현재가를 포함한 데이터가 반환된다.', async () => { + jest + .spyOn(stockBookmarkRepository, 'findBookmarkWithNameByUserId') + .mockResolvedValue([ + { + b_id: 1, + b_user_id: 1, + b_stock_code: '005930', + s_code: '005930', + s_name: '삼성전자', + s_market: 'KOSPI', + }, + ]); + + jest.spyOn(stockDetailService, 'getInquirePrice').mockResolvedValue({ + hts_kor_isnm: '삼성전자', + stck_shrn_iscd: '005930', + stck_prpr: '53600', + prdy_vrss: '-600', + prdy_vrss_sign: '5', + prdy_ctrt: '-1.11', + hts_avls: '3199803', + per: '25.15', + stck_mxpr: '70400', + stck_llam: '38000', + is_bookmarked: true, + }); + + const stockBookmarkResponse = new StockBookmarkResponseDto( + '삼성전자', + '005930', + '53600', + '-600', + '5', + '-1.11', + ); + + expect(await stockBookmarkService.getBookmarkList(1)).toEqual([ + stockBookmarkResponse, + ]); + }); });