From bdedb24a309adf87f4818aac3befb1d015a1b021 Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 2 Dec 2024 21:56:08 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20=EB=A7=88=EC=9D=B4=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=A1=B0=ED=9A=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 --- BE/src/asset/asset-service.spec.ts | 106 ++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/BE/src/asset/asset-service.spec.ts b/BE/src/asset/asset-service.spec.ts index e7cdd9a..96c165e 100644 --- a/BE/src/asset/asset-service.spec.ts +++ b/BE/src/asset/asset-service.spec.ts @@ -1,4 +1,6 @@ import { Test } from '@nestjs/testing'; +import { ApiProperty } from '@nestjs/swagger'; +import { DeepPartial } from 'typeorm'; import { AssetService } from './asset.service'; import { UserStockRepository } from './user-stock.repository'; import { AssetRepository } from './asset.repository'; @@ -6,6 +8,10 @@ import { StockDetailService } from '../stock/detail/stock-detail.service'; import { StockPriceSocketService } from '../stockSocket/stock-price-socket.service'; import { TradeType } from '../stock/order/enum/trade-type'; import { StatusType } from '../stock/order/enum/status-type'; +import { Asset } from './asset.entity'; +import { AssetResponseDto } from './dto/asset-response.dto'; +import { StockElementResponseDto } from './dto/stock-element-response.dto'; +import { MypageResponseDto } from './dto/mypage-response.dto'; describe('asset test', () => { let assetService: AssetService; @@ -15,13 +21,19 @@ describe('asset test', () => { let stockPriceSocketService: StockPriceSocketService; beforeEach(async () => { - const mockUserStockRepository = { findOneBy: jest.fn() }; + const mockUserStockRepository = { + findOneBy: jest.fn(), + findUserStockWithNameByUserId: jest.fn(), + findAllDistinctCode: jest.fn(), + find: jest.fn(), + }; const mockAssetRepository = { findAllPendingOrders: jest.fn(), findOneBy: jest.fn(), + save: jest.fn(), }; - const mockStockDetailService = {}; - const mockStockPriceSocketService = {}; + const mockStockDetailService = { getInquirePrice: jest.fn() }; + const mockStockPriceSocketService = { subscribeByCode: jest.fn() }; const module = await Test.createTestingModule({ providers: [ @@ -103,4 +115,92 @@ describe('asset test', () => { cash_balance: 0, }); }); + + it('마이페이지 조회 시 종목의 현재가를 반영한 총 자산을 반환한다.', async () => { + jest + .spyOn(userStockRepository, 'findUserStockWithNameByUserId') + .mockResolvedValue([ + { + user_stocks_id: 1, + user_stocks_user_id: 1, + user_stocks_stock_code: '005930', + user_stocks_quantity: 1, + user_stocks_avg_price: '1000', + user_stocks_last_updated: new Date(), + stocks_code: '005930', + stocks_name: '삼성전자', + stocks_market: 'KOSPI', + }, + ]); + + jest.spyOn(assetRepository, 'findOneBy').mockResolvedValue({ + id: 1, + user_id: 1, + stock_balance: 0, + cash_balance: 1000, + total_asset: 1000, + total_profit: 0, + total_profit_rate: 0, + }); + + jest + .spyOn(userStockRepository, 'findAllDistinctCode') + .mockResolvedValue([{ stock_code: '005930' }]); + + 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: false, + }); + + jest.spyOn(userStockRepository, 'find').mockResolvedValue([ + { + id: 1, + user_id: 1, + stock_code: '005930', + quantity: 1, + avg_price: 1000, + last_updated: new Date(), + }, + ]); + + jest + .spyOn(assetRepository, 'save') + .mockImplementation((updatedAsset) => + Promise.resolve(updatedAsset as DeepPartial & Asset), + ); + + const assetResponse = new AssetResponseDto( + 1000, + 53600, + 54600, + -9945400, + '-99.45', + false, + ); + const stockElementResponse = new StockElementResponseDto( + '삼성전자', + '005930', + 1, + 1000, + '53600', + '-600', + '5', + '-1.11', + ); + + const expected = new MypageResponseDto(); + expected.asset = assetResponse; + expected.stocks = [stockElementResponse]; + + expect(await assetService.getMyPage(1)).toEqual(expected); + }); });