|
| 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 |
0 commit comments