Skip to content

Commit

Permalink
fix: sinceDay 계산 로직 변경 (#349)
Browse files Browse the repository at this point in the history
* fix: sinceDay 계산 ChronoUnit.DAYS.between로 변경

* test: sinceDay 계산 테스트 코드 작성
  • Loading branch information
kdomo authored Feb 20, 2024
1 parent b663618 commit c49016c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.depromeet.domain.missionRecord.domain.MissionRecord;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public record FeedOneByProfileResponse(
@Schema(description = "미션 ID", defaultValue = "1") Long missionId,
Expand Down Expand Up @@ -42,7 +42,9 @@ public static FeedOneByProfileResponse of(MissionRecord record) {
record.getMission().getName(),
record.getImageUrl(),
record.getDuration().toMinutes(),
Duration.between(record.getMission().getStartedAt(), record.getStartedAt()).toDays()
ChronoUnit.DAYS.between(
record.getMission().getStartedAt().toLocalDate(),
record.getStartedAt().toLocalDate())
+ 1,
record.getStartedAt(),
record.getFinishedAt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public record FeedOneResponse(
@Schema(description = "작성자 ID", defaultValue = "1") Long memberId,
Expand Down Expand Up @@ -72,7 +73,7 @@ public FeedOneResponse(
remark,
recordImageUrl,
duration.toMinutes(),
Duration.between(startedAt, recordStartedAt).toDays() + 1,
ChronoUnit.DAYS.between(startedAt, recordStartedAt) + 1,
startedAt,
finishedAt,
recordStartedAt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,7 @@ public MissionRecordFindOneResponse findOneMissionRecord(Long recordId) {
missionRecordRepository
.findById(recordId)
.orElseThrow(() -> new CustomException(ErrorCode.MISSION_RECORD_NOT_FOUND));
long sinceDay =
Duration.between(
missionRecord.getMission().getStartedAt(),
missionRecord.getStartedAt())
.toDays()
+ DAYS_ADJUSTMENT;
return MissionRecordFindOneResponse.of(missionRecord, sinceDay);
return MissionRecordFindOneResponse.from(missionRecord);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public record MissionRecordFindOneResponse(
@Schema(description = "미션 기록 ID", defaultValue = "1") Long recordId,
Expand Down Expand Up @@ -33,13 +34,16 @@ public record MissionRecordFindOneResponse(
defaultValue = "2024-01-03 00:34:00",
type = "string")
LocalDateTime finishedAt) {
public static MissionRecordFindOneResponse of(MissionRecord missionRecord, long sinceDay) {
public static MissionRecordFindOneResponse from(MissionRecord missionRecord) {
return new MissionRecordFindOneResponse(
missionRecord.getId(),
missionRecord.getRemark(),
missionRecord.getImageUrl(),
missionRecord.getDuration().toMinutes(),
sinceDay,
ChronoUnit.DAYS.between(
missionRecord.getMission().getStartedAt().toLocalDate(),
missionRecord.getStartedAt().toLocalDate())
+ 1,
missionRecord.getStartedAt(),
missionRecord.getFinishedAt());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.depromeet.domain.missionRecord.application;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

import com.depromeet.DatabaseCleaner;
Expand All @@ -14,14 +15,13 @@
import com.depromeet.domain.missionRecord.dao.MissionRecordRepository;
import com.depromeet.domain.missionRecord.domain.MissionRecord;
import com.depromeet.domain.missionRecord.dto.request.MissionRecordCreateRequest;
import com.depromeet.domain.missionRecord.dto.response.MissionRecordFindOneResponse;
import com.depromeet.domain.missionRecord.dto.response.MissionStatisticsResponse;
import com.depromeet.global.error.exception.CustomException;
import com.depromeet.global.util.SecurityUtil;
import java.time.Duration;
import java.time.LocalDateTime;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand Down Expand Up @@ -78,7 +78,7 @@ void setUp() {
// then
Long missionId = mission.getId();

Assertions.assertThrows(
assertThrows(
CustomException.class, () -> missionRecordService.findOneMissionRecord(missionId));
}

Expand Down Expand Up @@ -121,12 +121,59 @@ void setUp() {
missionRecordService.findMissionStatistics(mission.getId());

// then
Assertions.assertEquals(missionStatistics.totalMissionHour(), 1);
Assertions.assertEquals(missionStatistics.totalMissionMinute(), 59);
Assertions.assertEquals(missionStatistics.totalSymbolStack(), 7);
Assertions.assertEquals(missionStatistics.continuousSuccessDay(), 4);
Assertions.assertEquals(missionStatistics.totalSuccessDay(), 7);
Assertions.assertEquals(missionStatistics.totalMissionAttainRate(), 46.7);
Assertions.assertEquals(missionStatistics.timeTable().size(), 7);
assertEquals(missionStatistics.totalMissionHour(), 1);
assertEquals(missionStatistics.totalMissionMinute(), 59);
assertEquals(missionStatistics.totalSymbolStack(), 7);
assertEquals(missionStatistics.continuousSuccessDay(), 4);
assertEquals(missionStatistics.totalSuccessDay(), 7);
assertEquals(missionStatistics.totalMissionAttainRate(), 46.7);
assertEquals(missionStatistics.timeTable().size(), 7);
}

@Nested
class 미션기록_단건_조회할_때 {
@Test
void 미션이_생성된_당일에_미션기록을_완료한_경우라면_sinceDay가_1이된다() {
// given
LocalDateTime missionRecordStartedAt = missionStartedAt;
LocalDateTime missionRecordFinishedAt =
missionRecordStartedAt.plusMinutes(32).plusSeconds(14);
Duration duration = Duration.ofMinutes(32).plusSeconds(14);
MissionRecord missionRecord =
MissionRecord.createMissionRecord(
duration, missionRecordStartedAt, missionRecordFinishedAt, mission);
missionRecord.updateUploadStatusPending();
missionRecord.updateUploadStatusComplete("remark", "imageUrl");
MissionRecord savedMissionRecord = missionRecordRepository.save(missionRecord);

// when
MissionRecordFindOneResponse queryMissionRecord =
missionRecordService.findOneMissionRecord(savedMissionRecord.getId());

// then
assertEquals(1, queryMissionRecord.sinceDay());
}

@Test
void 미션이_생성되고_일주일후에_미션기록을_완료한_경우라면_sinceDay가_8이된다() {
// given
LocalDateTime missionRecordStartedAt = missionStartedAt.plusDays(7);
LocalDateTime missionRecordFinishedAt =
missionRecordStartedAt.plusMinutes(32).plusSeconds(14);
Duration duration = Duration.ofMinutes(32).plusSeconds(14);
MissionRecord missionRecord =
MissionRecord.createMissionRecord(
duration, missionRecordStartedAt, missionRecordFinishedAt, mission);
missionRecord.updateUploadStatusPending();
missionRecord.updateUploadStatusComplete("remark", "imageUrl");
MissionRecord savedMissionRecord = missionRecordRepository.save(missionRecord);

// when
MissionRecordFindOneResponse queryMissionRecord =
missionRecordService.findOneMissionRecord(savedMissionRecord.getId());

// then
assertEquals(8, queryMissionRecord.sinceDay());
}
}
}

0 comments on commit c49016c

Please sign in to comment.