-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 테스트 시 db 데이터 지워줄 유틸리티 클래스 추가한다
- Loading branch information
1 parent
a43f3e2
commit ef014a8
Showing
2 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/test/java/life/offonoff/ab/application/testutil/AbCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters