From 2847ff3b2334563fe3ad6253c5d4c6c9d13a9847 Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 2 Dec 2024 15:30:33 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=94=A7=20fix:=20stock=20index=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=95=88=20=EB=93=A4=EC=96=B4?= =?UTF-8?q?=EC=99=94=EC=9D=84=20=EB=95=8C=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/stock/index/stock-index.service.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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( From 675f1c74f3b5f2b49dcc9b2f041f31eda27ef59b Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 2 Dec 2024 15:30:43 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=85=20test:=20stock=20index=20service?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=ED=86=B5?= =?UTF-8?q?=ED=95=A9=20=EB=B0=8F=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mockdata/stock.index.list.mockdata.ts | 0 .../mockdata/stock.index.value.mockdata.ts | 0 .../stock/index/stock.index.service.spec.ts | 86 +++++++++++++++++++ .../stock/index/stock.index.list.e2e-spec.ts | 49 ----------- .../stock/index/stock.index.value.e2e-spec.ts | 48 ----------- 5 files changed, 86 insertions(+), 97 deletions(-) rename BE/{test => src}/stock/index/mockdata/stock.index.list.mockdata.ts (100%) rename BE/{test => src}/stock/index/mockdata/stock.index.value.mockdata.ts (100%) create mode 100644 BE/src/stock/index/stock.index.service.spec.ts delete mode 100644 BE/test/stock/index/stock.index.list.e2e-spec.ts delete mode 100644 BE/test/stock/index/stock.index.value.e2e-spec.ts 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..205d76dd --- /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/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('데이터를 정상적으로 조회하지 못했습니다.'); - }); -}); From 50f22168791b21c1c86ba4738a0fa3b88788ef51 Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 2 Dec 2024 19:06:29 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=9A=9A=20rename:=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=B0=8F=20=EB=AA=A9=20=EB=8D=B0=EC=9D=B4=ED=84=B0?= =?UTF-8?q?=20=ED=8C=8C=EC=9D=BC=EB=AA=85=20=EB=B3=80=EA=B2=BD=20#237?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ck.index.list.mockdata.ts => stock-index-list.mockdata.ts} | 0 ....index.value.mockdata.ts => stock-index-value.mockdata.ts} | 0 ...tock.index.service.spec.ts => stock-index-service.spec.ts} | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename BE/src/stock/index/mockdata/{stock.index.list.mockdata.ts => stock-index-list.mockdata.ts} (100%) rename BE/src/stock/index/mockdata/{stock.index.value.mockdata.ts => stock-index-value.mockdata.ts} (100%) rename BE/src/stock/index/{stock.index.service.spec.ts => stock-index-service.spec.ts} (96%) diff --git a/BE/src/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/src/stock/index/mockdata/stock.index.list.mockdata.ts rename to BE/src/stock/index/mockdata/stock-index-list.mockdata.ts diff --git a/BE/src/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/src/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 similarity index 96% rename from BE/src/stock/index/stock.index.service.spec.ts rename to BE/src/stock/index/stock-index-service.spec.ts index 205d76dd..091977e3 100644 --- a/BE/src/stock/index/stock.index.service.spec.ts +++ b/BE/src/stock/index/stock-index-service.spec.ts @@ -2,8 +2,8 @@ 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 { 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'; From 749ad87e26157ebbb140926af34c29b16dbf22f9 Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 2 Dec 2024 19:11:13 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=9A=9A=20rename:=20service=20?= =?UTF-8?q?=EA=B5=AC=EB=B6=84=EC=9E=90=20.=EC=9C=BC=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20#237?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{stock-index-service.spec.ts => stock-index.service.spec.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename BE/src/stock/index/{stock-index-service.spec.ts => stock-index.service.spec.ts} (100%) diff --git a/BE/src/stock/index/stock-index-service.spec.ts b/BE/src/stock/index/stock-index.service.spec.ts similarity index 100% rename from BE/src/stock/index/stock-index-service.spec.ts rename to BE/src/stock/index/stock-index.service.spec.ts