Skip to content

Commit

Permalink
feat(Record): 추가된 로직에 대한 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
limehee committed Jan 2, 2025
1 parent 2d969b6 commit 6ced878
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -36,7 +57,7 @@ void setUp() {
}

@Test
void 오늘_보행_훈련_횟수와_주간_보행_훈련_횟수와_연속_보행_훈련_일수로_RecordStatisticsResponseDto로_매핑된다() {
void 오늘_보행_훈련_횟수와_주간_보행_훈련_횟수와_연속_보행_훈련_일수가_RecordStatisticsResponseDto로_매핑된다() {
// given
int todayWalkTrainingCount = 3;
int weeklyWalkTrainingCount = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6ced878

Please sign in to comment.