Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 89769f5

Browse files
authored
test(article-comment): missing gq test for upvote (#393)
1 parent 7ee6e11 commit 89769f5

File tree

8 files changed

+259
-12
lines changed

8 files changed

+259
-12
lines changed

lib/groupher_server/cms/delegates/article_comment_action.ex

+22-12
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
160160
def upvote_article_comment(comment_id, %User{id: user_id}) do
161161
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
162162
false <- comment.is_deleted do
163-
# TODO: is user upvoted before?
164163
Multi.new()
165164
|> Multi.run(:create_comment_upvote, fn _, _ ->
166165
ORM.create(ArticleCommentUpvote, %{article_comment_id: comment.id, user_id: user_id})
@@ -176,6 +175,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
176175
|> Multi.run(:check_article_author_upvoted, fn _, %{inc_upvotes_count: comment} ->
177176
update_article_author_upvoted_info(comment, user_id)
178177
end)
178+
|> Multi.run(:upvote_comment_done, fn _, %{check_article_author_upvoted: comment} ->
179+
viewer_has_upvoted = Enum.member?(comment.meta.upvoted_user_ids, user_id)
180+
viewer_has_reported = Enum.member?(comment.meta.reported_user_ids, user_id)
181+
182+
comment
183+
|> Map.merge(%{viewer_has_upvoted: viewer_has_upvoted})
184+
|> Map.merge(%{viewer_has_reported: viewer_has_reported})
185+
|> done
186+
end)
179187
|> Repo.transaction()
180188
|> result()
181189
end
@@ -204,6 +212,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
204212
|> Multi.run(:check_article_author_upvoted, fn _, %{desc_upvotes_count: updated_comment} ->
205213
update_article_author_upvoted_info(updated_comment, user_id)
206214
end)
215+
|> Multi.run(:upvote_comment_done, fn _, %{check_article_author_upvoted: comment} ->
216+
viewer_has_upvoted = Enum.member?(comment.meta.upvoted_user_ids, user_id)
217+
viewer_has_reported = Enum.member?(comment.meta.reported_user_ids, user_id)
218+
219+
comment
220+
|> Map.merge(%{viewer_has_upvoted: viewer_has_upvoted})
221+
|> Map.merge(%{viewer_has_reported: viewer_has_reported})
222+
|> done
223+
end)
207224
|> Repo.transaction()
208225
|> result()
209226
end
@@ -234,20 +251,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
234251
defp update_article_author_upvoted_info(%ArticleComment{} = comment, user_id) do
235252
with {:ok, article} = get_full_comment(comment.id) do
236253
is_article_author_upvoted = article.author.id == user_id
237-
238-
new_meta =
239-
comment.meta
240-
|> Map.from_struct()
241-
|> Map.merge(%{is_article_author_upvoted: is_article_author_upvoted})
242-
243-
comment |> ORM.update(%{meta: new_meta})
254+
meta = comment.meta |> Map.put(:is_article_author_upvoted, is_article_author_upvoted)
255+
comment |> ORM.update_meta(meta)
244256
end
245257
end
246258

247259
# 设计盖楼只保留一个层级,回复楼中的评论都会被放到顶楼的 replies 中
248-
defp get_parent_comment(%ArticleComment{reply_to_id: nil} = comment) do
249-
comment
250-
end
260+
defp get_parent_comment(%ArticleComment{reply_to_id: nil} = comment), do: comment
251261

252262
defp get_parent_comment(%ArticleComment{reply_to_id: reply_to_id} = comment)
253263
when not is_nil(reply_to_id) do
@@ -355,7 +365,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
355365

356366
defp result({:ok, %{create_article_comment: result}}), do: {:ok, result}
357367
defp result({:ok, %{add_reply_to: result}}), do: {:ok, result}
358-
defp result({:ok, %{check_article_author_upvoted: result}}), do: {:ok, result}
368+
defp result({:ok, %{upvote_comment_done: result}}), do: {:ok, result}
359369
defp result({:ok, %{fold_comment_report_too_many: result}}), do: {:ok, result}
360370
defp result({:ok, %{update_comment_flag: result}}), do: {:ok, result}
361371
defp result({:ok, %{delete_article_comment: result}}), do: {:ok, result}

lib/groupher_server/cms/models/article_comment.ex

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ defmodule GroupherServer.CMS.Model.ArticleComment do
7070
# 是否置顶
7171
field(:is_pinned, :boolean, default: false)
7272
field(:viewer_has_upvoted, :boolean, default: false, virtual: true)
73+
field(:viewer_has_reported, :boolean, default: false, virtual: true)
7374

7475
belongs_to(:reply_to, ArticleComment, foreign_key: :reply_to_id)
7576

lib/groupher_server_web/resolvers/cms_resolver.ex

+8
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ defmodule GroupherServerWeb.Resolvers.CMS do
312312
CMS.reply_article_comment(id, content, user)
313313
end
314314

315+
def upvote_article_comment(_root, ~m(id)a, %{context: %{cur_user: user}}) do
316+
CMS.upvote_article_comment(id, user)
317+
end
318+
319+
def undo_upvote_article_comment(_root, ~m(id)a, %{context: %{cur_user: user}}) do
320+
CMS.undo_upvote_article_comment(id, user)
321+
end
322+
315323
def emotion_to_comment(_root, ~m(id emotion)a, %{context: %{cur_user: user}}) do
316324
CMS.emotion_to_comment(id, emotion, user)
317325
end

lib/groupher_server_web/schema/cms/mutations/comment.ex

+16
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do
5555
middleware(M.Statistics.MakeContribute, for: :user)
5656
end
5757

58+
@desc "upvote to a comment"
59+
field :upvote_article_comment, :article_comment do
60+
arg(:id, non_null(:id))
61+
62+
middleware(M.Authorize, :login)
63+
resolve(&R.CMS.upvote_article_comment/3)
64+
end
65+
66+
@desc "undo upvote to a comment"
67+
field :undo_upvote_article_comment, :article_comment do
68+
arg(:id, non_null(:id))
69+
70+
middleware(M.Authorize, :login)
71+
resolve(&R.CMS.undo_upvote_article_comment/3)
72+
end
73+
5874
@desc "emotion to a comment"
5975
field :emotion_to_comment, :article_comment do
6076
arg(:id, non_null(:id))

test/groupher_server_web/mutation/cms/comments/blog_comment_test.exs

+53
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,59 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do
106106
end
107107
end
108108

109+
describe "[article comment upvote]" do
110+
@upvote_comment_query """
111+
mutation($id: ID!) {
112+
upvoteArticleComment(id: $id) {
113+
id
114+
upvotesCount
115+
viewerHasUpvoted
116+
}
117+
}
118+
"""
119+
@tag :wip
120+
test "login user can upvote a exsit blog comment", ~m(blog user guest_conn user_conn)a do
121+
{:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user)
122+
variables = %{id: comment.id}
123+
124+
assert guest_conn
125+
|> mutation_get_error?(@upvote_comment_query, variables, ecode(:account_login))
126+
127+
result =
128+
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")
129+
130+
assert result["id"] == to_string(comment.id)
131+
assert result["upvotesCount"] == 1
132+
assert result["viewerHasUpvoted"]
133+
end
134+
135+
@undo_upvote_comment_query """
136+
mutation($id: ID!) {
137+
undoUpvoteArticleComment(id: $id) {
138+
id
139+
upvotesCount
140+
viewerHasUpvoted
141+
}
142+
}
143+
"""
144+
@tag :wip
145+
test "login user can undo upvote a exsit blog comment", ~m(blog user guest_conn user_conn)a do
146+
{:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user)
147+
variables = %{id: comment.id}
148+
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")
149+
150+
assert guest_conn
151+
|> mutation_get_error?(@undo_upvote_comment_query, variables, ecode(:account_login))
152+
153+
result =
154+
user_conn
155+
|> mutation_result(@undo_upvote_comment_query, variables, "undoUpvoteArticleComment")
156+
157+
assert result["upvotesCount"] == 0
158+
assert not result["viewerHasUpvoted"]
159+
end
160+
end
161+
109162
describe "[article comment emotion]" do
110163
@emotion_comment_query """
111164
mutation($id: ID!, $emotion: ArticleCommentEmotion!) {

test/groupher_server_web/mutation/cms/comments/job_comment_test.exs

+53
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,59 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do
106106
end
107107
end
108108

109+
describe "[article comment upvote]" do
110+
@upvote_comment_query """
111+
mutation($id: ID!) {
112+
upvoteArticleComment(id: $id) {
113+
id
114+
upvotesCount
115+
viewerHasUpvoted
116+
}
117+
}
118+
"""
119+
@tag :wip
120+
test "login user can upvote a exsit job comment", ~m(job user guest_conn user_conn)a do
121+
{:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user)
122+
variables = %{id: comment.id}
123+
124+
assert guest_conn
125+
|> mutation_get_error?(@upvote_comment_query, variables, ecode(:account_login))
126+
127+
result =
128+
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")
129+
130+
assert result["id"] == to_string(comment.id)
131+
assert result["upvotesCount"] == 1
132+
assert result["viewerHasUpvoted"]
133+
end
134+
135+
@undo_upvote_comment_query """
136+
mutation($id: ID!) {
137+
undoUpvoteArticleComment(id: $id) {
138+
id
139+
upvotesCount
140+
viewerHasUpvoted
141+
}
142+
}
143+
"""
144+
@tag :wip
145+
test "login user can undo upvote a exsit job comment", ~m(job user guest_conn user_conn)a do
146+
{:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user)
147+
variables = %{id: comment.id}
148+
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")
149+
150+
assert guest_conn
151+
|> mutation_get_error?(@undo_upvote_comment_query, variables, ecode(:account_login))
152+
153+
result =
154+
user_conn
155+
|> mutation_result(@undo_upvote_comment_query, variables, "undoUpvoteArticleComment")
156+
157+
assert result["upvotesCount"] == 0
158+
assert not result["viewerHasUpvoted"]
159+
end
160+
end
161+
109162
describe "[article comment emotion]" do
110163
@emotion_comment_query """
111164
mutation($id: ID!, $emotion: ArticleCommentEmotion!) {

test/groupher_server_web/mutation/cms/comments/post_comment_test.exs

+53
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,59 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do
106106
end
107107
end
108108

109+
describe "[article comment upvote]" do
110+
@upvote_comment_query """
111+
mutation($id: ID!) {
112+
upvoteArticleComment(id: $id) {
113+
id
114+
upvotesCount
115+
viewerHasUpvoted
116+
}
117+
}
118+
"""
119+
@tag :wip
120+
test "login user can upvote a exsit post comment", ~m(post user guest_conn user_conn)a do
121+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user)
122+
variables = %{id: comment.id}
123+
124+
assert guest_conn
125+
|> mutation_get_error?(@upvote_comment_query, variables, ecode(:account_login))
126+
127+
result =
128+
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")
129+
130+
assert result["id"] == to_string(comment.id)
131+
assert result["upvotesCount"] == 1
132+
assert result["viewerHasUpvoted"]
133+
end
134+
135+
@undo_upvote_comment_query """
136+
mutation($id: ID!) {
137+
undoUpvoteArticleComment(id: $id) {
138+
id
139+
upvotesCount
140+
viewerHasUpvoted
141+
}
142+
}
143+
"""
144+
@tag :wip
145+
test "login user can undo upvote a exsit post comment", ~m(post user guest_conn user_conn)a do
146+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user)
147+
variables = %{id: comment.id}
148+
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")
149+
150+
assert guest_conn
151+
|> mutation_get_error?(@undo_upvote_comment_query, variables, ecode(:account_login))
152+
153+
result =
154+
user_conn
155+
|> mutation_result(@undo_upvote_comment_query, variables, "undoUpvoteArticleComment")
156+
157+
assert result["upvotesCount"] == 0
158+
assert not result["viewerHasUpvoted"]
159+
end
160+
end
161+
109162
describe "[article comment emotion]" do
110163
@emotion_comment_query """
111164
mutation($id: ID!, $emotion: ArticleCommentEmotion!) {

test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs

+53
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,59 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do
106106
end
107107
end
108108

109+
describe "[article comment upvote]" do
110+
@upvote_comment_query """
111+
mutation($id: ID!) {
112+
upvoteArticleComment(id: $id) {
113+
id
114+
upvotesCount
115+
viewerHasUpvoted
116+
}
117+
}
118+
"""
119+
@tag :wip
120+
test "login user can upvote a exsit repo comment", ~m(repo user guest_conn user_conn)a do
121+
{:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user)
122+
variables = %{id: comment.id}
123+
124+
assert guest_conn
125+
|> mutation_get_error?(@upvote_comment_query, variables, ecode(:account_login))
126+
127+
result =
128+
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")
129+
130+
assert result["id"] == to_string(comment.id)
131+
assert result["upvotesCount"] == 1
132+
assert result["viewerHasUpvoted"]
133+
end
134+
135+
@undo_upvote_comment_query """
136+
mutation($id: ID!) {
137+
undoUpvoteArticleComment(id: $id) {
138+
id
139+
upvotesCount
140+
viewerHasUpvoted
141+
}
142+
}
143+
"""
144+
@tag :wip
145+
test "login user can undo upvote a exsit repo comment", ~m(repo user guest_conn user_conn)a do
146+
{:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user)
147+
variables = %{id: comment.id}
148+
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")
149+
150+
assert guest_conn
151+
|> mutation_get_error?(@undo_upvote_comment_query, variables, ecode(:account_login))
152+
153+
result =
154+
user_conn
155+
|> mutation_result(@undo_upvote_comment_query, variables, "undoUpvoteArticleComment")
156+
157+
assert result["upvotesCount"] == 0
158+
assert not result["viewerHasUpvoted"]
159+
end
160+
end
161+
109162
describe "[article comment emotion]" do
110163
@emotion_comment_query """
111164
mutation($id: ID!, $emotion: ArticleCommentEmotion!) {

0 commit comments

Comments
 (0)