Skip to content
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

refactor: 테스트 최적화 #867

Merged
merged 48 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
08adaeb
feat: DB초기화 방식 변경
Sep 2, 2024
873d16e
테스트 수정중
Sep 2, 2024
3dddbd6
feat: BUS 테스트 변
Sep 2, 2024
cd4a38b
feat: 게시판 테스트 수
Sep 2, 2024
03e7d43
feat: 게시판 테스트 수정
Sep 2, 2024
c9020b7
feat: dining테스트 변
Sep 3, 2024
70bcaf0
feat: 사용하지 않는 임포트 제거
Sep 3, 2024
116044f
feat: 키워드 알림 테스트 수
Sep 3, 2024
cc6d69d
feat: 복덕방 테스트 수정
Sep 3, 2024
990340b
feat: 멤버 테스트 수정
Sep 3, 2024
0968158
feat: pull develop
Sep 3, 2024
6a0934d
Merge branch 'feature/853-test-optimization' of https://github.com/BC…
Sep 3, 2024
c15aa33
feat: test optimization admin(shop, user 제외)
krSeonghyeon Sep 3, 2024
8bf2904
refactor: 중간 커밋 (Tx 논의중)
Choi-JJunho Sep 3, 2024
e836f23
feat: 수동 커밋,롤백 테스트 작성
Sep 3, 2024
091b2ff
feat: test optimization admin(shop 3개제외 완성)
krSeonghyeon Sep 3, 2024
4d3e770
refactor: 불필요 어노테이션 제거
krSeonghyeon Sep 3, 2024
4565eea
refactor: 충돌해결 위치변경
krSeonghyeon Sep 3, 2024
482e68b
fix: git conflict 해결
krSeonghyeon Sep 3, 2024
79c9d0d
Merge remote-tracking branch 'origin/task/853-test-optimization-admin…
krSeonghyeon Sep 3, 2024
7cc253e
fix: merge 진행
krSeonghyeon Sep 3, 2024
feea3b4
Merge remote-tracking branch 'origin/task/853-test-optimization-admin…
krSeonghyeon Sep 3, 2024
943ebfa
Merge remote-tracking branch 'refs/remotes/origin/feature/853-test-op…
Choi-JJunho Sep 4, 2024
1786f38
feat: BeforeAll설정
Sep 4, 2024
7977dac
refactor: 테스트 3개 불통 왜 안되지..
Choi-JJunho Sep 4, 2024
e894bcd
refactor: 일단 커밋
Choi-JJunho Sep 4, 2024
8e6191e
refactor: displayName을 메소드명으로
Choi-JJunho Sep 4, 2024
16a1126
Merge pull request #870 from BCSDLab/task/853-test-optimization-admin
20HyeonsuLee Sep 4, 2024
ba0ffa7
Merge branch 'feature/853-test-optimization' of https://github.com/BC…
Sep 4, 2024
2bb3f35
test: mockmvc로 수정
daheeParkk Sep 4, 2024
b26f813
Merge pull request #872 from BCSDLab/task/853-test-optimization-user
20HyeonsuLee Sep 4, 2024
f7e1763
Merge branch 'feature/853-test-optimization' of https://github.com/BC…
Sep 4, 2024
649a45d
refactor: exception throws 추가
Choi-JJunho Sep 5, 2024
51ad0fd
Merge pull request #873 from BCSDLab/refactor/shop-test-fix
20HyeonsuLee Sep 5, 2024
e45b01e
feat: 테스트 병합
Sep 5, 2024
bd8fcd8
feat: 테스트 병합
Sep 5, 2024
4e7d0c0
feat: 리뷰 테스트 작성
Sep 5, 2024
fd03540
feat: 테스트 작성
Sep 5, 2024
78b99f9
feat: 테스트 작성
Sep 6, 2024
4804fce
Merge pull request #875 from BCSDLab/task/853-test-optimization-other
20HyeonsuLee Sep 6, 2024
93d563e
feat: pull develop
Sep 6, 2024
92d11a0
feat: pull develop
Sep 6, 2024
2ad37cc
feat: 트랜잭션 중복선언 제거
Sep 9, 2024
ca77bb4
feat: BCSD_Lab → BCSDLab으로 통
Sep 9, 2024
153b529
feat: DisplayName
Sep 9, 2024
00d6e3c
feat: 반환값 jsonPath이용한 검증으로 변
Sep 9, 2024
1327e4f
feat: 리뷰 반
Sep 9, 2024
ddcd062
feat: random part 제거
Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

