-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
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, | ||
); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟢 요 쪽 코드 한번만 설명해주실 수 있나요? axios를 써서 하려니까 잘 모르겠어서.. 저는 accessToken mock 하는거랑 동일하게 jest로 데이터를 넣었거든요
There was a problem hiding this comment.
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로 구분해서 다른 값 리턴하도록 구현했어요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아~~~~!!!
둘 다 get 메서드로 가져오는데, url은 다르니까! 아 좋습니당!!! 감사합니다 ㅎㅎㅎ