From 2c3b0602f346e6be54cf5a4b13e240809b1509ae Mon Sep 17 00:00:00 2001 From: jihwooon Date: Fri, 15 Mar 2024 08:54:37 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=EC=A3=BC=EB=AC=B8=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EB=B0=8F=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 주문 테스트 데이터 (server/src/fixture/orders.fixture.ts) 를 정리했습니다. - 실제 주문 데이터와 모의 주문 데이터를 구분하기 위해 별도의 배열로 분리했습니다. - 실제 주문 데이터는 orders 배열에, 모의 주문 데이터는 mockOrders 배열에 저장합니다. - 주문 목록 조회 서비스 (server/src/orders/application/orders-list.service.test.ts) 의 테스트 케이스를 작성했습니다. - findAll 함수를 모킹하여 테스트 데이터를 반환하도록 설정했습니다. - 테스트 케이스를 통해 서비스가 주문 목록을 정확하게 조회하는지 확인합니다. --- server/src/fixture/orders.fixture.ts | 60 +++++++++++++++++++ .../application/orders-list.service.test.ts | 41 +++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 server/src/fixture/orders.fixture.ts create mode 100644 server/src/orders/application/orders-list.service.test.ts diff --git a/server/src/fixture/orders.fixture.ts b/server/src/fixture/orders.fixture.ts new file mode 100644 index 0000000..b506f0c --- /dev/null +++ b/server/src/fixture/orders.fixture.ts @@ -0,0 +1,60 @@ +import Delivery from 'src/delivery/domain/delivery'; +import Order from 'src/orders/domain/order'; + +export const orders = [ + { + id: 1, + bookTitle: '어린왕자', + totalQuantity: 3, + totalPrice: 6000, + userId: 0, + delivery: { + id: 0, + address: '강원도 춘천시 동내면 대룡산길 227-314 24408 한국', + contact: '010-1234-5667', + receiver: '홍길동', + }, + deliveryId: 0, + createdAt: '2024-03-14T15:59:31.520Z', + }, + { + id: 2, + bookTitle: '어린왕자', + totalQuantity: 3, + totalPrice: 6000, + userId: 0, + delivery: { + id: 0, + address: '강원도 춘천시 동내면 대룡산길 227-314 24408 한국', + contact: '010-1234-5667', + receiver: '홍길동', + }, + deliveryId: 0, + createdAt: '2024-03-14T15:59:31.520Z', + }, +]; + +export const mockOrders = [ + new Order({ + id: 1, + bookTitle: 'Book 1', + totalQuantity: 2, + totalPrice: 20, + delivery: new Delivery({ + address: '123 Street', + receiver: 'John Doe', + contact: '1234567890', + }), + }), + new Order({ + id: 2, + bookTitle: 'Book 2', + totalQuantity: 1, + totalPrice: 15, + delivery: new Delivery({ + address: '456 Avenue', + receiver: 'Jane Smith', + contact: '9876543210', + }), + }), +]; diff --git a/server/src/orders/application/orders-list.service.test.ts b/server/src/orders/application/orders-list.service.test.ts new file mode 100644 index 0000000..8e8b38a --- /dev/null +++ b/server/src/orders/application/orders-list.service.test.ts @@ -0,0 +1,41 @@ +import { mockOrders } from 'src/fixture/orders.fixture'; + +import { findAll } from '../domain/order.repository'; +import { getAllOrders } from './orders-list.service'; + +jest.mock('../domain/order.repository.ts'); + +describe('orderList Service', () => { + beforeEach(() => { + (findAll as jest.Mock).mockResolvedValue(mockOrders); + }); + + describe('getAllOrders', () => { + context('주문 목록을 조회하면', () => { + it('주문 목록 리스트를 반환한다.', async () => { + const orders = await getAllOrders(); + + expect(orders).toEqual([ + { + address: '123 Street', + bookTitle: 'Book 1', + contact: '1234567890', + orderId: 1, + receiver: 'John Doe', + totalPrice: 20, + totalQuantity: 2, + }, + { + address: '456 Avenue', + bookTitle: 'Book 2', + contact: '9876543210', + orderId: 2, + receiver: 'Jane Smith', + totalPrice: 15, + totalQuantity: 1, + }, + ]); + }); + }); + }); +}); From 8a65b53b8427ebd98104d41bdbb604757bae93a1 Mon Sep 17 00:00:00 2001 From: jihwooon Date: Fri, 15 Mar 2024 09:09:18 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=EC=A3=BC=EB=AC=B8=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=97=90=EB=9F=AC=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 주문 목록 조회 서비스 (server/src/orders/application/orders-list.service.ts)에 에러 처리 로직을 추가했습니다. - 주문 목록이 비어있는 경우 NOT_FOUND 에러를 발생시킵니다. - 테스트 코드 (server/src/orders/application/orders-list.service.test.ts)를 업데이트하여 추가된 에러 처리 로직을 검증합니다. - 모의 주문 데이터가 없는 경우 서비스가 예외를 발생하는지 테스트합니다. --- .../application/orders-list.service.test.ts | 15 +++++++++++++++ .../src/orders/application/orders-list.service.ts | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/server/src/orders/application/orders-list.service.test.ts b/server/src/orders/application/orders-list.service.test.ts index 8e8b38a..2a09fec 100644 --- a/server/src/orders/application/orders-list.service.test.ts +++ b/server/src/orders/application/orders-list.service.test.ts @@ -1,5 +1,9 @@ import { mockOrders } from 'src/fixture/orders.fixture'; +import HttpException from 'src/utils/httpException'; + +import { StatusCodes } from 'http-status-codes'; + import { findAll } from '../domain/order.repository'; import { getAllOrders } from './orders-list.service'; @@ -37,5 +41,16 @@ describe('orderList Service', () => { ]); }); }); + + context('주문 목록에 존재하지 않으면', () => { + beforeEach(() => { + (findAll as jest.Mock).mockResolvedValue([]); + }); + it('error를 던져야 한다', async () => { + await expect(getAllOrders()).rejects.toThrow( + new HttpException('주문 목록을 찾을 수 없습니다.', StatusCodes.NOT_FOUND), + ); + }); + }); }); }); diff --git a/server/src/orders/application/orders-list.service.ts b/server/src/orders/application/orders-list.service.ts index 56a0dc5..112ce0a 100644 --- a/server/src/orders/application/orders-list.service.ts +++ b/server/src/orders/application/orders-list.service.ts @@ -1,7 +1,14 @@ +import HttpException from 'src/utils/httpException'; + +import { StatusCodes } from 'http-status-codes'; + import { findAll } from '../domain/order.repository'; export const getAllOrders = async () => { const orders = await findAll(); + if (orders.length === 0) { + throw new HttpException('주문 목록을 찾을 수 없습니다.', StatusCodes.NOT_FOUND); + } const findOrder = orders.map((item) => { return { From ce070ca4412fab7d97dcae719c7be553743be950 Mon Sep 17 00:00:00 2001 From: jihwooon Date: Sat, 16 Mar 2024 13:20:45 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=EC=A3=BC=EB=AC=B8=20=EC=83=81=EC=84=B8=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EB=B0=8F=20=EB=AA=A8=EC=9D=98=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=20=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 주문 상세 조회 서비스 (server/src/orders/application/orders-detail.service.ts)에 대한 테스트 코드를 추가했습니다. - 테스트 데이터 (server/src/fixture/orders.fixture.ts)에 주문 상세 정보를 정의했습니다. --- server/src/fixture/orders.fixture.ts | 2 + .../application/orders-detail.service.test.ts | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 server/src/orders/application/orders-detail.service.test.ts diff --git a/server/src/fixture/orders.fixture.ts b/server/src/fixture/orders.fixture.ts index b506f0c..b449914 100644 --- a/server/src/fixture/orders.fixture.ts +++ b/server/src/fixture/orders.fixture.ts @@ -58,3 +58,5 @@ export const mockOrders = [ }), }), ]; + +export const orderDetailMock = [{ author: '걍구두', bookId: 2, bookTitle: '신델렐라', price: 20000, quantity: 3 }]; diff --git a/server/src/orders/application/orders-detail.service.test.ts b/server/src/orders/application/orders-detail.service.test.ts new file mode 100644 index 0000000..92f889c --- /dev/null +++ b/server/src/orders/application/orders-detail.service.test.ts @@ -0,0 +1,41 @@ +import HttpException from 'src/utils/httpException'; + +import { StatusCodes } from 'http-status-codes'; + +import { orderDetailMock } from 'src/fixture/orders.fixture'; + +import { findAllWithOrderId } from '../domain/order.repository'; +import { getDetailOrders } from './orders-detail.service'; + +jest.mock('../domain/order.repository'); + +describe('orderDetail Service', () => { + describe('getDetailOrders', () => { + const ORDER_ID = 1; + const NOT_FOUNT_ID = 9999; + + beforeEach(() => { + (findAllWithOrderId as jest.Mock).mockReturnValue(orderDetailMock); + }); + context('장바구니에 주문 목록 상품을 선택하면', () => { + it('주문 목록 상품을 반환한다.', async () => { + const order = await getDetailOrders(ORDER_ID); + + expect(order).toStrictEqual([ + { author: '걍구두', bookId: 2, bookTitle: '신델렐라', price: 20000, quantity: 3 }, + ]); + }); + }); + + context('장바구니에 주문 목록 상품이 존재하지 않으면', () => { + beforeEach(() => { + (findAllWithOrderId as jest.Mock).mockReturnValue([]); + }); + it('error를 던져야 한다', async () => { + await expect(getDetailOrders(NOT_FOUNT_ID)).rejects.toThrow( + new HttpException('주문 된 상품을 찾을 수 없습니다.', StatusCodes.NOT_FOUND), + ); + }); + }); + }); +}); From bed5ea7c2a53a29eaf476b2113800bcbab667cc0 Mon Sep 17 00:00:00 2001 From: jihwooon Date: Sat, 16 Mar 2024 13:34:33 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=EC=A3=BC=EB=AC=B8=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=97=90=EB=9F=AC=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 주문 목록 조회 컨트롤러 (server/src/orders/web/orders-list.controller.ts)에 에러 처리 로직을 추가했습니다. - 주문 목록 조회에 실패하면 NOT_FOUND 에러를 발생시킵니다. - 테스트 코드 (server/src/orders/web/orders-list.controller.test.ts)를 업데이트하여 추가된 에러 처리 로직을 검증합니다. - 모의 서비스 에러를 발생시켜 컨트롤러가 예외를 처리하는지 테스트합니다. --- .../orders/web/orders-list.controller.test.ts | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/server/src/orders/web/orders-list.controller.test.ts b/server/src/orders/web/orders-list.controller.test.ts index 0769339..9edd888 100644 --- a/server/src/orders/web/orders-list.controller.test.ts +++ b/server/src/orders/web/orders-list.controller.test.ts @@ -1,9 +1,39 @@ import app from 'src/app'; import request from 'supertest'; +import { StatusCodes } from 'http-status-codes'; +import HttpException from 'src/utils/httpException'; + +import { getAllOrders } from '../application/orders-list.service'; + +jest.mock('../application/orders-list.service.ts'); + describe('getAllOrdersHandler Controller', () => { describe('GET /orders', () => { context('주문 조회에 성공하면', () => { + beforeEach(() => { + (getAllOrders as jest.Mock).mockReturnValue([ + { + address: '강원도 춘천시 동내면 대룡산길 227-314 24408 한국', + bookTitle: '어린왕자', + contact: '010-1234-5667', + orderId: 1, + receiver: '홍길동', + totalPrice: 6000, + totalQuantity: 3, + }, + { + address: '강원도 춘천시 동내면 대룡산길 227-314 24408 한국', + bookTitle: '어린왕자', + contact: '010-1234-5667', + orderId: 2, + receiver: '홍길동', + totalPrice: 6000, + totalQuantity: 3, + }, + ]); + }); + it('200 상태코드와 응답 메세지를 반환한다.', async () => { const { status, body } = await request(app).get('/orders'); @@ -30,5 +60,25 @@ describe('getAllOrdersHandler Controller', () => { ]); }); }); + + context('주문 조회 요청 실패 시', () => { + beforeEach(() => { + (getAllOrders as jest.Mock).mockRejectedValue( + new HttpException('주문 목록을 찾을 수 없습니다.', StatusCodes.NOT_FOUND), + ); + }); + + it('404 상태코드와 에러 메세지를 반환한다', async () => { + const { status, body } = await request(app).get('/orders'); + + expect(status).toBe(404); + expect(body).toEqual({ + message: '주문 목록을 찾을 수 없습니다.', + status: 404, + success: false, + timestamp: expect.any(String), + }); + }); + }); }); });