Skip to content

Commit

Permalink
fix: 검색 자동 완성 페이징 로직 수정 (#14)
Browse files Browse the repository at this point in the history
* test: 검색 자동 완성 검증 테스트 추가

* refactor: 검색 자동 완성 페이징 로직 수정
  • Loading branch information
Go-Jaecheol authored Jan 29, 2024
1 parent f1f256e commit 4b33265
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/funeat/product/application/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,20 @@ public RankingProductsResponse getTop3Products() {
}

public SearchProductsResponse searchProducts(final String query, final Long lastProductId) {
final List<Product> products = findAllByNameContaining(query, lastProductId);
final List<Product> findProducts = findAllByNameContaining(query, lastProductId);
final int resultSize = getResultSize(findProducts);
final List<Product> products = findProducts.subList(0, resultSize);

final boolean hasNext = products.size() > DEFAULT_PAGE_SIZE;
final boolean hasNext = hasNextPage(findProducts);
final List<SearchProductDto> productDtos = products.stream()
.map(SearchProductDto::toDto)
.collect(Collectors.toList());
.toList();

return SearchProductsResponse.toResponse(hasNext, productDtos);
}

private List<Product> findAllByNameContaining(final String query, final Long lastProductId) {
final PageRequest size = PageRequest.ofSize(DEFAULT_PAGE_SIZE);
final PageRequest size = PageRequest.ofSize(DEFAULT_CURSOR_PAGINATION_SIZE);
if (lastProductId == 0) {
return productRepository.findAllByNameContainingFirst(query, size);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static com.funeat.fixture.MemberFixture.멤버3;
import static com.funeat.fixture.PageFixture.FIRST_PAGE;
import static com.funeat.fixture.PageFixture.PAGE_SIZE;
import static com.funeat.fixture.PageFixture.SECOND_PAGE;
import static com.funeat.fixture.PageFixture.가격_내림차순;
import static com.funeat.fixture.PageFixture.가격_오름차순;
import static com.funeat.fixture.PageFixture.과거순;
Expand All @@ -41,7 +40,6 @@
import static com.funeat.fixture.PageFixture.응답_페이지_생성;
import static com.funeat.fixture.PageFixture.좋아요수_내림차순;
import static com.funeat.fixture.PageFixture.첫페이지O;
import static com.funeat.fixture.PageFixture.첫페이지X;
import static com.funeat.fixture.PageFixture.총_데이터_개수;
import static com.funeat.fixture.PageFixture.총_페이지;
import static com.funeat.fixture.PageFixture.최신순;
Expand Down Expand Up @@ -421,7 +419,7 @@ class searchProducts_성공_테스트 {

// then
STATUS_CODE를_검증한다(응답, 정상_처리);
상품_자동_완성_검색_결과를_검증한다(응답, List.of(상품2, 상품1));
상품_자동_완성_검색_결과를_검증한다(응답, false, List.of(상품2, 상품1));
}

@Test
Expand All @@ -436,7 +434,7 @@ class searchProducts_성공_테스트 {

// then
STATUS_CODE를_검증한다(응답, 정상_처리);
상품_자동_완성_검색_결과를_검증한다(응답, Collections.emptyList());
상품_자동_완성_검색_결과를_검증한다(응답, false, Collections.emptyList());
}

@Test
Expand Down Expand Up @@ -474,7 +472,22 @@ class searchProducts_성공_테스트 {

// then
STATUS_CODE를_검증한다(응답, 정상_처리);
상품_자동_완성_검색_결과를_검증한다(응답, List.of(상품11, 상품1, 상품10, 상품9, 상품8, 상품7, 상품6, 상품5, 상품4, 상품3));
상품_자동_완성_검색_결과를_검증한다(응답, true, List.of(상품11, 상품1, 상품10, 상품9, 상품8, 상품7, 상품6, 상품5, 상품4, 상품3));
}

@Test
void 검색_결과가_10개_이상일_때_해당_페이지_결과_10개만_반환한다() {
// given
final var 카테고리 = 카테고리_간편식사_생성();
단일_카테고리_저장(카테고리);
반복_애플망고_상품_저장(11, 카테고리);

// when
final var 응답 = 상품_자동_완성_검색_요청("망고", 0L);

// then
STATUS_CODE를_검증한다(응답, 정상_처리);
상품_자동_완성_검색_결과를_검증한다(응답, true, List.of(상품11, 상품10, 상품9, 상품8, 상품7, 상품6, 상품5, 상품4, 상품3, 상품2));
}
}

Expand Down Expand Up @@ -683,11 +696,15 @@ class getProductRecipes_성공_테스트 {
.isEqualTo(productIds);
}

private void 상품_자동_완성_검색_결과를_검증한다(final ExtractableResponse<Response> response, final List<Long> productIds) {
final var actual = response.jsonPath()
private void 상품_자동_완성_검색_결과를_검증한다(final ExtractableResponse<Response> response, final boolean hasNext,
final List<Long> productIds) {
final var actualHasNext = response.jsonPath()
.getBoolean("hasNext");
final var actualProducts = response.jsonPath()
.getList("products", SearchProductDto.class);

assertThat(actual).extracting(SearchProductDto::getId)
assertThat(actualHasNext).isEqualTo(hasNext);
assertThat(actualProducts).extracting(SearchProductDto::getId)
.isEqualTo(productIds);
}

Expand Down

0 comments on commit 4b33265

Please sign in to comment.