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

Commit 0023c38

Browse files
committed
fix(tests): error fix
1 parent 1fd6000 commit 0023c38

File tree

5 files changed

+19
-72
lines changed

5 files changed

+19
-72
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
266266
with {:ok, info} <- match(thread),
267267
# make sure the article exsit
268268
# author is passed by middleware, it's exsit for sure
269-
{:ok, article} <- ORM.find(info.model, article_id) do
269+
{:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do
270270
Multi.new()
271271
|> Multi.run(:create_article_comment, fn _, _ ->
272272
do_create_comment(content, info.foreign_key, article, user)
@@ -351,6 +351,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
351351
article_comment_id: comment.id,
352352
user_id: user_id
353353
})
354+
355+
{:ok, :pass}
354356
end)
355357
|> Multi.run(:desc_upvotes_count, fn _, _ ->
356358
count_query = from(c in ArticleCommentUpvote, where: c.article_comment_id == ^comment_id)
@@ -383,15 +385,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
383385
# set floor
384386
# TODO: parse editor-json
385387
# set default emotions
386-
defp do_create_comment(
387-
content,
388-
foreign_key,
389-
%{id: article_id, author_id: article_author_id},
390-
%User{
391-
id: user_id
392-
}
393-
) do
394-
count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article_id)
388+
defp do_create_comment(content, foreign_key, article, %User{id: user_id}) do
389+
count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article.id)
395390
floor = Repo.aggregate(count_query, :count) + 1
396391

397392
ArticleComment
@@ -402,11 +397,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
402397
body_html: content,
403398
emotions: @default_emotions,
404399
floor: floor,
405-
is_article_author: user_id == article_author_id,
400+
is_article_author: user_id == article.author.user.id,
406401
meta: @default_comment_meta
407402
},
408403
foreign_key,
409-
article_id
404+
article.id
410405
)
411406
)
412407
end
@@ -461,13 +456,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
461456
defp add_participator_to_article(_, _), do: {:ok, :pass}
462457

463458
defp get_article(%ArticleComment{post_id: post_id} = comment) when not is_nil(post_id) do
464-
with {:ok, article} <- ORM.find(Post, comment.post_id) do
459+
with {:ok, article} <- ORM.find(Post, comment.post_id, preload: [author: :user]) do
465460
{:post, article}
466461
end
467462
end
468463

469464
defp get_article(%ArticleComment{job_id: job_id} = comment) when not is_nil(job_id) do
470-
with {:ok, article} <- ORM.find(Job, comment.job_id) do
465+
with {:ok, article} <- ORM.find(Job, comment.job_id, preload: [author: :user]) do
471466
{:job, article}
472467
end
473468
end

lib/helper/orm.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@ defmodule Helper.ORM do
128128
end
129129

130130
def findby_delete(queryable, clauses) do
131-
case find_by(queryable, clauses) do
132-
{:ok, content} -> delete(content)
133-
{:error, _} -> {:ok, :pass}
131+
with {:ok, content} <- find_by(queryable, clauses) do
132+
delete(content)
134133
end
135134
end
136135

test/groupher_server/cms/article_comment_test.exs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
2222
end
2323

2424
describe "[basic article comment]" do
25+
@tag :wip2
2526
test "post, job are supported by article comment.", ~m(user post job)a do
2627
post_comment_1 = "post_comment 1"
2728
post_comment_2 = "post_comment 2"
@@ -41,7 +42,7 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
4142
assert List.first(job.article_comments).body_html == job_comment_1
4243
end
4344

44-
@tag :wip2
45+
@tag :wip
4546
test "comment should have default meta after create", ~m(user post)a do
4647
{:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user)
4748
assert comment.meta |> Map.from_struct() |> Map.delete(:id) == @default_comment_meta
@@ -118,7 +119,7 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
118119
assert List.first(comment.upvotes).user_id == user.id
119120
end
120121

