Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 팔로우 소식 도메인 테스트 코드 작성 #460

Merged
merged 9 commits into from
Nov 12, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.depromeet.mock.followinglog;

import com.depromeet.fixture.domain.friend.FriendFixture;
import com.depromeet.followinglog.domain.FollowingMemoryLog;
import com.depromeet.followinglog.port.out.persistence.FollowingMemoryLogPersistencePort;
import com.depromeet.friend.domain.Friend;
import com.depromeet.member.domain.Member;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class FakeFollowingMemoryLogRepository implements FollowingMemoryLogPersistencePort {
private Long followingMemoryLogAutoGeneratedId = 1L;
private Long friendAutoGeneratedId = 1L;
private List<FollowingMemoryLog> followingMemoryLogs = new ArrayList<>();
private List<Friend> friends = new ArrayList<>();

@Override
public Long save(FollowingMemoryLog followingMemoryLog) {
if (followingMemoryLog.getId() == null || followingMemoryLog.getId().equals(0L)) {
FollowingMemoryLog newFollowingMemoryLog =
FollowingMemoryLog.builder()
.id(++followingMemoryLogAutoGeneratedId)
penrose15 marked this conversation as resolved.
Show resolved Hide resolved
.memory(followingMemoryLog.getMemory())
.createdAt(LocalDateTime.now())
.build();
followingMemoryLogs.add(newFollowingMemoryLog);
return newFollowingMemoryLog.getId();
} else {
followingMemoryLogs.removeIf(item -> item.getId().equals(followingMemoryLog.getId()));
followingMemoryLogs.add(followingMemoryLog);
return followingMemoryLog.getId();
}
}

@Override
public List<FollowingMemoryLog> findLogsByMemberIdAndCursorId(Long memberId, Long cursorId) {
List<Long> friendFollowingIds =
friends.stream()
.filter(item -> item.getMember().getId().equals(memberId))
.map(item -> item.getFollowing().getId())
.toList();

List<FollowingMemoryLog> result =
followingMemoryLogs.stream()
.filter(
item ->
friendFollowingIds.contains(
item.getMemory().getMember().getId()))
.filter(item -> cursorIdLt(cursorId, item))
.sorted((o1, o2) -> (int) (o2.getId() - o1.getId()))
.toList();
return result.subList(0, Math.min(result.size(), 11));
}

private boolean cursorIdLt(Long cursorId, FollowingMemoryLog followingMemoryLog) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요기는 왜 @Override가 없나요??

Copy link
Collaborator Author

@penrose15 penrose15 Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FollowingMemoryLogRepository의 cursorIdLt() 메서드는 FollowingMemoryLogPersistencePort에는 없는 메서드라 @Override 를 할 수 없습니다. 또한 FollowingMemoryLogRepository의 cursorIdLt() 메서드 의 반환 타입은 BooleanExpression로 queryDSL에 종속된 클래스이므로 domain-module에 속한 persistencePort에서 사용할 수 없어 FollowingMemoryLogPersistencePort 인터페이스에 선언하지 않았습니다.

if (cursorId == null) return true;
return followingMemoryLog.getId() < cursorId;
}

@Override
public void deleteAllByMemoryIds(List<Long> memoryIds) {
followingMemoryLogs.removeIf(item -> memoryIds.contains(item.getMemory().getId()));
}

@Override
public void deleteAllByMemoryId(Long memoryId) {
followingMemoryLogs.removeIf(item -> item.getMemory().getId().equals(memoryId));
}

public List<FollowingMemoryLog> getFollowingMemoryLogs() {
return this.followingMemoryLogs;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거는 테스트코드에서만 쓰이는 메서드인가요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 테스트 코드에서만 사용하는 코드입니다.

}

