Skip to content

Commit

Permalink
fix(article): use id instead of created_at for order_by
Browse files Browse the repository at this point in the history
  • Loading branch information
retroinspect committed Jan 23, 2024
1 parent 06fc587 commit c5997bd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
),
),
]
4 changes: 2 additions & 2 deletions apps/core/models/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
]

Expand Down
3 changes: 2 additions & 1 deletion apps/core/views/viewsets/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
14 changes: 4 additions & 10 deletions tests/test_communication_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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),
)

# 좋아요 개수 오름차순 정렬 확인
Expand All @@ -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

Expand All @@ -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),
)

# 답변 진행 상황 필터링 확인
Expand Down

0 comments on commit c5997bd

Please sign in to comment.