-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEAT] truncate extension #45
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 6 additions & 1 deletion
7
backend/src/test/java/ddangkong/controller/BaseControllerTest.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
package ddangkong.service; | ||
|
||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
|
||
import ddangkong.support.extension.DatabaseCleanerExtension; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.jdbc.Sql; | ||
|
||
@SpringBootTest | ||
@ExtendWith(DatabaseCleanerExtension.class) | ||
@SpringBootTest(webEnvironment = WebEnvironment.NONE) | ||
@Sql(scripts = "/init-test.sql") | ||
public abstract class BaseServiceTest { | ||
} |
47 changes: 47 additions & 0 deletions
47
backend/src/test/java/ddangkong/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,47 @@ | ||
package ddangkong.support.extension; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import java.util.List; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
public class DatabaseCleanerExtension implements BeforeEachCallback { | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext extensionContext) { | ||
ApplicationContext context = SpringExtension.getApplicationContext(extensionContext); | ||
cleanup(context); | ||
} | ||
|
||
private void cleanup(ApplicationContext context) { | ||
EntityManager em = context.getBean(EntityManager.class); | ||
TransactionTemplate transactionTemplate = context.getBean(TransactionTemplate.class); | ||
|
||
transactionTemplate.execute(action -> { | ||
em.clear(); | ||
truncateTables(em); | ||
return null; | ||
}); | ||
} | ||
|
||
private void truncateTables(EntityManager em) { | ||
em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate(); | ||
for (String tableName : findTableNames(em)) { | ||
em.createNativeQuery("TRUNCATE TABLE %s RESTART IDENTITY".formatted(tableName)).executeUpdate(); | ||
} | ||
em.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 질문) 이건 무슨 에노테이션인가요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
private List<String> findTableNames(EntityManager em) { | ||
String tableNameSelectQuery = """ | ||
SELECT TABLE_NAME | ||
FROM INFORMATION_SCHEMA.TABLES | ||
WHERE TABLE_SCHEMA = 'PUBLIC' | ||
"""; | ||
return em.createNativeQuery(tableNameSelectQuery).getResultList(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,34 +1,32 @@ | ||
SET REFERENTIAL_INTEGRITY FALSE; | ||
|
||
TRUNCATE TABLE member; | ||
TRUNCATE TABLE balance_option; | ||
TRUNCATE TABLE balance_content; | ||
TRUNCATE TABLE balance_vote; | ||
TRUNCATE TABLE room; | ||
TRUNCATE TABLE room_content; | ||
|
||
ALTER TABLE member ALTER COLUMN ID RESTART WITH 1; | ||
ALTER TABLE balance_option ALTER COLUMN ID RESTART WITH 1; | ||
ALTER TABLE balance_content ALTER COLUMN ID RESTART WITH 1; | ||
ALTER TABLE balance_vote ALTER COLUMN ID RESTART WITH 1; | ||
ALTER TABLE room ALTER COLUMN ID RESTART WITH 1; | ||
ALTER TABLE room_content ALTER COLUMN ID RESTART WITH 1; | ||
|
||
INSERT INTO room() VALUES (); | ||
|
||
INSERT INTO member(nickname, room_id) | ||
VALUES ('mohamedeu al katan', 1), ('deundeun', 1), ('rupi', 1), ('rapper lee', 1); | ||
|
||
INSERT INTO room_content(room_id, balance_content_id, created_at) | ||
VALUES (1, 2, '2024-07-18 19:50:00.000'), (1, 1, '2024-07-18 20:00:00.000'); | ||
|
||
INSERT INTO balance_content(category, name) | ||
VALUES ('EXAMPLE', '민초 vs 반민초'), ('EXAMPLE', '월 200 백수 vs 월 500 직장인'); | ||
|
||
INSERT INTO balance_option(name, balance_content_id) | ||
VALUES ('민초', 1), ('반민초', 1), ('월 200 백수', 2), ('월 200 직장인', 2); | ||
|
||
INSERT INTO balance_vote(balance_option_id, member_id) | ||
VALUES (4, 1), (4, 2), (4, 3), (4, 4), (1, 1), (1, 2), (1, 3), (2, 4); | ||
|
||
SET REFERENTIAL_INTEGRITY TRUE; | ||
INSERT INTO room () | ||
VALUES (); | ||
|
||
INSERT INTO member (nickname, room_id) | ||
VALUES ('mohamedeu al katan', 1), | ||
('deundeun', 1), | ||
('rupi', 1), | ||
('rapper lee', 1); | ||
|
||
INSERT INTO balance_content (category, name) | ||
VALUES ('EXAMPLE', '민초 vs 반민초'), | ||
('EXAMPLE', '월 200 백수 vs 월 500 직장인'); | ||
|
||
INSERT INTO room_content (room_id, balance_content_id, created_at) | ||
VALUES (1, 2, '2024-07-18 19:50:00.000'), | ||
(1, 1, '2024-07-18 20:00:00.000'); | ||
|
||
INSERT INTO balance_option (name, balance_content_id) | ||
VALUES ('민초', 1), | ||
('반민초', 1), | ||
('월 200 백수', 2), | ||
('월 200 직장인', 2); | ||
|
||
INSERT INTO balance_vote (balance_option_id, member_id) | ||
VALUES (4, 1), | ||
(4, 2), | ||
(4, 3), | ||
(4, 4), | ||
(1, 1), | ||
(1, 2), | ||
(1, 3), | ||
(2, 4); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오타 수정 감사합니다~~ ㅠ_ㅠ