Skip to content

Commit

Permalink
✅ 커서 기반 필터링 적용에 대한 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
weonest committed Dec 26, 2023
1 parent 01cb055 commit 372456c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 44 deletions.
2 changes: 0 additions & 2 deletions src/main/java/dev/steady/global/config/QueryDslConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.querydsl.jpa.JPQLTemplates;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.querydsl.sql.MySQLTemplates;
import com.querydsl.sql.SQLTemplates;
import jakarta.persistence.EntityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import static dev.steady.steady.fixture.SteadyFixtures.createSteadyRequest;
import static dev.steady.steady.fixture.SteadyFixtures.createSteadyUpdateRequest;
import static dev.steady.user.fixture.UserFixtures.createPosition;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willDoNothing;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
Expand Down Expand Up @@ -153,35 +155,38 @@ void findMySteadiesTest() throws Exception {
@DisplayName("검색 조건에 따른 전체 조회 결과를 반환한다.")
void getSteadiesByConditionTest() throws Exception {
// given
var searchRequest = new SteadySearchRequest(null,
0,
"desc",
var searchRequest = new SteadySearchRequest(
null,
null,
null,
null,
null,
null,
null,
null,
null,
"online",
"Java",
"Backend",
"recruiting",
"false",
"");
null);

MultiValueMap params = new LinkedMultiValueMap<>() {{
add("steadyType", null);
add("page", "0");
add("direction", "desc");
add("page", null);
add("direction", null);
add("criteria", null);
add("steadyMode", "online");
add("stack", "Java");
add("position", "Backend");
add("status", "recruiting");
add("cursor", null);
add("steadyType", null);
add("steadyMode", null);
add("stack", null);
add("position", null);
add("status", null);
add("like", "false");
add("keyword", "");
add("keyword", null);
}};

var pageable = searchRequest.toPageable();
var condition = FilterConditionDto.from(searchRequest);
var steady = createSteady();
var response = createSteadyPageResponse(steady, pageable);

given(steadyService.getSteadies(new UserInfo(null), condition, pageable)).willReturn(response);
given(steadyService.getSteadies(eq(new UserInfo(null)), any(FilterConditionDto.class), eq(pageable))).willReturn(response);

// when & then
mockMvc.perform(get("/api/v1/steadies/search")
Expand All @@ -190,10 +195,11 @@ void getSteadiesByConditionTest() throws Exception {
resourceDetails().tag("스테디").description("스테디 검색 및 필터링 조회")
.responseSchema(Schema.schema("PageResponse")),
queryParameters(
parameterWithName("steadyType").description("스테디 타입").optional(),
parameterWithName("page").description("요청 페이지 번호"),
parameterWithName("direction").description("내림/오름차순").optional(),
parameterWithName("criteria").description("정렬 조건").optional(),
parameterWithName("cursor").description("페이징 커서").optional(),
parameterWithName("steadyType").description("스테디 타입").optional(),
parameterWithName("steadyMode").description("스테디 진행 방식").optional(),
parameterWithName("stack").description("스테디 기술 스택").optional(),
parameterWithName("position").description("스테디 포지션").optional(),
Expand All @@ -210,6 +216,7 @@ void getSteadiesByConditionTest() throws Exception {
fieldWithPath("content[].status").type(STRING).description("스테디 상태"),
fieldWithPath("content[].deadline").type(STRING).description("모집 마감일"),
fieldWithPath("content[].createdAt").type(STRING).description("스테디 생성일"),
fieldWithPath("content[].promotedAt").type(STRING).description("스테디 끌어올린 시간"),
fieldWithPath("content[].participantLimit").type(NUMBER).description("모집 정원"),
fieldWithPath("content[].numberOfParticipants").type(NUMBER).description("스테디 참여 인원"),
fieldWithPath("content[].viewCount").type(NUMBER).description("조회수"),
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/dev/steady/steady/fixture/SteadyFixtures.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import dev.steady.steady.dto.response.PageResponse;
import dev.steady.steady.dto.response.ParticipantResponse;
import dev.steady.steady.dto.response.ParticipantsResponse;
import dev.steady.steady.dto.response.SteadyQueryResponse;
import dev.steady.steady.dto.response.SteadyQuestionResponse;
import dev.steady.steady.dto.response.SteadyQuestionsResponse;
import dev.steady.steady.dto.response.SteadySearchResponse;
import dev.steady.user.domain.Position;
import dev.steady.user.domain.Stack;
import dev.steady.user.domain.User;
Expand Down Expand Up @@ -154,9 +154,9 @@ public static Steady createSteady(User user, List<Stack> stacks, SteadyStatus st
return steady;
}

public static PageResponse<SteadySearchResponse> createSteadyPageResponse(Steady steady, Pageable pageable) {
public static PageResponse<SteadyQueryResponse> createSteadyPageResponse(Steady steady, Pageable pageable) {
Page<Steady> steadies = new PageImpl<>(List.of(steady), pageable, 1);
return PageResponse.from(steadies.map(v -> SteadySearchResponse.from(v, 0)));
return PageResponse.from(steadies.map(SteadyQueryResponse::from));
}

public static ParticipantsResponse createParticipantsResponse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ void findAllByConditionTest() {
entityManager.clear();

// when
var request = new SteadySearchRequest("study",
0,
var request = new SteadySearchRequest(
null,
"DESC",
null,
null,
"study",
"online",
"Java",
"백엔드",
Expand All @@ -98,7 +100,7 @@ void findAllByConditionTest() {

var pageable = request.toPageable();
var condition = FilterConditionDto.from(request);
var response = queryDslRepository.findAllBySearchCondition(null, condition, pageable);
var response = queryDslRepository.findAllByFilterCondition(null, condition, pageable);
var returnedSteady = response.getContent().get(0);

// then
Expand Down Expand Up @@ -126,10 +128,12 @@ void findAllByConditionNotInTest() {
entityManager.clear();

// when
var request = new SteadySearchRequest("study",
0,
"DESC",
var request = new SteadySearchRequest(
null,
null,
null,
null,
"study",
"both",
"Java",
"데브옵스",
Expand All @@ -139,7 +143,7 @@ void findAllByConditionNotInTest() {

var pageable = request.toPageable();
var condition = FilterConditionDto.from(request);
var response = queryDslRepository.findAllBySearchCondition(null, condition, pageable);
var response = queryDslRepository.findAllByFilterCondition(null, condition, pageable);

// then
assertThat(response.getTotalElements()).isZero();
Expand All @@ -162,9 +166,11 @@ void findAllByBasicConditionTest() {
entityManager.clear();

// when
var request = new SteadySearchRequest("study",
0,
"DESC",
var request = new SteadySearchRequest(
null,
null,
null,
null,
null,
null,
null,
Expand All @@ -174,7 +180,7 @@ void findAllByBasicConditionTest() {
null);
var pageable = request.toPageable();
var condition = FilterConditionDto.from(request);
var response = queryDslRepository.findAllBySearchCondition(null, condition, pageable);
var response = queryDslRepository.findAllByFilterCondition(null, condition, pageable);

// then
int expectedSize = 1;
Expand Down
21 changes: 12 additions & 9 deletions src/test/java/dev/steady/steady/service/SteadyServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import dev.steady.steady.dto.response.PageResponse;
import dev.steady.steady.dto.response.ParticipantsResponse;
import dev.steady.steady.dto.response.SteadyDetailResponse;
import dev.steady.steady.dto.response.SteadyQueryResponse;
import dev.steady.steady.dto.response.SteadyQuestionsResponse;
import dev.steady.steady.dto.response.SteadySearchResponse;
import dev.steady.user.domain.User;
import dev.steady.user.domain.repository.PositionRepository;
import dev.steady.user.domain.repository.StackRepository;
Expand Down Expand Up @@ -146,7 +146,8 @@ void getSteadiesSearchTest() {

// when
SteadySearchRequest searchRequest = new SteadySearchRequest(null,
0,
null,
null,
null,
null,
null,
Expand All @@ -157,14 +158,14 @@ void getSteadiesSearchTest() {
null);
FilterConditionDto condition = FilterConditionDto.from(searchRequest);
Pageable pageable = searchRequest.toPageable();
PageResponse<SteadySearchResponse> response = steadyService.getSteadies(userInfo, condition, pageable);
PageResponse<SteadyQueryResponse> response = steadyService.getSteadies(userInfo, condition, pageable);

// then
List<Steady> steadies = steadyRepository.findAll();
List<SteadySearchResponse> content = response.content();
List<SteadyQueryResponse> content = response.content();
assertAll(
() -> assertThat(content).hasSameSizeAs(steadies),
() -> assertThat(content.get(0).createdAt()).isAfter(content.get(1).createdAt())
() -> assertThat(content.get(0).promotedAt()).isAfter(content.get(1).promotedAt())
);
}

Expand All @@ -185,23 +186,25 @@ void getSteadiesSearchOrderByDeadlineTest() {
entityManager.clear();

// when
SteadySearchRequest searchRequest = new SteadySearchRequest(null,
0,
SteadySearchRequest searchRequest = new SteadySearchRequest(
null,
"asc",
"deadline",
null,
null,
null,
null,
null,
null,
"false",
null);
FilterConditionDto condition = FilterConditionDto.from(searchRequest);
Pageable pageable = searchRequest.toPageable();
PageResponse<SteadySearchResponse> response = steadyService.getSteadies(userInfo, condition, pageable);
PageResponse<SteadyQueryResponse> response = steadyService.getSteadies(userInfo, condition, pageable);

// then
List<Steady> steadies = steadyRepository.findAll();
List<SteadySearchResponse> content = response.content();
List<SteadyQueryResponse> content = response.content();
assertAll(
() -> assertThat(content).hasSameSizeAs(steadies),
() -> assertThat(content.get(0).deadline()).isBefore(content.get(1).deadline())
Expand Down

0 comments on commit 372456c

Please sign in to comment.