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

Commit 842657a

Browse files
committed
refactor(article-emotions): support job
1 parent 53d24c1 commit 842657a

File tree

4 files changed

+196
-1
lines changed

4 files changed

+196
-1
lines changed

lib/groupher_server/cms/job.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ defmodule GroupherServer.CMS.Job do
7070
# 评论参与者,只保留最近 5 个
7171
embeds_many(:article_comments_participators, Accounts.User, on_replace: :delete)
7272

73+
embeds_one(:emotions, Embeds.ArticleEmotion, on_replace: :update)
74+
7375
many_to_many(
7476
:tags,
7577
Tag,

lib/groupher_server/cms/post.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ defmodule GroupherServer.CMS.Post do
7272
# 评论参与者,只保留最近 5 个
7373
embeds_many(:article_comments_participators, Accounts.User, on_replace: :delete)
7474

75-
#
7675
embeds_one(:emotions, Embeds.ArticleEmotion, on_replace: :update)
7776

7877
# The keys are inflected from the schema names!
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule GroupherServer.Repo.Migrations.AddEmotionsToCmsJobs do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:cms_jobs) do
6+
add(:emotions, :map)
7+
end
8+
end
9+
end
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
defmodule GroupherServer.Test.CMS.Emotions.JobEmotions do
2+
@moduledoc false
3+
4+
use GroupherServer.TestTools
5+
6+
alias Helper.ORM
7+
alias GroupherServer.CMS
8+
9+
alias CMS.{Embeds, ArticlerEmotion, ArticleUserEmotion}
10+
11+
@default_emotions Embeds.ArticleEmotion.default_emotions()
12+
13+
setup do
14+
{:ok, user} = db_insert(:user)
15+
{:ok, community} = db_insert(:community)
16+
{:ok, user2} = db_insert(:user)
17+
{:ok, user3} = db_insert(:user)
18+
19+
job_attrs = mock_attrs(:job, %{community_id: community.id})
20+
21+
{:ok, ~m(user user2 user3 community job_attrs)a}
22+
end
23+
24+
describe "[emotion in paged jobs]" do
25+
@tag :wip2
26+
test "login user should got viewer has emotioned status",
27+
~m(community job_attrs user)a do
28+
total_count = 10
29+
page_number = 10
30+
page_size = 20
31+
32+
all_jobs =
33+
Enum.reduce(0..total_count, [], fn _, acc ->
34+
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
35+
acc ++ [job]
36+
end)
37+
38+
random_job = all_jobs |> Enum.at(3)
39+
40+
{:ok, _} = CMS.emotion_to_article(:job, random_job.id, :downvote, user)
41+
{:ok, _} = CMS.emotion_to_article(:job, random_job.id, :beer, user)
42+
{:ok, _} = CMS.emotion_to_article(:job, random_job.id, :popcorn, user)
43+
44+
{:ok, paged_articles} =
45+
CMS.paged_articles(:job, %{page: page_number, size: page_size}, user)
46+
47+
target = Enum.find(paged_articles.entries, &(&1.id == random_job.id))
48+
49+
assert target.emotions.downvote_count == 1
50+
assert user_exist_in?(user, target.emotions.latest_downvote_users)
51+
assert target.emotions.viewer_has_downvoteed
52+
53+
assert target.emotions.beer_count == 1
54+
assert user_exist_in?(user, target.emotions.latest_beer_users)
55+
assert target.emotions.viewer_has_beered
56+
57+
assert target.emotions.popcorn_count == 1
58+
assert user_exist_in?(user, target.emotions.latest_popcorn_users)
59+
assert target.emotions.viewer_has_popcorned
60+
end
61+
end
62+
63+
describe "[basic article emotion]" do
64+
@tag :wip2
65+
test "job has default emotions after created", ~m(community job_attrs user)a do
66+
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
67+
68+
emotions = job.emotions |> Map.from_struct() |> Map.delete(:id)
69+
assert @default_emotions == emotions
70+
end
71+
72+
@tag :wip2
73+
test "can make emotion to job", ~m(community job_attrs user user2)a do
74+
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
75+
76+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :downvote, user)
77+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :downvote, user2)
78+
79+
{:ok, %{emotions: emotions}} = ORM.find(CMS.Job, job.id)
80+
81+
assert emotions.downvote_count == 2
82+
assert user_exist_in?(user, emotions.latest_downvote_users)
83+
assert user_exist_in?(user2, emotions.latest_downvote_users)
84+
end
85+
86+
@tag :wip2
87+
test "can undo emotion to job", ~m(community job_attrs user user2)a do
88+
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
89+
90+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :downvote, user)
91+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :downvote, user2)
92+
93+
{:ok, _} = CMS.undo_emotion_to_article(:job, job.id, :downvote, user)
94+
{:ok, _} = CMS.undo_emotion_to_article(:job, job.id, :downvote, user2)
95+
96+
{:ok, %{emotions: emotions}} = ORM.find(CMS.Job, job.id)
97+
98+
assert emotions.downvote_count == 0
99+
assert not user_exist_in?(user, emotions.latest_downvote_users)
100+
assert not user_exist_in?(user2, emotions.latest_downvote_users)
101+
end
102+
103+
@tag :wip2
104+
test "same user make same emotion to same job.", ~m(community job_attrs user)a do
105+
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
106+
107+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :downvote, user)
108+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :downvote, user)
109+
110+
{:ok, job} = ORM.find(CMS.Job, job.id)
111+
112+
assert job.emotions.downvote_count == 1
113+
assert user_exist_in?(user, job.emotions.latest_downvote_users)
114+
end
115+
116+
@tag :wip2
117+
test "same user same emotion to same job only have one user_emotion record",
118+
~m(community job_attrs user)a do
119+
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
120+
121+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :downvote, user)
122+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :heart, user)
123+
124+
{:ok, job} = ORM.find(CMS.Job, job.id)
125+
126+
{:ok, records} = ORM.find_all(ArticleUserEmotion, %{page: 1, size: 10})
127+
assert records.total_count == 1
128+
129+
{:ok, record} = ORM.find_by(ArticleUserEmotion, %{job_id: job.id, user_id: user.id})
130+
assert record.downvote
131+
assert record.heart
132+
end
133+
134+
@tag :wip2
135+
test "different user can make same emotions on same job",
136+
~m(community job_attrs user user2 user3)a do
137+
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
138+
139+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :beer, user)
140+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :beer, user2)
141+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :beer, user3)
142+
143+
{:ok, %{emotions: emotions}} = ORM.find(CMS.Job, job.id)
144+
145+
assert emotions.beer_count == 3
146+
assert user_exist_in?(user, emotions.latest_beer_users)
147+
assert user_exist_in?(user2, emotions.latest_beer_users)
148+
assert user_exist_in?(user3, emotions.latest_beer_users)
149+
end
150+
151+
@tag :wip2
152+
test "same user can make differcent emotions on same job", ~m(community job_attrs user)a do
153+
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
154+
155+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :downvote, user)
156+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :downvote, user)
157+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :beer, user)
158+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :heart, user)
159+
{:ok, _} = CMS.emotion_to_article(:job, job.id, :orz, user)
160+
161+
{:ok, %{emotions: emotions}} = ORM.find(CMS.Job, job.id)
162+
163+
assert emotions.downvote_count == 1
164+
assert user_exist_in?(user, emotions.latest_downvote_users)
165+
166+
assert emotions.beer_count == 1
167+
assert user_exist_in?(user, emotions.latest_beer_users)
168+
169+
assert emotions.heart_count == 1
170+
assert user_exist_in?(user, emotions.latest_heart_users)
171+
172+
assert emotions.orz_count == 1
173+
assert user_exist_in?(user, emotions.latest_orz_users)
174+
175+
assert emotions.pill_count == 0
176+
assert not user_exist_in?(user, emotions.latest_pill_users)
177+
178+
assert emotions.biceps_count == 0
179+
assert not user_exist_in?(user, emotions.latest_biceps_users)
180+
181+
assert emotions.confused_count == 0
182+
assert not user_exist_in?(user, emotions.latest_confused_users)
183+
end
184+
end
185+
end

0 commit comments

Comments
 (0)