Skip to content

Commit

Permalink
회원 정보로 배송 정보를 조회하는 DeliveryRepository를 구현하라
Browse files Browse the repository at this point in the history
회원 정보로 회원의 배송지 정보를 조회하는 로직을 구현했습니다
  • Loading branch information
jihwooon committed Nov 3, 2023
1 parent e1bb487 commit d4b05b2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
74 changes: 74 additions & 0 deletions server/src/delivery/domain/delivery.repository.test.ts
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([])
})
})
})
})
30 changes: 30 additions & 0 deletions server/src/delivery/domain/delivery.repository.ts
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'],
}))
}
}

0 comments on commit d4b05b2

Please sign in to comment.