Skip to content

Commit

Permalink
Merge pull request #144 from kakao-tech-campus-2nd-step3/feat/#87-lik…
Browse files Browse the repository at this point in the history
…ed-place

[refactor] #87 LikedPlaceRepoTest, PlaceRepoTest에 인메모리 db를 사용하도록 했어요
  • Loading branch information
sanghee0820 authored Nov 11, 2024
2 parents d559e3e + 479d740 commit 9890e91
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 349 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package team7.inplace.config.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;

@Target(ElementType.TYPE) // 클래스에 붙일 수 있도록 설정
@Retention(RetentionPolicy.RUNTIME) // 런타임에 유지
@SpringBootTest // SpringBootTest를 포함
@TestPropertySource(properties = {
"spring.datasource.url=jdbc:h2:mem:testdb",
"spring.datasource.driver-class-name=org.h2.Driver",
"spring.datasource.username=sa",
"spring.datasource.password=",
"spring.jpa.hibernate.ddl-auto=create-drop",
"spring.sql.init.mode=never"
})
public @interface CustomRepositoryTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import team7.inplace.config.annotation.CustomRepositoryTest;
import team7.inplace.influencer.domain.Influencer;
import team7.inplace.influencer.persistence.InfluencerRepository;

@SpringBootTest
@CustomRepositoryTest
public class InfluencerRepositoryTest {

@Autowired
Expand All @@ -25,8 +25,8 @@ public void findAllTest() {

List<Influencer> savedInfluencers = influencerRepository.findAll();

assertThat(savedInfluencers.get(3)).usingRecursiveComparison().isEqualTo(influencer4);
assertThat(savedInfluencers.get(4)).usingRecursiveComparison().isEqualTo(influencer5);
assertThat(savedInfluencers.get(0)).usingRecursiveComparison().isEqualTo(influencer4);
assertThat(savedInfluencers.get(1)).usingRecursiveComparison().isEqualTo(influencer5);
}

}
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
package team7.inplace.likedPlace.persistence;


import static org.assertj.core.api.Assertions.assertThat;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import team7.inplace.config.annotation.CustomRepositoryTest;
import team7.inplace.global.exception.InplaceException;
import team7.inplace.global.exception.code.LikedPlaceErrorCode;
import team7.inplace.likedPlace.domain.LikedPlace;
import team7.inplace.place.domain.Place;
import team7.inplace.user.domain.Role;
import team7.inplace.user.domain.User;
import team7.inplace.user.domain.UserType;

@SpringBootTest
@CustomRepositoryTest
@Transactional
class LikedPlaceRepositoryTest {

@PersistenceContext
EntityManager entityManager;
private EntityManager entityManager;

@Autowired
private LikedPlaceRepository likedPlaceRepository;

Place place1;

User user1;
private User user1;
private Place place1;

@BeforeEach
public void init() throws NoSuchFieldException, IllegalAccessException {
likedPlaceRepository.deleteAll();
public void init() {
// 유저 생성 및 persist
user1 = new User("user1", "pass1", "nick1", UserType.KAKAO, Role.USER);
entityManager.persist(user1);

// 장소 생성 및 persist
place1 = new Place("Place 1",
"\"wifi\": true, \"pet\": false, \"parking\": false, \"forDisabled\": true, \"nursery\": false, \"smokingRoom\": false}",
"menuImg.url", "카페",
Expand All @@ -54,44 +58,52 @@ public void init() throws NoSuchFieldException, IllegalAccessException {
);
entityManager.persist(place1);

user1 = new User("user1", "pass1", "nick1", UserType.KAKAO, Role.USER);
entityManager.persist(user1);
// EntityManager flush 및 clear
entityManager.flush();
entityManager.clear();
}

@Test
@DisplayName("FindByUserIdAndPlaceId 테스트")
public void test1() {
//given
LikedPlace expected = new LikedPlace(user1, place1);
// given
LikedPlace likedPlace = new LikedPlace(user1, place1);
entityManager.persist(likedPlace);

//when
LikedPlace actual = likedPlaceRepository.findByUserIdAndPlaceId(user1.getId(),
place1.getId())
.orElseGet(() -> new LikedPlace(user1, place1));
// when
Optional<LikedPlace> actual = likedPlaceRepository.findByUserIdAndPlaceId(
user1.getId(), place1.getId()
);

//then
assertThat(actual.getUser().getUsername()).isEqualTo(expected.getUser().getUsername());
assertThat(actual.getPlace().getName()).isEqualTo(expected.getPlace().getName());
// then
assertThat(actual).isPresent();
assertThat(actual.get().getUser().getUsername()).isEqualTo(user1.getUsername());
assertThat(actual.get().getPlace().getName()).isEqualTo(place1.getName());
}

@Test
@DisplayName("좋아요 및 상태 업데이트 테스트")
public void test2() {
// given
LikedPlace likedPlace = likedPlaceRepository.findByUserIdAndPlaceId(user1.getId(),
place1.getId())
.orElseGet(() -> new LikedPlace(user1, place1));
LikedPlace likedPlace = new LikedPlace(user1, place1);
entityManager.persist(likedPlace);

// 처음에는 좋아요 상태가 false임을 가정
// 초기 좋아요 상태는 false로 설정
assertThat(likedPlace.isLiked()).isFalse();

// when: 좋아요 상태를 true로 업데이트
likedPlace.updateLike(true);
likedPlaceRepository.save(likedPlace);

// then: 업데이트 후 좋아요 상태가 true인지 확인
LikedPlace updatedLikedPlace = likedPlaceRepository.findByUserIdAndPlaceId(user1.getId(),
place1.getId()).get();
// EntityManager flush 및 clear 후 다시 조회
entityManager.flush();
entityManager.clear();

// then
LikedPlace updatedLikedPlace = likedPlaceRepository.findByUserIdAndPlaceId(
user1.getId(), place1.getId()
).orElseThrow(() -> InplaceException.of(LikedPlaceErrorCode.NOT_FOUND));

assertThat(updatedLikedPlace.isLiked()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import team7.inplace.config.annotation.CustomRepositoryTest;
import team7.inplace.influencer.domain.Influencer;
import team7.inplace.place.domain.Category;
import team7.inplace.place.domain.Place;
import team7.inplace.video.domain.Video;

@SpringBootTest

@CustomRepositoryTest
@Transactional
class PlaceRepositoryTest {

Expand Down
Loading

0 comments on commit 9890e91

Please sign in to comment.