Skip to content

Commit

Permalink
Test: add find random questions
Browse files Browse the repository at this point in the history
  • Loading branch information
emost22 committed Jun 21, 2024
1 parent 545e5f2 commit 9e47e87
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.sirius.spurt.common.config.QuerydslConfigTest;
import com.sirius.spurt.store.repository.database.entity.ExperienceEntity;
import com.sirius.spurt.store.repository.database.entity.CategoryEntity;
import com.sirius.spurt.store.repository.database.entity.QuestionEntity;
import com.sirius.spurt.store.repository.database.entity.UserEntity;
import com.sirius.spurt.test.ExperienceTest;
import com.sirius.spurt.test.CategoryTest;
import com.sirius.spurt.test.QuestionTest;
import com.sirius.spurt.test.UserTest;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,30 +24,30 @@
@ActiveProfiles("test")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Import(QuerydslConfigTest.class)
class QuestionRepositoryTest implements QuestionTest, ExperienceTest {
class QuestionRepositoryTest implements QuestionTest, UserTest, CategoryTest {
@Autowired private QuestionRepository questionRepository;
@Autowired private UserRepository userRepository;
@Autowired private ExperienceRepository experienceRepository;

private UserEntity savedUser;
private ExperienceEntity savedExperience;
private CategoryEntity savedCategory;
private QuestionEntity savedQuestion;

@BeforeEach
void init() {
savedUser = userRepository.save(TEST_USER);
savedExperience = experienceRepository.save(TEST_EXPERIENCE);
savedCategory = CategoryEntity.builder().category(TEST_CATEGORY).build();
savedQuestion =
questionRepository.save(
QuestionEntity.builder()
.questionId(TEST_QUESTION_ID)
.questionId(QuestionTest.TEST_QUESTION_ID)
.userId(savedUser.getUserId())
.subject(TEST_QUESTION_SUBJECT)
.mainText(TEST_QUESTION_MAIN_TEXT)
.jobGroup(TEST_QUESTION_JOB_GROUP)
.pinIndicator(TEST_PIN_INDICATOR)
.pinUpdatedTime(TEST_PIN_UPDATED_TIME)
.experienceId(savedExperience.getExperienceId())
.experienceId(null)
.categoryEntityList(List.of(savedCategory))
.build());
}

Expand Down Expand Up @@ -122,14 +124,14 @@ void init() {
// given
questionRepository.save(
QuestionEntity.builder()
.questionId(2L)
.questionId(QuestionTest.TEST_QUESTION_ID)
.userId(savedUser.getUserId())
.subject(TEST_QUESTION_SUBJECT)
.mainText(TEST_QUESTION_MAIN_TEXT)
.jobGroup(TEST_QUESTION_JOB_GROUP)
.pinIndicator(TEST_PIN_INDICATOR)
.pinUpdatedTime(TEST_PIN_UPDATED_TIME)
.experienceId(savedExperience.getExperienceId())
.experienceId(null)
.build());
PageRequest pageRequest = PageRequest.of(0, 1);

Expand All @@ -143,4 +145,39 @@ void init() {
assertThat(pages.getTotalElements()).isEqualTo(2);
assertThat(pages.getContent().get(0)).isEqualTo(savedQuestion);
}

@Test
void 랜덤_질문_조회_테스트() {
// given
CategoryEntity savedCategory2 = CategoryEntity.builder().category(TEST_CATEGORY).build();
questionRepository.save(
QuestionEntity.builder()
.questionId(QuestionTest.TEST_ANOTHER_QUESTION_ID)
.userId(savedUser.getUserId())
.subject(TEST_QUESTION_SUBJECT)
.mainText(TEST_QUESTION_MAIN_TEXT)
.jobGroup(TEST_QUESTION_JOB_GROUP)
.pinIndicator(TEST_PIN_INDICATOR)
.pinUpdatedTime(TEST_PIN_UPDATED_TIME)
.experienceId(null)
.categoryEntityList(List.of(savedCategory2))
.build());

// when
List<QuestionEntity> questionEntity1 =
questionRepository.randomQuestion(
TEST_QUESTION_JOB_GROUP, TEST_ANOTHER_USER_ID, 1, TEST_CATEGORY);

List<QuestionEntity> questionEntity2 = null;
int cnt = 100;
do {
questionEntity2 =
questionRepository.randomQuestion(
TEST_QUESTION_JOB_GROUP, TEST_ANOTHER_USER_ID, 1, TEST_CATEGORY);
cnt--;
} while (questionEntity1.get(0).equals(questionEntity2.get(0)) && cnt > 0);

// then
assertThat(questionEntity1.get(0)).isNotEqualTo(questionEntity2.get(0));
}
}
1 change: 1 addition & 0 deletions src/test/java/com/sirius/spurt/test/QuestionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

public interface QuestionTest {
Long TEST_QUESTION_ID = 1L;
Long TEST_ANOTHER_QUESTION_ID = 2L;
String TEST_USER_ID = "userId";
String TEST_QUESTION_SUBJECT = "subject";
String TEST_QUESTION_MAIN_TEXT = "mainText";
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/sirius/spurt/test/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public interface UserTest {
String TEST_USER_ID = "userId";
String TEST_ANOTHER_USER_ID = "anotherUserId";
JobGroup TEST_JOB_GROUP = JobGroup.DEVELOPER;
String TEST_EMAIL = "[email protected]";
Boolean TEST_HAS_PINED = Boolean.TRUE;
Expand Down

0 comments on commit 9e47e87

Please sign in to comment.