Skip to content

Commit 0f8e637

Browse files
authored
커뮤니티 조회 함수 키워드 필터, 정렬 unit test 코드 작성 (#510)
1 parent 7250ae0 commit 0f8e637

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

backend/community/tests.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,96 @@ def test_get_post_list_with_post_type(self):
307307
self.assertSuccess(response)
308308
self.assertEqual(response.data["data"]["total"], 1)
309309

310+
def test_get_post_list_with_keyword(self):
311+
"""키워드로 게시글을 필터링할 수 있다."""
312+
# 테스트 데이터 준비
313+
self.client.force_login(self.user)
314+
self.client.post(self.post_list_url, {"title":"HI Annyeong", "content" : "What Im saying is HIIIIIIII","post_type" : "ARTICLE" })
315+
self.client.post(self.post_list_url, {"title":"HI Annyeong", "content" : "What Im saying is HIIIIIIII","post_type" : "QUESTION" })
316+
self.client.post(self.post_list_url, {"title":"Bye Jalga", "content" : "What Im saying is B22222","post_type" : "ARTICLE" })
317+
318+
# API 호출
319+
response = self.client.get(self.post_list_url, {"keyword" : "HI"})
320+
# 상태값 검증
321+
self.assertSuccess(response)
322+
self.assertEqual(response.data["data"]["total"], 2)
323+
324+
# API 호출
325+
response = self.client.get(self.post_list_url, {"keyword" : "Bye"})
326+
# 상태값 검증
327+
self.assertSuccess(response)
328+
self.assertEqual(response.data["data"]["total"], 1)
329+
330+
# API 호출
331+
response = self.client.get(self.post_list_url, {"keyword" : "is"})
332+
# 상태값 검증
333+
self.assertSuccess(response)
334+
self.assertEqual(response.data["data"]["total"],3)
335+
336+
def test_get_post_list_order_by_newest(self):
337+
"""게시글을 최신 순으로 정렬할 수 있다."""
338+
#테스트 데이터 준비
339+
self.client.force_login(self.user)
340+
self.client.post(self.post_list_url, {"title" : "Post1", "content": "content1", "post_type": "ARTICLE"})
341+
self.client.post(self.post_list_url, {"title" : "Post2", "content": "content2", "post_type": "QUESTION"})
342+
self.client.post(self.post_list_url, {"title" : "Post3", "content": "content3", "post_type": "ARTICLE"})
343+
344+
# API 호출
345+
response = self.client.get(self.post_list_url, {"sort_type" : "NEWEST"})
346+
# 상태값 검증
347+
self.assertSuccess(response)
348+
results = response.data["data"]["results"]
349+
self.assertEqual(results[0]["title"], "Post3")
350+
self.assertEqual(results[1]["title"], "Post2")
351+
self.assertEqual(results[2]["title"], "Post1")
352+
353+
def test_get_post_list_order_by_oldest(self):
354+
"""게시글을 오래된 순으로 정렬할 수 있다."""
355+
#테스트 데이터 준비
356+
self.client.force_login(self.user)
357+
self.client.post(self.post_list_url, {"title" : "Post1", "content": "content1", "post_type": "ARTICLE"})
358+
self.client.post(self.post_list_url, {"title" : "Post2", "content": "content2", "post_type": "QUESTION"})
359+
self.client.post(self.post_list_url, {"title" : "Post3", "content": "content3", "post_type": "ARTICLE"})
360+
361+
#API 호출
362+
response = self.client.get(self.post_list_url, {"sort_type" : "OLDEST"})
363+
#상태값 검증
364+
self.assertSuccess(response)
365+
results = response.data["data"]["results"]
366+
self.assertEqual(results[0]["title"], "Post1")
367+
self.assertEqual(results[1]["title"], "Post2")
368+
self.assertEqual(results[2]["title"], "Post3")
369+
370+
def test_get_post_list_order_by_comments(self):
371+
"""게시글을 댓글 많은 순으로 정렬할 수 있다."""
372+
#테스트 데이터 준비
373+
self.client.force_login(self.user)
374+
## Post1, 댓글 3개
375+
self.client.post(self.post_list_url, {"title" : "Post1", "content": "content1", "post_type": "ARTICLE"})
376+
post1 = Post.objects.get(title="Post1")
377+
Comment.objects.create(post=post1, author=self.user, content="comment1")
378+
Comment.objects.create(post=post1, author=self.user, content="comment2")
379+
Comment.objects.create(post=post1, author=self.user, content="comment3")
380+
## Post2, 댓글 5개
381+
self.client.post(self.post_list_url, {"title" : "Post2", "content": "content2", "post_type": "QUESTION"})
382+
post2 = Post.objects.get(title="Post2")
383+
Comment.objects.create(post=post2, author=self.user, content="comment1")
384+
Comment.objects.create(post=post2, author=self.user, content="comment2")
385+
Comment.objects.create(post=post2, author=self.user, content="comment3")
386+
Comment.objects.create(post=post2, author=self.user, content="comment4")
387+
Comment.objects.create(post=post2, author=self.user, content="comment5")
388+
## Post3, 댓글 0개
389+
self.client.post(self.post_list_url, {"title" : "Post3", "content": "content3", "post_type": "ARTICLE"})
390+
391+
#API 호출
392+
response = self.client.get(self.post_list_url, {"sort_type" : "COMMENT"})
393+
#상태값 검증
394+
self.assertSuccess(response)
395+
results = response.data["data"]["results"]
396+
self.assertEqual(results[0]["title"], "Post2")
397+
self.assertEqual(results[1]["title"], "Post1")
398+
self.assertEqual(results[2]["title"], "Post3")
399+
310400
def test_get_post_detail(self):
311401
"""게시글 상세 정보를 조회할 수 있다."""
312402
response = self.client.get(self.post_detail_url)

0 commit comments

Comments
 (0)