Skip to content

Commit

Permalink
✅ test: 주가 지수 값 조회 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
sieunie committed Nov 7, 2024
1 parent 2e10d75 commit 9d8ff33
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
5 changes: 4 additions & 1 deletion BE/src/stock/index/stock.index.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ export class StockIndexService {
accessToken,
);

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

return new StockIndexValueElementDto(
code,
result.output.bstp_nmix_prpr,
result.output.bstp_nmix_prdy_vrss,
result.output.bstp_nmix_prdy_vrss,
result.output.bstp_nmix_prdy_ctrt,
result.output.prdy_vrss_sign,
);
}
Expand Down
55 changes: 55 additions & 0 deletions BE/test/stock/index/mockdata/stock.index.value.mockdata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export const STOCK_INDEX_VALUE_MOCK = {
VALID_DATA: {
data: {
output: {
bstp_nmix_prpr: '857.60',
bstp_nmix_prdy_vrss: '-1.61',
prdy_vrss_sign: '5',
bstp_nmix_prdy_ctrt: '-0.19',
acml_vol: '1312496',
prdy_vol: '1222188',
acml_tr_pbmn: '11507962',
prdy_tr_pbmn: '11203385',
bstp_nmix_oprc: '863.69',
prdy_nmix_vrss_nmix_oprc: '4.48',
oprc_vrss_prpr_sign: '2',
bstp_nmix_oprc_prdy_ctrt: '0.52',
bstp_nmix_hgpr: '864.24',
prdy_nmix_vrss_nmix_hgpr: '5.03',
hgpr_vrss_prpr_sign: '2',
bstp_nmix_hgpr_prdy_ctrt: '0.59',
bstp_nmix_lwpr: '854.72',
prdy_clpr_vrss_lwpr: '-4.49',
lwpr_vrss_prpr_sign: '5',
prdy_clpr_vrss_lwpr_rate: '-0.52',
ascn_issu_cnt: '828',
uplm_issu_cnt: '5',
stnr_issu_cnt: '94',
down_issu_cnt: '716',
lslm_issu_cnt: '1',
dryy_bstp_nmix_hgpr: '890.06',
dryy_hgpr_vrss_prpr_rate: '3.65',
dryy_bstp_nmix_hgpr_date: '20240109',
dryy_bstp_nmix_lwpr: '786.28',
dryy_lwpr_vrss_prpr_rate: '-9.07',
dryy_bstp_nmix_lwpr_date: '20240201',
total_askp_rsqn: '24146999',
total_bidp_rsqn: '40450437',
seln_rsqn_rate: '37.38',
shnu_rsqn_rate: '62.62',
ntby_rsqn: '16303438',
},
rt_cd: '0',
msg_cd: 'MCA00000',
msg1: '정상처리 되었습니다.',
},
},
INVALID_DATA: {
data: {
output: {},
rt_cd: '1',
msg_cd: 'MCA00000',
msg1: '유효하지 않은 토큰입니다.',
},
},
};
4 changes: 2 additions & 2 deletions BE/test/stock/index/stock.index.list.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('stock index list test', () => {
stockIndexService = module.get(StockIndexService);
});

it('주가 지수 API에서 정상적인 데이터를 조회한 경우, 형식에 맞춰 정상적으로 반환한다.', async () => {
it('주가 지수 차트 조회 API에서 정상적인 데이터를 조회한 경우, 형식에 맞춰 정상적으로 반환한다.', async () => {
(axios.get as jest.Mock).mockResolvedValue(
STOCK_INDEX_LIST_MOCK.VALID_DATA,
);
Expand All @@ -37,7 +37,7 @@ describe('stock index list test', () => {
});
});

it('주가 지수 API에서 데이터를 조회하지 못한 경우, 에러를 발생시킨다.', async () => {
it('주가 지수 차트 조회 API에서 데이터를 조회하지 못한 경우, 에러를 발생시킨다.', async () => {
(axios.get as jest.Mock).mockResolvedValue(
STOCK_INDEX_LIST_MOCK.INVALID_DATA,
);
Expand Down
48 changes: 48 additions & 0 deletions BE/test/stock/index/stock.index.value.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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('데이터를 정상적으로 조회하지 못했습니다.');
});
});

0 comments on commit 9d8ff33

Please sign in to comment.