Skip to content

Commit

Permalink
fix: 최신순 정렬을 사용하는 ServiceTest, RepositoryTest에 아이디 기준 내림차순 옵션 추가 (#77)
Browse files Browse the repository at this point in the history
* refactor: 정렬 조건을 추가할 수 있도록 로직 변경

* refactor: 모든 최신순 정렬 조건에 아이디 기준 내림차순 정렬 추가
  • Loading branch information
70825 authored Jun 7, 2024
1 parent 9ec1ed0 commit 2a2bdd7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
18 changes: 13 additions & 5 deletions src/test/java/com/funeat/fixture/PageFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressWarnings("NonAsciiCharacters")
public class PageFixture {

Expand Down Expand Up @@ -34,12 +38,16 @@ public class PageFixture {
return PageRequest.of(page, size);
}

public static PageRequest 페이지요청_생성(final int page, final int size, final String sort) {
final String[] splitSort = sort.split(",");
final String order = splitSort[0];
final Direction direction = Direction.fromString(splitSort[1]);
public static PageRequest 페이지요청_생성(final int page, final int size, final String... sorts) {
final List<Sort.Order> orders = new ArrayList<>();
Arrays.stream(sorts).forEach(sort -> {
final String[] splitSort = sort.split(",");
final String property = splitSort[0];
final Direction direction = Direction.fromString(splitSort[1]);
orders.add(new Sort.Order(direction, property));
});

return PageRequest.of(page, size, Sort.by(direction, order));
return PageRequest.of(page, size, Sort.by(orders));
}

public static PageDto 응답_페이지_생성(final Long totalDataCount, final Long totalPages, final boolean firstPage,
Expand Down
19 changes: 9 additions & 10 deletions src/test/java/com/funeat/recipe/application/RecipeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static com.funeat.fixture.MemberFixture.멤버_멤버2_생성;
import static com.funeat.fixture.MemberFixture.멤버_멤버3_생성;
import static com.funeat.fixture.PageFixture.과거순;
import static com.funeat.fixture.PageFixture.아이디_내림차순;
import static com.funeat.fixture.PageFixture.좋아요수_내림차순;
import static com.funeat.fixture.PageFixture.최신순;
import static com.funeat.fixture.PageFixture.페이지요청_생성;
Expand Down Expand Up @@ -219,7 +220,7 @@ class findRecipeByMember_성공_테스트 {
final var recipeImage1_2 = 레시피이미지_생성(recipe1_2);
복수_꿀조합_이미지_저장(recipeImage1_1, recipeImage1_2);

final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);

// when
final var actual = recipeService.findRecipeByMember(member1.getId(), page);
Expand All @@ -240,7 +241,7 @@ class findRecipeByMember_성공_테스트 {
final var member1 = 멤버_멤버1_생성();
단일_멤버_저장(member1);

final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);

// when
final var actual = recipeService.findRecipeByMember(member1.getId(), page);
Expand All @@ -260,7 +261,7 @@ class findRecipeByMember_실패_테스트 {
void 존재하지_않는_멤버가_해당_멤버의_레시피를_조회하면_예외가_발생한다() {
// given
final var notExistMemberId = 99999L;
final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);

// when & then
assertThatThrownBy(() -> recipeService.findRecipeByMember(notExistMemberId, page))
Expand Down Expand Up @@ -327,7 +328,7 @@ class 정렬_기준에_따른_테스트 {
}

@Test
void 꿀조합을_최신순으로_정렬할_수_있다() throws InterruptedException {
void 꿀조합을_최신순으로_정렬할_수_있다() {
// given
final var loginId = -1L;
final var member1 = 멤버_멤버1_생성();
Expand All @@ -344,9 +345,7 @@ class 정렬_기준에_따른_테스트 {
복수_상품_저장(product1, product2, product3);

final var recipe1_1 = 레시피_생성(member1, 1L);
Thread.sleep(100);
final var recipe1_2 = 레시피_생성(member1, 3L);
Thread.sleep(100);
final var recipe1_3 = 레시피_생성(member1, 2L);
복수_꿀조합_저장(recipe1_1, recipe1_2, recipe1_3);

Expand All @@ -363,7 +362,7 @@ class 정렬_기준에_따른_테스트 {
final var recipeImage1_2_2 = 레시피이미지_생성(recipe1_2);
복수_꿀조합_이미지_저장(recipeImage1_1_1, recipeImage1_2_1);

final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);

// when
final var actual = recipeService.getSortingRecipes(loginId, page).getRecipes();
Expand Down Expand Up @@ -818,7 +817,7 @@ class findBookmarkRecipeByMember_성공_테스트 {
final var request = 레시피북마크요청_생성(북마크O);
recipeService.bookmarkRecipe(member1.getId(), recipe2.getId(), request);

final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);

// when
final var actual = recipeService.findBookmarkRecipeByMember(member1.getId(), page);
Expand All @@ -843,7 +842,7 @@ class findBookmarkRecipeByMember_성공_테스트 {
final var member1 = 멤버_멤버1_생성();
단일_멤버_저장(member1);

final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);

// when
final var actual = recipeService.findBookmarkRecipeByMember(member1.getId(), page);
Expand All @@ -863,7 +862,7 @@ class findBookmarkRecipeByMember_실패_테스트 {
void 존재하지_않는_멤버가_해당_멤버의_저장한_레시피를_조회하면_예외가_발생한다() {
// given
final var notExistMemberId = 99999L;
final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);

// when & then
assertThatThrownBy(() -> recipeService.findBookmarkRecipeByMember(notExistMemberId, page))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static com.funeat.fixture.MemberFixture.멤버_멤버2_생성;
import static com.funeat.fixture.MemberFixture.멤버_멤버3_생성;
import static com.funeat.fixture.PageFixture.과거순;
import static com.funeat.fixture.PageFixture.아이디_내림차순;
import static com.funeat.fixture.PageFixture.좋아요수_내림차순;
import static com.funeat.fixture.PageFixture.최신순;
import static com.funeat.fixture.PageFixture.페이지요청_생성;
Expand Down Expand Up @@ -166,7 +167,7 @@ class findAllRecipes_성공_테스트 {
}

@Test
void 꿀조합을_최신순으로_정렬한다() throws InterruptedException {
void 꿀조합을_최신순으로_정렬한다() {
// given
final var member1 = 멤버_멤버1_생성();
final var member2 = 멤버_멤버2_생성();
Expand All @@ -182,9 +183,7 @@ class findAllRecipes_성공_테스트 {
복수_상품_저장(product1, product2, product3);

final var recipe1_1 = 레시피_생성(member1, 1L);
Thread.sleep(100);
final var recipe1_2 = 레시피_생성(member1, 3L);
Thread.sleep(100);
final var recipe1_3 = 레시피_생성(member1, 2L);
복수_꿀조합_저장(recipe1_1, recipe1_2, recipe1_3);

Expand All @@ -200,7 +199,7 @@ class findAllRecipes_성공_테스트 {
final var recipeImage1_2 = 레시피이미지_생성(recipe1_2);
복수_꿀조합_이미지_저장(recipeImage1_1, recipeImage1_2);

final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);
final var expected = List.of(recipe1_3, recipe1_2, recipe1_1);

// when
Expand Down Expand Up @@ -371,7 +370,7 @@ class findBookmarkedRecipesByMember_성공_테스트 {
복수_레시피_북마크_저장(bookmarkRecipe1, bookmarkRecipe2, bookmarkRecipe3);

final var expected = List.of(recipe3, recipe2);
final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);

// when
final var actual = recipeRepository.findBookmarkedRecipesByMember(member, page).getContent();
Expand All @@ -392,7 +391,7 @@ class findBookmarkedRecipesByMember_성공_테스트 {
복수_꿀조합_저장(recipe1, recipe2);

final var expected = Collections.emptyList();
final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);

// when
final var actual = recipeRepository.findBookmarkedRecipesByMember(member, page).getContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static com.funeat.fixture.MemberFixture.멤버_멤버1_생성;
import static com.funeat.fixture.MemberFixture.멤버_멤버2_생성;
import static com.funeat.fixture.MemberFixture.멤버_비로그인_생성;
import static com.funeat.fixture.PageFixture.아이디_내림차순;
import static com.funeat.fixture.PageFixture.최신순;
import static com.funeat.fixture.PageFixture.페이지요청_생성;
import static com.funeat.fixture.ProductFixture.상품_삼각김밥_가격1000원_평점2점_생성;
Expand Down Expand Up @@ -674,7 +675,7 @@ class findReviewByMember_성공_테스트 {
복수_리뷰_저장(review1_1, review2_1, review2_2, review3_1, review3_2);

// when
final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);
final var member1Id = member1.getId();
final var result = reviewService.findReviewByMember(member1Id, page);

Expand All @@ -699,7 +700,7 @@ class findReviewByMember_실패_테스트 {
void 존재하지_않은_사용자가_작성한_리뷰를_조회할때_예외가_발생한다() {
// given
final var notExistMemberId = 999999L;
final var page = 페이지요청_생성(0, 10, 최신순);
final var page = 페이지요청_생성(0, 10, 최신순, 아이디_내림차순);

// when & then
assertThatThrownBy(() -> reviewService.findReviewByMember(notExistMemberId, page))
Expand Down

0 comments on commit 2a2bdd7

Please sign in to comment.