-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #288 from Team-Smeme/sohyeon_#287
[TEST] 일기 저장 관련 테스트 코드 작성
- Loading branch information
Showing
5 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
smeem-api/src/test/java/com/smeem/diary/service/DiaryCommandServiceIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.smeem.diary.service; | ||
|
||
import com.smeem.api.diary.service.DiaryCommandService; | ||
import com.smeem.api.diary.service.dto.request.DiaryCreateServiceRequest; | ||
import com.smeem.domain.diary.repository.DiaryRepository; | ||
import com.smeem.domain.member.exception.MemberException; | ||
import com.smeem.domain.member.model.Member; | ||
import com.smeem.domain.member.repository.MemberRepository; | ||
import com.smeem.support.ServiceIntegrationTest; | ||
import com.smeem.support.fixture.MemberFixture; | ||
import lombok.val; | ||
import org.junit.jupiter.api.*; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static com.smeem.common.code.failure.MemberFailureCode.CANNOT_WRITE_DIARY; | ||
import static org.assertj.core.api.Assertions.*; | ||
|
||
public class DiaryCommandServiceIntegrationTest extends ServiceIntegrationTest { | ||
|
||
@Autowired | ||
private DiaryCommandService diaryService; | ||
|
||
@Autowired | ||
private MemberRepository memberRepository; | ||
|
||
@Autowired | ||
private DiaryRepository diaryRepository; | ||
|
||
private final String MEMBER_EXCEPTION_PREFIX_MESSAGE = "[MemberException] : "; | ||
|
||
@Nested | ||
@DisplayName("일기 저장 테스트") | ||
class SaveTest { | ||
|
||
private Member member; | ||
private final String diaryContent = "test-diary-content"; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
memberRepository.deleteAllInBatch(); | ||
diaryRepository.deleteAllInBatch(); | ||
member = memberRepository.save(MemberFixture.member().build()); | ||
} | ||
@Test | ||
@Transactional | ||
@DisplayName("[성공] 오늘 일기를 작성하지 않은 회원은 일기를 작성할 수 있고, 연속 일기 작성 수가 업데이트된다.") | ||
void saveWithMemberNotCreateDiaryToday() { | ||
// given | ||
val request = new DiaryCreateServiceRequest(member.getId(), diaryContent, null); | ||
|
||
// when | ||
val result = diaryService.createDiary(request); | ||
|
||
// then | ||
assertThat(member.getDiaryComboCount()).isEqualTo(1); | ||
|
||
val diary = diaryRepository.findFirstByMemberOrderByCreatedAtDesc(member); | ||
assertThat(diary).isPresent(); | ||
assertThat(diary.get().getId()).isEqualTo(result.diaryId()); | ||
assertThat(diary.get().getCreatedAt().toLocalDate()).isEqualTo(LocalDate.now()); | ||
assertThat(diary.get().getMember()).isEqualTo(member); | ||
} | ||
|
||
@Test | ||
@DisplayName("[예외] 오늘 일기를 작성한 회원은 일기를 작성할 수 없다.") | ||
void saveWithMemberCreateDiaryToday() { | ||
// given | ||
val request = new DiaryCreateServiceRequest(member.getId(), diaryContent, null); | ||
diaryService.createDiary(request); | ||
|
||
// when & then | ||
assertThatThrownBy(() -> diaryService.createDiary(request)) | ||
.isInstanceOf(MemberException.class) | ||
.hasMessage(MEMBER_EXCEPTION_PREFIX_MESSAGE + CANNOT_WRITE_DIARY.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
smeem-api/src/test/java/com/smeem/support/fixture/MemberFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.smeem.support.fixture; | ||
|
||
import com.smeem.domain.member.model.LangType; | ||
import com.smeem.domain.member.model.Member; | ||
import com.smeem.domain.member.model.SocialType; | ||
import lombok.NoArgsConstructor; | ||
|
||
import static com.smeem.domain.member.model.LangType.en; | ||
import static com.smeem.domain.member.model.SocialType.KAKAO; | ||
import static lombok.AccessLevel.PRIVATE; | ||
|
||
@NoArgsConstructor(access = PRIVATE) | ||
public class MemberFixture { | ||
|
||
private Long id; | ||
private SocialType social = KAKAO; | ||
private String socialId = "test-social-id"; | ||
private LangType targetLang = en; | ||
|
||
public static MemberFixture member() { | ||
return new MemberFixture(); | ||
} | ||
|
||
public MemberFixture id(Long id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public Member build() { | ||
return new Member(id, social, socialId, targetLang); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters