-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from Django-Wanted-Internship-3-Team/feature/i…
…ssue-010 게시물 상세 API 작성
- Loading branch information
Showing
6 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.db.models.signals import pre_save | ||
from django.dispatch import receiver | ||
|
||
from posts.models import Post | ||
|
||
|
||
@receiver(pre_save, sender=Post) | ||
def increment_view_count(sender, instance, **kwargs): | ||
# 게시글을 조회할 때 조회수 증가 | ||
instance.view_count += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import uuid | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.db import transaction | ||
from django.urls import reverse | ||
from django.utils import timezone | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
from posts.models import HashTag, Post | ||
|
||
User = get_user_model() | ||
|
||
|
||
class PostDetailViewTest(APITestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
# 테스트 사용자 생성 | ||
cls.user = User.objects.create_user(username="testuser1", password="testpass1", email="test1@email.com") | ||
|
||
# 테스트 해시태그 생성 | ||
cls.hashtag = HashTag.objects.create(name="hashtag1") | ||
|
||
# 테스트 게시물 생성 및 content_id 저장 | ||
cls.content_ids = [] | ||
|
||
# 테스트 게시물 생성 | ||
for i in range(1, 6): | ||
post = Post.objects.create( | ||
content_id=uuid.uuid4(), | ||
user=cls.user, | ||
post_type="facebook", | ||
title=f"test title {i}", | ||
content=f"test content {i}", | ||
view_count=10 * i, | ||
like_count=5 * i, | ||
share_count=2 * i, | ||
created_at=timezone.now(), | ||
updated_at=timezone.now(), | ||
) | ||
post.hashtag.set([cls.hashtag]) | ||
cls.content_ids.append(str(post.content_id)) | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def test_get_existing_post_content_id(self): | ||
response = self.client.get( | ||
path=reverse("post-detail", kwargs={"content_id": self.content_ids[0]}), | ||
data={"type": "all", "hashtag": self.hashtag.id, "ordering": "created_at", "search": ""}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_get_not_existing_post_content_id(self): | ||
non_existing_content_id = "00000000-0000-0000-0000-000000000001" | ||
response = self.client.get( | ||
path=reverse("post-detail", kwargs={"content_id": non_existing_content_id}), | ||
data={"type": "all", "hashtag": self.hashtag.id, "ordering": "created_at", "search": ""}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
def test_increment_view_count(self): | ||
content_id = self.content_ids[0] | ||
|
||
# 현재 조회수 가져오기 | ||
# 초기값 - 1 (조회와 동시에 count가 됨) | ||
post = Post.objects.get(content_id=content_id) | ||
initial_view_count = post.view_count - 1 | ||
|
||
# 게시물 조회 | ||
with transaction.atomic(): | ||
response = self.client.get( | ||
path=reverse("post-detail", kwargs={"content_id": content_id}), | ||
data={"type": "all", "hashtag": self.hashtag.id, "ordering": "created_at", "search": ""}, | ||
) | ||
|
||
# 조회수 업데이트 확인 | ||
post.refresh_from_db() | ||
updated_view_count = post.view_count | ||
|
||
print("initial_view_count", initial_view_count) | ||
|
||
self.assertEqual(updated_view_count, initial_view_count + 1) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from django.urls import path | ||
|
||
from posts.views import PostListView, StatisticsListView | ||
from posts.views import PostDetailView, PostListView, StatisticsListView | ||
|
||
urlpatterns = [ | ||
path("statistics/", StatisticsListView.as_view(), name="statistics"), | ||
path("", PostListView.as_view(), name="list"), | ||
path("<str:content_id>/", PostDetailView.as_view(), name="post-detail"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters