Skip to content

Commit

Permalink
refactor: test 속도를 개선한다. (#612)
Browse files Browse the repository at this point in the history
* test: truncate sql로 실행하는 버전

* feat: jdbc-url에 cacheDefaultTimeZone 설정 추가

* refactor: 클리너 불필요한 메서드 제거, 리팩터링
  • Loading branch information
dusdn1702 authored Oct 7, 2021
1 parent a7b0bd0 commit 49d1d68
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.woowacourse.zzimkkong;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@Component
@Profile("test")
public class DatabaseCleaner implements InitializingBean {
@PersistenceContext
private EntityManager entityManager;

private List<String> tableNames;

@Override
public void afterPropertiesSet() {
tableNames = entityManager.getMetamodel().getEntities().stream()
.map(entry -> entry.getName().toLowerCase(Locale.ROOT))
.collect(Collectors.toList());
}

@Transactional
public void execute() {
entityManager.flush();
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
for (String tableName : tableNames) {
entityManager.createNativeQuery("TRUNCATE TABLE " + tableName).executeUpdate();
entityManager.createNativeQuery("ALTER TABLE " + tableName + " ALTER COLUMN id RESTART WITH 1").executeUpdate();
}
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.woowacourse.zzimkkong.controller;

import com.woowacourse.zzimkkong.DatabaseCleaner;
import com.woowacourse.zzimkkong.dto.map.MapCreateUpdateRequest;
import com.woowacourse.zzimkkong.dto.member.LoginRequest;
import com.woowacourse.zzimkkong.dto.member.MemberSaveRequest;
Expand All @@ -12,6 +13,7 @@
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,7 +24,6 @@
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

Expand All @@ -38,7 +39,6 @@
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.documentationConfiguration;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@AutoConfigureRestDocs
@ActiveProfiles("test")
Expand Down Expand Up @@ -85,6 +85,9 @@ class AcceptanceTest {
@LocalServerPort
int port;

@Autowired
private DatabaseCleaner databaseCleaner;

@MockBean
private StorageUploader storageUploader;

Expand All @@ -111,4 +114,9 @@ void setUp(RestDocumentationContextProvider restDocumentation) {
given(storageUploader.upload(anyString(), any(File.class)))
.willReturn(MAP_IMAGE_URL);
}

@AfterEach
void deleteAll() {
databaseCleaner.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class AdminControllerTest extends AcceptanceTest {
.enabledDayOfWeek(BE_ENABLED_DAY_OF_WEEK)
.build();
private static final Space BE = Space.builder()
.id(1L)
.name(BE_NAME)
.color(BE_COLOR)
.map(LUTHER)
Expand Down Expand Up @@ -155,7 +154,6 @@ void getReservations() {
SALLY_DESCRIPTION);
saveReservation(beReservationApi, newReservationCreateUpdateWithPasswordRequest);
Reservation reservation = Reservation.builder()
.id(1L)
.startTime(newReservationCreateUpdateWithPasswordRequest.getStartDateTime())
.endTime(newReservationCreateUpdateWithPasswordRequest.getEndDateTime())
.userName(newReservationCreateUpdateWithPasswordRequest.getName())
Expand Down

0 comments on commit 49d1d68

Please sign in to comment.