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

Commit b9b651a

Browse files
committed
refactor: split some test, video module enhance
1 parent 7d931bf commit b9b651a

File tree

12 files changed

+310
-27
lines changed

12 files changed

+310
-27
lines changed

lib/mastani_server/cms/delegates/article_curd.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ defmodule MastaniServer.CMS.Delegate.ArticleCURD do
126126
false ->
127127
{:ok, "pass"}
128128
end
129-
130-
# Repo.insert(%CMS.PostCommunityFlag{post_id: content.id, community_id: community.id, })
131129
end)
132130
|> Multi.run(:set_tag, fn %{add_content_author: content} ->
133131
case attrs |> Map.has_key?(:tags) do

lib/mastani_server/cms/job.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule MastaniServer.CMS.Job do
44

55
use Ecto.Schema
66
import Ecto.Changeset
7-
alias MastaniServer.CMS.{Author, Community, JobFavorite, JobCommunityFlag, Tag}
7+
alias MastaniServer.CMS.{Author, Community, JobComment, JobFavorite, JobCommunityFlag, Tag}
88

99
@required_fields ~w(title company company_logo location body digest length)a
1010
@optional_fields ~w(link_addr link_source min_education)a
@@ -41,7 +41,7 @@ defmodule MastaniServer.CMS.Job do
4141
field(:pin, :boolean, default_value: false, virtual: true)
4242
field(:trash, :boolean, default_value: false, virtual: true)
4343

44-
# has_many(:comments, {"jobs_comments", JobComment})
44+
has_many(:comments, {"jobs_comments", JobComment})
4545
has_many(:favorites, {"jobs_favorites", JobFavorite})
4646
# has_many(:stars, {"posts_stars", PostStar})
4747

lib/mastani_server/cms/utils/matcher.ex

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ defmodule MastaniServer.CMS.Utils.Matcher do
4040

4141
defguard valid_feeling(feel) when feel in [:like, :dislike]
4242

43-
# posts ...
43+
#########################################
44+
## posts ...
45+
#########################################
4446
def match_action(:post, :self), do: {:ok, %{target: Post, reactor: Post, preload: :author}}
4547

4648
def match_action(:post, :favorite),
@@ -61,19 +63,9 @@ defmodule MastaniServer.CMS.Utils.Matcher do
6163
def match_action(:post_comment, :dislike),
6264
do: {:ok, %{target: PostComment, reactor: PostCommentDislike}}
6365

64-
# videos ...
65-
def match_action(:video, :self), do: {:ok, %{target: Video, reactor: Video, preload: :author}}
66-
67-
def match_action(:video, :community),
68-
do: {:ok, %{target: Video, reactor: Community, flag: VideoCommunityFlag}}
69-
70-
# repos ...
71-
def match_action(:repo, :self), do: {:ok, %{target: Repo, reactor: Repo, preload: :author}}
72-
73-
def match_action(:repo, :community),
74-
do: {:ok, %{target: Repo, reactor: Community, flag: RepoCommunityFlag}}
75-
76-
# jobs ...
66+
#########################################
67+
## jobs ...
68+
#########################################
7769
def match_action(:job, :self), do: {:ok, %{target: Job, reactor: Job, preload: :author}}
7870

7971
def match_action(:job, :community),
@@ -88,6 +80,22 @@ defmodule MastaniServer.CMS.Utils.Matcher do
8880
def match_action(:job, :favorite),
8981
do: {:ok, %{target: Job, reactor: JobFavorite, preload: :user}}
9082

83+
#########################################
84+
## videos ...
85+
#########################################
86+
def match_action(:video, :self), do: {:ok, %{target: Video, reactor: Video, preload: :author}}
87+
88+
def match_action(:video, :community),
89+
do: {:ok, %{target: Video, reactor: Community, flag: VideoCommunityFlag}}
90+
91+
#########################################
92+
## repos ...
93+
#########################################
94+
def match_action(:repo, :self), do: {:ok, %{target: Repo, reactor: Repo, preload: :author}}
95+
96+
def match_action(:repo, :community),
97+
do: {:ok, %{target: Repo, reactor: Community, flag: RepoCommunityFlag}}
98+
9199
def dynamic_where(thread, id) do
92100
case thread do
93101
:post ->

lib/mastani_server/cms/video.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ defmodule MastaniServer.CMS.Video do
66
import Ecto.Changeset
77
alias MastaniServer.CMS.{Author, Community, VideoCommunityFlag, Tag}
88

9-
@required_fields ~w(title poster desc duration duration_sec source)a
10-
@optional_fields ~w(link original_author original_author_link publish_at)
9+
@required_fields ~w(title poster thumbnil desc duration duration_sec source link original_author original_author_link publish_at)a
10+
# @optional_fields ~w()a
1111

1212
@type t :: %Video{}
1313
schema "cms_videos" do
1414
field(:title, :string)
1515
field(:poster, :string)
16+
field(:thumbnil, :string)
1617
field(:desc, :string)
1718
field(:duration, :string)
1819
field(:duration_sec, :integer)
@@ -59,7 +60,7 @@ defmodule MastaniServer.CMS.Video do
5960
@doc false
6061
def changeset(%Video{} = video, attrs) do
6162
video
62-
|> cast(attrs, @optional_fields ++ @required_fields)
63+
|> cast(attrs, @required_fields)
6364
|> validate_required(@required_fields)
6465

6566
# |> foreign_key_constraint(:posts_tags, name: :posts_tags_tag_id_fkey)

lib/mastani_server_web/schema/cms/cms_misc.ex

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,31 @@ defmodule MastaniServerWeb.Schema.CMS.Misc do
166166
_, _ -> nil
167167
end)
168168
end
169+
170+
# @desc """
171+
# The `DateTime` scalar type represents a date and time in the UTC
172+
# timezone. The DateTime appears in a JSON response as an ISO8601 formatted
173+
# string, including UTC timezone ("Z"). The parsed date and time string will
174+
# be converted to UTC and any UTC offset other than 0 will be rejected.
175+
# """
176+
# scalar :datetime, name: "DateTime" do
177+
# serialize &DateTime.to_iso8601/1
178+
# parse &parse_datetime/1
179+
# end
180+
181+
# @spec parse_datetime(Absinthe.Blueprint.Input.String.t) :: {:ok, DateTime.t} | :error
182+
# @spec parse_datetime(Absinthe.Blueprint.Input.Null.t) :: {:ok, nil}
183+
# defp parse_datetime(%Absinthe.Blueprint.Input.String{value: value}) do
184+
# case DateTime.from_iso8601(value) do
185+
# {:ok, datetime, 0} -> {:ok, datetime}
186+
# {:ok, _datetime, _offset} -> :error
187+
# _error -> :error
188+
# end
189+
# end
190+
# defp parse_datetime(%Absinthe.Blueprint.Input.Null{}) do
191+
# {:ok, nil}
192+
# end
193+
# defp parse_datetime(_) do
194+
# :error
195+
# end
169196
end

