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 acc7ae2 commit a8336ae
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions BE/src/stock/order/stock-order.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Test } from '@nestjs/testing';
import { BadRequestException } from '@nestjs/common';
import {
BadRequestException,
ConflictException,
ForbiddenException,
} from '@nestjs/common';
import { StockOrderService } from './stock-order.service';
import { StockOrderRepository } from './stock-order.repository';
import { StockPriceSocketService } from '../../stockSocket/stock-price-socket.service';
Expand All @@ -22,9 +26,15 @@ describe('stock order test', () => {
findBy: jest.fn(),
save: jest.fn(),
create: jest.fn(),
findOneBy: jest.fn(),
remove: jest.fn(),
existsBy: jest.fn(),
};
const mockAssetRepository = { findOneBy: jest.fn() };
const mockStockPriceSocketService = { subscribeByCode: jest.fn() };
const mockStockPriceSocketService = {
subscribeByCode: jest.fn(),
unsubscribeByCode: jest.fn(),
};
const mockUserStockRepository = { findOneBy: jest.fn() };

const module = await Test.createTestingModule({
Expand Down Expand Up @@ -181,4 +191,64 @@ describe('stock order test', () => {
}),
).rejects.toThrow(BadRequestException);
});

it('사용자 본인의 미체결된 주문에 대해 취소를 요청할 경우, 해당 주문이 DB에서 삭제된다.', async () => {
jest.spyOn(stockOrderRepository, 'findOneBy').mockResolvedValue({
id: 1,
user_id: 1,
stock_code: '005930',
trade_type: TradeType.SELL,
amount: 1,
price: 1000,
status: StatusType.PENDING,
created_at: new Date(),
});

const removeMock = jest.fn();
jest.spyOn(stockOrderRepository, 'remove').mockImplementation(removeMock);

await stockOrderService.cancel(1, 1);

expect(removeMock).toHaveBeenCalled();
});

it('사용자 본인의 주문이 아닌 주문에 대해 취소를 요청할 경우, Forbidden 예외가 발생한다.', async () => {
jest.spyOn(stockOrderRepository, 'findOneBy').mockResolvedValue({
id: 1,
user_id: 2,
stock_code: '005930',
trade_type: TradeType.SELL,
amount: 1,
price: 1000,
status: StatusType.PENDING,
created_at: new Date(),
});

const removeMock = jest.fn();
jest.spyOn(stockOrderRepository, 'remove').mockImplementation(removeMock);

await expect(stockOrderService.cancel(1, 1)).rejects.toThrow(
ForbiddenException,
);
});

it('이미 체결된 주문에 대해 취소를 요청할 경우, Conflict 예외가 발생한다.', async () => {
jest.spyOn(stockOrderRepository, 'findOneBy').mockResolvedValue({
id: 1,
user_id: 1,
stock_code: '005930',
trade_type: TradeType.SELL,
amount: 1,
price: 1000,
status: StatusType.COMPLETE,
created_at: new Date(),
});

const removeMock = jest.fn();
jest.spyOn(stockOrderRepository, 'remove').mockImplementation(removeMock);

await expect(stockOrderService.cancel(1, 1)).rejects.toThrow(
ConflictException,
);
});
});

0 comments on commit a8336ae

Please sign in to comment.