From 68f556225d568e2e3e90ff8092f8041f8fb0b6c5 Mon Sep 17 00:00:00 2001 From: SimonLab Date: Sat, 8 Oct 2022 15:32:11 +0100 Subject: [PATCH] Add item controller test for lists - Add test for item controller managing lists - Fix https://github.com/dwyl/mvp/issues/168 --- lib/app/item.ex | 3 - lib/app_web/controllers/item_controller.ex | 15 ++--- lib/app_web/router.ex | 3 +- .../migrations/20221005142257_add_list.exs | 2 +- test/app/item_test.exs | 1 - test/app/list_test.exs | 4 +- .../controllers/item_controller_test.exs | 55 +++++++++++++------ 7 files changed, 47 insertions(+), 36 deletions(-) diff --git a/lib/app/item.ex b/lib/app/item.ex index 448d621a..13496cc5 100644 --- a/lib/app/item.ex +++ b/lib/app/item.ex @@ -31,7 +31,6 @@ defmodule App.Item do end def changeset_with_lists(item, list_ids) do - # get list based on ids lists = Repo.all(from l in L, where: l.id in ^list_ids) item @@ -182,10 +181,8 @@ defmodule App.Item do accumulate_item_timers(values) |> Enum.map(fn t -> Map.put(t, :tags, items_tags[t.id].tags) - # Map.put(t, :lists, items_tags[t.id].lists) end) |> Enum.map(fn t -> - # Map.put(t, :tags, items_tags[t.id].tags) Map.put(t, :lists, items_tags[t.id].lists) end) end diff --git a/lib/app_web/controllers/item_controller.ex b/lib/app_web/controllers/item_controller.ex index f8ad1b05..e693c9ee 100644 --- a/lib/app_web/controllers/item_controller.ex +++ b/lib/app_web/controllers/item_controller.ex @@ -20,7 +20,6 @@ defmodule AppWeb.ItemController do end def update(conn, %{"id" => id} = params) do - person_id = conn.assigns[:person][:id] || 0 item = Item.get_item!(id) list_ids = @@ -29,16 +28,10 @@ defmodule AppWeb.ItemController do ids -> ids end - case Item.update_item_with_lists(item, list_ids) do - {:ok, _item} -> - conn - |> put_flash(:info, "Item's list updated successfully.") - |> redirect(to: "/") - - {:error, %Ecto.Changeset{} = changeset} -> - lists = List.list_person_lists(person_id) |> Enum.map(&{&1.name, &1.id}) + {:ok, _item} = Item.update_item_with_lists(item, list_ids) - render(conn, "edit.html", item: item, lists: lists, changeset: changeset) - end + conn + |> put_flash(:info, "Item's list updated successfully.") + |> redirect(to: "/") end end diff --git a/lib/app_web/router.ex b/lib/app_web/router.ex index a6ed78d7..5b925068 100644 --- a/lib/app_web/router.ex +++ b/lib/app_web/router.ex @@ -1,6 +1,6 @@ defmodule AppWeb.Router do use AppWeb, :router - alias App.{Person, Repo} + alias App.Person pipeline :browser do plug :accepts, ["html"] @@ -52,7 +52,6 @@ defmodule AppWeb.Router do # add name to their profile for sharing items feature defp profile_name(conn, _opts) do person_id = conn.assigns[:person][:id] || 0 - # person = Person.get_person!(person_id) person = Person.get_or_insert(person_id) diff --git a/priv/repo/migrations/20221005142257_add_list.exs b/priv/repo/migrations/20221005142257_add_list.exs index 71a3fa4e..58cc1316 100644 --- a/priv/repo/migrations/20221005142257_add_list.exs +++ b/priv/repo/migrations/20221005142257_add_list.exs @@ -19,7 +19,7 @@ defmodule App.Repo.Migrations.AddList do create table(:lists_items, primary_key: false) do add(:item_id, references(:items, on_delete: :delete_all)) - add(:list_id, references(:tags, on_delete: :delete_all)) + add(:list_id, references(:lists, on_delete: :delete_all)) timestamps() end diff --git a/test/app/item_test.exs b/test/app/item_test.exs index 4ca5abf0..d1f9152e 100644 --- a/test/app/item_test.exs +++ b/test/app/item_test.exs @@ -1,7 +1,6 @@ defmodule App.ItemTest do use App.DataCase, async: false alias App.{Item, Person, Timer} - alias App.List, as: L setup [:create_person] diff --git a/test/app/list_test.exs b/test/app/list_test.exs index 51cb93db..acfb3b1a 100644 --- a/test/app/list_test.exs +++ b/test/app/list_test.exs @@ -42,8 +42,8 @@ defmodule App.ListTest do end test "get lists from ids" do - {:ok, _list} = List.create_list(@valid_attrs) - assert lists = List.get_lists_from_ids([1, 2, 3]) + {:ok, list} = List.create_list(@valid_attrs) + assert lists = List.get_lists_from_ids([list.id]) assert length(lists) == 1 end diff --git a/test/app_web/controllers/item_controller_test.exs b/test/app_web/controllers/item_controller_test.exs index 2a78337c..20ae0eba 100644 --- a/test/app_web/controllers/item_controller_test.exs +++ b/test/app_web/controllers/item_controller_test.exs @@ -18,12 +18,11 @@ defmodule AppWeb.ItemControllerTest do item end - describe "edit item" do + describe "edit item's list" do setup [:create_item, :create_list] - test "renders form for editing chosen item's lists", %{ + test "renders form for editing item's lists", %{ conn: conn, - list: list, item: item } do conn = @@ -31,22 +30,46 @@ defmodule AppWeb.ItemControllerTest do |> assign(:person, %{id: 1}) |> get(Routes.item_path(conn, :edit, item)) - assert html_response(conn, 200) =~ "Edit Item's lists" + assert html_response(conn, 200) =~ "Edit Item" end end - # describe "update item's list" do - # setup [:create_item, :create_list] - # - # test "redirects to index when data is valid", %{conn: conn, list: list} do - # conn = - # conn - # |> assign(:person, %{id: 1}) - # |> put(Routes.list_path(conn, :update, list), list: @update_attrs) - # - # assert redirected_to(conn) == Routes.list_path(conn, :index) - # end - # end + describe "update item's list" do + setup [:create_item, :create_list] + + test "Add a list to an item", %{ + conn: conn, + list: list, + item: item + } do + update_params = %{item_lists: [list.id]} + + conn = + conn + |> assign(:person, %{id: 1}) + |> put(Routes.item_path(conn, :update, item), item: update_params) + + assert length(Item.get_item!(item.id).lists) == 1 + + assert redirected_to(conn) == "/" + end + + test "Remove list from item", %{ + conn: conn, + item: item + } do + update_params = %{item_lists: ""} + + conn = + conn + |> assign(:person, %{id: 1}) + |> put(Routes.item_path(conn, :update, item), item: update_params) + + assert length(Item.get_item!(item.id).lists) == 0 + + assert redirected_to(conn) == "/" + end + end defp create_list(_) do list = fixture(:list)