lib/mastani_server_web/schema/cms/cms_types.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ defmodule MastaniServerWeb.Schema.CMS.Types do
225225
interface(:article)
226226
field(:id, :id)
227227
field(:title, :string)
228+
field(:poster, :string)
229+
field(:thumbnil, :string)
228230
field(:desc, :string)
229231
field(:duration, :string)
230232
field(:author, :user, resolve: dataloader(CMS, :author))

lib/mastani_server_web/schema/cms/mutations/video.ex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Video do
88
@desc "create a video"
99
field :create_video, :video do
1010
arg(:title, non_null(:string))
11-
arg(:body, non_null(:string))
12-
arg(:digest, non_null(:string))
13-
arg(:length, non_null(:integer))
14-
arg(:link_addr, :string)
11+
arg(:poster, non_null(:string))
12+
arg(:thumbnil, non_null(:string))
13+
arg(:desc, non_null(:string))
14+
arg(:duration, non_null(:string))
15+
arg(:duration_sec, non_null(:integer))
16+
17+
arg(:source, non_null(:string))
18+
arg(:link, non_null(:string))
19+
arg(:original_author, non_null(:string))
20+
arg(:original_author_link, non_null(:string))
21+
arg(:publish_at, non_null(:datetime))
22+
1523
arg(:community_id, non_null(:id))
1624
arg(:thread, :cms_thread, default_value: :video)
1725
arg(:tags, list_of(:ids))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule MastaniServer.Repo.Migrations.AddThumbnilToVideos do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:cms_videos) do
6+
add(:thumbnil, :string)
7+
end
8+
end
9+
end
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
defmodule MastaniServer.Test.JobComment do
2+
# currently only test comments for job type, rename and seprherate later
3+
use MastaniServer.TestTools
4+
5+
alias Helper.ORM
6+
alias MastaniServer.CMS
7+
8+
alias CMS.{JobComment, JobCommentReply}
9+
10+
setup do
11+
{:ok, job} = db_insert(:job)
12+
{:ok, user} = db_insert(:user)
13+
14+
body = "this is a test comment"
15+
16+
{:ok, comment} = CMS.create_comment(:job, job.id, body, user)
17+
18+
{:ok, ~m(job user comment)a}
19+
end
20+
21+
describe "[comment CURD]" do
22+
test "login user comment to exsiting job", ~m(job user)a do
23+
body = "this is a test comment"
24+
25+
assert {:ok, comment} = CMS.create_comment(:job, job.id, body, user)
26+
27+
assert comment.job_id == job.id
28+
assert comment.body == body
29+
assert comment.author_id == user.id
30+
end
31+
32+
test "created comment should have a increased floor number", ~m(job user)a do
33+
body = "this is a test comment"
34+
35+
assert {:ok, comment1} = CMS.create_comment(:job, job.id, body, user)
36+
37+
{:ok, user2} = db_insert(:user)
38+
39+
assert {:ok, comment2} = CMS.create_comment(:job, job.id, body, user2)
40+
41+
assert comment1.floor == 2
42+
assert comment2.floor == 3
43+
end
44+
45+
test "create comment to non-exsit job fails", ~m(user)a do
46+
body = "this is a test comment"
47+
48+
assert {:error, _} = CMS.create_comment(:job, non_exsit_id(), body, user)
49+
end
50+
51+
test "can reply a comment, and reply should be in comment replies list", ~m(comment user)a do
52+
reply_body = "this is a reply comment"
53+
54+
{:ok, reply} = CMS.reply_comment(:job, comment.id, reply_body, user)
55+
56+
{:ok, reply_preload} = ORM.find(JobComment, reply.id, preload: :reply_to)
57+
{:ok, comment_preload} = ORM.find(JobComment, comment.id, preload: :replies)
58+
59+
assert reply_preload.reply_to.id == comment.id
60+
assert reply_preload.author_id == user.id
61+
assert reply_preload.body == reply_body
62+
# reply id should be in comments replies list
63+
assert comment_preload.replies |> Enum.any?(&(&1.reply_id == reply.id))
64+
end
65+
66+
test "comment can be deleted", ~m(job user)a do
67+
body = "this is a test comment"
68+
69+
assert {:ok, comment} = CMS.create_comment(:job, job.id, body, user)
70+
71+
{:ok, deleted} = CMS.delete_comment(:job, comment.id)
72+
assert deleted.id == comment.id
73+
end
74+
75+
test "after delete, the coments of id > deleted.id should decrease the floor number",
76+
~m(job user)a do
77+
body = "this is a test comment"
78+
# in setup we have a comment
79+
total = 30 + 1
80+
81+
comments =
82+
Enum.reduce(1..total, [], fn _, acc ->
83+
{:ok, value} = CMS.create_comment(:job, job.id, body, user)
84+
85+
acc ++ [value]
86+
end)
87+
88+
[comment_1, comment_2, comment_3, comment_last] = comments |> firstn_and_last(3)
89+
90+
assert comment_1.floor == 2
91+
assert comment_2.floor == 3
92+
assert comment_3.floor == 4
93+
assert comment_last.floor == total + 1
94+
95+
{:ok, _} = CMS.delete_comment(:job, comment_1.id)
96+
97+
{:ok, new_comment_2} = ORM.find(JobComment, comment_2.id)
98+
{:ok, new_comment_3} = ORM.find(JobComment, comment_3.id)
99+
{:ok, new_comment_last} = ORM.find(JobComment, comment_last.id)
100+
101+
assert new_comment_2.floor == 2
102+
assert new_comment_3.floor == 3
103+
assert new_comment_last.floor == total
104+
end
105+
106+
test "comment with replies should be deleted together", ~m(comment user)a do
107+
reply_body = "this is a reply comment"
108+
109+
{:ok, reply} = CMS.reply_comment(:job, comment.id, reply_body, user)
110+
111+
JobComment |> ORM.find_delete(comment.id)
112+
113+
{:error, _} = ORM.find(JobComment, comment.id)
114+
{:error, _} = ORM.find(JobComment, reply.id)
115+
116+
{:error, _} = JobCommentReply |> ORM.find_by(job_comment_id: comment.id, reply_id: reply.id)
117+
end
118+
119+
test "comments pagination should work", ~m(job user)a do
120+
body = "fake comment"
121+
122+
Enum.reduce(1..30, [], fn _, acc ->
123+
{:ok, value} = CMS.create_comment(:job, job.id, body, user)
124+
125+
acc ++ [value]
126+
end)
127+
128+
{:ok, results} = CMS.list_comments(:job, job.id, %{page: 1, size: 10})
129+
130+
assert results |> is_valid_pagination?(:raw)
131+
end
132+
133+
test "comment reply can be list one-by-one --> by replied user", ~m(comment)a do
134+
{:ok, user1} = db_insert(:user)
135+
{:ok, user2} = db_insert(:user)
136+
{:ok, user3} = db_insert(:user)
137+
138+
{:ok, _} = CMS.reply_comment(:job, comment.id, "reply by user1", user1)
139+
140+
{:ok, _} = CMS.reply_comment(:job, comment.id, "reply by user2", user2)
141+
142+
{:ok, _} = CMS.reply_comment(:job, comment.id, "reply by user3", user3)
143+
144+
{:ok, found_reply1} = CMS.list_replies(:job, comment.id, user1)
145+
assert user1.id == found_reply1 |> List.first() |> Map.get(:author_id)
146+
147+
{:ok, found_reply2} = CMS.list_replies(:job, comment.id, user2)
148+
assert user2.id == found_reply2 |> List.first() |> Map.get(:author_id)
149+
150+
{:ok, found_reply3} = CMS.list_replies(:job, comment.id, user3)
151+
assert user3.id == found_reply3 |> List.first() |> Map.get(:author_id)
152+
end
153+
end
154+
end

test/mastani_server/cms/comment_test.exs renamed to test/mastani_server/cms/post_comment_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule MastaniServer.Test.Comment do
1+
defmodule MastaniServer.Test.PostComment do
22
# currently only test comments for post type, rename and seprherate later
33
use MastaniServer.TestTools
44

0 commit comments

Comments
 (0)