Files
Claper/test/claper/polls_test.exs
2023-04-20 17:15:31 +02:00

142 lines
4.8 KiB
Elixir

defmodule Claper.PollsTest do
use Claper.DataCase
alias Claper.Polls
describe "polls" do
alias Claper.Polls.Poll
import Claper.{PollsFixtures, PresentationsFixtures}
@invalid_attrs %{title: nil}
test "list_polls/1 returns all polls from a presentation" do
presentation_file = presentation_file_fixture()
poll = poll_fixture(%{presentation_file_id: presentation_file.id})
assert Polls.list_polls(presentation_file.id) == [poll]
end
test "list_polls_at_position/2 returns all polls from a presentation at a given position" do
presentation_file = presentation_file_fixture()
poll = poll_fixture(%{presentation_file_id: presentation_file.id, position: 5})
assert Polls.list_polls_at_position(presentation_file.id, 5) == [poll]
end
test "get_poll!/1 returns the poll with given id" do
presentation_file = presentation_file_fixture()
poll =
poll_fixture(%{presentation_file_id: presentation_file.id})
|> Claper.Polls.set_percentages()
assert Polls.get_poll!(poll.id) == poll
end
test "create_poll/1 with valid data creates a poll" do
presentation_file = presentation_file_fixture()
valid_attrs = %{
title: "some title",
presentation_file_id: presentation_file.id,
position: 0,
poll_opts: [
%{content: "some option 1", vote_count: 0},
%{content: "some option 2", vote_count: 0}
]
}
assert {:ok, %Poll{} = poll} = Polls.create_poll(valid_attrs)
assert poll.title == "some title"
end
test "create_poll/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Polls.create_poll(@invalid_attrs)
end
test "update_poll/3 with valid data updates the poll" do
presentation_file = presentation_file_fixture()
poll = poll_fixture(%{presentation_file_id: presentation_file.id})
update_attrs = %{title: "some updated title"}
assert {:ok, %Poll{} = poll} =
Polls.update_poll(presentation_file.event_id, poll, update_attrs)
assert poll.title == "some updated title"
end
test "update_poll/3 with invalid data returns error changeset" do
presentation_file = presentation_file_fixture()
poll = poll_fixture(%{presentation_file_id: presentation_file.id})
assert {:error, %Ecto.Changeset{}} =
Polls.update_poll(presentation_file.event_id, poll, @invalid_attrs)
assert poll |> Claper.Polls.set_percentages() == Polls.get_poll!(poll.id)
end
test "delete_poll/2 deletes the poll" do
presentation_file = presentation_file_fixture()
poll = poll_fixture(%{presentation_file_id: presentation_file.id})
assert {:ok, %Poll{}} = Polls.delete_poll(presentation_file.event_id, poll)
assert_raise Ecto.NoResultsError, fn -> Polls.get_poll!(poll.id) end
end
test "change_poll/1 returns a poll changeset" do
presentation_file = presentation_file_fixture()
poll = poll_fixture(%{presentation_file_id: presentation_file.id})
assert %Ecto.Changeset{} = Polls.change_poll(poll)
end
end
describe "poll_opts" do
import Claper.{PollsFixtures, PresentationsFixtures}
test "add_poll_opt/1 returns poll changeset plus the added poll_opt" do
presentation_file = presentation_file_fixture()
poll = poll_fixture(%{presentation_file_id: presentation_file.id})
poll_changeset = poll |> Polls.change_poll()
assert Ecto.Changeset.get_field(Polls.add_poll_opt(poll_changeset), :poll_opts)
|> Enum.count() == 3
end
test "remove_poll_opt/2 returns poll changeset minus the removed poll_opt" do
presentation_file = presentation_file_fixture()
poll = poll_fixture(%{presentation_file_id: presentation_file.id})
poll_changeset = poll |> Polls.change_poll()
assert Ecto.Changeset.get_field(
Polls.remove_poll_opt(poll_changeset, Enum.at(poll.poll_opts, 0)),
:poll_opts
)
|> Enum.count() == 1
end
end
describe "poll_votes" do
import Claper.{PollsFixtures, PresentationsFixtures}
test "get_poll_vote/2 returns the poll_vote with given id and user id" do
poll_vote = poll_vote_fixture()
assert Polls.get_poll_vote(poll_vote.user_id, poll_vote.poll_id) == [poll_vote]
end
test "vote/4 with valid data creates a poll_vote" do
presentation_file = presentation_file_fixture(%{}, [:event])
poll = poll_fixture(%{presentation_file_id: presentation_file.id})
[poll_opt | _] = poll.poll_opts
assert {:ok, %Polls.Poll{}} =
Polls.vote(
presentation_file.event.user_id,
presentation_file.event_id,
[poll_opt],
poll.id
)
end
end
end