@@ -10,6 +10,7 @@ import org.mockito.junit.jupiter.MockitoExtension
1010import org.mockito.kotlin.any
1111import org.mockito.kotlin.whenever
1212import org.runnect.server.course.entity.Course
13+ import org.runnect.server.publicCourse.entity.PublicCourse
1314import org.runnect.server.record.entity.Record
1415import org.runnect.server.record.repository.RecordRepository
1516import org.runnect.server.user.entity.RunnectUser
@@ -115,6 +116,49 @@ class RecordRankingServiceTest {
115116 assertThat(myRanking.rank).isNull()
116117 }
117118
119+ @Test
120+ fun `backfillAll은 publicCourse가 있는 기록만 대상으로 랭킹을 갱신하고 실제 갱신된 개수를 반환한다` () {
121+ val user1 = buildUser(1L , " 런너A" )
122+ val user2 = buildUser(2L , " 런너B" )
123+ val recordWithPublicCourse1 = buildRecordWithPublicCourse(id = 100L , owner = user1, publicCourseId = 1L )
124+ val recordWithPublicCourse2 = buildRecordWithPublicCourse(id = 101L , owner = user2, publicCourseId = 1L )
125+
126+ whenever(recordRepository.findAllByPublicCourseIsNotNull())
127+ .thenReturn(listOf (recordWithPublicCourse1, recordWithPublicCourse2))
128+
129+ val hashOps: HashOperations <String , String , String > = mock(HashOperations ::class .java) as HashOperations <String , String , String >
130+ whenever(stringRedisTemplate.opsForHash<String , String >()).thenReturn(hashOps)
131+ // 첫 번째 기록만 실제로 더 빠른 기록으로 갱신되고, 두 번째는 갱신되지 않는 상황을 시뮬레이션
132+ whenever(stringRedisTemplate.execute(any<RedisCallback <Boolean >>()))
133+ .thenReturn(true )
134+ .thenReturn(false )
135+
136+ val updatedCount = service.backfillAll()
137+
138+ assertThat(updatedCount).isEqualTo(1 )
139+ }
140+
141+ private fun buildRecordWithPublicCourse (id : Long , owner : RunnectUser , publicCourseId : Long ): Record {
142+ val course = buildCourse(id + 1000 , owner)
143+ val publicCourse = PublicCourse .builder()
144+ .course(course)
145+ .title(" 공개 코스" )
146+ .description(" 설명" )
147+ .build()
148+ ReflectionTestUtils .setField(publicCourse, " id" , publicCourseId)
149+
150+ val record = Record .builder()
151+ .runnectUser(owner)
152+ .course(course)
153+ .publicCourse(publicCourse)
154+ .title(" 퇴근길 러닝" )
155+ .pace(Time .valueOf(" 00:05:30" ))
156+ .time(Time .valueOf(" 00:25:00" ))
157+ .build()
158+ ReflectionTestUtils .setField(record, " id" , id)
159+ return record
160+ }
161+
118162 private fun buildUser (id : Long , nickname : String ): RunnectUser {
119163 val user = RunnectUser .builder()
120164 .nickname(nickname)
0 commit comments