-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
회원 정보로 배송 정보를 조회하는 DeliveryRepository를 구현하라
회원 정보로 회원의 배송지 정보를 조회하는 로직을 구현했습니다
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
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,74 @@ | ||
import { RowDataPacket } from 'mysql2/promise' | ||
import { MYSQL_CONNECTION } from '../../database/constants' | ||
import { DeliveryRepository } from './delivery.repository' | ||
import { userMock } from '../../fixture/memberFixture' | ||
import { dbDeliveriesMock } from '../../fixture/deliveryFixture' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
|
||
describe('DeliveryRepository class', () => { | ||
let deliveryRepository: DeliveryRepository | ||
|
||
const DeliveryRepositoryMock = { | ||
execute: jest.fn(), | ||
} | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
DeliveryRepository, | ||
{ | ||
provide: MYSQL_CONNECTION, | ||
useValue: DeliveryRepositoryMock, | ||
}, | ||
], | ||
}).compile() | ||
|
||
deliveryRepository = module.get<DeliveryRepository>(DeliveryRepository) | ||
}) | ||
|
||
describe('findByMember method', () => { | ||
context('Member 정보가 주어지고 조회 값을 반환하면', () => { | ||
beforeEach(() => { | ||
DeliveryRepositoryMock.execute.mockResolvedValue([[dbDeliveriesMock] as RowDataPacket[], []]) | ||
}) | ||
it('배송지 정보를 리턴해야 한다', async () => { | ||
const deliveries = await deliveryRepository.findByMember(userMock()) | ||
|
||
const expectedResults = dbDeliveriesMock.map((row) => ({ | ||
id: row['delivery_id'], | ||
deliveryName: row['delivery_name'], | ||
deliveryFee: row['delivery_fee'], | ||
member: row['member_id'], | ||
createTime: row['create_time'], | ||
updateTime: row['update_time'], | ||
createBy: row['create_by'], | ||
modifiedBy: row['modified_by'], | ||
})) | ||
|
||
expect(deliveries).toStrictEqual(expectedResults) | ||
}) | ||
}) | ||
|
||
context('Member 정보가 주어지고 undefined를 반환하면', () => { | ||
beforeEach(() => { | ||
DeliveryRepositoryMock.execute.mockResolvedValue([[undefined] as RowDataPacket[], []]) | ||
}) | ||
it('빈 배열을 리턴해야 한다', async () => { | ||
const deliveries = await deliveryRepository.findByMember(userMock()) | ||
|
||
expect(deliveries).toStrictEqual([]) | ||
}) | ||
}) | ||
|
||
context('Member 정보가 주어지고 null를 반환하면', () => { | ||
beforeEach(() => { | ||
DeliveryRepositoryMock.execute.mockResolvedValue([[null] as RowDataPacket[], []]) | ||
}) | ||
it('빈 배열을 리턴해야 한다', async () => { | ||
const deliveries = await deliveryRepository.findByMember(userMock()) | ||
|
||
expect(deliveries).toStrictEqual([]) | ||
}) | ||
}) | ||
}) | ||
}) |
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,30 @@ | ||
import { Connection, RowDataPacket } from 'mysql2/promise' | ||
import { Inject } from '@nestjs/common' | ||
import { MYSQL_CONNECTION } from '../../database/constants' | ||
import { Member } from '../../members/domain/member.entity' | ||
import { Delivery } from './delivery.entity' | ||
|
||
export class DeliveryRepository { | ||
constructor( | ||
@Inject(MYSQL_CONNECTION) | ||
private connection: Connection, | ||
) {} | ||
|
||
async findByMember(member: Member): Promise<Delivery[]> { | ||
const [[row]] = await this.connection.execute<RowDataPacket[]>( | ||
'SELECT delivery_id, delivery_name, delivery_fee, member_id, create_time, update_time, create_by, modified_by FROM delivery WHERE member_id = ?', | ||
[member.memberId], | ||
) | ||
|
||
return (row ?? []).map((row) => ({ | ||
id: row['delivery_id'], | ||
deliveryName: row['delivery_name'], | ||
deliveryFee: row['delivery_fee'], | ||
member: row['member_id'], | ||
createTime: row['create_time'], | ||
updateTime: row['update_time'], | ||
createBy: row['create_by'], | ||
modifiedBy: row['modified_by'], | ||
})) | ||
} | ||
} |