Skip to content
Snippets Groups Projects
Commit a761f6de authored by tpgus2603's avatar tpgus2603
Browse files

bugfix/#11

parent 5f0d6b6d
Branches
No related tags found
2 merge requests!31Develop,!14[#11] dto설정, 프렌드,서비스로직 테스트 및 로직변경
......@@ -9,212 +9,214 @@ const friendService = require('../services/friendService'); // FriendService 임
const { Op } = require('sequelize');
beforeAll(async () => {
// 테스트 전에 데이터베이스를 동기화하고 테이블을 생성합니다.
await sequelize.sync({ force: true });
await sequelize.sync({ force: true });
});
beforeEach(async () => {
// 각 테스트 전에 데이터베이스를 초기화하여 독립성을 보장합니다.
await sequelize.sync({ force: true });
// 더미 사용자 생성
await User.bulkCreate([
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' },
{ id: 3, name: 'Charlie', email: 'charlie@example.com' },
]);
await sequelize.sync({ force: true });
// 더미 사용자 생성
await User.bulkCreate([
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' },
{ id: 3, name: 'Charlie', email: 'charlie@example.com' },
]);
});
afterAll(async () => {
// 모든 테스트가 끝난 후 데이터베이스 연결을 종료합니다.
await sequelize.close();
// 모든 테스트가 끝난 후 데이터베이스 연결을 종료합니다.
await sequelize.close();
});
describe('Friend Service', () => {
describe('validUser', () => {
test('should return user when user exists', async () => {
const user = await friendService.validUser(1);
expect(user).toBeDefined();
expect(user.name).toBe('Alice');
});
describe('validUser', () => {
test('should return user when user exists', async () => {
const user = await friendService.validUser(1);
expect(user).toBeDefined();
expect(user.name).toBe('Alice');
});
test('should throw error when user does not exist', async () => {
await expect(friendService.validUser(999)).rejects.toThrow('User not found');
});
});
describe('sendFriendRequest', () => {
test('should send a friend request successfully', async () => {
const friendRequest = await friendService.sendFriendRequest(1, 3); // Alice sends request to Charlie
expect(friendRequest).toBeDefined();
expect(friendRequest.requester_id).toBe(1);
expect(friendRequest.receiver_id).toBe(3);
expect(friendRequest.status).toBe('PENDING');
test('should throw error when user does not exist', async () => {
await expect(friendService.validUser(999)).rejects.toThrow('User not found');
});
});
test('should throw error when sending friend request to self', async () => {
await expect(friendService.sendFriendRequest(1, 1)).rejects.toThrow('Cannot send friend request to yourself');
});
describe('sendFriendRequest', () => {
test('should send a friend request successfully', async () => {
const friendRequestDTO = await friendService.sendFriendRequest(1, 3); // Alice sends request to Charlie
console.log('sendFriendRequest DTO:', friendRequestDTO); // 디버깅을 위한 로그 추가
expect(friendRequestDTO).toBeDefined();
expect(friendRequestDTO.requester.id).toBe(1);
expect(friendRequestDTO.receiver.id).toBe(3);
expect(friendRequestDTO.status).toBe('PENDING');
});
test('should throw error when sending duplicate friend request', async () => {
await friendService.sendFriendRequest(1, 2);
test('should throw error when sending friend request to self', async () => {
await expect(friendService.sendFriendRequest(1, 1)).rejects.toThrow('Cannot send friend request to yourself');
});
await friendService.acceptFriendRequest(2, 1);
test('should throw error when sending duplicate friend request', async () => {
// Alice sends a friend request to Bob
await friendService.sendFriendRequest(1, 2);
// Bob accepts Alice's request
await friendService.acceptFriendRequest(2, 1);
await expect(friendService.sendFriendRequest(1, 2)).rejects.toThrow('Friend request already exists');
});
// Alice tries to send another friend request to Bob
await expect(friendService.sendFriendRequest(1, 2)).rejects.toThrow('Friend request already exists');
});
test('should throw error when user does not exist', async () => {
await expect(friendService.sendFriendRequest(1, 999)).rejects.toThrow('User not found');
await expect(friendService.sendFriendRequest(999, 1)).rejects.toThrow('User not found');
test('should throw error when user does not exist', async () => {
await expect(friendService.sendFriendRequest(1, 999)).rejects.toThrow('User not found');
await expect(friendService.sendFriendRequest(999, 1)).rejects.toThrow('User not found');
});
});
});
describe('getReceivedRequests', () => {
test('friend requests', async () => {
await friendService.sendFriendRequest(3, 1);
describe('getReceivedRequests', () => {
test('friend requests', async () => {
await friendService.sendFriendRequest(3, 1);
const receivedRequests = await friendService.getReceivedRequests(1);
expect(receivedRequests.length).toBe(1);
expect(receivedRequests[0].requester.name).toBe('Charlie');
});
const receivedRequests = await friendService.getReceivedRequests(1);
expect(receivedRequests.length).toBe(1);
expect(receivedRequests[0].requester.name).toBe('Charlie');
});
test('not send request', async () => {
const receivedRequests = await friendService.getReceivedRequests(2);
expect(receivedRequests.length).toBe(0);
test('not send request', async () => {
const receivedRequests = await friendService.getReceivedRequests(2); // Bob has no pending requests
expect(receivedRequests.length).toBe(0);
});
});
});
describe('getSentRequests', () => {
test('should retrieve sent friend requests', async () => {
describe('getSentRequests', () => {
test('should retrieve sent friend requests', async () => {
await friendService.sendFriendRequest(1, 3);
await friendService.sendFriendRequest(1, 3);
const sentRequests = await friendService.getSentRequests(1);
expect(sentRequests.length).toBe(1);
expect(sentRequests[0].receiver.name).toBe('Charlie');
});
const sentRequests = await friendService.getSentRequests(1);
expect(sentRequests.length).toBe(1);
expect(sentRequests[0].receiver.name).toBe('Charlie');
test('should return empty array when no sent requests', async () => {
const sentRequests = await friendService.getSentRequests(3); // Charlie has not sent any PENDING requests
expect(sentRequests.length).toBe(0);
});
});
test('should return empty array when no sent requests', async () => {
const sentRequests = await friendService.getSentRequests(3); // Charlie has not sent any PENDING requests
expect(sentRequests.length).toBe(0);
});
});
describe('acceptFriendRequest', () => {
test('should accept a pending friend request successfully', async () => {
await friendService.sendFriendRequest(3, 1);
const updatedRequest = await friendService.acceptFriendRequest(1, 3);
expect(updatedRequest).toBeDefined();
expect(updatedRequest.status).toBe('ACCEPTED');
//Db상태 확인
const request = await Friend.findOne({
where: {
requester_id: 3,
receiver_id: 1,
},
});
expect(request.status).toBe('ACCEPTED');
});
describe('acceptFriendRequest', () => {
test('should accept a pending friend request successfully', async () => {
await friendService.sendFriendRequest(3, 1);
test('없는 요청수락', async () => {
await expect(friendService.acceptFriendRequest(1, 999)).rejects.toThrow('Friend request not found');
});
});
describe('rejectFriendRequest', () => {
test('should reject a pending friend request successfully', async () => {
await friendService.sendFriendRequest(2, 3);
const result = await friendService.rejectFriendRequest(3, 2);
expect(result).toBe(1);
const request = await Friend.findOne({
where: {
requester_id: 2,
receiver_id: 3,
},
});
expect(request).toBeNull();
});
const updatedRequestDTO = await friendService.acceptFriendRequest(1, 3);
expect(updatedRequestDTO).toBeDefined();
expect(updatedRequestDTO.status).toBe('ACCEPTED');
test('should throw error when rejecting non-existing friend request', async () => {
await expect(friendService.rejectFriendRequest(1, 999)).rejects.toThrow('Friend request not found');
});
});
describe('getFriendList', () => {
test('should retrieve friend list with correct pagination', async () => {
await friendService.sendFriendRequest(1, 2);
await friendService.acceptFriendRequest(2, 1);
await friendService.sendFriendRequest(1, 3);
await friendService.acceptFriendRequest(3, 1);
// 추가 더미데이터 생성
for (let i = 4; i <= 23; i++) {
// Create dummy users
await User.create({
id: i,
name: `User${i}`,
email: `user${i}@example.com`,
});
//Alice랑 친구맺기
await friendService.sendFriendRequest(1, i);
await friendService.acceptFriendRequest(i, 1);
}
// Alice 친구: Bob (2), Charlie (3),User4 to User23 (20 friends)
const limit = 5;
const offset = 0;
const friendsPage1 = await friendService.getFriendList(1, limit, offset);
expect(friendsPage1.length).toBe(limit);
expect(['Bob', 'Charlie', 'User4', 'User5', 'User6']).toContain(friendsPage1[0].friendInfo.name);
const friendsPage2 = await friendService.getFriendList(1, limit, limit);
expect(friendsPage2.length).toBe(limit);
expect(['User7', 'User8', 'User9', 'User10', 'User11']).toContain(friendsPage2[0].friendInfo.name);
// Db상태 확인
const request = await Friend.findOne({
where: {
requester_id: 3,
receiver_id: 1,
},
});
expect(request.status).toBe('ACCEPTED');
});
test('should throw error when accepting non-existing friend request', async () => {
await expect(friendService.acceptFriendRequest(1, 999)).rejects.toThrow('Friend request not found');
});
});
test('should return empty array when user has no friends', async () => {
const friends = await friendService.getFriendList(999); // Non-existing user
expect(friends.length).toBe(0);
describe('rejectFriendRequest', () => {
test('should reject a pending friend request successfully', async () => {
await friendService.sendFriendRequest(2, 3);
const result = await friendService.rejectFriendRequest(3, 2);
expect(result).toBe(1);
const request = await Friend.findOne({
where: {
requester_id: 2,
receiver_id: 3,
},
});
expect(request).toBeNull();
});
test('should throw error when rejecting non-existing friend request', async () => {
await expect(friendService.rejectFriendRequest(1, 999)).rejects.toThrow('Friend request not found');
});
});
});
describe('deleteFriend', () => {
test('should delete an existing friend relationship successfully', async () => {
await friendService.sendFriendRequest(1, 2);
await friendService.acceptFriendRequest(2, 1);
const result = await friendService.deleteFriend(1, 2);
expect(result).toBe(1);
const relationship = await Friend.findOne({
where: {
[Op.or]: [
{ requester_id: 1, receiver_id: 2 },
{ requester_id: 2, receiver_id: 1 },
],
status: 'ACCEPTED',
},
});
expect(relationship).toBeNull();
describe('getFriendList', () => {
test('should retrieve friend list with correct pagination', async () => {
await friendService.sendFriendRequest(1, 2);
await friendService.acceptFriendRequest(2, 1);
await friendService.sendFriendRequest(1, 3);
await friendService.acceptFriendRequest(3, 1);
// 추가 더미데이터 생성
for (let i = 4; i <= 23; i++) {
// Create dummy users
await User.create({
id: i,
name: `User${i}`,
email: `user${i}@example.com`,
});
// Alice랑 친구맺기
await friendService.sendFriendRequest(1, i);
await friendService.acceptFriendRequest(i, 1);
}
// Alice 친구: Bob (2), Charlie (3), User4부터 User23까지 (총 22명)
const limit = 5;
const offset = 0;
const friendsPage1 = await friendService.getFriendList(1, limit, offset);
//console.log('getFriendList Page 1:', friendsPage1); // 디버깅을 위한 로그 추가
expect(friendsPage1.length).toBe(limit);
const expectedNamesPage1 = ['Bob', 'Charlie', 'User4', 'User5', 'User6'];
const receivedNamesPage1 = friendsPage1.map(friend => friend.friendInfo.name);
expectedNamesPage1.forEach(name => {
expect(receivedNamesPage1).toContain(name);
});
const friendsPage2 = await friendService.getFriendList(1, limit, limit);
//console.log('getFriendList Page 2:', friendsPage2); // 디버깅을 위한 로그 추가
expect(friendsPage2.length).toBe(limit);
const expectedNamesPage2 = ['User7', 'User8', 'User9', 'User10', 'User11'];
const receivedNamesPage2 = friendsPage2.map(friend => friend.friendInfo.name);
expectedNamesPage2.forEach(name => {
expect(receivedNamesPage2).toContain(name);
});
});
test('should return empty array when user has no friends', async () => {
const friends = await friendService.getFriendList(999); // Non-existing user
expect(friends.length).toBe(0);
});
});
test('should throw error when deleting a non-existing friend relationship', async () => {
await expect(friendService.deleteFriend(1, 999)).rejects.toThrow('Friend relationship not found');
describe('deleteFriend', () => {
test('should delete an existing friend relationship successfully', async () => {
await friendService.sendFriendRequest(1, 2);
await friendService.acceptFriendRequest(2, 1);
const result = await friendService.deleteFriend(1, 2);
expect(result).toBe(1);
const relationship = await Friend.findOne({
where: {
[Op.or]: [
{ requester_id: 1, receiver_id: 2 },
{ requester_id: 2, receiver_id: 1 },
],
status: 'ACCEPTED',
},
});
expect(relationship).toBeNull();
});
test('should throw error when deleting a non-existing friend relationship', async () => {
await expect(friendService.deleteFriend(1, 999)).rejects.toThrow('Friend relationship not found');
});
});
});
});
......@@ -94,17 +94,6 @@ describe('User and Friend Relationships', () => {
expect(bob.sentRequests[0].receiver.name).toBe('Alice');
});
test('self friend reqeust', async () => {
await expect(
Friend.create({
id: 3,
requester_id: 1,
receiver_id: 1, // 자신에게 요청
status: 'PENDING',
})
).rejects.toThrow();
});
test('already request test', async () => {
// Alice가 Bob에게 이미 친구 요청을 보냈으므로, 다시 보내면 에러 발생
await expect(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment