Skip to content

Commit 9b23842

Browse files
committed
test: 위경도/카테고리에 따른 스타카토 목록 조회 repository 테스트 보충
1 parent 2d0d830 commit 9b23842

File tree

3 files changed

+95
-77
lines changed

3 files changed

+95
-77
lines changed

backend/src/main/java/com/staccato/staccato/repository/StaccatoRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public interface StaccatoRepository extends JpaRepository<Staccato, Long> {
1919
JOIN FETCH s.category c
2020
JOIN FETCH c.categoryMembers cm
2121
WHERE cm.member = :member
22+
AND c.id = :categoryId
2223
AND (
2324
(:minLat IS NULL OR :maxLat IS NULL OR :minLng IS NULL OR :maxLng IS NULL)
2425
OR (s.spot.latitude BETWEEN :minLat AND :maxLat AND s.spot.longitude BETWEEN :minLng AND :maxLng)
2526
)
26-
AND (:categoryId IS NULL OR c.id = :categoryId)
2727
""")
2828
List<Staccato> findByMemberAndLocationRangeAndCategory(
2929
@Param("member") Member member,

backend/src/test/java/com/staccato/staccato/controller/StaccatoControllerV2Test.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,4 @@ void failReadAllStaccatoWithInvalidSingleLatLng(String neLat, String neLng, Stri
104104
.andExpect(status().isBadRequest())
105105
.andExpect(content().json(objectMapper.writeValueAsString(exceptionResponse)));
106106
}
107-
108-
@DisplayName("유효하지 않은 카테고리 식별자로 스타카토 목록 조회 시 예외가 발생한다.")
109-
@Test
110-
void failReadAllStaccatoWithInvalidCategoryId() throws Exception {
111-
// given
112-
ExceptionResponse exceptionResponse = new ExceptionResponse(
113-
HttpStatus.BAD_REQUEST.toString(),
114-
"카테고리 식별자는 양수로 이루어져야 합니다.");
115-
116-
// when & then
117-
mockMvc.perform(get("/v2/staccatos")
118-
.param("categoryId", "0")
119-
.param("neLat", "37.5")
120-
.param("neLng", "127.0")
121-
.param("swLat", "37.0")
122-
.param("swLng", "126.8")
123-
.header(HttpHeaders.AUTHORIZATION, "token"))
124-
.andExpect(status().isBadRequest())
125-
.andExpect(content().json(objectMapper.writeValueAsString(exceptionResponse)));
126-
}
127107
}

backend/src/test/java/com/staccato/staccato/repository/StaccatoRepositoryTest.java

Lines changed: 94 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import java.util.List;
66
import jakarta.persistence.EntityManager;
77
import jakarta.persistence.PersistenceContext;
8-
import org.hibernate.boot.model.source.internal.hbm.XmlElementMetadata;
9-
import org.junit.jupiter.api.BeforeAll;
108
import org.junit.jupiter.api.BeforeEach;
119
import org.junit.jupiter.api.DisplayName;
1210
import org.junit.jupiter.api.Nested;
@@ -44,7 +42,7 @@ class StaccatoRepositoryTest extends RepositoryTest {
4442
class FindAllStaccatosBy {
4543
private static final BigDecimal MIN_LATITUDE = new BigDecimal("37.4");
4644
private static final BigDecimal MAX_LATITUDE = new BigDecimal("37.5");
47-
private static final BigDecimal MIN_LONGITUDE= new BigDecimal("127.1");
45+
private static final BigDecimal MIN_LONGITUDE = new BigDecimal("127.1");
4846
private static final BigDecimal MAX_LONGITUDE = new BigDecimal("127.2");
4947

5048
Member member;
@@ -72,67 +70,107 @@ void init() {
7270
.buildAndSave(staccatoRepository);
7371
}
7472

75-
@DisplayName("아무 조건 없음 (staccatoInCategory1, staccatoInCategory2)")
76-
@Test
77-
void findAllStaccatoByMemberWithoutAnyCondition() {
78-
// given
79-
Member anotherMember = MemberFixtures.defaultMember().buildAndSave(memberRepository);
80-
Category anotherCategory = CategoryFixtures.defaultCategory()
81-
.buildAndSaveWithMember(anotherMember, categoryRepository);
82-
Staccato anotherStaccato = StaccatoFixtures.defaultStaccato()
83-
.withCategory(anotherCategory)
84-
.buildAndSave(staccatoRepository);
73+
@Nested
74+
@DisplayName("findByMemberAndLocationRange()")
75+
class FindByMemberAndLocationRange {
76+
@DisplayName("아무 조건 없음 (staccatoInCategory1, staccatoInCategory2)")
77+
@Test
78+
void findAllStaccatoByMemberWithoutAnyCondition() {
79+
// given
80+
Member anotherMember = MemberFixtures.defaultMember().buildAndSave(memberRepository);
81+
Category anotherCategory = CategoryFixtures.defaultCategory()
82+
.buildAndSaveWithMember(anotherMember, categoryRepository);
83+
Staccato anotherStaccato = StaccatoFixtures.defaultStaccato()
84+
.withCategory(anotherCategory)
85+
.buildAndSave(staccatoRepository);
8586

86-
// when
87-
List<Staccato> result = staccatoRepository.findByMemberAndLocationRangeAndCategory(member, null, null, null, null, null);
87+
// when
88+
List<Staccato> result = staccatoRepository.findByMemberAndLocationRange(member, null, null, null, null);
8889

89-
// then
90-
assertThat(result).hasSize(2).containsExactlyInAnyOrder(staccatoInCategory1, staccatoInCategory2)
91-
.doesNotContain(anotherStaccato);
92-
}
90+
// then
91+
assertThat(result).hasSize(2).containsExactlyInAnyOrder(staccatoInCategory1, staccatoInCategory2)
92+
.doesNotContain(anotherStaccato);
93+
}
9394

94-
@DisplayName("위경도 범위 내 (staccatoInCategory1, staccatoInCategory2)")
95-
@Test
96-
void findAllStaccatoByMemberWithLocationRange() {
97-
// given
98-
BigDecimal threshold = new BigDecimal("0.01");
99-
Staccato underMinLatitude = StaccatoFixtures.defaultStaccato()
100-
.withCategory(category1ByMember)
101-
.withSpot(MIN_LATITUDE.subtract(threshold), MAX_LONGITUDE)
102-
.buildAndSave(staccatoRepository);
103-
Staccato overMaxLatitude = StaccatoFixtures.defaultStaccato()
104-
.withCategory(category1ByMember)
105-
.withSpot(MAX_LATITUDE.add(threshold), MIN_LONGITUDE)
106-
.buildAndSave(staccatoRepository);
107-
Staccato underMinLongitude = StaccatoFixtures.defaultStaccato()
108-
.withCategory(category1ByMember)
109-
.withSpot(MAX_LATITUDE, MIN_LONGITUDE.subtract(threshold))
110-
.buildAndSave(staccatoRepository);
111-
Staccato overMaxLongitude = StaccatoFixtures.defaultStaccato()
112-
.withCategory(category1ByMember)
113-
.withSpot(MIN_LATITUDE, MAX_LONGITUDE.add(threshold))
114-
.buildAndSave(staccatoRepository);
95+
@DisplayName("위경도 범위 내 (staccatoInCategory1, staccatoInCategory2)")
96+
@Test
97+
void findAllStaccatoByMemberWithLocationRange() {
98+
// given
99+
BigDecimal threshold = new BigDecimal("0.01");
100+
Staccato underMinLatitude = StaccatoFixtures.defaultStaccato()
101+
.withCategory(category1ByMember)
102+
.withSpot(MIN_LATITUDE.subtract(threshold), MAX_LONGITUDE)
103+
.buildAndSave(staccatoRepository);
104+
Staccato overMaxLatitude = StaccatoFixtures.defaultStaccato()
105+
.withCategory(category1ByMember)
106+
.withSpot(MAX_LATITUDE.add(threshold), MIN_LONGITUDE)
107+
.buildAndSave(staccatoRepository);
108+
Staccato underMinLongitude = StaccatoFixtures.defaultStaccato()
109+
.withCategory(category1ByMember)
110+
.withSpot(MAX_LATITUDE, MIN_LONGITUDE.subtract(threshold))
111+
.buildAndSave(staccatoRepository);
112+
Staccato overMaxLongitude = StaccatoFixtures.defaultStaccato()
113+
.withCategory(category1ByMember)
114+
.withSpot(MIN_LATITUDE, MAX_LONGITUDE.add(threshold))
115+
.buildAndSave(staccatoRepository);
115116

116-
// when
117-
List<Staccato> result = staccatoRepository.findByMemberAndLocationRangeAndCategory(
118-
member, MIN_LATITUDE, MAX_LATITUDE, MIN_LONGITUDE, MAX_LONGITUDE, null
119-
);
117+
// when
118+
List<Staccato> result = staccatoRepository.findByMemberAndLocationRange(
119+
member, MIN_LATITUDE, MAX_LATITUDE, MIN_LONGITUDE, MAX_LONGITUDE
120+
);
120121

121-
// then
122-
assertThat(result).hasSize(2).containsExactlyInAnyOrder(staccatoInCategory1, staccatoInCategory2)
123-
.doesNotContain(underMinLatitude, overMaxLatitude, underMinLongitude, overMaxLongitude);
122+
// then
123+
assertThat(result).hasSize(2).containsExactlyInAnyOrder(staccatoInCategory1, staccatoInCategory2)
124+
.doesNotContain(underMinLatitude, overMaxLatitude, underMinLongitude, overMaxLongitude);
125+
}
124126
}
125127

126-
@DisplayName("특정 카테고리(category1ByMember)에 속한 모든 스타카토 (staccatoInCategory1)")
127-
@Test
128-
void findAllStaccatoByMemberWithCategory() {
129-
// when
130-
List<Staccato> result = staccatoRepository.findByMemberAndLocationRangeAndCategory(member, null, null, null, null, category1ByMember.getId());
128+
@Nested
129+
@DisplayName("findByMemberAndLocationRangeAndCategory()")
130+
class FindByMemberAndLocationRangeAndCategory {
131+
@DisplayName("특정 카테고리(category1ByMember)에 속한 모든 스타카토 (staccatoInCategory1)")
132+
@Test
133+
void findAllStaccatoByMemberWithCategory() {
134+
// when
135+
List<Staccato> result = staccatoRepository.findByMemberAndLocationRangeAndCategory(member, null, null, null, null, category1ByMember.getId());
136+
137+
// then
138+
assertThat(result).hasSize(1)
139+
.containsExactlyInAnyOrder(staccatoInCategory1)
140+
.doesNotContain(staccatoInCategory2);
141+
}
142+
143+
@DisplayName("특정 카테고리(category1ByMember)에 속하면서 위경도 범위 내 모든 스타카토 (staccatoInCategory1)")
144+
@Test
145+
void findAllStaccatoByMemberWithCategoryAndInRange() {
146+
// given
147+
BigDecimal threshold = new BigDecimal("0.01");
148+
Staccato underMinLatitude = StaccatoFixtures.defaultStaccato()
149+
.withCategory(category1ByMember)
150+
.withSpot(MIN_LATITUDE.subtract(threshold), MAX_LONGITUDE)
151+
.buildAndSave(staccatoRepository);
152+
Staccato overMaxLatitude = StaccatoFixtures.defaultStaccato()
153+
.withCategory(category1ByMember)
154+
.withSpot(MAX_LATITUDE.add(threshold), MIN_LONGITUDE)
155+
.buildAndSave(staccatoRepository);
156+
Staccato underMinLongitude = StaccatoFixtures.defaultStaccato()
157+
.withCategory(category1ByMember)
158+
.withSpot(MAX_LATITUDE, MIN_LONGITUDE.subtract(threshold))
159+
.buildAndSave(staccatoRepository);
160+
Staccato overMaxLongitude = StaccatoFixtures.defaultStaccato()
161+
.withCategory(category1ByMember)
162+
.withSpot(MIN_LATITUDE, MAX_LONGITUDE.add(threshold))
163+
.buildAndSave(staccatoRepository);
164+
165+
// when
166+
List<Staccato> result = staccatoRepository.findByMemberAndLocationRangeAndCategory(
167+
member, MIN_LATITUDE, MAX_LATITUDE, MIN_LONGITUDE, MAX_LONGITUDE, category1ByMember.getId());
131168

132-
// then
133-
assertThat(result).hasSize(1)
134-
.containsExactlyInAnyOrder(staccatoInCategory1)
135-
.doesNotContain(staccatoInCategory2);
169+
// then
170+
assertThat(result).hasSize(1)
171+
.containsExactlyInAnyOrder(staccatoInCategory1)
172+
.doesNotContain(staccatoInCategory2, underMinLatitude, overMaxLatitude, underMinLongitude, overMaxLongitude);
173+
}
136174
}
137175
}
138176

0 commit comments

Comments
 (0)