cannery/test/cannery/containers_test.exs

92 lines
3.2 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule Cannery.ContainersTest do
2022-02-08 22:10:48 -05:00
@moduledoc """
Tests for the Containers context
"""
2021-09-02 23:31:14 -04:00
use Cannery.DataCase
alias Cannery.Containers
2022-02-16 22:18:05 -05:00
alias Cannery.{Containers.Container}
2022-02-01 00:22:44 -05:00
alias Ecto.Changeset
2021-09-02 23:31:14 -04:00
2022-02-16 20:49:33 -05:00
@moduletag :containers_test
@valid_attrs %{
"desc" => "some desc",
"location" => "some location",
"name" => "some name",
"type" => "some type"
}
@update_attrs %{
"desc" => "some updated desc",
"location" => "some updated location",
"name" => "some updated name",
"type" => "some updated type"
}
@invalid_attrs %{"desc" => nil, "location" => nil, "name" => nil, "type" => nil}
2021-09-02 23:31:14 -04:00
2022-02-08 22:10:48 -05:00
describe "containers" do
2022-02-16 20:49:33 -05:00
setup do
current_user = user_fixture()
container = container_fixture(current_user)
[current_user: current_user, container: container]
2021-09-02 23:31:14 -04:00
end
2022-02-16 20:49:33 -05:00
test "list_containers/1 returns all containers",
%{current_user: current_user, container: container} do
2022-02-19 00:31:17 -05:00
assert Containers.list_containers(current_user) ==
[container |> Repo.preload([:ammo_groups, :tags])]
2021-09-02 23:31:14 -04:00
end
2022-02-16 20:49:33 -05:00
test "get_container!/1 returns the container with given id",
%{current_user: current_user, container: container} do
2022-02-19 00:31:17 -05:00
assert Containers.get_container!(container.id, current_user) ==
container |> Repo.preload([:ammo_groups, :tags])
2021-09-02 23:31:14 -04:00
end
2022-02-16 22:18:05 -05:00
test "create_container/1 with valid data creates a container", %{current_user: current_user} do
2022-02-16 20:49:33 -05:00
assert {:ok, %Container{} = container} =
@valid_attrs |> Containers.create_container(current_user)
2021-09-02 23:31:14 -04:00
assert container.desc == "some desc"
assert container.location == "some location"
assert container.name == "some name"
assert container.type == "some type"
2022-02-16 20:49:33 -05:00
assert container.user_id == current_user.id
2021-09-02 23:31:14 -04:00
end
2022-02-16 20:49:33 -05:00
test "create_container/1 with invalid data returns error changeset",
%{current_user: current_user} do
assert {:error, %Changeset{}} = @invalid_attrs |> Containers.create_container(current_user)
2021-09-02 23:31:14 -04:00
end
2022-02-16 20:49:33 -05:00
test "update_container/2 with valid data updates the container",
%{current_user: current_user, container: container} do
2022-01-22 17:21:13 -05:00
assert {:ok, %Container{} = container} =
2022-02-16 20:49:33 -05:00
Containers.update_container(container, current_user, @update_attrs)
2022-01-22 17:21:13 -05:00
2021-09-02 23:31:14 -04:00
assert container.desc == "some updated desc"
assert container.location == "some updated location"
assert container.name == "some updated name"
assert container.type == "some updated type"
end
2022-02-16 20:49:33 -05:00
test "update_container/2 with invalid data returns error changeset",
%{current_user: current_user, container: container} do
assert {:error, %Changeset{}} =
Containers.update_container(container, current_user, @invalid_attrs)
2022-02-19 00:31:17 -05:00
assert container |> Repo.preload([:ammo_groups, :tags]) ==
Containers.get_container!(container.id, current_user)
2021-09-02 23:31:14 -04:00
end
2022-02-16 20:49:33 -05:00
test "delete_container/1 deletes the container",
%{current_user: current_user, container: container} do
assert {:ok, %Container{}} = Containers.delete_container(container, current_user)
assert_raise Ecto.NoResultsError, fn ->
Containers.get_container!(container.id, current_user)
end
2021-09-02 23:31:14 -04:00
end
end
end