Skip to content

Commit 141a438

Browse files
authored
Merge pull request #61 from codeit-part3-team2/api/feature/interest
[feat] 관심사 테스트 코드 작성
2 parents 8b681b6 + 65ed736 commit 141a438

5 files changed

Lines changed: 508 additions & 5 deletions

File tree

monew-api/src/main/java/com/monew/monew_api/interest/service/InterestServiceImpl.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ public CursorPageResponseInterestDto getInterests(Long userId,
106106

107107
Slice<Interest> slices = interestRepository.findAll(
108108
keyword, orderBy, direction, cursor, after, limit);
109-
log.info("REQ userId={}, keyword={}, orderBy={}, direction={}, cursor={}, after={}, limit={}",
110-
userId, keyword, orderBy, direction, cursor, after, limit);
111109

112110
List<Interest> interests = slices.getContent();
113111

@@ -126,9 +124,6 @@ public CursorPageResponseInterestDto getInterests(Long userId,
126124
boolean subscribedByMe = subscribedIds.contains(interest.getId());
127125
InterestDto dto = interestMapper.toInterestDto(interest, keywords, subscribedByMe,
128126
subscriberCount);
129-
130-
log.info("DBG dto id={}, name={}, subscriberCount={} subscribedByMe={}",
131-
dto.id(), dto.name(), dto.subscriberCount(), dto.subscribedByMe());
132127
interestDtos.add(dto);
133128
}
134129

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.monew.monew_api.interest;
2+
3+
import com.monew.monew_api.interest.entity.Interest;
4+
import com.monew.monew_api.interest.entity.Keyword;
5+
import java.util.List;
6+
import java.util.concurrent.atomic.AtomicLong;
7+
import org.springframework.test.util.ReflectionTestUtils;
8+
9+
public class TestInterestForm {
10+
11+
// interestId Long 생성기
12+
private static Long generatedId(){
13+
return new AtomicLong(1).getAndIncrement();
14+
}
15+
16+
public static Interest create(String name, List<String> keywords) {
17+
Interest interest = Interest.create(name);
18+
19+
for (String keyword : keywords) {
20+
interest.addKeyword(new Keyword(keyword));
21+
}
22+
ReflectionTestUtils.setField(interest, "id", generatedId());
23+
return interest;
24+
}
25+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.monew.monew_api.interest.repository;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.monew.monew_api.common.config.QuerydslConfig;
6+
import com.monew.monew_api.interest.dto.InterestOrderBy;
7+
import com.monew.monew_api.interest.entity.Interest;
8+
import com.monew.monew_api.interest.entity.Keyword;
9+
import com.querydsl.core.types.Order;
10+
import java.time.LocalDateTime;
11+
import java.util.Comparator;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Test;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.beans.factory.annotation.Qualifier;
17+
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
18+
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
19+
import org.springframework.context.annotation.Import;
20+
import org.springframework.data.domain.Slice;
21+
import org.springframework.test.context.ActiveProfiles;
22+
23+
24+
@DataJpaTest
25+
@ActiveProfiles("test")
26+
@Import(QuerydslConfig.class)
27+
public class InterestRepositoryCustomTest {
28+
29+
@Qualifier("interestRepositoryCustomImpl")
30+
@Autowired
31+
InterestRepositoryCustom interestRepositoryCustom;
32+
33+
@Autowired
34+
TestEntityManager em;
35+
36+
@BeforeEach
37+
void setUp() {
38+
Interest i1 = Interest.create("interest1");
39+
Interest i2 = Interest.create("interest2");
40+
Interest i3 = Interest.create("interest3");
41+
42+
// i1: 3명 i2: 2명 i3: 1명 구독
43+
i1.addSubscriberCount();
44+
i1.addSubscriberCount();
45+
i1.addSubscriberCount();
46+
i2.addSubscriberCount();
47+
i2.addSubscriberCount();
48+
i3.addSubscriberCount();
49+
50+
em.persist(i1);
51+
em.persist(i2);
52+
em.persist(i3);
53+
54+
Keyword k1 = new Keyword("keyword1");
55+
Keyword k2 = new Keyword("keyword2");
56+
Keyword k3 = new Keyword("keyword3");
57+
Keyword k4 = new Keyword("keyword4");
58+
59+
// i1: k1,k2 i2: k2,k3 i3: k4
60+
i1.addKeyword(k1);
61+
i1.addKeyword(k2);
62+
i2.addKeyword(k2);
63+
i2.addKeyword(k3);
64+
i3.addKeyword(k4);
65+
66+
em.persist(k1);
67+
em.persist(k2);
68+
em.persist(k3);
69+
em.persist(k4);
70+
71+
em.flush();
72+
em.clear();
73+
}
74+
75+
@Test
76+
@DisplayName("관심사 전체 조회 - name ASC")
77+
void testFindAllNameASC() {
78+
String keyword = null;
79+
InterestOrderBy orderBy = InterestOrderBy.name;
80+
Order direction = Order.ASC;
81+
String cursor = null;
82+
LocalDateTime after = null;
83+
int limit = 2;
84+
85+
Slice<Interest> result = interestRepositoryCustom.findAll(
86+
keyword, orderBy, direction, cursor, after, limit
87+
);
88+
89+
assertThat(result).hasSize(2);
90+
assertThat(result.hasNext()).isTrue();
91+
assertThat(result.getContent())
92+
.isSortedAccordingTo(Comparator.comparing(Interest::getName));
93+
94+
}
95+
96+
@Test
97+
@DisplayName("검색어로 관심사 조회 - subscriberCount DESC")
98+
void testFindAllSubscriberCountDESC() {
99+
String keyword = "interest1";
100+
InterestOrderBy orderBy = InterestOrderBy.subscriberCount;
101+
Order direction = Order.DESC;
102+
int limit = 6;
103+
104+
Slice<Interest> result = interestRepositoryCustom.findAll(
105+
keyword, orderBy, direction, null, null, limit
106+
);
107+
108+
assertThat(result).isNotEmpty();
109+
assertThat(result.getContent())
110+
.allMatch(i -> i.getName().contains("interest1"));
111+
}
112+
113+
@Test
114+
@DisplayName("커서 조회 확인 - subscriberCount ASC")
115+
void testFindAllSubscriberCountASCWithCursor() {
116+
Slice<Interest> firstSlice = interestRepositoryCustom.findAll(
117+
null, InterestOrderBy.subscriberCount, Order.ASC, null, null, 2
118+
);
119+
assertThat(firstSlice).hasSize(2);
120+
assertThat(firstSlice.hasNext()).isTrue();
121+
122+
Interest last = firstSlice.getContent().get(1);
123+
String nextCursor = String.valueOf(last.getSubscriberCount());
124+
LocalDateTime after = last.getCreatedAt();
125+
126+
Slice<Interest> secondSlice = interestRepositoryCustom.findAll(
127+
null, InterestOrderBy.subscriberCount, Order.DESC, nextCursor, after, 2
128+
);
129+
assertThat(secondSlice).isNotEmpty();
130+
}
131+
132+
@Test
133+
@DisplayName("관심사 전체 카운트")
134+
void testFindAllSubscriberCount() {
135+
long count = interestRepositoryCustom.countFilteredTotalElements(null);
136+
assertThat(count).isEqualTo(3);
137+
}
138+
139+
@Test
140+
@DisplayName("검색어로 관심사 카운트")
141+
void testCountFilteredTotalElementsWithKeyword() {
142+
long count = interestRepositoryCustom.countFilteredTotalElements("keyword1");
143+
assertThat(count).isEqualTo(1);
144+
}
145+
146+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.monew.monew_api.interest.repository;
2+
3+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
4+
5+
import com.monew.monew_api.common.config.QuerydslConfig;
6+
import com.monew.monew_api.interest.entity.Interest;
7+
import com.monew.monew_api.interest.entity.Keyword;
8+
import jakarta.persistence.EntityManager;
9+
import java.util.List;
10+
import org.junit.jupiter.api.DisplayName;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
14+
import org.springframework.context.annotation.Import;
15+
import org.springframework.test.context.ActiveProfiles;
16+
17+
@DataJpaTest
18+
@ActiveProfiles("test")
19+
@Import(QuerydslConfig.class)
20+
public class KeywordRepositoryTest {
21+
22+
@Autowired
23+
KeywordRepository keywordRepository;
24+
25+
@Autowired
26+
InterestRepository interestRepository;
27+
28+
@Autowired
29+
EntityManager em;
30+
31+
@DisplayName("관심사 안에 포함되지 않는 키워드 조회")
32+
@Test
33+
public void findOrphanKeywordsIn() {
34+
Keyword keyword1 = keywordRepository.save(new Keyword("keyword1"));
35+
Keyword keyword2 = keywordRepository.save(new Keyword("keyword2"));
36+
Keyword keyword3 = keywordRepository.save(new Keyword("keyword3")); // 고아 키워드
37+
38+
Interest interest = Interest.create("interest1");
39+
interest.addKeyword(keyword1);
40+
interest.addKeyword(keyword2);
41+
interestRepository.saveAndFlush(interest);
42+
43+
em.flush();
44+
em.clear();
45+
46+
List<Keyword> orphanKeywords = keywordRepository.findOrphanKeywordsIn(
47+
List.of(keyword1, keyword2, keyword3));
48+
49+
assertThat(orphanKeywords).hasSize(1);
50+
assertThat(orphanKeywords.get(0).getKeyword()).isEqualTo("keyword3");
51+
}
52+
}

0 commit comments

Comments
 (0)