Skip to content

Commit

Permalink
✅ test: 주식 가격 상승률 기반 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
demian-m00n committed Nov 17, 2024
1 parent e2d8c44 commit c956d58
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions packages/backend/src/stock/stock.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-lines-per-function */
import { instanceToPlain } from 'class-transformer';
import { DataSource } from 'typeorm';
import { Logger } from 'winston';
Expand Down Expand Up @@ -187,4 +188,71 @@ describe('StockService 테스트', () => {
},
]);
});

test('주식 상승률 기준 상위 데이터를 반환한다.', async () => {
const limit = 20;
// QueryBuilder Mock
const queryBuilderMock = {
leftJoin: jest.fn().mockReturnThis(),
select: jest.fn().mockReturnThis(),
orderBy: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
getRawMany: jest.fn().mockResolvedValue([
{
id: 'A005930',
name: '삼성전자',
currentPrice: '100000.0',
changeRate: '2.5',
volume: '500000',
marketCap: '500000000000.00',
},
{
id: 'A051910',
name: 'LG화학',
currentPrice: '75000.0',
changeRate: '-1.2',
volume: '300000',
marketCap: '20000000000.00',
},
]),
};

// Manager Mock
const managerMock = {
getRepository: jest.fn().mockReturnValue({
createQueryBuilder: jest.fn().mockReturnValue(queryBuilderMock),
}),
};
const dataSource = createDataSourceMock(managerMock);
const stockService = new StockService(dataSource as DataSource, logger);

const result = await stockService.getTopStocksByGainers(limit);

expect(managerMock.getRepository).toHaveBeenCalledWith(Stock);
expect(queryBuilderMock.orderBy).toHaveBeenCalledWith(
'stockLiveData.changeRate',
'DESC',
);
expect(queryBuilderMock.limit).toHaveBeenCalledWith(limit);
expect(queryBuilderMock.getRawMany).toHaveBeenCalled();

expect(instanceToPlain(result)).toEqual([
{
id: 'A005930',
name: '삼성전자',
currentPrice: 100000.0,
changeRate: 2.5,
volume: 500000,
marketCap: '500000000000.00',
},
{
id: 'A051910',
name: 'LG화학',
currentPrice: 75000.0,
changeRate: -1.2,
volume: 300000,
marketCap: '20000000000.00',
},
]);
});
});

0 comments on commit c956d58

Please sign in to comment.