Skip to content

Commit

Permalink
test: 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
punchdrunkard committed Oct 7, 2024
1 parent 1508d07 commit 0b5c57e
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,67 +181,6 @@ void findThumbnailImageByFestivalId() {
assertThat(thumbnailImageByFestivalId.get(festival3.getId())).isNull();
}

@DisplayName("현재 유저가 북마크 한 페스티벌들을 조회한다.")
@Test
void findBookmarkedFestivals() {
// given
User currentUser = createUser();
em.persist(currentUser);

User otherUser = createUser();
em.persist(otherUser);

Sido sido = createSido();
em.persist(sido);

List<Festival> bookmarkedFestivals = new ArrayList<>();

for (int i = 0; i < 30; i++) {
Festival festival = createFestival(LocalDate.of(2024, 10, 1), LocalDate.of(2024, 10, 10), sido.getId());
em.persist(festival);

for (int j = 0; j < 3; j++) {
FestivalImage image = createFestivalImage(festival);
em.persist(image);
}

FestivalBookmark bookmark = FestivalBookmark.builder()
.festivalId(festival.getId())
.userId(currentUser.getId())
.build();

em.persist(bookmark);

bookmarkedFestivals.add(festival);
}

for (int i = 0; i < 10; i++) {
Festival festival = createFestival(LocalDate.of(2024, 10, 1), LocalDate.of(2024, 10, 10), sido.getId());
em.persist(festival);

FestivalBookmark bookmark = FestivalBookmark.builder()
.festivalId(festival.getId())
.userId(otherUser.getId())
.build();
em.persist(bookmark);
}

Pageable pageable = PageRequest.of(0, 6);

// when
Page<FestivalInfoWithBookmark> result = festivalRepository.findBookmarkedFestivals(currentUser.getId(),
pageable);

// then
assertThat(result).isNotNull();
assertThat(result.getTotalElements()).isEqualTo(30);
assertEquals(6, result.getSize());
List<FestivalInfoWithBookmark> content = result.getContent();
for (FestivalInfoWithBookmark festivalInfoWithBookmark : content) {
assertThat(festivalInfoWithBookmark.getIsBookmarked()).isTrue();
}
}

private static FestivalImage createFestivalImage(Festival festival) {
return FestivalImage.builder()
.festivalId(festival.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ void getFestival() {
.collect(Collectors.toList())
);

FestivalImage image1 = FestivalImage.builder().festivalId(festival.getId()).imageUrl("imageUrl1").build();
FestivalImage image2 = FestivalImage.builder().festivalId(festival.getId()).imageUrl("imageUrl2").build();
FestivalImage image1 = createFestivalImage(festival);
FestivalImage image2 = createFestivalImage(festival);

List<FestivalImage> images = festivalImageRepository.saveAll(List.of(image1, image2));

Expand Down Expand Up @@ -840,6 +840,109 @@ private static FestivalBookmark createFestivalBookmark(Long festvialId, Long use
.build();
}

@DisplayName("유저가 북마크한 페스티벌 조회 - 성공 케이스, 페스티벌이 하나의 이미지만 가지고 있음")
@Test
void findBookmarkedFestivals() {
// given
User currentUser = userRepository.save(createUser());

Festival festival = createFestival();
Festival festival2 = createFestival();
festivalRepository.saveAll(List.of(festival, festival2));

FestivalImage image = createFestivalImage(festival);
festivalImageRepository.save(image);

FestivalBookmark bookmark = festivalBookmarkRepository.save(
createFestivalBookmark(festival.getId(), currentUser.getId()));
festivalBookmarkRepository.save(bookmark);

int bookmarkCount = 1;

// when
Page<FestivalInfoWithBookmark> bookmarkedFestivals = festivalService.getBookmarkedFestivals(currentUser,
PageRequest.of(0, 5));

// then
assertThat(bookmarkedFestivals.getContent())
.hasSize(1)
.extracting("festivalId", "thumbnailImage", "isBookmarked")
.containsExactly(
tuple(festival.getId(), image.getImageUrl(), true)
);

assertEquals(bookmarkedFestivals.getTotalElements(), bookmarkCount);
}

@DisplayName("유저가 북마크한 페스티벌 조회 - 페스티벌의 이미지가 여러 개인 경우에도 첫 번째 이미지만 반환한다.")
@Test
void findBookmarkedFestivals_multipleFestivalImages() {
// given
User currentUser = userRepository.save(createUser());

Festival festival = createFestival();
festivalRepository.save(festival);

FestivalImage image1 = createFestivalImage(festival);
FestivalImage image2 = createFestivalImage(festival);
FestivalImage image3 = createFestivalImage(festival);
festivalImageRepository.saveAll(List.of(image1, image2, image3));

FestivalBookmark bookmark = festivalBookmarkRepository.save(
createFestivalBookmark(festival.getId(), currentUser.getId()));
festivalBookmarkRepository.save(bookmark);

// when
Page<FestivalInfoWithBookmark> bookmarkedFestivals = festivalService.getBookmarkedFestivals(currentUser,
PageRequest.of(0, 5));

// then
assertThat(bookmarkedFestivals.getContent())
.hasSize(1)
.extracting("festivalId", "thumbnailImage", "isBookmarked")
.containsExactly(
tuple(festival.getId(), image1.getImageUrl(), true)
);
}

@DisplayName("유저가 북마크한 페스티벌 조회 - 성공, 북마크 한 페스티벌이 여러 개 일 때")
@Test
void findBookmarkedFestivals_Success() {
// given
User currentUser = userRepository.save(createUser());

Festival festival = createFestival();
Festival festival2 = createFestival();
Festival festival3 = createFestival();
Festival festival4 = createFestival();
Festival festival5 = createFestival();
festivalRepository.saveAll(List.of(festival, festival2, festival3, festival4, festival5));

FestivalBookmark bookmark1 = festivalBookmarkRepository.save(
createFestivalBookmark(festival.getId(), currentUser.getId()));
FestivalBookmark bookmark2 = festivalBookmarkRepository.save(
createFestivalBookmark(festival.getId(), currentUser.getId()));
FestivalBookmark bookmark3 = festivalBookmarkRepository.save(
createFestivalBookmark(festival.getId(), currentUser.getId()));

festivalBookmarkRepository.saveAll(List.of(bookmark1, bookmark2, bookmark3));

int pageSize = 2;
int bookmarkedFestivalCount = 3;

// when
Page<FestivalInfoWithBookmark> bookmarkedFestivals = festivalService.getBookmarkedFestivals(currentUser,
PageRequest.of(0, pageSize));

// then
assertThat(bookmarkedFestivals.getContent()).hasSize(pageSize);
assertThat(bookmarkedFestivals.getTotalElements()).isEqualTo(bookmarkedFestivalCount);
}

private static FestivalImage createFestivalImage(Festival festival) {
return FestivalImage.builder().festivalId(festival.getId()).imageUrl("imageUrl1").build();
}

private static Festival createFestival(LocalDate startDate, LocalDate endDate, Long sidoId) {
return Festival.builder()
.userId(1L)
Expand Down Expand Up @@ -906,6 +1009,10 @@ private static Festival createFestival(String name, LocalDate currentDate, Doubl
.build();
}

private static Festival createFestival() {
return createFestival("페스티벌 이름");
}

private static Festival createFestival(String name) {
return Festival.builder()
.userId(1L)
Expand Down

0 comments on commit 0b5c57e

Please sign in to comment.