|
| 1 | +package com.gitranker.api.domain.log; |
| 2 | + |
| 3 | +import com.gitranker.api.domain.user.Role; |
| 4 | +import com.gitranker.api.domain.user.User; |
| 5 | +import com.gitranker.api.domain.user.UserRepository; |
| 6 | +import com.gitranker.api.domain.user.vo.ActivityStatistics; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; |
| 12 | +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
| 13 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 14 | +import org.springframework.test.context.TestPropertySource; |
| 15 | +import org.testcontainers.containers.MySQLContainer; |
| 16 | +import org.testcontainers.junit.jupiter.Container; |
| 17 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 18 | + |
| 19 | +import java.time.LocalDate; |
| 20 | +import java.time.LocalDateTime; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +@DataJpaTest |
| 26 | +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) |
| 27 | +@Testcontainers |
| 28 | +@TestPropertySource(properties = { |
| 29 | + "spring.jpa.hibernate.ddl-auto=create", |
| 30 | + "spring.batch.jdbc.initialize-schema=never" |
| 31 | +}) |
| 32 | +class ActivityLogRepositoryIT { |
| 33 | + |
| 34 | + @Container |
| 35 | + @ServiceConnection |
| 36 | + static MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.0") |
| 37 | + .withDatabaseName("gitranker_test"); |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private ActivityLogRepository activityLogRepository; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private UserRepository userRepository; |
| 44 | + |
| 45 | + private User savedUser; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp() { |
| 49 | + activityLogRepository.deleteAll(); |
| 50 | + userRepository.deleteAll(); |
| 51 | + |
| 52 | + savedUser = userRepository.save(User.builder() |
| 53 | + .githubId(1L) |
| 54 | + .nodeId("node1") |
| 55 | + .username("testuser") |
| 56 | + .githubCreatedAt(LocalDateTime.of(2020, 1, 1, 0, 0)) |
| 57 | + .role(Role.USER) |
| 58 | + .build()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @DisplayName("여러 로그 중 가장 최근 날짜의 로그를 조회한다") |
| 63 | + void should_findLatestLog_when_multipleLogsExist() { |
| 64 | + ActivityStatistics stats = ActivityStatistics.of(10, 2, 1, 0, 3); |
| 65 | + ActivityStatistics diff = ActivityStatistics.of(5, 1, 0, 0, 1); |
| 66 | + |
| 67 | + activityLogRepository.save(ActivityLog.of(savedUser, stats, diff, LocalDate.of(2025, 1, 1))); |
| 68 | + activityLogRepository.save(ActivityLog.of(savedUser, stats, diff, LocalDate.of(2025, 1, 2))); |
| 69 | + ActivityLog latestLog = activityLogRepository.save( |
| 70 | + ActivityLog.of(savedUser, stats, diff, LocalDate.of(2025, 1, 3))); |
| 71 | + |
| 72 | + ActivityLog found = activityLogRepository.getTopByUserOrderByActivityDateDesc(savedUser); |
| 73 | + |
| 74 | + assertThat(found).isNotNull(); |
| 75 | + assertThat(found.getActivityDate()).isEqualTo(LocalDate.of(2025, 1, 3)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + @DisplayName("로그가 없으면 null을 반환한다") |
| 80 | + void should_returnNull_when_noLogExists() { |
| 81 | + ActivityLog found = activityLogRepository.getTopByUserOrderByActivityDateDesc(savedUser); |
| 82 | + |
| 83 | + assertThat(found).isNull(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @DisplayName("특정 날짜 이전의 가장 최근 로그를 조회한다") |
| 88 | + void should_findBaselineLog_when_logBeforeDateExists() { |
| 89 | + ActivityStatistics stats = ActivityStatistics.of(10, 2, 1, 0, 3); |
| 90 | + ActivityStatistics diff = ActivityStatistics.of(5, 1, 0, 0, 1); |
| 91 | + |
| 92 | + activityLogRepository.save(ActivityLog.of(savedUser, stats, diff, LocalDate.of(2025, 1, 1))); |
| 93 | + activityLogRepository.save(ActivityLog.of(savedUser, stats, diff, LocalDate.of(2025, 1, 10))); |
| 94 | + activityLogRepository.save(ActivityLog.of(savedUser, stats, diff, LocalDate.of(2025, 1, 20))); |
| 95 | + |
| 96 | + // 1월 15일 이전의 가장 최근 로그 → 1월 10일 |
| 97 | + Optional<ActivityLog> found = activityLogRepository |
| 98 | + .findTopByUserAndActivityDateLessThanOrderByActivityDateDesc( |
| 99 | + savedUser, LocalDate.of(2025, 1, 15)); |
| 100 | + |
| 101 | + assertThat(found).isPresent(); |
| 102 | + assertThat(found.get().getActivityDate()).isEqualTo(LocalDate.of(2025, 1, 10)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + @DisplayName("기준 날짜 이전에 로그가 없으면 빈 Optional을 반환한다") |
| 107 | + void should_returnEmpty_when_noLogBeforeDateExists() { |
| 108 | + ActivityStatistics stats = ActivityStatistics.of(10, 2, 1, 0, 3); |
| 109 | + ActivityStatistics diff = ActivityStatistics.of(5, 1, 0, 0, 1); |
| 110 | + |
| 111 | + activityLogRepository.save(ActivityLog.of(savedUser, stats, diff, LocalDate.of(2025, 6, 1))); |
| 112 | + |
| 113 | + // 2025년 1월 이전 → 없음 |
| 114 | + Optional<ActivityLog> found = activityLogRepository |
| 115 | + .findTopByUserAndActivityDateLessThanOrderByActivityDateDesc( |
| 116 | + savedUser, LocalDate.of(2025, 1, 1)); |
| 117 | + |
| 118 | + assertThat(found).isEmpty(); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + @DisplayName("사용자와 날짜로 정확히 일치하는 로그를 조회한다") |
| 123 | + void should_findLog_when_userAndDateMatch() { |
| 124 | + ActivityStatistics stats = ActivityStatistics.of(50, 10, 5, 3, 8); |
| 125 | + ActivityStatistics diff = ActivityStatistics.of(5, 1, 1, 0, 2); |
| 126 | + |
| 127 | + activityLogRepository.save(ActivityLog.of(savedUser, stats, diff, LocalDate.of(2025, 3, 15))); |
| 128 | + |
| 129 | + Optional<ActivityLog> found = activityLogRepository.findByUserAndActivityDate( |
| 130 | + savedUser, LocalDate.of(2025, 3, 15)); |
| 131 | + |
| 132 | + assertThat(found).isPresent(); |
| 133 | + assertThat(found.get().getCommitCount()).isEqualTo(50); |
| 134 | + assertThat(found.get().getIssueCount()).isEqualTo(10); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + @DisplayName("사용자의 모든 로그를 삭제한다") |
| 139 | + void should_deleteAllLogs_when_deleteAllByUserCalled() { |
| 140 | + ActivityStatistics stats = ActivityStatistics.of(10, 2, 1, 0, 3); |
| 141 | + ActivityStatistics diff = ActivityStatistics.of(5, 1, 0, 0, 1); |
| 142 | + |
| 143 | + activityLogRepository.save(ActivityLog.of(savedUser, stats, diff, LocalDate.of(2025, 1, 1))); |
| 144 | + activityLogRepository.save(ActivityLog.of(savedUser, stats, diff, LocalDate.of(2025, 1, 2))); |
| 145 | + |
| 146 | + activityLogRepository.deleteAllByUser(savedUser); |
| 147 | + |
| 148 | + assertThat(activityLogRepository.getTopByUserOrderByActivityDateDesc(savedUser)).isNull(); |
| 149 | + } |
| 150 | +} |
0 commit comments