잘 몰라서 혹시 테스트 최적화와 관련있는 내용인가요?

Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ public void createShop(AdminCreateShopRequest adminCreateShopRequest) {
.shop(savedShop)
.name(categoryName)
.build();
adminMenuCategoryRepository.save(menuCategory);
savedShop.getMenuCategories().add(menuCategory);
}
for (String imageUrl : adminCreateShopRequest.imageUrls()) {
ShopImage shopImage = ShopImage.builder()
.shop(savedShop)
.imageUrl(imageUrl)
.build();
adminShopImageRepository.save(shopImage);
savedShop.getShopImages().add(shopImage);
}
for (InnerShopOpen open : adminCreateShopRequest.open()) {
ShopOpen shopOpen = ShopOpen.builder()
Expand All @@ -128,15 +128,15 @@ public void createShop(AdminCreateShopRequest adminCreateShopRequest) {
.dayOfWeek(open.dayOfWeek())
.closed(open.closed())
.build();
adminShopOpenRepository.save(shopOpen);
savedShop.getShopOpens().add(shopOpen);
}
List<ShopCategory> categories = adminShopCategoryRepository.findAllByIdIn(adminCreateShopRequest.categoryIds());
for (ShopCategory shopCategory : categories) {
ShopCategoryMap shopCategoryMap = ShopCategoryMap.builder()
.shopCategory(shopCategory)
.shop(savedShop)
.build();
adminShopCategoryMapRepository.save(shopCategoryMap);
savedShop.getShopCategories().add(shopCategoryMap);
}
}

