From c5997bd16f0ac4190f69f55bb892122651f6d6f6 Mon Sep 17 00:00:00 2001 From: retroinspect Date: Tue, 23 Jan 2024 12:28:48 +0000 Subject: [PATCH] fix(article): use id instead of created_at for order_by --- .../0057_alter_article_name_type_and_more.py | 3 +-- apps/core/models/article.py | 4 ++-- apps/core/views/viewsets/article.py | 3 ++- tests/test_communication_article.py | 14 ++++---------- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/apps/core/migrations/0057_alter_article_name_type_and_more.py b/apps/core/migrations/0057_alter_article_name_type_and_more.py index d23bfcbf..e3c7ed88 100644 --- a/apps/core/migrations/0057_alter_article_name_type_and_more.py +++ b/apps/core/migrations/0057_alter_article_name_type_and_more.py @@ -23,8 +23,7 @@ class Migration(migrations.Migration): migrations.AddIndex( model_name="article", index=models.Index( - fields=["created_at", "parent_board_id"], - name="created_at_parent_board_id_idx", + fields=["id", "parent_board_id"], name="id_parent_board_id_idx" ), ), ] diff --git a/apps/core/models/article.py b/apps/core/models/article.py index 4525bc4e..dd9eee68 100644 --- a/apps/core/models/article.py +++ b/apps/core/models/article.py @@ -160,8 +160,8 @@ class Meta(MetaDataModel.Meta): indexes = [ models.Index( - fields=["created_at", "parent_board_id"], - name="created_at_parent_board_id_idx", + fields=["id", "parent_board_id"], + name="id_parent_board_id_idx", ) ] diff --git a/apps/core/views/viewsets/article.py b/apps/core/views/viewsets/article.py index ccacb708..d175716f 100644 --- a/apps/core/views/viewsets/article.py +++ b/apps/core/views/viewsets/article.py @@ -45,7 +45,8 @@ class ArticleViewSet(viewsets.ModelViewSet, ActionAPIViewSet): queryset = Article.objects.all() filterset_class = ArticleFilter - ordering_fields = ["created_at", "positive_vote_count"] + ordering_fields = ["id", "positive_vote_count"] + ordering = ["id"] serializer_class = ArticleSerializer action_serializer_class = { diff --git a/tests/test_communication_article.py b/tests/test_communication_article.py index ffbc4a1b..6402fab1 100644 --- a/tests/test_communication_article.py +++ b/tests/test_communication_article.py @@ -271,7 +271,7 @@ def test_descending_ordering_by_positive_vote_count(self): self.user, "get", "articles", - querystring="ordering=-positive_vote_count,-created_at", + querystring="ordering=-positive_vote_count,-id", ) assert res.status_code == HTTP_200_OK @@ -283,14 +283,11 @@ def test_descending_ordering_by_positive_vote_count(self): # 좋아요 개수 같은 경우 최신 글이 앞에 있는지 확인 res_vote_cnt_eq = [ - el.get("created_at") - for el in res_result - if el.get("positive_vote_count") == 2 + el.get("id") for el in res_result if el.get("positive_vote_count") == 2 ] assert res_vote_cnt_eq == sorted( res_vote_cnt_eq, reverse=True, - key=lambda date_str: datetime.fromisoformat(date_str), ) # 좋아요 개수 오름차순 정렬 확인 @@ -305,7 +302,7 @@ def test_ascending_ordering_by_positive_vote_count(self): self.user, "get", "articles", - querystring="ordering=positive_vote_count,-created_at", + querystring="ordering=positive_vote_count,-id", ) assert res.status_code == HTTP_200_OK @@ -318,14 +315,11 @@ def test_ascending_ordering_by_positive_vote_count(self): # 좋아요 개수 같은 경우 최신 글이 앞에 있는지 확인 res_vote_cnt_eq = [ - el.get("created_at") - for el in res_result - if el.get("positive_vote_count") == 2 + el.get("id") for el in res_result if el.get("positive_vote_count") == 2 ] assert res_vote_cnt_eq == sorted( res_vote_cnt_eq, reverse=True, - key=lambda date_str: datetime.fromisoformat(date_str), ) # 답변 진행 상황 필터링 확인