diff --git a/BE/test/stock/index/mockdata/stock.index.list.mockdata.ts b/BE/src/stock/index/mockdata/stock-index-list.mockdata.ts similarity index 100% rename from BE/test/stock/index/mockdata/stock.index.list.mockdata.ts rename to BE/src/stock/index/mockdata/stock-index-list.mockdata.ts diff --git a/BE/test/stock/index/mockdata/stock.index.value.mockdata.ts b/BE/src/stock/index/mockdata/stock-index-value.mockdata.ts similarity index 100% rename from BE/test/stock/index/mockdata/stock.index.value.mockdata.ts rename to BE/src/stock/index/mockdata/stock-index-value.mockdata.ts diff --git a/BE/src/stock/index/stock-index.service.spec.ts b/BE/src/stock/index/stock-index.service.spec.ts new file mode 100644 index 00000000..091977e3 --- /dev/null +++ b/BE/src/stock/index/stock-index.service.spec.ts @@ -0,0 +1,86 @@ +import { Test } from '@nestjs/testing'; +import axios from 'axios'; +import { InternalServerErrorException } from '@nestjs/common'; +import { StockIndexService } from './stock-index.service'; +import { STOCK_INDEX_LIST_MOCK } from './mockdata/stock-index-list.mockdata'; +import { STOCK_INDEX_VALUE_MOCK } from './mockdata/stock-index-value.mockdata'; +import { SocketGateway } from '../../common/websocket/socket.gateway'; +import { KoreaInvestmentDomainService } from '../../common/koreaInvestment/korea-investment.domain-service'; +import { StockIndexListChartElementDto } from './dto/stock-index-list-chart.element.dto'; +import { StockIndexValueElementDto } from './dto/stock-index-value-element.dto'; +import { StockIndexResponseElementDto } from './dto/stock-index-response-element.dto'; +import { StockIndexResponseDto } from './dto/stock-index-response.dto'; + +jest.mock('axios'); + +describe('stock index list test', () => { + let stockIndexService: StockIndexService; + let koreaInvestmentDomainService: KoreaInvestmentDomainService; + + beforeEach(async () => { + const module = await Test.createTestingModule({ + providers: [ + StockIndexService, + SocketGateway, + KoreaInvestmentDomainService, + ], + }).compile(); + + stockIndexService = module.get(StockIndexService); + koreaInvestmentDomainService = module.get(KoreaInvestmentDomainService); + + jest + .spyOn(koreaInvestmentDomainService, 'getAccessToken') + .mockResolvedValue('accessToken'); + }); + + it('주가 지수 차트 조회 API에서 정상적인 데이터를 조회한 경우, 형식에 맞춰 정상적으로 반환한다.', async () => { + (axios.get as jest.Mock).mockImplementation((url: string) => { + if (url.includes('inquire-index-timeprice')) + return STOCK_INDEX_LIST_MOCK.VALID_DATA; + if (url.includes('inquire-index-price')) + return STOCK_INDEX_VALUE_MOCK.VALID_DATA; + return new Error(); + }); + + const stockIndexListValueElementDto = new StockIndexValueElementDto( + STOCK_INDEX_VALUE_MOCK.VALID_DATA.data.output.bstp_nmix_prpr, + STOCK_INDEX_VALUE_MOCK.VALID_DATA.data.output.bstp_nmix_prdy_vrss, + STOCK_INDEX_VALUE_MOCK.VALID_DATA.data.output.bstp_nmix_prdy_ctrt, + STOCK_INDEX_VALUE_MOCK.VALID_DATA.data.output.prdy_vrss_sign, + ); + const stockIndexListChartElementDto = new StockIndexListChartElementDto( + STOCK_INDEX_LIST_MOCK.VALID_DATA.data.output[0].bsop_hour, + STOCK_INDEX_LIST_MOCK.VALID_DATA.data.output[0].bstp_nmix_prpr, + STOCK_INDEX_LIST_MOCK.VALID_DATA.data.output[0].bstp_nmix_prdy_vrss, + ); + + const stockIndexResponseElementDto = new StockIndexResponseElementDto(); + stockIndexResponseElementDto.value = stockIndexListValueElementDto; + stockIndexResponseElementDto.chart = [stockIndexListChartElementDto]; + + const stockIndexResponseDto = new StockIndexResponseDto(); + stockIndexResponseDto.KOSPI = stockIndexResponseElementDto; + stockIndexResponseDto.KOSDAQ = stockIndexResponseElementDto; + stockIndexResponseDto.KOSPI200 = stockIndexResponseElementDto; + stockIndexResponseDto.KSQ150 = stockIndexResponseElementDto; + + expect(await stockIndexService.getDomesticStockIndexList()).toEqual( + stockIndexResponseDto, + ); + }); + + it('주가 지수 차트 조회 API에서 데이터를 조회하지 못한 경우, 에러를 발생시킨다.', async () => { + (axios.get as jest.Mock).mockImplementation((url: string) => { + if (url.includes('inquire-index-timeprice')) + return STOCK_INDEX_LIST_MOCK.INVALID_DATA; + if (url.includes('inquire-index-price')) + return STOCK_INDEX_VALUE_MOCK.INVALID_DATA; + return new Error(); + }); + + await expect(stockIndexService.getDomesticStockIndexList()).rejects.toThrow( + InternalServerErrorException, + ); + }); +}); diff --git a/BE/src/stock/index/stock-index.service.ts b/BE/src/stock/index/stock-index.service.ts index f68aa9d1..62dae6c1 100644 --- a/BE/src/stock/index/stock-index.service.ts +++ b/BE/src/stock/index/stock-index.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, InternalServerErrorException } from '@nestjs/common'; import { StockIndexListChartElementDto } from './dto/stock-index-list-chart.element.dto'; import { StockIndexValueElementDto } from './dto/stock-index-value-element.dto'; import { @@ -87,6 +87,11 @@ export class StockIndexService { queryParams, ); + if (result.rt_cd !== '0') + throw new InternalServerErrorException( + '데이터를 정상적으로 조회하지 못했습니다.', + ); + return result.output.map((element) => { return new StockIndexListChartElementDto( element.bsop_hour, @@ -109,6 +114,11 @@ export class StockIndexService { queryParams, ); + if (result.rt_cd !== '0') + throw new InternalServerErrorException( + '데이터를 정상적으로 조회하지 못했습니다.', + ); + const data = result.output; return new StockIndexValueElementDto( diff --git a/BE/test/stock/index/stock.index.list.e2e-spec.ts b/BE/test/stock/index/stock.index.list.e2e-spec.ts deleted file mode 100644 index 9a844581..00000000 --- a/BE/test/stock/index/stock.index.list.e2e-spec.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { Test } from '@nestjs/testing'; -import axios from 'axios'; -import { StockIndexService } from '../../../src/stock/index/stock-index.service'; -import { STOCK_INDEX_LIST_MOCK } from './mockdata/stock.index.list.mockdata'; - -jest.mock('axios'); - -describe('stock index list test', () => { - let stockIndexService: StockIndexService; - - beforeEach(async () => { - const module = await Test.createTestingModule({ - providers: [StockIndexService], - }).compile(); - - stockIndexService = module.get(StockIndexService); - }); - - it('주가 지수 차트 조회 API에서 정상적인 데이터를 조회한 경우, 형식에 맞춰 정상적으로 반환한다.', async () => { - (axios.get as jest.Mock).mockResolvedValue( - STOCK_INDEX_LIST_MOCK.VALID_DATA, - ); - - expect( - await stockIndexService.getDomesticStockIndexListByCode( - 'code', - 'accessToken', - ), - ).toEqual({ - code: 'code', - chart: [ - { - time: STOCK_INDEX_LIST_MOCK.VALID_DATA.data.output[0].bsop_hour, - value: STOCK_INDEX_LIST_MOCK.VALID_DATA.data.output[0].bstp_nmix_prpr, - }, - ], - }); - }); - - it('주가 지수 차트 조회 API에서 데이터를 조회하지 못한 경우, 에러를 발생시킨다.', async () => { - (axios.get as jest.Mock).mockResolvedValue( - STOCK_INDEX_LIST_MOCK.INVALID_DATA, - ); - - await expect( - stockIndexService.getDomesticStockIndexListByCode('code', 'accessToken'), - ).rejects.toThrow('데이터를 정상적으로 조회하지 못했습니다.'); - }); -}); diff --git a/BE/test/stock/index/stock.index.value.e2e-spec.ts b/BE/test/stock/index/stock.index.value.e2e-spec.ts deleted file mode 100644 index 2008f2e4..00000000 --- a/BE/test/stock/index/stock.index.value.e2e-spec.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { Test } from '@nestjs/testing'; -import axios from 'axios'; -import { StockIndexService } from '../../../src/stock/index/stock-index.service'; -import { STOCK_INDEX_VALUE_MOCK } from './mockdata/stock.index.value.mockdata'; - -jest.mock('axios'); - -describe('stock index list test', () => { - let stockIndexService: StockIndexService; - - beforeEach(async () => { - const module = await Test.createTestingModule({ - providers: [StockIndexService], - }).compile(); - - stockIndexService = module.get(StockIndexService); - }); - - it('주가 지수 값 조회 API에서 정상적인 데이터를 조회한 경우, 형식에 맞춰 정상적으로 반환한다.', async () => { - (axios.get as jest.Mock).mockResolvedValue( - STOCK_INDEX_VALUE_MOCK.VALID_DATA, - ); - - expect( - await stockIndexService.getDomesticStockIndexValueByCode( - 'code', - 'accessToken', - ), - ).toEqual({ - code: 'code', - value: STOCK_INDEX_VALUE_MOCK.VALID_DATA.data.output.bstp_nmix_prpr, - diff: STOCK_INDEX_VALUE_MOCK.VALID_DATA.data.output.bstp_nmix_prdy_vrss, - diffRate: - STOCK_INDEX_VALUE_MOCK.VALID_DATA.data.output.bstp_nmix_prdy_ctrt, - sign: STOCK_INDEX_VALUE_MOCK.VALID_DATA.data.output.prdy_vrss_sign, - }); - }); - - it('주가 지수 값 조회 API에서 데이터를 조회하지 못한 경우, 에러를 발생시킨다.', async () => { - (axios.get as jest.Mock).mockResolvedValue( - STOCK_INDEX_VALUE_MOCK.INVALID_DATA, - ); - - await expect( - stockIndexService.getDomesticStockIndexValueByCode('code', 'accessToken'), - ).rejects.toThrow('데이터를 정상적으로 조회하지 못했습니다.'); - }); -});