From 6ced878ef7a75b5805e8bef11f3386dd21a7316e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EA=B4=80=ED=9D=AC?= Date: Thu, 2 Jan 2025 17:41:15 +0900 Subject: [PATCH] =?UTF-8?q?feat(Record):=20=EC=B6=94=EA=B0=80=EB=90=9C=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=EC=97=90=20=EB=8C=80=ED=95=9C=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stempo/mapper/RecordDtoMapperTest.java | 25 +++++++++- .../stempo/service/RecordServiceImplTest.java | 48 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/stempo-application/src/test/java/com/stempo/mapper/RecordDtoMapperTest.java b/stempo-application/src/test/java/com/stempo/mapper/RecordDtoMapperTest.java index 187c97f1..9d95524a 100644 --- a/stempo-application/src/test/java/com/stempo/mapper/RecordDtoMapperTest.java +++ b/stempo-application/src/test/java/com/stempo/mapper/RecordDtoMapperTest.java @@ -3,8 +3,10 @@ import static org.assertj.core.api.Assertions.assertThat; import com.stempo.dto.response.RecordItemDto; +import com.stempo.dto.response.RecordResponseDto; import com.stempo.dto.response.RecordStatisticsResponseDto; import java.time.LocalDate; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -18,7 +20,26 @@ void setUp() { } @Test - void accuracy와_duration과_steps와_date로_RecordResponseDto로_매핑된다() { + void accuracyAverage와_records가_RecordResponseDto로_매핑된다() { + // given + int accuracyAverage = 95; + RecordItemDto recordItemDto = RecordItemDto.builder() + .accuracy(95.5) + .duration(1200) + .steps(1500) + .date(LocalDate.of(2023, 10, 24)) + .build(); + + // when + RecordResponseDto responseDto = recordDtoMapper.toDto(accuracyAverage, List.of(recordItemDto)); + + // then + assertThat(responseDto.getAccuracyAverage()).isEqualTo(accuracyAverage); + assertThat(responseDto.getRecords()).containsExactly(recordItemDto); + } + + @Test + void accuracy_duration_steps_date가_RecordItemDto로_매핑된다() { // given Double accuracy = 95.5; Integer duration = 1200; @@ -36,7 +57,7 @@ void setUp() { } @Test - void 오늘_보행_훈련_횟수와_주간_보행_훈련_횟수와_연속_보행_훈련_일수로_RecordStatisticsResponseDto로_매핑된다() { + void 오늘_보행_훈련_횟수와_주간_보행_훈련_횟수와_연속_보행_훈련_일수가_RecordStatisticsResponseDto로_매핑된다() { // given int todayWalkTrainingCount = 3; int weeklyWalkTrainingCount = 10; diff --git a/stempo-application/src/test/java/com/stempo/service/RecordServiceImplTest.java b/stempo-application/src/test/java/com/stempo/service/RecordServiceImplTest.java index 45489ab6..066c99ac 100644 --- a/stempo-application/src/test/java/com/stempo/service/RecordServiceImplTest.java +++ b/stempo-application/src/test/java/com/stempo/service/RecordServiceImplTest.java @@ -5,6 +5,7 @@ import static org.mockito.ArgumentMatchers.anyDouble; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -154,6 +155,53 @@ record = Record.builder() verify(mapper, times(1)).toDto(anyInt(), any(List.class)); } + @Test + void startDate_이전의_최신_데이터가_없는_경우_startDate_이전_날짜로_0으로_초기화된_값을_생성한다() { + // given + LocalDate startDate = LocalDate.of(2024, 10, 21); + LocalDate endDate = LocalDate.of(2024, 10, 27); + LocalDateTime startDateTime = startDate.atStartOfDay(); + LocalDateTime endDateTime = endDate.plusDays(1).atStartOfDay(); + + when(userService.getCurrentDeviceTag()).thenReturn(deviceTag); + when(recordRepository.findLatestBeforeStartDate(deviceTag, startDateTime)) + .thenReturn(Optional.empty()); + when(recordRepository.findByDateBetween(deviceTag, startDateTime, endDateTime)) + .thenReturn(List.of()); + when(mapper.toDto(anyDouble(), anyInt(), anyInt(), eq(startDate.minusDays(1)))) + .thenReturn(RecordItemDto.builder() + .accuracy(0.0) + .duration(0) + .steps(0) + .build()); + when(mapper.toDto(anyInt(), any(List.class))) + .thenReturn(RecordResponseDto.builder() + .accuracyAverage(0) + .records(List.of( + RecordItemDto.builder() + .accuracy(0.0) + .duration(0) + .steps(0) + .build() + )) + .build()); + + // when + RecordResponseDto result = recordService.getRecordsByDateRange(startDate, endDate); + + // then + assertThat(result.getAccuracyAverage()).isZero(); + assertThat(result.getRecords()).hasSize(1); + assertThat(result.getRecords().getFirst().getAccuracy()).isEqualTo(0.0); + assertThat(result.getRecords().getFirst().getDuration()).isZero(); + assertThat(result.getRecords().getFirst().getSteps()).isZero(); + verify(userService).getCurrentDeviceTag(); + verify(recordRepository).findLatestBeforeStartDate(deviceTag, startDateTime); + verify(recordRepository).findByDateBetween(deviceTag, startDateTime, endDateTime); + verify(mapper).toDto(anyDouble(), anyInt(), anyInt(), eq(startDate.minusDays(1))); + verify(mapper).toDto(anyInt(), any(List.class)); + } + @Test void 통계정보를_조회한다() { // given