Skip to content

Commit

Permalink
feat: 테스트 시 db 데이터 지워줄 유틸리티 클래스 추가한다
Browse files Browse the repository at this point in the history
  • Loading branch information
melonturtle committed Mar 14, 2024
1 parent a43f3e2 commit ef014a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/test/java/life/offonoff/ab/application/testutil/AbCleaner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package life.offonoff.ab.application.testutil;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RequiredArgsConstructor
@Service
public class AbCleaner {
private final JdbcTemplate jdbcTemplate;
private List<String> truncateQueries;

@PostConstruct
public void loadTruncateQueries(){
truncateQueries = jdbcTemplate.queryForList(
"SELECT Concat('TRUNCATE TABLE ', TABLE_NAME, ';') AS q FROM INFORMATION_SCHEMA.TABLES " +
"WHERE TABLE_SCHEMA IN ('PUBLIC', 'localab', 'ab')",
String.class);
}

@Transactional
public void cleanTables() {
jdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0");
truncateQueries.forEach(jdbcTemplate::execute);
jdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1");
}
}
7 changes: 5 additions & 2 deletions src/test/java/life/offonoff/ab/domain/TestEntityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import life.offonoff.ab.domain.topic.choice.Choice;
import life.offonoff.ab.domain.topic.choice.ChoiceOption;
import life.offonoff.ab.domain.topic.choice.content.ChoiceContent;
import life.offonoff.ab.domain.topic.choice.content.ImageTextChoiceContent;
import life.offonoff.ab.domain.vote.Vote;
import lombok.Builder;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -64,7 +63,11 @@ public static Topic createRandomTopic() {
}

public static Topic createRandomTopicHavingChoices(ChoiceOption... options) {
Topic topic = createRandomTopic();
return createRandomTopicByMemberWithChoices(createRandomMember(), TopicSide.TOPIC_B, options);
}

public static Topic createRandomTopicByMemberWithChoices(Member author, TopicSide side, ChoiceOption... options) {
Topic topic = createRandomTopicByMember(author, side);

for (var option : options) {
createChoice(topic, option, null);
Expand Down

0 comments on commit ef014a8

Please sign in to comment.