Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MemberJpaRepository 테스트 구현 #624

Merged
merged 8 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpStatus;

import codezap.global.exception.CodeZapException;
Expand All @@ -28,7 +29,7 @@ default Member fetchByTemplateId(Long templateId) {
}

@Query("SELECT t.member FROM Template t WHERE t.id = :templateId")
Optional<Member> findByTemplateId(Long templateId);
Optional<Member> findByTemplateId(@Param("templateId") Long templateId);
kyum-q marked this conversation as resolved.
Show resolved Hide resolved

Optional<Member> findByName(String name);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,129 @@
package codezap.member.repository;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;

import codezap.category.domain.Category;
import codezap.category.repository.CategoryJpaRepository;
import codezap.category.repository.CategoryRepository;
import codezap.fixture.CategoryFixture;
import codezap.fixture.MemberFixture;
import codezap.global.exception.CodeZapException;
import codezap.global.repository.JpaRepositoryTest;
import codezap.member.domain.Member;
import codezap.member.fixture.MemberFixture;
import codezap.template.domain.Template;
import codezap.template.repository.TemplateJpaRepository;
import codezap.template.repository.TemplateRepository;

@JpaRepositoryTest
class MemberJpaRepositoryTest {

private final MemberJpaRepository memberJpaRepository;
private final TemplateJpaRepository templateJpaRepository;
private final CategoryJpaRepository categoryJpaRepository;
@Autowired
private MemberRepository memberRepository;

@Autowired
private TemplateRepository templateRepository;

@Autowired
MemberJpaRepositoryTest(MemberJpaRepository memberJpaRepository, TemplateJpaRepository templateJpaRepository,
CategoryJpaRepository categoryJpaRepository
) {
this.memberJpaRepository = memberJpaRepository;
this.templateJpaRepository = templateJpaRepository;
this.categoryJpaRepository = categoryJpaRepository;
private CategoryRepository categoryRepository;

@Nested
@DisplayName("id로 Member 조회")
class fetchById {
@Test
@DisplayName("성공 : id로 Member를 알아낼 수 있다.")
zangsu marked this conversation as resolved.
Show resolved Hide resolved
void fetchByIdSuccess() {
Member member = memberRepository.save(MemberFixture.getFirstMember());

Member actual = memberRepository.fetchById(member.getId());

assertThat(actual).isEqualTo(member);
}

@Test
@DisplayName("실패 : 존재하지 않는 id인 경우 에러가 발생한다.")
void fetchByIdFailByNotExistsId() {
long notExistId = 100;

assertThatThrownBy(() -> memberRepository.fetchById(notExistId))
.isInstanceOf(CodeZapException.class)
.hasMessage("식별자 " + notExistId + "에 해당하는 멤버가 존재하지 않습니다.");
}
jminkkk marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
void fetchByTemplateId() {
// Then
Member member = ((JpaRepository<Member, Long>)memberJpaRepository).save(MemberFixture.memberFixture());
Category category = ((JpaRepository<Category, Long>)categoryJpaRepository).save(CategoryFixture.getFirstCategory());
Template template = new Template(member, "title", "description", category);
Template savedTemplate = ((JpaRepository<Template, Long>)templateJpaRepository).save(template);
@Nested
@DisplayName("닉네임(아이디)로 Member 조회")
class fetchByName {
@Test
@DisplayName("성공 : 닉네임으로 Member를 알아낼 수 있다.")
void fetchByNameSuccess() {
Member member = memberRepository.save(MemberFixture.getFirstMember());

Member actual = memberRepository.fetchByName(member.getName());

assertThat(actual).isEqualTo(member);
}

@Test
@DisplayName("실패 : 존재하지 않는 닉네임인 경우 에러가 발생한다.")
void fetchByNameFailByNotExistsId() {
String notExistName = "켬미";

assertThatThrownBy(() -> memberRepository.fetchByName(notExistName))
.isInstanceOf(CodeZapException.class)
.hasMessage("존재하지 않는 아이디 " + notExistName + " 입니다.");
}
}

@Nested
@DisplayName("템플릿 id로 Member 조회")
class fetchByTemplateId {
@Test
@DisplayName("성공 : 템플릿 id로 Member를 알아낼 수 있다.")
void fetchByTemplateIdSuccess() {
Member member = memberRepository.save(MemberFixture.getFirstMember());
Category category = categoryRepository.save(CategoryFixture.getFirstCategory());
Template template = new Template(member, "title", "description", category);
template = templateRepository.save(template);
jminkkk marked this conversation as resolved.
Show resolved Hide resolved

Member actual = memberRepository.fetchByTemplateId(template.getId());

assertThat(actual).isEqualTo(member);
}

@Test
@DisplayName("실패 : 존재하지 않는 템플릿 id인 경우 에러가 발생한다.")
void fetchByTemplateIdFailByNotExistsId() {
long id = 100;

assertThatThrownBy(() -> memberRepository.fetchByTemplateId(id))
.isInstanceOf(CodeZapException.class)
.hasMessage("템플릿에 대한 멤버가 존재하지 않습니다.");
zangsu marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Nested
@DisplayName("닉네임(아이디) 존재 여부")
class existsByName {
@Test
@DisplayName("성공 : 해당 닉네임이 존재하면 true를 반환한다.")
void existsByNameReturnTrue() {
Member member = memberRepository.save(MemberFixture.getFirstMember());

boolean actual = memberRepository.existsByName(member.getName());

assertThat(actual).isTrue();
zangsu marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
@DisplayName("성공 : 해당 닉네임이 존재하지 않으면 false를 반환한다.")
void existsByNameReturnFalse() {
boolean actual = memberRepository.existsByName("notExist");

assertThat(memberJpaRepository.fetchByTemplateId(savedTemplate.getId())).isEqualTo(member);
assertThat(actual).isFalse();
}
}
}