121-
@tag :wip2
122+
@tag :wip
122123
test "article author upvote post comment will have flag", ~m(post user)a do
123124
comment = "post_comment"
124125
{:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user)
@@ -166,7 +167,7 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
166167
assert comment.upvotes_count == 2
167168
end
168169

169-
@tag :wip2
170+
@tag :wip
170171
test "user can undo upvote a post comment", ~m(user post)a do
171172
content = "post_comment"
172173
{:ok, comment} = CMS.create_article_comment(:post, post.id, content, user)
@@ -179,7 +180,7 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
179180
assert 0 == comment.upvotes_count
180181
end
181182

182-
@tag :wip2
183+
@tag :wip
183184
test "user can undo upvote a post comment with no upvote", ~m(user post)a do
184185
content = "post_comment"
185186
{:ok, comment} = CMS.create_article_comment(:post, post.id, content, user)
@@ -440,12 +441,12 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
440441
end
441442

442443
describe "[article comment info]" do
443-
@tag :wip
444+
@tag :wip2
444445
test "author of the article comment a comment should have flag", ~m(user post)a do
445446
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
446447
assert not comment.is_article_author
447448

448-
{:ok, author_user} = db_insert(:user, %{id: post.author.user.id})
449+
author_user = post.author.user
449450
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", author_user)
450451
assert comment.is_article_author
451452
end

test/groupher_server_web/mutation/cms/cms_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ defmodule GroupherServer.Test.Mutation.CMS.Basic do
705705
assert false == cur_subscribers.entries |> Enum.any?(&(&1.id == user.id))
706706
end
707707

708+
@tag :wip2
708709
test "other login user unsubscribe community fails", ~m(user_conn community)a do
709710
variables = %{communityId: community.id}
710711

test/groupher_server_web/query/cms/post_comment_test.exs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -129,55 +129,6 @@ defmodule GroupherServer.Test.Query.PostComment do
129129

130130
assert participators["totalCount"] == 10
131131
end
132-
133-
@query """
134-
query($filter: PagedPostsFilter) {
135-
pagedPosts(filter: $filter) {
136-
entries {
137-
id
138-
commentsParticipators(filter: { first: 5 }) {
139-
id
140-
nickname
141-
}
142-
}
143-
}
144-
}
145-
"""
146-
test "top N per Group test v2", ~m(user guest_conn)a do
147-
body = "this is a test comment"
148-
149-
{:ok, community} = db_insert(:community)
150-
{:ok, post} = CMS.create_content(community, :post, mock_attrs(:post), user)
151-
{:ok, post2} = CMS.create_content(community, :post, mock_attrs(:post), user)
152-
{:ok, users_list} = db_insert_multi(:user, 10)
153-
{:ok, users_list2} = db_insert_multi(:user, 10)
154-
155-
post_last_comment_user_id = users_list |> List.last() |> Map.get(:id)
156-
post2_last_comment_user_id = users_list2 |> List.last() |> Map.get(:id)
157-
158-
Enum.each(
159-
users_list,
160-
&CMS.create_comment(:post, post.id, %{community: community.raw, body: body}, &1)
161-
)
162-
163-
Enum.each(
164-
users_list2,
165-
&CMS.create_comment(:post, post2.id, %{community: community.raw, body: body}, &1)
166-
)
167-
168-
variables = %{filter: %{community: community.raw}}
169-
results = guest_conn |> query_result(@query, variables, "pagedPosts")
170-
171-
first_result = results["entries"] |> List.first() |> Map.get("commentsParticipators")
172-
last_result = results["entries"] |> List.last() |> Map.get("commentsParticipators")
173-
174-
assert 5 == first_result |> length
175-
assert 5 == last_result |> length
176-
177-
# 默认显示最新评论的登陆用户
178-
assert to_string(post_last_comment_user_id) == first_result |> List.first() |> Map.get("id")
179-
assert to_string(post2_last_comment_user_id) == last_result |> List.first() |> Map.get("id")
180-
end
181132
end
182133

183134
@query """

0 commit comments

Comments
 (0)