Skip to content

Commit

Permalink
test: DAO 객체 생성으로 인한 test
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoya324 committed Sep 28, 2024
1 parent 5d81f0d commit 9521cd3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
import mju.iphak.maru_egg.answer.repository.AnswerRepository;
import mju.iphak.maru_egg.common.MockTest;
import mju.iphak.maru_egg.common.utils.PhraseExtractionUtils;
import mju.iphak.maru_egg.question.dao.request.SelectQuestionCores;
import mju.iphak.maru_egg.question.dao.response.QuestionCore;
import mju.iphak.maru_egg.question.domain.Question;
import mju.iphak.maru_egg.question.domain.QuestionCategory;
import mju.iphak.maru_egg.question.domain.QuestionType;
import mju.iphak.maru_egg.question.dto.response.QuestionCore;
import mju.iphak.maru_egg.question.dto.response.QuestionResponse;
import mju.iphak.maru_egg.question.repository.QuestionRepository;
import okhttp3.mockwebserver.MockResponse;
Expand Down Expand Up @@ -87,16 +88,17 @@ void shutdown() throws IOException {
@Test
void 질문_조회_성공() {
// given
QuestionType type = QuestionType.SUSI;
QuestionCategory category = QuestionCategory.ADMISSION_GUIDELINE;
String content = "테스트 질문입니다.";
String contentToken = PhraseExtractionUtils.extractPhrases(content);
SelectQuestionCores selectQuestionCores = SelectQuestionCores.of(QuestionType.SUSI,
QuestionCategory.ADMISSION_GUIDELINE, content, contentToken);

when(questionRepository.searchQuestionsByContentTokenAndTypeAndCategory(anyString(), eq(type), eq(category)))
when(questionRepository.searchQuestions(eq(selectQuestionCores)))
.thenReturn(Optional.of(List.of(QuestionCore.of(1L, contentToken))));

// when
QuestionResponse result = questionProcessingService.question(type, category, content);
QuestionResponse result = questionProcessingService.question(selectQuestionCores.type(),
selectQuestionCores.category(), content);

// then
assertThat(result).isEqualTo(createExpectedQuestionResponse());
Expand All @@ -106,20 +108,20 @@ void shutdown() throws IOException {
@Test
void 질문_없을_때_새로운_질문_요청() {
// given
QuestionType type = QuestionType.SUSI;
QuestionCategory category = QuestionCategory.ADMISSION_GUIDELINE;
String content = "새로운 질문입니다.";
SelectQuestionCores selectQuestionCores = SelectQuestionCores.of(QuestionType.SUSI,
QuestionCategory.ADMISSION_GUIDELINE, content, content);
String contentToken = PhraseExtractionUtils.extractPhrases(content);

when(questionRepository.searchQuestionsByContentTokenAndTypeAndCategory(eq(contentToken), eq(type),
eq(category)))
when(questionRepository.searchQuestions(eq(selectQuestionCores)))
.thenReturn(Optional.of(Collections.emptyList()));

// when
questionProcessingService.question(type, category, content);
questionProcessingService.question(selectQuestionCores.type(), selectQuestionCores.category(), content);

// then
verify(answerManager, times(1)).processNewQuestion(eq(type), eq(category), eq(content), eq(contentToken));
verify(answerManager, times(1)).processNewQuestion(eq(selectQuestionCores.type()),
eq(selectQuestionCores.category()), eq(content), eq(contentToken));
}

@DisplayName("질문 조회 시 존재하지 않는 질문 ID로 인한 예외 처리")
Expand All @@ -140,19 +142,19 @@ void shutdown() throws IOException {
void 질문_검색_서버_내부_오류_빈배열_반환() {
// given
String contentToken = "서버 오류 테스트";
QuestionType type = QuestionType.SUSI;
QuestionCategory category = QuestionCategory.ADMISSION_GUIDELINE;
SelectQuestionCores selectQuestionCores = SelectQuestionCores.of(QuestionType.SUSI,
QuestionCategory.ADMISSION_GUIDELINE, contentToken, contentToken);
List<QuestionCore> questionCores = List.of();

when(questionRepository.searchQuestionsByContentTokenAndTypeAndCategory(eq(contentToken), eq(type),
eq(category)))
when(questionRepository.searchQuestions(eq(selectQuestionCores)))
.thenReturn(Optional.of(questionCores));

// when
questionProcessingService.question(type, category, "서버 오류 테스트");
questionProcessingService.question(selectQuestionCores.type(), selectQuestionCores.category(), "서버 오류 테스트");

// then
verify(answerManager, times(1)).processNewQuestion(eq(type), eq(category), eq("서버 오류 테스트"), eq(contentToken));
verify(answerManager, times(1)).processNewQuestion(eq(selectQuestionCores.type()),
eq(selectQuestionCores.category()), eq("서버 오류 테스트"), eq(contentToken));
}

@DisplayName("MOCK LLM 서버에 질문을 요청합니다.")
Expand Down Expand Up @@ -192,8 +194,7 @@ private void setupMockEntities() {
when(answerManager.getAnswerByQuestionId(1L)).thenReturn(answer);
when(answerRepository.findByQuestionId(anyLong())).thenReturn(Optional.of(answer));

when(questionRepository.searchQuestionsByContentTokenAndTypeAndCategory(anyString(), any(QuestionType.class),
any(QuestionCategory.class)))
when(questionRepository.searchQuestions(any(SelectQuestionCores.class)))
.thenReturn(Optional.of(List.of(QuestionCore.of(1L, "테스트 질문입니다."))));
when(questionRepository.findById(1L)).thenReturn(Optional.of(question));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
import mju.iphak.maru_egg.answer.repository.AnswerRepository;
import mju.iphak.maru_egg.common.MockTest;
import mju.iphak.maru_egg.common.dto.pagination.SliceQuestionResponse;
import mju.iphak.maru_egg.question.dao.request.SelectQuestionCores;
import mju.iphak.maru_egg.question.dao.request.SelectQuestions;
import mju.iphak.maru_egg.question.dao.response.QuestionCore;
import mju.iphak.maru_egg.question.domain.Question;
import mju.iphak.maru_egg.question.domain.QuestionCategory;
import mju.iphak.maru_egg.question.domain.QuestionType;
import mju.iphak.maru_egg.question.dto.request.CreateQuestionRequest;
import mju.iphak.maru_egg.question.dto.response.QuestionCore;
import mju.iphak.maru_egg.question.dto.response.QuestionListItemResponse;
import mju.iphak.maru_egg.question.dto.response.SearchedQuestionsResponse;
import mju.iphak.maru_egg.question.repository.QuestionRepository;
Expand Down Expand Up @@ -81,8 +83,7 @@ void setUp() {
when(answer.getId()).thenReturn(1L);
when(answerManager.getAnswerByQuestionId(1L)).thenReturn(answer);
when(answerRepository.findByQuestionId(anyLong())).thenReturn(Optional.of(answer));
when(questionRepository.searchQuestionsByContentTokenAndTypeAndCategory(anyString(), any(QuestionType.class),
any(QuestionCategory.class)))
when(questionRepository.searchQuestions(any(SelectQuestionCores.class)))
.thenReturn(Optional.of(List.of(QuestionCore.of(1L, "테스트 질문입니다."))));
when(questionRepository.findById(1L)).thenReturn(Optional.of(question));
questionService = new QuestionService(questionRepository, answerManager);
Expand Down Expand Up @@ -144,27 +145,22 @@ void tearDown() throws IOException {
String content = "example content";
Integer cursorViewCount = 0;
Long questionId = 0L;
Integer size = 5;
QuestionType type = QuestionType.SUSI;
QuestionCategory category = QuestionCategory.ADMISSION_GUIDELINE;
int size = 5;
Pageable pageable = PageRequest.of(0, size);
SelectQuestions selectQuestions = SelectQuestions.of(QuestionType.SUSI, QuestionCategory.ADMISSION_GUIDELINE,
content, cursorViewCount, questionId, pageable);

SearchedQuestionsResponse searchedQuestionsResponse = new SearchedQuestionsResponse(1L, "example content",
true);
SliceQuestionResponse<SearchedQuestionsResponse> expectedResponse = new SliceQuestionResponse<>(
List.of(searchedQuestionsResponse), 0, size, false, null, null);

when(questionRepository.searchQuestionsOfCursorPagingByContentWithFullTextSearch(type, category, content,
cursorViewCount,
questionId, pageable)).thenReturn(expectedResponse);

when(questionRepository.searchQuestionsOfCursorPagingByContentWithLikeFunction(type, category, content,
cursorViewCount,
questionId, pageable)).thenReturn(expectedResponse);
when(questionRepository.searchQuestionsOfCursorPaging(selectQuestions)).thenReturn(expectedResponse);

// when
SliceQuestionResponse<SearchedQuestionsResponse> result = questionService.searchQuestionsOfCursorPaging(type,
category, content,
SliceQuestionResponse<SearchedQuestionsResponse> result = questionService.searchQuestionsOfCursorPaging(
selectQuestions.type(),
selectQuestions.category(), content,
cursorViewCount, questionId, size);

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import mju.iphak.maru_egg.answer.repository.AnswerRepository;
import mju.iphak.maru_egg.common.RepositoryTest;
import mju.iphak.maru_egg.common.dto.pagination.SliceQuestionResponse;
import mju.iphak.maru_egg.question.dao.request.SelectQuestionCores;
import mju.iphak.maru_egg.question.dao.request.SelectQuestions;
import mju.iphak.maru_egg.question.dao.response.QuestionCore;
import mju.iphak.maru_egg.question.domain.QQuestion;
import mju.iphak.maru_egg.question.domain.Question;
import mju.iphak.maru_egg.question.domain.QuestionCategory;
import mju.iphak.maru_egg.question.domain.QuestionType;
import mju.iphak.maru_egg.question.dto.response.QuestionCore;
import mju.iphak.maru_egg.question.dto.response.SearchedQuestionsResponse;

class QuestionRepositoryImplTest extends RepositoryTest {
Expand Down Expand Up @@ -109,7 +111,7 @@ public void setUp() throws Exception {
QQuestion question = QQuestion.question;
String contentToken = "수시 입학 요강 대해";
QuestionType type = QuestionType.SUSI;
NumberTemplate<Double> numberTemplate = createBooleanTemplate(question, contentToken);
NumberTemplate<Double> numberTemplate = createBooleanTemplateByContentToken(question, contentToken);

// when
List<Question> results = queryFactory.selectFrom(question)
Expand All @@ -126,11 +128,11 @@ public void setUp() throws Exception {
void contentToken_type으로_질문_검색_실패() {
// given
String invalidContentToken = "잘못된 질문";
QuestionType type = QuestionType.SUSI;
SelectQuestionCores selectQuestionCores = SelectQuestionCores.of(QuestionType.SUSI, null, invalidContentToken,
invalidContentToken);

// when
Optional<List<QuestionCore>> result = questionRepositoryImpl.searchQuestionsByContentTokenAndType(
invalidContentToken, type);
Optional<List<QuestionCore>> result = questionRepositoryImpl.searchQuestions(selectQuestionCores);

// then
assertThat(result).isPresent();
Expand All @@ -145,7 +147,7 @@ public void setUp() throws Exception {
String contentToken = "수시 입학 요강 대해";
QuestionType type = QuestionType.SUSI;
QuestionCategory category = QuestionCategory.ADMISSION_GUIDELINE;
NumberTemplate<Double> numberTemplate = createBooleanTemplate(question, contentToken);
NumberTemplate<Double> numberTemplate = createBooleanTemplateByContentToken(question, contentToken);

// when
List<String> results = queryFactory.select(question.contentToken)
Expand All @@ -165,12 +167,12 @@ public void setUp() throws Exception {
void contentToken_type_Category로_질문_검색_실패() {
// given
String invalidContentToken = "잘못된 질문";
QuestionType type = QuestionType.SUSI;
QuestionCategory category = QuestionCategory.ADMISSION_GUIDELINE;
SelectQuestionCores selectQuestionCores = SelectQuestionCores.of(QuestionType.SUSI,
QuestionCategory.ADMISSION_GUIDELINE, invalidContentToken,
invalidContentToken);

// when
Optional<List<QuestionCore>> result = questionRepositoryImpl.searchQuestionsByContentTokenAndTypeAndCategory(
invalidContentToken, type, category);
Optional<List<QuestionCore>> result = questionRepositoryImpl.searchQuestions(selectQuestionCores);

// then
assertThat(result).isPresent();
Expand All @@ -182,13 +184,13 @@ public void setUp() throws Exception {
void content로_질문_페이지네이션_조회_실패() {
// given
String content = "존재하지 않는 질문";
QuestionType type = QuestionType.SUSI;
QuestionCategory category = QuestionCategory.ADMISSION_GUIDELINE;
Pageable pageable = PageRequest.of(0, 3);
SelectQuestions selectQuestions = SelectQuestions.of(QuestionType.SUSI, QuestionCategory.ADMISSION_GUIDELINE,
content, null, null, pageable);

// when
SliceQuestionResponse<SearchedQuestionsResponse> result = questionRepositoryImpl.searchQuestionsOfCursorPagingByContentWithFullTextSearch(
type, category, content, null, null, pageable);
SliceQuestionResponse<SearchedQuestionsResponse> result = questionRepositoryImpl.searchQuestionsOfCursorPaging(
selectQuestions);

// then
assertThat(result.data()).isEmpty();
Expand All @@ -199,7 +201,7 @@ void contentToken_type() {
QQuestion question = QQuestion.question;
String contentToken = "수시 입학 요강 대해";
QuestionType type = QuestionType.SUSI;
NumberTemplate<Double> numberTemplate = createBooleanTemplate(question, contentToken);
NumberTemplate<Double> numberTemplate = createBooleanTemplateByContentToken(question, contentToken);

List<String> results = queryFactory.select(question.contentToken)
.from(question)
Expand All @@ -216,7 +218,7 @@ void contentToken_type() {
QQuestion question = QQuestion.question;
String contentToken = "수시 입학 요강 대해";

NumberTemplate<Double> numberTemplate = createBooleanTemplate(question, contentToken);
NumberTemplate<Double> numberTemplate = createBooleanTemplateByContentToken(question, contentToken);

// when
List<Question> results = queryFactory.selectFrom(question)
Expand All @@ -233,7 +235,7 @@ void contentToken_type() {
// given
QQuestion question = QQuestion.question;
String invalidContentToken = "존재하지 않는 질문";
NumberTemplate<Double> numberTemplate = createBooleanTemplate(question, invalidContentToken);
NumberTemplate<Double> numberTemplate = createBooleanTemplateByContentToken(question, invalidContentToken);

// when
List<Question> results = queryFactory.selectFrom(question)
Expand All @@ -244,7 +246,7 @@ void contentToken_type() {
assertThat(results).isEmpty();
}

private NumberTemplate<Double> createBooleanTemplate(QQuestion question, String content) {
private NumberTemplate<Double> createBooleanTemplateByContentToken(QQuestion question, String content) {
return Expressions.numberTemplate(Double.class, "function('match', {0}, {1})", question.content, content);
}
}

0 comments on commit 9521cd3

Please # to comment.