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

Commit 7d0d5c5

Browse files
committed
refactor(cited-article): more test
1 parent c3e422b commit 7d0d5c5

File tree

6 files changed

+167
-9
lines changed

6 files changed

+167
-9
lines changed

lib/groupher_server/cms/delegates/cite_tasks.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
193193
{:ok, "https://coderplanets.com/post/190220"}
194194
"""
195195
defp parse_link(attrs) do
196-
with {"href", link} <- Enum.find(attrs, fn {a, v} -> a == "href" end) do
196+
with {"href", link} <- Enum.find(attrs, fn {a, _v} -> a == "href" end) do
197197
{:ok, link}
198198
else
199199
_ -> {:error, "invalid fmt"}

priv/repo/migrations/20210611015726_create_cited_articles.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ defmodule GroupherServer.Repo.Migrations.CreateCitedArticles do
1111
add(:article_comment_id, references(:articles_comments, on_delete: :delete_all))
1212

1313
add(:post_id, references(:cms_posts, on_delete: :delete_all))
14-
add(:repo_id, references(:cms_posts, on_delete: :delete_all))
15-
add(:job_id, references(:cms_posts, on_delete: :delete_all))
16-
add(:blog_id, references(:cms_posts, on_delete: :delete_all))
14+
add(:repo_id, references(:cms_repos, on_delete: :delete_all))
15+
add(:job_id, references(:cms_jobs, on_delete: :delete_all))
16+
add(:blog_id, references(:cms_blogs, on_delete: :delete_all))
1717

1818
add(:block_linker, {:array, :string})
1919
end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
defmodule GroupherServer.Test.CMS.CiteContent.Blog do
2+
use GroupherServer.TestTools
3+
4+
import Helper.Utils, only: [get_config: 2]
5+
6+
alias Helper.ORM
7+
alias GroupherServer.CMS
8+
9+
alias CMS.Model.Blog
10+
11+
alias CMS.Delegate.CiteTasks
12+
13+
@site_host get_config(:general, :site_host)
14+
15+
setup do
16+
{:ok, user} = db_insert(:user)
17+
{:ok, user2} = db_insert(:user)
18+
{:ok, blog} = db_insert(:blog)
19+
{:ok, blog2} = db_insert(:blog)
20+
{:ok, blog3} = db_insert(:blog)
21+
{:ok, blog4} = db_insert(:blog)
22+
{:ok, blog5} = db_insert(:blog)
23+
24+
{:ok, community} = db_insert(:community)
25+
26+
blog_attrs = mock_attrs(:blog, %{community_id: community.id})
27+
28+
{:ok, ~m(user user2 community blog blog2 blog3 blog4 blog5 blog_attrs)a}
29+
end
30+
31+
describe "[cite basic]" do
32+
@tag :wip
33+
test "cited multi blog should work", ~m(user community blog2 blog3 blog4 blog5 blog_attrs)a do
34+
body =
35+
mock_rich_text(
36+
~s(the <a href=#{@site_host}/blog/#{blog2.id} /> and <a href=#{@site_host}/blog/#{
37+
blog2.id
38+
}>same la</a> is awesome, the <a href=#{@site_host}/blog/#{blog3.id}></a> is awesome too.),
39+
# second paragraph
40+
~s(the paragraph 2 <a href=#{@site_host}/blog/#{blog2.id} class=#{blog2.title}> again</a>, the paragraph 2 <a href=#{
41+
@site_host
42+
}/blog/#{blog4.id}> again</a>, the paragraph 2 <a href=#{@site_host}/blog/#{blog5.id}> again</a>)
43+
)
44+
45+
blog_attrs = blog_attrs |> Map.merge(%{body: body})
46+
{:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user)
47+
48+
body = mock_rich_text(~s(the <a href=#{@site_host}/blog/#{blog3.id} />))
49+
blog_attrs = blog_attrs |> Map.merge(%{body: body})
50+
{:ok, blog_n} = CMS.create_article(community, :blog, blog_attrs, user)
51+
52+
CiteTasks.handle(blog)
53+
CiteTasks.handle(blog_n)
54+
55+
{:ok, blog2} = ORM.find(Blog, blog2.id)
56+
{:ok, blog3} = ORM.find(Blog, blog3.id)
57+
{:ok, blog4} = ORM.find(Blog, blog4.id)
58+
{:ok, blog5} = ORM.find(Blog, blog5.id)
59+
60+
assert blog2.meta.citing_count == 1
61+
assert blog3.meta.citing_count == 2
62+
assert blog4.meta.citing_count == 1
63+
assert blog5.meta.citing_count == 1
64+
end
65+
66+
@tag :wip
67+
test "cited blog itself should not work", ~m(user community blog_attrs)a do
68+
{:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user)
69+
70+
body = mock_rich_text(~s(the <a href=#{@site_host}/blog/#{blog.id} />))
71+
{:ok, blog} = CMS.update_article(blog, %{body: body})
72+
73+
CiteTasks.handle(blog)
74+
75+
{:ok, blog} = ORM.find(Blog, blog.id)
76+
assert blog.meta.citing_count == 0
77+
end
78+
end
79+
end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
defmodule GroupherServer.Test.CMS.CiteContent.Job do
2+
use GroupherServer.TestTools
3+
4+
import Helper.Utils, only: [get_config: 2]
5+
6+
alias Helper.ORM
7+
alias GroupherServer.CMS
8+
9+
alias CMS.Model.Job
10+
11+
alias CMS.Delegate.CiteTasks
12+
13+
@site_host get_config(:general, :site_host)
14+
15+
setup do
16+
{:ok, user} = db_insert(:user)
17+
{:ok, user2} = db_insert(:user)
18+
{:ok, job} = db_insert(:job)
19+
{:ok, job2} = db_insert(:job)
20+
{:ok, job3} = db_insert(:job)
21+
{:ok, job4} = db_insert(:job)
22+
{:ok, job5} = db_insert(:job)
23+
24+
{:ok, community} = db_insert(:community)
25+
26+
job_attrs = mock_attrs(:job, %{community_id: community.id})
27+
28+
{:ok, ~m(user user2 community job job2 job3 job4 job5 job_attrs)a}
29+
end
30+
31+
describe "[cite basic]" do
32+
@tag :wip
33+
test "cited multi job should work", ~m(user community job2 job3 job4 job5 job_attrs)a do
34+
body =
35+
mock_rich_text(
36+
~s(the <a href=#{@site_host}/job/#{job2.id} /> and <a href=#{@site_host}/job/#{job2.id}>same la</a> is awesome, the <a href=#{
37+
@site_host
38+
}/job/#{job3.id}></a> is awesome too.),
39+
# second paragraph
40+
~s(the paragraph 2 <a href=#{@site_host}/job/#{job2.id} class=#{job2.title}> again</a>, the paragraph 2 <a href=#{
41+
@site_host
42+
}/job/#{job4.id}> again</a>, the paragraph 2 <a href=#{@site_host}/job/#{job5.id}> again</a>)
43+
)
44+
45+
job_attrs = job_attrs |> Map.merge(%{body: body})
46+
{:ok, job} = CMS.create_article(community, :job, job_attrs, user)
47+
48+
body = mock_rich_text(~s(the <a href=#{@site_host}/job/#{job3.id} />))
49+
job_attrs = job_attrs |> Map.merge(%{body: body})
50+
{:ok, job_n} = CMS.create_article(community, :job, job_attrs, user)
51+
52+
CiteTasks.handle(job)
53+
CiteTasks.handle(job_n)
54+
55+
{:ok, job2} = ORM.find(Job, job2.id)
56+
{:ok, job3} = ORM.find(Job, job3.id)
57+
{:ok, job4} = ORM.find(Job, job4.id)
58+
{:ok, job5} = ORM.find(Job, job5.id)
59+
60+
assert job2.meta.citing_count == 1
61+
assert job3.meta.citing_count == 2
62+
assert job4.meta.citing_count == 1
63+
assert job5.meta.citing_count == 1
64+
end
65+
66+
@tag :wip
67+
test "cited job itself should not work", ~m(user community job_attrs)a do
68+
{:ok, job} = CMS.create_article(community, :job, job_attrs, user)
69+
70+
body = mock_rich_text(~s(the <a href=#{@site_host}/job/#{job.id} />))
71+
{:ok, job} = CMS.update_article(job, %{body: body})
72+
73+
CiteTasks.handle(job)
74+
75+
{:ok, job} = ORM.find(Job, job.id)
76+
assert job.meta.citing_count == 0
77+
end
78+
end
79+
end

test/groupher_server/cms/cite_contents/cite_post_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule GroupherServer.Test.CMS.CiteContent do
1+
defmodule GroupherServer.Test.CMS.CiteContent.Post do
22
use GroupherServer.TestTools
33

44
import Helper.Utils, only: [get_config: 2]
@@ -64,7 +64,7 @@ defmodule GroupherServer.Test.CMS.CiteContent do
6464
end
6565

6666
@tag :wip
67-
test "cited post itself should not work", ~m(user community post post_attrs)a do
67+
test "cited post itself should not work", ~m(user community post_attrs)a do
6868
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
6969

7070
body = mock_rich_text(~s(the <a href=#{@site_host}/post/#{post.id} />))

test/support/factory.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ defmodule GroupherServer.Support.Factory do
113113
desc = Faker.Lorem.sentence(%Range{first: 15, last: 60})
114114

115115
%{
116-
meta: @default_article_meta,
116+
meta: @default_article_meta |> Map.merge(%{thread: "REPO"}),
117117
title: String.slice(desc, 1, 49),
118118
owner_name: "coderplanets",
119119
owner_url: "http://www.github.com/coderplanets",
@@ -183,7 +183,7 @@ defmodule GroupherServer.Support.Factory do
183183
text = Faker.Lorem.sentence(%Range{first: 80, last: 120})
184184

185185
%{
186-
meta: @default_article_meta,
186+
meta: @default_article_meta |> Map.merge(%{thread: "JOB"}),
187187
title: String.slice(text, 1, 49),
188188
company: Faker.Company.name(),
189189
body: mock_rich_text(),
@@ -205,7 +205,7 @@ defmodule GroupherServer.Support.Factory do
205205
text = Faker.Lorem.sentence(%Range{first: 80, last: 120})
206206

207207
%{
208-
meta: @default_article_meta,
208+
meta: @default_article_meta |> Map.merge(%{thread: "BLOG"}),
209209
title: String.slice(text, 1, 49),
210210
body: mock_rich_text(),
211211
digest: String.slice(text, 1, 150),

0 commit comments

Comments
 (0)