From c27c162562403cd8e82e521aedcb5ac026c7bf20 Mon Sep 17 00:00:00 2001 From: shibao Date: Tue, 8 Feb 2022 22:10:48 -0500 Subject: [PATCH] improve containers context --- lib/cannery/containers.ex | 40 +++++++------- lib/cannery/containers/container.ex | 14 ++++- .../live/container_live/form_component.ex | 12 +++-- lib/cannery_web/live/container_live/index.ex | 2 +- lib/cannery_web/live/container_live/show.ex | 2 +- priv/gettext/errors.pot | 2 +- test/cannery/containers_test.exs | 52 +++++++++++-------- test/cannery_web/live/container_live_test.exs | 33 ++++++------ test/support/data_case.ex | 1 + test/support/fixtures/accounts_fixtures.ex | 4 +- 10 files changed, 97 insertions(+), 65 deletions(-) diff --git a/lib/cannery/containers.ex b/lib/cannery/containers.ex index 18e3ad1..6eefb72 100644 --- a/lib/cannery/containers.ex +++ b/lib/cannery/containers.ex @@ -44,17 +44,18 @@ defmodule Cannery.Containers do ## Examples - iex> create_container(%{field: value}) + iex> create_container(%{field: value}, user) {:ok, %Container{}} - iex> create_container(%{field: bad_value}) + iex> create_container(%{field: bad_value}, user) {:error, %Changeset{}} """ - @spec create_container(attrs :: map()) :: + @spec create_container(attrs :: map(), User.t()) :: {:ok, Container.t()} | {:error, Changeset.t(Container.new_container())} - def create_container(attrs) do - %Container{} |> Container.changeset(attrs) |> Repo.insert() + def create_container(attrs, %User{id: user_id}) do + attrs = attrs |> Map.put("user_id", user_id) + %Container{} |> Container.create_changeset(attrs) |> Repo.insert() end @doc """ @@ -62,17 +63,17 @@ defmodule Cannery.Containers do ## Examples - iex> update_container(container, %{field: new_value}) + iex> update_container(container, user, %{field: new_value}) {:ok, %Container{}} - iex> update_container(container, %{field: bad_value}) + iex> update_container(container, user, %{field: bad_value}) {:error, %Changeset{}} """ - @spec update_container(Container.t(), attrs :: map()) :: + @spec update_container(Container.t(), User.t(), attrs :: map()) :: {:ok, Container.t()} | {:error, Changeset.t(Container.t())} - def update_container(container, attrs) do - container |> Container.changeset(attrs) |> Repo.update() + def update_container(%Container{user_id: user_id} = container, %User{id: user_id}, attrs) do + container |> Container.update_changeset(attrs) |> Repo.update() end @doc """ @@ -80,16 +81,16 @@ defmodule Cannery.Containers do ## Examples - iex> delete_container(container) + iex> delete_container(container, user) {:ok, %Container{}} - iex> delete_container(container) + iex> delete_container(container, user) {:error, %Changeset{}} """ - @spec delete_container(Container.t()) :: + @spec delete_container(Container.t(), User.t()) :: {:ok, Container.t()} | {:error, Changeset.t(Container.t())} - def delete_container(container) do + def delete_container(%Container{user_id: user_id} = container, %User{id: user_id}) do Repo.one( from ag in AmmoGroup, where: ag.container_id == ^container.id, @@ -125,13 +126,13 @@ defmodule Cannery.Containers do ## Examples - iex> delete_container(container) + iex> delete_container(container, user) %Container{} """ - @spec delete_container!(Container.t()) :: Container.t() - def delete_container!(container) do - {:ok, container} = container |> delete_container() + @spec delete_container!(Container.t(), User.t()) :: Container.t() + def delete_container!(container, user) do + {:ok, container} = container |> delete_container(user) container end @@ -151,7 +152,8 @@ defmodule Cannery.Containers do Changeset.t(Container.t() | Container.new_container()) @spec change_container(Container.t() | Container.new_container(), attrs :: map()) :: Changeset.t(Container.t() | Container.new_container()) - def change_container(container, attrs \\ %{}), do: container |> Container.changeset(attrs) + def change_container(container, attrs \\ %{}), + do: container |> Container.update_changeset(attrs) @doc """ Adds a tag to a container diff --git a/lib/cannery/containers/container.ex b/lib/cannery/containers/container.ex index d48c511..8b3f0c5 100644 --- a/lib/cannery/containers/container.ex +++ b/lib/cannery/containers/container.ex @@ -39,10 +39,20 @@ defmodule Cannery.Containers.Container do @type id :: UUID.t() @doc false - @spec changeset(t() | new_container(), attrs :: map()) :: Changeset.t(t() | new_container()) - def changeset(container, attrs) do + @spec create_changeset(t() | new_container(), attrs :: map()) :: + Changeset.t(t() | new_container()) + def create_changeset(container, attrs) do container |> cast(attrs, [:name, :desc, :type, :location, :user_id]) |> validate_required([:name, :type, :user_id]) end + + @doc false + @spec update_changeset(t() | new_container(), attrs :: map()) :: + Changeset.t(t() | new_container()) + def update_changeset(container, attrs) do + container + |> cast(attrs, [:name, :desc, :type, :location]) + |> validate_required([:name, :type]) + end end diff --git a/lib/cannery_web/live/container_live/form_component.ex b/lib/cannery_web/live/container_live/form_component.ex index 6342c59..d7f4092 100644 --- a/lib/cannery_web/live/container_live/form_component.ex +++ b/lib/cannery_web/live/container_live/form_component.ex @@ -22,7 +22,6 @@ defmodule CanneryWeb.ContainerLive.FormComponent do end def handle_event("save", %{"container" => container_params}, socket) do - container_params = container_params |> Map.put("user_id", socket.assigns.current_user.id) save_container(socket, socket.assigns.action, container_params) end @@ -88,7 +87,12 @@ defmodule CanneryWeb.ContainerLive.FormComponent do end defp save_container(socket, :edit, container_params) do - case Containers.update_container(socket.assigns.container, container_params) do + Containers.update_container( + socket.assigns.container, + socket.assigns.current_user, + container_params + ) + |> case do {:ok, _container} -> {:noreply, socket @@ -101,7 +105,9 @@ defmodule CanneryWeb.ContainerLive.FormComponent do end defp save_container(socket, :new, container_params) do - case Containers.create_container(container_params) do + container_params + |> Containers.create_container(socket.assigns.current_user) + |> case do {:ok, _container} -> {:noreply, socket diff --git a/lib/cannery_web/live/container_live/index.ex b/lib/cannery_web/live/container_live/index.ex index 32c1d09..bb1d6c1 100644 --- a/lib/cannery_web/live/container_live/index.ex +++ b/lib/cannery_web/live/container_live/index.ex @@ -46,7 +46,7 @@ defmodule CanneryWeb.ContainerLive.Index do container -> container - |> Containers.delete_container() + |> Containers.delete_container(socket.assigns.current_user) |> case do {:ok, container} -> socket diff --git a/lib/cannery_web/live/container_live/show.ex b/lib/cannery_web/live/container_live/show.ex index d16cecc..254db7a 100644 --- a/lib/cannery_web/live/container_live/show.ex +++ b/lib/cannery_web/live/container_live/show.ex @@ -28,7 +28,7 @@ defmodule CanneryWeb.ContainerLive.Show do def handle_event("delete", _, socket) do socket = socket.assigns.container - |> Containers.delete_container() + |> Containers.delete_container(socket.assigns.current_user) |> case do {:ok, container} -> socket diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index 7b040f8..29f2e55 100644 --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -94,7 +94,7 @@ msgid "must be equal to %{number}" msgstr "" #, elixir-format, ex-autogen -#: lib/cannery/containers.ex:104 +#: lib/cannery/containers.ex:105 msgid "There is still %{amount} ammo group in this container!" msgid_plural "There are still %{amount} ammo groups in this container!" msgstr[0] "" diff --git a/test/cannery/containers_test.exs b/test/cannery/containers_test.exs index 24b90cd..5d7a219 100644 --- a/test/cannery/containers_test.exs +++ b/test/cannery/containers_test.exs @@ -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 diff --git a/test/cannery_web/live/container_live_test.exs b/test/cannery_web/live/container_live_test.exs index 7a51499..c1fba86 100644 --- a/test/cannery_web/live/container_live_test.exs +++ b/test/cannery_web/live/container_live_test.exs @@ -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 diff --git a/test/support/data_case.ex b/test/support/data_case.ex index d336fc5..784d2cc 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -25,6 +25,7 @@ defmodule Cannery.DataCase do import Ecto.Changeset import Ecto.Query import Cannery.DataCase + import Cannery.AccountsFixtures end end diff --git a/test/support/fixtures/accounts_fixtures.ex b/test/support/fixtures/accounts_fixtures.ex index bef7855..b0e6dad 100644 --- a/test/support/fixtures/accounts_fixtures.ex +++ b/test/support/fixtures/accounts_fixtures.ex @@ -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()