Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 27.07 주가 지수 서비스 테스트 코드 작성 #237 #240

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion BE/src/stock/index/stock-index.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -109,6 +114,11 @@ export class StockIndexService {
queryParams,
);

if (result.rt_cd !== '0')
throw new InternalServerErrorException(
'데이터를 정상적으로 조회하지 못했습니다.',
);

const data = result.output;

return new StockIndexValueElementDto(
Expand Down
86 changes: 86 additions & 0 deletions BE/src/stock/index/stock.index.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 요 쪽 코드 한번만 설명해주실 수 있나요? axios를 써서 하려니까 잘 모르겠어서.. 저는 accessToken mock 하는거랑 동일하게 jest로 데이터를 넣었거든요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 완벽히 이해하고 쓴 코드는 아니라서... 일단 설명 드리면 axios.get 메소드를 뒤에 있는 콜백 함수로 모킹하는 방식입니다. 근데 여기서 차트 데이터 가지고 오는 api도 get 메소드를 쓰고, 값 가지고 오는 api도 get 메소드를 쓰니까 뒤에 url로 구분해서 다른 값 리턴하도록 구현했어요!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아~~~~!!!
둘 다 get 메서드로 가져오는데, url은 다르니까! 아 좋습니당!!! 감사합니다 ㅎㅎㅎ

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,
);
});
});
49 changes: 0 additions & 49 deletions BE/test/stock/index/stock.index.list.e2e-spec.ts

This file was deleted.

48 changes: 0 additions & 48 deletions BE/test/stock/index/stock.index.value.e2e-spec.ts

This file was deleted.

Loading