-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 테스트 데이터 클리너 구현 * feat: 테스트 클리너 상수, 메서드 분리 * refactor: 데이터베이스 클리너 적용
- Loading branch information
Showing
7 changed files
with
96 additions
and
71 deletions.
There are no files selected for viewing
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
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
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
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
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
52 changes: 52 additions & 0 deletions
52
server/src/test/java/server/haengdong/support/extension/DatabaseCleaner.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,52 @@ | ||
package server.haengdong.support.extension; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import java.util.List; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
class DatabaseCleaner { | ||
|
||
private static final String REFERENTIAL_FORMAT = "set referential_integrity %b;"; | ||
private static final String TRUNCATE_FORMAT = "truncate table %s restart identity;"; | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
private String truncateTablesQuery; | ||
|
||
@PostConstruct | ||
public void createTruncateQuery() { | ||
List<String> tableNames = getTableNames(); | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
|
||
for (String tableName : tableNames) { | ||
String truncateQuery = String.format(TRUNCATE_FORMAT, tableName); | ||
stringBuilder.append(truncateQuery); | ||
} | ||
truncateTablesQuery = stringBuilder.toString(); | ||
} | ||
|
||
private List<String> getTableNames() { | ||
String sql = """ | ||
select table_name | ||
from information_schema.tables | ||
where table_schema = 'PUBLIC' | ||
"""; | ||
return em.createNativeQuery(sql).getResultList(); | ||
} | ||
|
||
@Transactional | ||
public void clear() { | ||
em.clear(); | ||
truncate(); | ||
} | ||
|
||
private void truncate() { | ||
em.createNativeQuery(String.format(REFERENTIAL_FORMAT, false)).executeUpdate(); | ||
em.createNativeQuery(truncateTablesQuery).executeUpdate(); | ||
em.createNativeQuery(String.format(REFERENTIAL_FORMAT, true)).executeUpdate(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
server/src/test/java/server/haengdong/support/extension/DatabaseCleanerExtension.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,19 @@ | ||
package server.haengdong.support.extension; | ||
|
||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
public class DatabaseCleanerExtension implements AfterEachCallback { | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) { | ||
DatabaseCleaner databaseCleaner = getDataCleaner(context); | ||
databaseCleaner.clear(); | ||
} | ||
|
||
private DatabaseCleaner getDataCleaner(ExtensionContext extensionContext) { | ||
return SpringExtension.getApplicationContext(extensionContext) | ||
.getBean(DatabaseCleaner.class); | ||
} | ||
} |