2020-07-09 17:24:28 +02:00
|
|
|
defmodule Mobilizon.DiscussionsTest do
|
2020-02-18 08:57:00 +01:00
|
|
|
use Mobilizon.DataCase
|
|
|
|
|
|
|
|
import Mobilizon.Factory
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Discussions
|
|
|
|
alias Mobilizon.Discussions.Comment
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
describe "comments" do
|
|
|
|
@valid_attrs %{text: "some text"}
|
|
|
|
@update_attrs %{text: "some updated text"}
|
|
|
|
@invalid_attrs %{text: nil, url: nil}
|
|
|
|
|
|
|
|
test "get_comment!/1 returns the comment with given id" do
|
|
|
|
%Comment{id: comment_id} = insert(:comment)
|
2020-07-09 17:24:28 +02:00
|
|
|
comment_fetched = Discussions.get_comment!(comment_id)
|
2020-02-18 08:57:00 +01:00
|
|
|
assert comment_fetched.id == comment_id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create_comment/1 with valid data creates a comment" do
|
|
|
|
%Actor{} = actor = insert(:actor)
|
|
|
|
comment_data = Map.merge(@valid_attrs, %{actor_id: actor.id})
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
case Discussions.create_comment(comment_data) do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, %Comment{} = comment} ->
|
|
|
|
assert comment.text == "some text"
|
|
|
|
assert comment.actor_id == actor.id
|
|
|
|
|
|
|
|
err ->
|
|
|
|
flunk("Failed to create a comment #{inspect(err)}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create_comment/1 with invalid data returns error changeset" do
|
2020-07-09 17:24:28 +02:00
|
|
|
assert {:error, %Ecto.Changeset{}} = Discussions.create_comment(@invalid_attrs)
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "update_comment/2 with valid data updates the comment" do
|
|
|
|
%Comment{} = comment = insert(:comment)
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
case Discussions.update_comment(comment, @update_attrs) do
|
2020-02-18 08:57:00 +01:00
|
|
|
{:ok, %Comment{} = comment} ->
|
|
|
|
assert comment.text == "some updated text"
|
|
|
|
|
|
|
|
err ->
|
|
|
|
flunk("Failed to update a comment #{inspect(err)}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "update_comment/2 with invalid data returns error changeset" do
|
|
|
|
%Comment{} = comment = insert(:comment)
|
2020-07-09 17:24:28 +02:00
|
|
|
assert {:error, %Ecto.Changeset{}} = Discussions.update_comment(comment, @invalid_attrs)
|
|
|
|
%Comment{} = comment_fetched = Discussions.get_comment!(comment.id)
|
2020-11-26 11:41:13 +01:00
|
|
|
assert comment.text == comment_fetched.text
|
|
|
|
assert comment.url == comment_fetched.url
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_comment/1 deletes the comment" do
|
|
|
|
%Comment{} = comment = insert(:comment)
|
2020-07-09 17:24:28 +02:00
|
|
|
assert {:ok, %Comment{}} = Discussions.delete_comment(comment)
|
|
|
|
refute is_nil(Discussions.get_comment!(comment.id).deleted_at)
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|