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

Api: ✏️ 월별 지출내역 조회시 발생하는 N+1 문제 개선 #110

Merged
merged 12 commits into from
Jun 23, 2024
Merged
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
test: searchspendings customcategory fetch 테스트 작성
  • Loading branch information
asn6878 committed Jun 6, 2024
commit a9eed9f87435d85142d48731423fabd20ddb0a08
Original file line number Diff line number Diff line change
@@ -1,55 +1,50 @@
package kr.co.pennyway.api.apis.ledger.service;

import jakarta.persistence.EntityManager;
import kr.co.pennyway.api.config.ExternalApiDBTestConfig;
import kr.co.pennyway.api.config.ExternalApiIntegrationTest;
import kr.co.pennyway.api.config.fixture.SpendingCustomCategoryFixture;
import kr.co.pennyway.api.config.fixture.SpendingFixture;
import kr.co.pennyway.api.config.fixture.UserFixture;
import kr.co.pennyway.domain.domains.spending.domain.Spending;
import kr.co.pennyway.domain.domains.spending.domain.SpendingCustomCategory;
import kr.co.pennyway.domain.domains.spending.service.SpendingCustomCategoryService;
import kr.co.pennyway.domain.domains.spending.service.SpendingService;
import kr.co.pennyway.domain.domains.user.domain.User;
import kr.co.pennyway.domain.domains.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.LazyInitializationException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

@Slf4j
@ExtendWith(MockitoExtension.class)
@ExternalApiIntegrationTest
class SpendingSearchServiceTest extends ExternalApiDBTestConfig {
@Autowired
private SpendingService spendingService;
@Autowired
private EntityManager em;
@Autowired
private SpendingCustomCategoryService spendingCustomCategoryService;
private SpendingSearchService spendingSearchService;
@Autowired
private UserService userService;
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;

@Test
@Transactional
@DisplayName("커스텀 카테고리 지출 내역을 기간별 조회시 카테고리를 바로 fetch 한다.")
void testReadSpendingsLazyLoading() {
// given
User user = userService.createUser(UserFixture.GENERAL_USER.toUser());

SpendingCustomCategory customCategory = SpendingCustomCategoryFixture.GENERAL_SPENDING_CUSTOM_CATEGORY.toCustomSpendingCategory(user);
spendingCustomCategoryService.createSpendingCustomCategory(customCategory);

Spending spending = SpendingFixture.CUSTOM_CATEGORY_SPENDING.toCustomCategorySpending(user, customCategory);
spendingService.createSpending(spending);
SpendingFixture.bulkInsertSpending(user, 100, true, jdbcTemplate);

// when - then
Assertions.assertThrows(LazyInitializationException.class, () -> {
em.clear();
spending.getSpendingCustomCategory().getName();
List<Spending> spendings = spendingSearchService.readSpendings(user.getId(), LocalDate.now().getYear(), LocalDate.now().getMonthValue());
assertDoesNotThrow(() -> {
String categoryName = spendings.get(0).getSpendingCustomCategory().getName();
log.info("{}", categoryName);
});
}
}
}