Expand All @@ -160,30 +160,30 @@ public void createMenu(Integer shopId, AdminCreateMenuRequest adminCreateMenuReq
.menuCategory(menuCategory)
.menu(savedMenu)
.build();
adminMenuCategoryMapRepository.save(menuCategoryMap);
savedMenu.getMenuCategoryMaps().add(menuCategoryMap);
}
for (String imageUrl : adminCreateMenuRequest.imageUrls()) {
MenuImage menuImage = MenuImage.builder()
.imageUrl(imageUrl)
.menu(savedMenu)
.build();
adminMenuImageRepository.save(menuImage);
savedMenu.getMenuImages().add(menuImage);
}
if (adminCreateMenuRequest.optionPrices() == null) {
MenuOption menuOption = MenuOption.builder()
.option(savedMenu.getName())
.price(adminCreateMenuRequest.singlePrice())
.menu(menu)
.build();
adminMenuDetailRepository.save(menuOption);
savedMenu.getMenuOptions().add(menuOption);
} else {
for (var option : adminCreateMenuRequest.optionPrices()) {
MenuOption menuOption = MenuOption.builder()
.option(option.option())
.price(option.price())
.menu(menu)
.build();
adminMenuDetailRepository.save(menuOption);
savedMenu.getMenuOptions().add(menuOption);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public void saveDiningImage(DiningImageRequest imageRequest) {
if (isOpened && !isImageExist) {
eventPublisher.publishEvent(new DiningImageUploadEvent(dining.getId(), dining.getImageUrl()));
}

dining.setImageUrl(imageRequest.imageUrl());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ public void createOwnerShops(Integer ownerId, OwnerShopsRequest ownerShopsReques
.shop(savedShop)
.name(categoryName)
.build();
menuCategoryRepository.save(menuCategory);
savedShop.getMenuCategories().add(menuCategory);
}
for (String imageUrl : ownerShopsRequest.imageUrls()) {
ShopImage shopImage = ShopImage.builder()
.shop(savedShop)
.imageUrl(imageUrl)
.build();
shopImageRepository.save(shopImage);
savedShop.getShopImages().add(shopImage);
}
for (OwnerShopsRequest.InnerOpenRequest open : ownerShopsRequest.open()) {
ShopOpen shopOpen = ShopOpen.builder()
Expand All @@ -114,15 +114,15 @@ public void createOwnerShops(Integer ownerId, OwnerShopsRequest ownerShopsReques
.dayOfWeek(open.dayOfWeek())
.closed(open.closed())
.build();
shopOpenRepository.save(shopOpen);
savedShop.getShopOpens().add(shopOpen);
}
List<ShopCategory> shopCategories = shopCategoryRepository.findAllByIdIn(ownerShopsRequest.categoryIds());
for (ShopCategory shopCategory : shopCategories) {
ShopCategoryMap shopCategoryMap = ShopCategoryMap.builder()
.shopCategory(shopCategory)
.shop(savedShop)
.build();
shopCategoryMapRepository.save(shopCategoryMap);
savedShop.getShopCategories().add(shopCategoryMap);
}
}

Expand Down Expand Up @@ -188,30 +188,30 @@ public void createMenu(Integer shopId, Integer ownerId, CreateMenuRequest create
.menuCategory(menuCategory)
.menu(savedMenu)
.build();
menuCategoryMapRepository.save(menuCategoryMap);
savedMenu.getMenuCategoryMaps().add(menuCategoryMap);
}
for (String imageUrl : createMenuRequest.imageUrls()) {
MenuImage menuImage = MenuImage.builder()
.imageUrl(imageUrl)
.menu(savedMenu)
.build();
menuImageRepository.save(menuImage);
savedMenu.getMenuImages().add(menuImage);
}
if (createMenuRequest.optionPrices() == null) {
MenuOption menuOption = MenuOption.builder()
.option(savedMenu.getName())
.price(createMenuRequest.singlePrice())
.menu(menu)
.build();
menuDetailRepository.save(menuOption);
savedMenu.getMenuOptions().add(menuOption);
} else {
for (var option : createMenuRequest.optionPrices()) {
MenuOption menuOption = MenuOption.builder()
.option(option.option())
.price(option.price())
.menu(menu)
.build();
menuDetailRepository.save(menuOption);
savedMenu.getMenuOptions().add(menuOption);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ default Menu getById(Integer menuId) {
}

List<Menu> findAllByShopId(Integer shopId);

List<Menu> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public ModelAndView authenticate(AuthTokenRequest request) {
}

Student student = studentTemporaryStatus.get().toStudent(passwordEncoder);

studentRepository.save(student);
userRepository.save(student.getUser());

Expand Down
48 changes: 28 additions & 20 deletions src/test/java/in/koreatech/koin/AcceptanceTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package in.koreatech.koin;

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

import java.time.Clock;

import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.transaction.TestTransaction;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
Expand All @@ -28,21 +28,21 @@
import in.koreatech.koin.domain.shop.model.ReviewEventListener;
import in.koreatech.koin.domain.shop.model.ShopEventListener;
import in.koreatech.koin.domain.user.model.StudentEventListener;
import in.koreatech.koin.util.TestCircuitBreakerClient;
import in.koreatech.koin.support.DBInitializer;
import io.restassured.RestAssured;
import in.koreatech.koin.util.TestCircuitBreakerClient;
import jakarta.persistence.EntityManager;

@SpringBootTest(webEnvironment = RANDOM_PORT)
@SpringBootTest
@AutoConfigureMockMvc
@Import({DBInitializer.class, TestJpaConfiguration.class, TestTimeConfig.class, TestRedisConfiguration.class})
@ActiveProfiles("test")
public abstract class AcceptanceTest {

private static final String ROOT = "test";
private static final String ROOT_PASSWORD = "1234";

@LocalServerPort
protected int port;
@Autowired
public MockMvc mockMvc;

@MockBean
protected OwnerEventListener ownerEventListener;
Expand Down Expand Up @@ -97,29 +97,37 @@ private static void configureProperties(final DynamicPropertyRegistry registry)

static {
mySqlContainer = (MySQLContainer)new MySQLContainer("mysql:8.0.29")
.withDatabaseName("test")
.withUsername(ROOT)
.withPassword(ROOT_PASSWORD)
.withCommand("--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci");
.withDatabaseName("test")
.withUsername(ROOT)
.withPassword(ROOT_PASSWORD)
.withCommand("--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci");

redisContainer = new GenericContainer<>(
DockerImageName.parse("redis:7.0.9"))
.withExposedPorts(6379);
DockerImageName.parse("redis:7.0.9"))
.withExposedPorts(6379);

mongoContainer = new GenericContainer<>(
DockerImageName.parse("mongo:6.0.14"))
.withExposedPorts(27017);
DockerImageName.parse("mongo:6.0.14"))
.withExposedPorts(27017);

mySqlContainer.start();
redisContainer.start();
mongoContainer.start();
}

@BeforeEach
void delete() {
if (RestAssured.port == RestAssured.UNDEFINED_PORT) {
RestAssured.port = port;
}
void initIncrement() {
dataInitializer.initIncrement();
dataInitializer.clearRedis();
}
Comment on lines +119 to +122
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

resetAutoIncrement같은 네이밍은 어떤가요? 함수 용도를 유추하기 애매한 것 같아요


protected void clear() {
dataInitializer.clear();
}
Comment on lines +124 to 126
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

이것도 어떤 걸 clear하는지 명시하면 좋겠어요 (ex. dbClear)


protected void forceVerify(Runnable runnable) {
TestTransaction.flagForCommit();
TestTransaction.end();
runnable.run();
}
Comment on lines +128 to +132
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

이건 어떤 용도인가요?

}
57 changes: 26 additions & 31 deletions src/test/java/in/koreatech/koin/acceptance/ActivityApiTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
package in.koreatech.koin.acceptance;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.time.LocalDate;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;

import in.koreatech.koin.AcceptanceTest;
import in.koreatech.koin.fixture.ActivityFixture;
import in.koreatech.koin.support.JsonAssertions;
import io.restassured.RestAssured;

@SuppressWarnings("NonAsciiCharacters")
@Transactional
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ActivityApiTest extends AcceptanceTest {

@Autowired
protected ActivityFixture activityFixture;

@Test
@DisplayName("BCSD Lab 활동 내역을 조회한다.")
void getActivities() {
void BCSDLab_활동_내역을_조회한다() throws Exception {
activityFixture.builder()
.title("BCSD/KAP 통합")
.description("BCSD와 KAP가 통합되었습니다.")
Expand Down Expand Up @@ -48,17 +52,13 @@ void getActivities() {
.isDeleted(false)
.build();

var response = RestAssured
.given()
.when()
.param("year", 2019)
.get("/activities")
.then()
.statusCode(HttpStatus.OK.value())
.extract();

JsonAssertions.assertThat(response.asPrettyString())
.isEqualTo("""
mockMvc.perform(
get("/activities")
.param("year", "2019")
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(content().json("""
{
"Activities": [
{
Expand Down Expand Up @@ -88,12 +88,11 @@ void getActivities() {
}
]
}
""");
"""));
}

@Test
@DisplayName("BCSD Lab 활동 내역을 조회한다. - 파라미터가 없는 경우 전체조회")
void getActivitiesWithoutYear() {
void BCSDLab_활동_내역을_조회한다_파라미터가_없는_경우_전체조회() throws Exception {
activityFixture.builder()
.title("BCSD/KAP 통합")
.description("BCSD와 KAP가 통합되었습니다.")
Expand Down Expand Up @@ -121,16 +120,12 @@ void getActivitiesWithoutYear() {
.isDeleted(false)
.build();

var response = RestAssured
.given()
.when()
.get("/activities")
.then()
.statusCode(HttpStatus.OK.value())
.extract();

JsonAssertions.assertThat(response.asPrettyString())
.isEqualTo("""
mockMvc.perform(
get("/activities")
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(content().json("""
{
"Activities": [
{
Expand Down Expand Up @@ -172,6 +167,6 @@ void getActivitiesWithoutYear() {
}
]
}
""");
"""));
}
}
Loading
Loading