public void saveFriends(Member member, List<Member> followings) {
for (Member following : followings) {
Friend friend = FriendFixture.makeFriends(friendAutoGeneratedId++, member, following);
friends.add(friend);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.depromeet.service.followinglog;

import static org.assertj.core.api.Assertions.assertThat;

import com.depromeet.fixture.domain.member.MemberFixture;
import com.depromeet.fixture.domain.memory.MemoryFixture;
import com.depromeet.followinglog.domain.FollowingMemoryLog;
import com.depromeet.followinglog.domain.vo.FollowingLogSlice;
import com.depromeet.followinglog.port.in.command.CreateFollowingMemoryCommand;
import com.depromeet.followinglog.service.FollowingMemoryLogService;
import com.depromeet.member.domain.Member;
import com.depromeet.memory.domain.Memory;
import com.depromeet.mock.followinglog.FakeFollowingMemoryLogRepository;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class FollowingMemoryLogServiceTest {
private FakeFollowingMemoryLogRepository followingMemoryLogRepository;
private FollowingMemoryLogService followingMemoryLogService;

private Long memberId = 1L;

@BeforeEach
void init() {
followingMemoryLogRepository = new FakeFollowingMemoryLogRepository();
followingMemoryLogService = new FollowingMemoryLogService(followingMemoryLogRepository);
}

@Test
void 팔로잉_소식_추가() {
// given
Member member = MemberFixture.make(1L);
Memory memory = MemoryFixture.make(1L, member, LocalDate.now());

// when
followingMemoryLogService.save(new CreateFollowingMemoryCommand(memory));

// then
List<FollowingMemoryLog> followingMemoryLogs =
followingMemoryLogRepository.getFollowingMemoryLogs();
assertThat(followingMemoryLogs).isNotEmpty();
}

@Test
void 팔로잉_소식_cursorId_없이_조회() {
// given
saveFollowingMemoryLogs();

// when
FollowingLogSlice followingLogSlice =
followingMemoryLogService.findLogsByMemberIdAndCursorId(memberId, null);

// then
List<FollowingMemoryLog> result = followingLogSlice.getContents();
List<Long> followingMemoryLogIds =
result.stream().mapToLong(FollowingMemoryLog::getId).boxed().toList();

assertThat(result.size()).isEqualTo(10);
assertThat(followingMemoryLogIds).isSortedAccordingTo(Comparator.reverseOrder());
assertThat(followingLogSlice.isHasNext()).isTrue();
assertThat(followingLogSlice.getCursorId()).isEqualTo(11L);
}

@Test
void 팔로잉_소식_cursorId_추가해서_조회() {
// given
saveFollowingMemoryLogs();

// when
FollowingLogSlice followingLogSlice =
followingMemoryLogService.findLogsByMemberIdAndCursorId(memberId, 11L);

// then
List<FollowingMemoryLog> result = followingLogSlice.getContents();
List<Long> followingMemoryLogIds =
result.stream().mapToLong(FollowingMemoryLog::getId).boxed().toList();

assertThat(result.size()).isEqualTo(9);
assertThat(followingMemoryLogIds).isSortedAccordingTo(Comparator.reverseOrder());
assertThat(followingLogSlice.isHasNext()).isFalse();
assertThat(followingLogSlice.getCursorId()).isEqualTo(null);
}

private void saveFollowingMemoryLogs() {
Member member = MemberFixture.make(memberId);
List<Member> members = getMembers();

List<Memory> memories = getMemories(members);
followingMemoryLogRepository.saveFriends(member, members);

for (Memory m : memories) {
followingMemoryLogService.save(new CreateFollowingMemoryCommand(m));
}
}
Comment on lines +88 to +98
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저희 친구관계가 생기기 전의 log도 저장이 되었나요 !

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아니요. 해당 메서드는 팔로잉_소식_cursorId_없이_조회() 에서 더미 데이터를 만들기 위해 만들어진 메서드로 Member, Memory를 만든 후, 팔로우 관계를 맺은 상태에서 FollowingMemoryLog를 만드는 로직입니다. 해당 과정이 필요한 이유는 팔로잉 소식 조회 시 Friend 를 조인하여 조회하기 때문입니다.


private List<Member> getMembers() {
List<Member> members = new ArrayList<>();
long followingMemberId = 2L;
for (int i = 2; i <= 20; i++) {
Member followings = MemberFixture.make(followingMemberId++);
members.add(followings);
}
return members;
}

private List<Memory> getMemories(List<Member> members) {
long memoryId = 1L;
List<Memory> memories = new ArrayList<>();
for (Member m : members) {
Memory followingsMemory = MemoryFixture.make(memoryId++, m, LocalDate.now());
memories.add(followingsMemory);
}
return memories;
}

@Test
void memoryIds로_팔로잉_소식_삭제() {
// given
Member member = MemberFixture.make(memberId);
List<Member> members = getMembers();

List<Memory> memories = getMemories(members);
followingMemoryLogRepository.saveFriends(member, members);

for (Memory m : memories) {
followingMemoryLogService.save(new CreateFollowingMemoryCommand(m));
}

// when
List<Long> deleteMemoryIds =
memories.stream()
.mapToLong(Memory::getId)
.boxed()
.filter(id -> id % 2 == 0)
.toList();
followingMemoryLogService.deleteAllByMemoryIds(deleteMemoryIds);

// then
int remainFollowingMemoryLogCount = memories.size() - deleteMemoryIds.size();
List<FollowingMemoryLog> followingMemoryLogs =
followingMemoryLogRepository.getFollowingMemoryLogs();
assertThat(followingMemoryLogs.size()).isEqualTo(remainFollowingMemoryLogCount);
}

@Test
void memoryId로_팔로잉_소식_삭제() {
// given
saveFollowingMemoryLogs();

// when
int beforeDeleteFollowingMemoryLogCount =
followingMemoryLogRepository.getFollowingMemoryLogs().size();
followingMemoryLogService.deleteAllByMemoryId(1L);

// then
List<FollowingMemoryLog> followingMemoryLogs =
followingMemoryLogRepository.getFollowingMemoryLogs();
assertThat(followingMemoryLogs.size()).isEqualTo(beforeDeleteFollowingMemoryLogCount - 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ void init() {
@Test
void 팔로우_추가() {
// given
Member member = MemberFixture.make(1L, "USER");
Member following = MemberFixture.make(2L, "USER");
Member member = MemberFixture.make(1L);
Member following = MemberFixture.make(2L);

// when
boolean isFollowed = followService.addOrDeleteFollow(member, following);
Expand All @@ -38,8 +38,8 @@ void init() {
@Test
void 팔로우_취소() {
// given
Member member = MemberFixture.make(1L, "USER");
Member following = MemberFixture.make(2L, "USER");
Member member = MemberFixture.make(1L);
Member following = MemberFixture.make(2L);

// when
followService.addOrDeleteFollow(member, following);
Expand All @@ -52,7 +52,7 @@ void init() {
@Test
void 자기_자신을_팔로우시_예외_처리() {
// given
Member member = MemberFixture.make(1L, "USER");
Member member = MemberFixture.make(1L);

// then
assertThatThrownBy(() -> followService.addOrDeleteFollow(member, member))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.depromeet.fixture.domain.clock.ClockFixture;
import com.depromeet.fixture.domain.image.ImageFixture;
import com.depromeet.fixture.domain.member.MemberFixture;
import com.depromeet.fixture.domain.memory.MemoryDetailFixture;
import com.depromeet.fixture.domain.memory.MemoryFixture;
import com.depromeet.fixture.domain.pool.PoolFixture;
import com.depromeet.image.domain.Image;
import com.depromeet.image.domain.ImageUploadStatus;
import com.depromeet.image.domain.vo.ImagePresignedUrlVo;
Expand All @@ -17,14 +15,10 @@
import com.depromeet.image.service.ImageUploadService;
import com.depromeet.member.domain.Member;
import com.depromeet.memory.domain.Memory;
import com.depromeet.memory.domain.MemoryDetail;
import com.depromeet.mock.image.FakeImageRepository;
import com.depromeet.mock.image.FakeS3ImageManager;
import com.depromeet.mock.member.FakeMemberRepository;
import com.depromeet.mock.memory.FakeMemoryDetailRepository;
import com.depromeet.mock.memory.FakeMemoryRepository;
import com.depromeet.mock.pool.FakePoolRepository;
import com.depromeet.pool.domain.Pool;
import com.depromeet.util.ImageNameUtil;
import java.time.Clock;
import java.time.LocalDate;
Expand All @@ -40,9 +34,7 @@ class ImageServiceTest {
private FakeS3ImageManager s3ImageManager;
private FakeImageRepository imageRepository;
private FakeMemoryRepository memoryRepository;
private FakeMemoryDetailRepository memoryDetailRepository;
private FakeMemberRepository memberRepository;
private FakePoolRepository poolRepository;

private ImageGetService imageGetService;
private ImageUploadService imageUploadService;
Expand All @@ -61,21 +53,13 @@ void init() {
s3ImageManager = new FakeS3ImageManager();
imageRepository = new FakeImageRepository();
memoryRepository = new FakeMemoryRepository();
memoryDetailRepository = new FakeMemoryDetailRepository();
memberRepository = new FakeMemberRepository();
poolRepository = new FakePoolRepository();
clock = new ClockFixture();

Member member = MemberFixture.make();
member = memberRepository.save(member);

Pool pool = PoolFixture.make("testPool", "test address", 25);
pool = poolRepository.save(pool);

MemoryDetail memoryDetail = MemoryDetailFixture.make();
memoryDetail = memoryDetailRepository.save(memoryDetail);

memory = MemoryFixture.make(member, pool, memoryDetail, LocalDate.of(2024, 7, 1));
memory = MemoryFixture.make(member, LocalDate.of(2024, 7, 1));
memory = memoryRepository.save(memory);

imageGetService = new ImageGetService(imageRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.depromeet.fixture.domain.member.MemberFixture;
import com.depromeet.fixture.domain.memory.MemoryDetailFixture;
import com.depromeet.fixture.domain.memory.MemoryFixture;
import com.depromeet.fixture.domain.pool.PoolFixture;
import com.depromeet.member.domain.Member;
import com.depromeet.memory.domain.Memory;
import com.depromeet.memory.domain.MemoryDetail;
import com.depromeet.memory.domain.vo.TimelineSlice;
import com.depromeet.memory.service.TimelineService;
import com.depromeet.mock.member.FakeMemberRepository;
import com.depromeet.mock.memory.FakeMemoryDetailRepository;
import com.depromeet.mock.memory.FakeMemoryRepository;
import com.depromeet.mock.memory.FakeStrokeRepository;
import com.depromeet.mock.pool.FakePoolRepository;
import com.depromeet.pool.domain.Pool;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -25,10 +18,7 @@

public class TimelineServiceTest {
private FakeMemoryRepository memoryRepository;
private FakeMemoryDetailRepository memoryDetailRepository;
private FakeMemberRepository memberRepository;
private FakePoolRepository poolRepository;
private FakeStrokeRepository strokeRepository;
private TimelineService timelineService;

private Long memberId = 1L; // 로그인한 사용자 아이디 임의 지정
Expand All @@ -41,13 +31,10 @@ public class TimelineServiceTest {
void init() {
// dependencies
memoryRepository = new FakeMemoryRepository();
memoryDetailRepository = new FakeMemoryDetailRepository();
memberRepository = new FakeMemberRepository();
poolRepository = new FakePoolRepository();
strokeRepository = new FakeStrokeRepository();

// member create
member = MemberFixture.make(memberId, "USER");
member = MemberFixture.make(memberId);
memberRepository.save(member);
timelineService = new TimelineService(memoryRepository);

Expand All @@ -60,11 +47,7 @@ List<Memory> saveMemory() {
LocalDate initDate = LocalDate.of(2024, 7, 1);
List<Memory> memories = new ArrayList<>();
for (int i = 0; i < 15; i++) {
Pool pool = PoolFixture.make("test name", "test address", 25);
pool = poolRepository.save(pool);
MemoryDetail memoryDetail = MemoryDetailFixture.make();
memoryDetail = memoryDetailRepository.save(memoryDetail);
Memory memory = MemoryFixture.make(member, pool, memoryDetail, initDate);
Memory memory = MemoryFixture.make(member, initDate);

memory = memoryRepository.save(memory);
memories.add(memory);
Expand Down
Loading
Loading