improve containers context
This commit is contained in:
@ -1,12 +1,17 @@
|
||||
defmodule Cannery.ContainersTest do
|
||||
@moduledoc """
|
||||
Tests for the Containers context
|
||||
"""
|
||||
|
||||
use Cannery.DataCase
|
||||
|
||||
alias Cannery.Containers
|
||||
alias Cannery.{Accounts.User, Containers.Container}
|
||||
alias Ecto.Changeset
|
||||
|
||||
describe "containers" do
|
||||
alias Cannery.Containers.Container
|
||||
@moduletag :containers
|
||||
|
||||
describe "containers" do
|
||||
@valid_attrs %{
|
||||
"desc" => "some desc",
|
||||
"location" => "some location",
|
||||
@ -19,44 +24,47 @@ defmodule Cannery.ContainersTest do
|
||||
"name" => "some updated name",
|
||||
"type" => "some updated type"
|
||||
}
|
||||
@invalid_attrs %{desc: nil, location: nil, name: nil, type: nil}
|
||||
@invalid_attrs %{"desc" => nil, "location" => nil, "name" => nil, "type" => nil}
|
||||
|
||||
def container_fixture(attrs \\ %{}) do
|
||||
{:ok, container} =
|
||||
attrs
|
||||
|> Enum.into(@valid_attrs)
|
||||
|> Containers.create_container()
|
||||
@spec container_fixture(User.t(), map()) :: Container.t()
|
||||
def container_fixture(user, attrs \\ %{}) do
|
||||
{:ok, container} = @valid_attrs |> Map.merge(attrs) |> Containers.create_container(user)
|
||||
|
||||
container
|
||||
end
|
||||
|
||||
test "list_containers/0 returns all containers" do
|
||||
container = container_fixture()
|
||||
assert Containers.list_containers() == [container]
|
||||
test "list_containers/1 returns all containers" do
|
||||
user = user_fixture()
|
||||
container = user |> container_fixture()
|
||||
assert Containers.list_containers(user) == [container]
|
||||
end
|
||||
|
||||
test "get_container!/1 returns the container with given id" do
|
||||
container = container_fixture()
|
||||
container = user_fixture() |> container_fixture()
|
||||
assert Containers.get_container!(container.id) == container
|
||||
end
|
||||
|
||||
test "create_container/1 with valid data creates a container" do
|
||||
assert {:ok, %Container{} = container} = Containers.create_container(@valid_attrs)
|
||||
user = user_fixture()
|
||||
assert {:ok, %Container{} = container} = @valid_attrs |> Containers.create_container(user)
|
||||
assert container.desc == "some desc"
|
||||
assert container.location == "some location"
|
||||
assert container.name == "some name"
|
||||
assert container.type == "some type"
|
||||
assert container.user_id == user.id
|
||||
end
|
||||
|
||||
test "create_container/1 with invalid data returns error changeset" do
|
||||
assert {:error, %Changeset{}} = Containers.create_container(@invalid_attrs)
|
||||
assert {:error, %Changeset{}} =
|
||||
@invalid_attrs |> Containers.create_container(user_fixture())
|
||||
end
|
||||
|
||||
test "update_container/2 with valid data updates the container" do
|
||||
container = container_fixture()
|
||||
user = user_fixture()
|
||||
container = user |> container_fixture()
|
||||
|
||||
assert {:ok, %Container{} = container} =
|
||||
Containers.update_container(container, @update_attrs)
|
||||
Containers.update_container(container, user, @update_attrs)
|
||||
|
||||
assert container.desc == "some updated desc"
|
||||
assert container.location == "some updated location"
|
||||
@ -65,19 +73,21 @@ defmodule Cannery.ContainersTest do
|
||||
end
|
||||
|
||||
test "update_container/2 with invalid data returns error changeset" do
|
||||
container = container_fixture()
|
||||
assert {:error, %Changeset{}} = Containers.update_container(container, @invalid_attrs)
|
||||
user = user_fixture()
|
||||
container = user |> container_fixture()
|
||||
assert {:error, %Changeset{}} = Containers.update_container(container, user, @invalid_attrs)
|
||||
assert container == Containers.get_container!(container.id)
|
||||
end
|
||||
|
||||
test "delete_container/1 deletes the container" do
|
||||
container = container_fixture()
|
||||
assert {:ok, %Container{}} = Containers.delete_container(container)
|
||||
user = user_fixture()
|
||||
container = user |> container_fixture()
|
||||
assert {:ok, %Container{}} = Containers.delete_container(container, user)
|
||||
assert_raise Ecto.NoResultsError, fn -> Containers.get_container!(container.id) end
|
||||
end
|
||||
|
||||
test "change_container/1 returns a container changeset" do
|
||||
container = container_fixture()
|
||||
container = user_fixture() |> container_fixture()
|
||||
assert %Changeset{} = Containers.change_container(container)
|
||||
end
|
||||
end
|
||||
|
@ -1,9 +1,11 @@
|
||||
defmodule CanneryWeb.ContainerLiveTest do
|
||||
use CanneryWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
|
||||
import CanneryWeb.Gettext
|
||||
alias Cannery.Containers
|
||||
alias Cannery.{Accounts.User, Containers.Container}
|
||||
|
||||
@moduletag :containers_live
|
||||
|
||||
@create_attrs %{
|
||||
"desc" => "some desc",
|
||||
@ -19,13 +21,14 @@ defmodule CanneryWeb.ContainerLiveTest do
|
||||
}
|
||||
@invalid_attrs %{desc: nil, location: nil, name: nil, type: nil}
|
||||
|
||||
defp fixture(:container) do
|
||||
{:ok, container} = Containers.create_container(@create_attrs)
|
||||
@spec fixture(:container, User.t()) :: Container.t()
|
||||
defp fixture(:container, user) do
|
||||
{:ok, container} = Containers.create_container(@create_attrs, user)
|
||||
container
|
||||
end
|
||||
|
||||
defp create_container(_) do
|
||||
container = fixture(:container)
|
||||
defp create_container(%{user: user}) do
|
||||
container = fixture(:container, user)
|
||||
%{container: container}
|
||||
end
|
||||
|
||||
@ -35,15 +38,15 @@ defmodule CanneryWeb.ContainerLiveTest do
|
||||
test "lists all containers", %{conn: conn, container: container} do
|
||||
{:ok, _index_live, html} = live(conn, Routes.container_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Listing Containers"
|
||||
assert html =~ gettext("Listing Containers")
|
||||
assert html =~ container.desc
|
||||
end
|
||||
|
||||
test "saves new container", %{conn: conn} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("a", "New Container") |> render_click() =~
|
||||
"New Container"
|
||||
assert index_live |> element("a", gettext("New Container")) |> render_click() =~
|
||||
gettext("New Container")
|
||||
|
||||
assert_patch(index_live, Routes.container_index_path(conn, :new))
|
||||
|
||||
@ -57,7 +60,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Container created successfully"
|
||||
assert html =~ gettext("Container created successfully")
|
||||
assert html =~ "some desc"
|
||||
end
|
||||
|
||||
@ -65,7 +68,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
||||
{:ok, index_live, _html} = live(conn, Routes.container_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#container-#{container.id} a", "Edit") |> render_click() =~
|
||||
"Edit Container"
|
||||
gettext("Edit Container")
|
||||
|
||||
assert_patch(index_live, Routes.container_index_path(conn, :edit, container))
|
||||
|
||||
@ -79,7 +82,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.container_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Container updated successfully"
|
||||
assert html =~ gettext("Container updated successfully")
|
||||
assert html =~ "some updated desc"
|
||||
end
|
||||
|
||||
@ -97,7 +100,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
||||
test "displays container", %{conn: conn, container: container} do
|
||||
{:ok, _show_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
|
||||
|
||||
assert html =~ "Show Container"
|
||||
assert html =~ gettext("Show Container")
|
||||
assert html =~ container.desc
|
||||
end
|
||||
|
||||
@ -105,7 +108,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
||||
{:ok, show_live, _html} = live(conn, Routes.container_show_path(conn, :show, container))
|
||||
|
||||
assert show_live |> element("a", "Edit") |> render_click() =~
|
||||
"Edit Container"
|
||||
gettext("Edit Container")
|
||||
|
||||
assert_patch(show_live, Routes.container_show_path(conn, :edit, container))
|
||||
|
||||
@ -119,7 +122,7 @@ defmodule CanneryWeb.ContainerLiveTest do
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.container_show_path(conn, :show, container))
|
||||
|
||||
assert html =~ "Container updated successfully"
|
||||
assert html =~ gettext("Container updated successfully")
|
||||
assert html =~ "some updated desc"
|
||||
end
|
||||
end
|
||||
|
@ -25,6 +25,7 @@ defmodule Cannery.DataCase do
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
import Cannery.DataCase
|
||||
import Cannery.AccountsFixtures
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -15,8 +15,8 @@ defmodule Cannery.AccountsFixtures do
|
||||
{:ok, user} =
|
||||
attrs
|
||||
|> Enum.into(%{
|
||||
email: unique_user_email(),
|
||||
password: valid_user_password()
|
||||
"email" => unique_user_email(),
|
||||
"password" => valid_user_password()
|
||||
})
|
||||
|> Accounts.register_user()
|
||||
|
||||
|
Reference in New Issue
Block a user