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

bugfix/#11

parent 5f0d6b6d
No related branches found
No related tags found
2 merge requests!31Develop,!14[#11] dto설정, 프렌드,서비스로직 테스트 및 로직변경
......@@ -9,12 +9,10 @@ const friendService = require('../services/friendService'); // FriendService 임
const { Op } = require('sequelize');
beforeAll(async () => {
// 테스트 전에 데이터베이스를 동기화하고 테이블을 생성합니다.
await sequelize.sync({ force: true });
});
beforeEach(async () => {
// 각 테스트 전에 데이터베이스를 초기화하여 독립성을 보장합니다.
await sequelize.sync({ force: true });
// 더미 사용자 생성
......@@ -45,11 +43,12 @@ describe('Friend Service', () => {
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');
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 friend request to self', async () => {
......@@ -57,12 +56,12 @@ describe('Friend Service', () => {
});
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);
// Alice tries to send another friend request to Bob
await expect(friendService.sendFriendRequest(1, 2)).rejects.toThrow('Friend request already exists');
});
......@@ -82,14 +81,13 @@ describe('Friend Service', () => {
});
test('not send request', async () => {
const receivedRequests = await friendService.getReceivedRequests(2);
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 () => {
await friendService.sendFriendRequest(1, 3);
const sentRequests = await friendService.getSentRequests(1);
......@@ -105,12 +103,11 @@ describe('Friend Service', () => {
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');
const updatedRequestDTO = await friendService.acceptFriendRequest(1, 3);
expect(updatedRequestDTO).toBeDefined();
expect(updatedRequestDTO.status).toBe('ACCEPTED');
// Db상태 확인
const request = await Friend.findOne({
......@@ -122,17 +119,15 @@ describe('Friend Service', () => {
expect(request.status).toBe('ACCEPTED');
});
test('없는 요청수락', async () => {
test('should throw error when accepting non-existing friend request', 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);
......@@ -172,16 +167,26 @@ describe('Friend Service', () => {
await friendService.acceptFriendRequest(i, 1);
}
// Alice 친구: Bob (2), Charlie (3),User4 to User23 (20 friends)
// 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);
expect(['Bob', 'Charlie', 'User4', 'User5', 'User6']).toContain(friendsPage1[0].friendInfo.name);
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);
expect(['User7', 'User8', 'User9', 'User10', 'User11']).toContain(friendsPage2[0].friendInfo.name);
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 () => {
......@@ -192,15 +197,12 @@ describe('Friend Service', () => {
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]: [
......
......@@ -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