|
| 1 | +defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + use GroupherServer.TestTools |
| 5 | + |
| 6 | + alias Helper.ORM |
| 7 | + alias GroupherServer.CMS |
| 8 | + |
| 9 | + alias CMS.{ArticleComment, Embeds, ArticleCommentUserEmotion} |
| 10 | + |
| 11 | + @default_emotions Embeds.ArticleCommentEmotion.default_emotions() |
| 12 | + |
| 13 | + setup do |
| 14 | + {:ok, user} = db_insert(:user) |
| 15 | + {:ok, user2} = db_insert(:user) |
| 16 | + {:ok, user3} = db_insert(:user) |
| 17 | + |
| 18 | + {:ok, job} = db_insert(:job) |
| 19 | + |
| 20 | + {:ok, ~m(user user2 user3 job)a} |
| 21 | + end |
| 22 | + |
| 23 | + describe "[emotion in paged article comment]" do |
| 24 | + @tag :wip3 |
| 25 | + test "login user should got viewer has emotioned status", ~m(job user)a do |
| 26 | + total_count = 0 |
| 27 | + page_number = 10 |
| 28 | + page_size = 20 |
| 29 | + |
| 30 | + all_comment = |
| 31 | + Enum.reduce(0..total_count, [], fn _, acc -> |
| 32 | + {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) |
| 33 | + acc ++ [comment] |
| 34 | + end) |
| 35 | + |
| 36 | + first_comment = List.first(all_comment) |
| 37 | + |
| 38 | + {:ok, _} = CMS.emotion_to_comment(first_comment.id, :downvote, user) |
| 39 | + {:ok, _} = CMS.emotion_to_comment(first_comment.id, :beer, user) |
| 40 | + {:ok, _} = CMS.emotion_to_comment(first_comment.id, :popcorn, user) |
| 41 | + |
| 42 | + {:ok, paged_comments} = |
| 43 | + CMS.list_article_comments( |
| 44 | + :job, |
| 45 | + job.id, |
| 46 | + %{page: page_number, size: page_size}, |
| 47 | + :replies, |
| 48 | + user |
| 49 | + ) |
| 50 | + |
| 51 | + target = Enum.find(paged_comments.entries, &(&1.id == first_comment.id)) |
| 52 | + |
| 53 | + assert target.emotions.downvote_count == 1 |
| 54 | + assert user_exist_in?(user, target.emotions.latest_downvote_users) |
| 55 | + assert target.emotions.viewer_has_downvoteed |
| 56 | + |
| 57 | + assert target.emotions.beer_count == 1 |
| 58 | + assert user_exist_in?(user, target.emotions.latest_beer_users) |
| 59 | + assert target.emotions.viewer_has_beered |
| 60 | + |
| 61 | + assert target.emotions.popcorn_count == 1 |
| 62 | + assert user_exist_in?(user, target.emotions.latest_popcorn_users) |
| 63 | + assert target.emotions.viewer_has_popcorned |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + describe "[basic article comment emotion]" do |
| 68 | + @tag :wip |
| 69 | + test "comment has default emotions after created", ~m(job user)a do |
| 70 | + parent_content = "parent comment" |
| 71 | + |
| 72 | + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) |
| 73 | + {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) |
| 74 | + |
| 75 | + emotions = parent_comment.emotions |> Map.from_struct() |> Map.delete(:id) |
| 76 | + assert @default_emotions == emotions |
| 77 | + end |
| 78 | + |
| 79 | + @tag :wip2 |
| 80 | + test "can make emotion to comment", ~m(job user user2)a do |
| 81 | + parent_content = "parent comment" |
| 82 | + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) |
| 83 | + |
| 84 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) |
| 85 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user2) |
| 86 | + |
| 87 | + {:ok, %{emotions: emotions}} = ORM.find(ArticleComment, parent_comment.id) |
| 88 | + |
| 89 | + assert emotions.downvote_count == 2 |
| 90 | + assert user_exist_in?(user, emotions.latest_downvote_users) |
| 91 | + assert user_exist_in?(user2, emotions.latest_downvote_users) |
| 92 | + end |
| 93 | + |
| 94 | + @tag :wip2 |
| 95 | + test "can undo emotion to comment", ~m(job user user2)a do |
| 96 | + parent_content = "parent comment" |
| 97 | + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) |
| 98 | + |
| 99 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) |
| 100 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user2) |
| 101 | + |
| 102 | + {:ok, %{emotions: emotions}} = ORM.find(ArticleComment, parent_comment.id) |
| 103 | + |
| 104 | + assert emotions.downvote_count == 2 |
| 105 | + assert user_exist_in?(user, emotions.latest_downvote_users) |
| 106 | + assert user_exist_in?(user2, emotions.latest_downvote_users) |
| 107 | + |
| 108 | + {:ok, _} = CMS.undo_emotion_to_comment(parent_comment.id, :downvote, user) |
| 109 | + {:ok, _} = CMS.undo_emotion_to_comment(parent_comment.id, :downvote, user2) |
| 110 | + |
| 111 | + {:ok, %{emotions: emotions}} = ORM.find(ArticleComment, parent_comment.id) |
| 112 | + assert emotions.downvote_count == 0 |
| 113 | + assert not user_exist_in?(user, emotions.latest_downvote_users) |
| 114 | + assert not user_exist_in?(user2, emotions.latest_downvote_users) |
| 115 | + end |
| 116 | + |
| 117 | + @tag :wip2 |
| 118 | + test "same user make same emotion to same comment.", ~m(job user)a do |
| 119 | + parent_content = "parent comment" |
| 120 | + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) |
| 121 | + |
| 122 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) |
| 123 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) |
| 124 | + |
| 125 | + {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) |
| 126 | + |
| 127 | + assert parent_comment.emotions.downvote_count == 1 |
| 128 | + assert user_exist_in?(user, parent_comment.emotions.latest_downvote_users) |
| 129 | + end |
| 130 | + |
| 131 | + @tag :wip2 |
| 132 | + test "same user same emotion to same comment only have one user_emotion record", |
| 133 | + ~m(job user)a do |
| 134 | + parent_content = "parent comment" |
| 135 | + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) |
| 136 | + |
| 137 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) |
| 138 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :heart, user) |
| 139 | + |
| 140 | + {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) |
| 141 | + |
| 142 | + {:ok, records} = ORM.find_all(ArticleCommentUserEmotion, %{page: 1, size: 10}) |
| 143 | + assert records.total_count == 1 |
| 144 | + |
| 145 | + {:ok, record} = |
| 146 | + ORM.find_by(ArticleCommentUserEmotion, %{ |
| 147 | + article_comment_id: parent_comment.id, |
| 148 | + user_id: user.id |
| 149 | + }) |
| 150 | + |
| 151 | + assert record.downvote |
| 152 | + assert record.heart |
| 153 | + end |
| 154 | + |
| 155 | + @tag :wip2 |
| 156 | + test "different user can make same emotions on same comment", ~m(job user user2 user3)a do |
| 157 | + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent comment", user) |
| 158 | + |
| 159 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user) |
| 160 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user2) |
| 161 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user3) |
| 162 | + |
| 163 | + {:ok, %{emotions: emotions}} = ORM.find(ArticleComment, parent_comment.id) |
| 164 | + # IO.inspect(emotions, label: "the parent_comment") |
| 165 | + |
| 166 | + assert emotions.beer_count == 3 |
| 167 | + assert user_exist_in?(user, emotions.latest_beer_users) |
| 168 | + assert user_exist_in?(user2, emotions.latest_beer_users) |
| 169 | + assert user_exist_in?(user3, emotions.latest_beer_users) |
| 170 | + end |
| 171 | + |
| 172 | + @tag :wip |
| 173 | + test "same user can make differcent emotions on same comment", ~m(job user)a do |
| 174 | + parent_content = "parent comment" |
| 175 | + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) |
| 176 | + |
| 177 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) |
| 178 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) |
| 179 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user) |
| 180 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :heart, user) |
| 181 | + {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :orz, user) |
| 182 | + |
| 183 | + {:ok, %{emotions: emotions}} = ORM.find(ArticleComment, parent_comment.id) |
| 184 | + |
| 185 | + assert emotions.downvote_count == 1 |
| 186 | + assert user_exist_in?(user, emotions.latest_downvote_users) |
| 187 | + |
| 188 | + assert emotions.beer_count == 1 |
| 189 | + assert user_exist_in?(user, emotions.latest_beer_users) |
| 190 | + |
| 191 | + assert emotions.heart_count == 1 |
| 192 | + assert user_exist_in?(user, emotions.latest_heart_users) |
| 193 | + |
| 194 | + assert emotions.orz_count == 1 |
| 195 | + assert user_exist_in?(user, emotions.latest_orz_users) |
| 196 | + |
| 197 | + assert emotions.pill_count == 0 |
| 198 | + assert not user_exist_in?(user, emotions.latest_pill_users) |
| 199 | + |
| 200 | + assert emotions.biceps_count == 0 |
| 201 | + assert not user_exist_in?(user, emotions.latest_biceps_users) |
| 202 | + |
| 203 | + assert emotions.confused_count == 0 |
| 204 | + assert not user_exist_in?(user, emotions.latest_confused_users) |
| 205 | + end |
| 206 | + end |
| 207 | +end |
0 commit comments