Skip to content

Commit

Permalink
✅ test: 주식 매도 테스트 코드 작성 #237
Browse files Browse the repository at this point in the history
  • Loading branch information
sieunie committed Dec 2, 2024
1 parent b69ee2c commit acc7ae2
Showing 1 changed file with 67 additions and 8 deletions.
75 changes: 67 additions & 8 deletions BE/src/stock/order/stock-order.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@ describe('stock order test', () => {
let stockOrderService: StockOrderService;
let stockOrderRepository: StockOrderRepository;
let assetRepository: AssetRepository;
let userStockRepository: UserStockRepository;

beforeEach(async () => {
const mockStockOrderRepository = {
findBy: jest.fn(),
save: jest.fn(),
create: jest.fn(),
};
const mockAssetRepository = {
findOneBy: jest.fn(),
};
const mockStockPriceSocketService = {
subscribeByCode: jest.fn(),
};
const mockUserStockRepository = {};
const mockAssetRepository = { findOneBy: jest.fn() };
const mockStockPriceSocketService = { subscribeByCode: jest.fn() };
const mockUserStockRepository = { findOneBy: jest.fn() };

const module = await Test.createTestingModule({
providers: [
Expand All @@ -46,6 +43,7 @@ describe('stock order test', () => {
stockOrderService = module.get(StockOrderService);
stockOrderRepository = module.get(StockOrderRepository);
assetRepository = module.get(AssetRepository);
userStockRepository = module.get(UserStockRepository);
});

it('충분한 자산을 가지고 특정 주식에 대해 매수를 요청할 경우, 요청이 DB에 정상적으로 등록된다.', async () => {
Expand Down Expand Up @@ -108,14 +106,75 @@ describe('stock order test', () => {
},
]);

await expect(
stockOrderService.buy(1, {
stock_code: '005930',
price: 1000,
amount: 1,
}),
).rejects.toThrow(BadRequestException);
});

it('충분한 주식을 가지고 특정 주식에 대해 매도를 요청할 경우, 요청이 DB에 정상적으로 등록된다.', async () => {
jest.spyOn(userStockRepository, 'findOneBy').mockResolvedValue({
id: 1,
user_id: 1,
stock_code: '005930',
quantity: 1,
avg_price: 1000,
last_updated: new Date(),
});

jest.spyOn(stockOrderRepository, 'findBy').mockResolvedValue([]);

const createMock = jest.fn();
jest.spyOn(stockOrderRepository, 'create').mockImplementation(createMock);

const saveMock = jest.fn();
jest.spyOn(stockOrderRepository, 'save').mockImplementation(saveMock);

await stockOrderService.sell(1, {
stock_code: '005930',
price: 1000,
amount: 1,
});

expect(createMock).toHaveBeenCalledWith({
user_id: 1,
stock_code: '005930',
trade_type: TradeType.SELL,
amount: 1,
price: 1000,
status: StatusType.PENDING,
});
expect(saveMock).toHaveBeenCalled();
});

it('주식이 부족한 상태로 특정 주식에 대해 매도를 요청할 경우, BadRequest 예외가 발생한다.', async () => {
jest.spyOn(userStockRepository, 'findOneBy').mockResolvedValue({
id: 1,
user_id: 1,
stock_code: '005930',
quantity: 1,
avg_price: 1000,
last_updated: new Date(),
});

jest.spyOn(stockOrderRepository, 'findBy').mockResolvedValue([
{
id: 1,
user_id: 1,
stock_code: '005930',
trade_type: TradeType.SELL,
amount: 1,
price: 1000,
status: StatusType.PENDING,
created_at: new Date(),
},
]);

await expect(
stockOrderService.buy(1, {
stockOrderService.sell(1, {
stock_code: '005930',
price: 1000,
amount: 1,
Expand Down

0 comments on commit acc7ae2

Please sign in to comment.