From acc7ae2c937171706ad5efbd9192b4ff653d833b Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 2 Dec 2024 18:04:24 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20=EC=A3=BC=EC=8B=9D=20?= =?UTF-8?q?=EB=A7=A4=EB=8F=84=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 --- .../stock/order/stock-order.service.spec.ts | 75 +++++++++++++++++-- 1 file changed, 67 insertions(+), 8 deletions(-) diff --git a/BE/src/stock/order/stock-order.service.spec.ts b/BE/src/stock/order/stock-order.service.spec.ts index b5dc8c9..3f41eed 100644 --- a/BE/src/stock/order/stock-order.service.spec.ts +++ b/BE/src/stock/order/stock-order.service.spec.ts @@ -15,6 +15,7 @@ describe('stock order test', () => { let stockOrderService: StockOrderService; let stockOrderRepository: StockOrderRepository; let assetRepository: AssetRepository; + let userStockRepository: UserStockRepository; beforeEach(async () => { const mockStockOrderRepository = { @@ -22,13 +23,9 @@ describe('stock order test', () => { save: jest.fn(), create: jest.fn(), }; - const mockAssetRepository = { - findOneBy: jest.fn(), - }; - const mockStockPriceSocketService = { - subscribeByCode: jest.fn(), - }; - const mockUserStockRepository = {}; + const mockAssetRepository = { findOneBy: jest.fn() }; + const mockStockPriceSocketService = { subscribeByCode: jest.fn() }; + const mockUserStockRepository = { findOneBy: jest.fn() }; const module = await Test.createTestingModule({ providers: [ @@ -46,6 +43,7 @@ describe('stock order test', () => { stockOrderService = module.get(StockOrderService); stockOrderRepository = module.get(StockOrderRepository); assetRepository = module.get(AssetRepository); + userStockRepository = module.get(UserStockRepository); }); it('충분한 자산을 가지고 특정 주식에 대해 매수를 요청할 경우, 요청이 DB에 정상적으로 등록된다.', async () => { @@ -108,14 +106,75 @@ describe('stock order test', () => { }, ]); + await expect( + stockOrderService.buy(1, { + stock_code: '005930', + price: 1000, + amount: 1, + }), + ).rejects.toThrow(BadRequestException); + }); + + it('충분한 주식을 가지고 특정 주식에 대해 매도를 요청할 경우, 요청이 DB에 정상적으로 등록된다.', async () => { + jest.spyOn(userStockRepository, 'findOneBy').mockResolvedValue({ + id: 1, + user_id: 1, + stock_code: '005930', + quantity: 1, + avg_price: 1000, + last_updated: new Date(), + }); + + jest.spyOn(stockOrderRepository, 'findBy').mockResolvedValue([]); + const createMock = jest.fn(); jest.spyOn(stockOrderRepository, 'create').mockImplementation(createMock); const saveMock = jest.fn(); jest.spyOn(stockOrderRepository, 'save').mockImplementation(saveMock); + await stockOrderService.sell(1, { + stock_code: '005930', + price: 1000, + amount: 1, + }); + + expect(createMock).toHaveBeenCalledWith({ + user_id: 1, + stock_code: '005930', + trade_type: TradeType.SELL, + amount: 1, + price: 1000, + status: StatusType.PENDING, + }); + expect(saveMock).toHaveBeenCalled(); + }); + + it('주식이 부족한 상태로 특정 주식에 대해 매도를 요청할 경우, BadRequest 예외가 발생한다.', async () => { + jest.spyOn(userStockRepository, 'findOneBy').mockResolvedValue({ + id: 1, + user_id: 1, + stock_code: '005930', + quantity: 1, + avg_price: 1000, + last_updated: new Date(), + }); + + jest.spyOn(stockOrderRepository, 'findBy').mockResolvedValue([ + { + id: 1, + user_id: 1, + stock_code: '005930', + trade_type: TradeType.SELL, + amount: 1, + price: 1000, + status: StatusType.PENDING, + created_at: new Date(), + }, + ]); + await expect( - stockOrderService.buy(1, { + stockOrderService.sell(1, { stock_code: '005930', price: 1000, amount: 1,