From c8c9c5c94f7852db8ad7ab33c6235231f32e0322 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Wed, 30 Aug 2023 16:27:52 +0100 Subject: [PATCH] tests passing again. reorg with cids works a charm. #145 #356 #410 --- lib/app/item.ex | 50 ++++++++++------- lib/app/list_items.ex | 6 ++- lib/app_web/controllers/init_controller.ex | 3 ++ lib/app_web/live/app_live.ex | 15 +++--- .../migrations/20230824153220_add_cid.exs | 4 +- test/app_web/live/app_live_test.exs | 54 +++++++++++-------- 6 files changed, 79 insertions(+), 53 deletions(-) diff --git a/lib/app/item.ex b/lib/app/item.ex index 1e5ae060..418cf373 100644 --- a/lib/app/item.ex +++ b/lib/app/item.ex @@ -225,8 +225,10 @@ defmodule App.Item do def items_with_timers(person_id \\ 0) do all_list = App.List.get_all_list_for_person(person_id) # dbg(all_list) + # |> Enum.join(",") seq = App.ListItems.get_list_items(all_list.cid) + # dbg(seq) sql = """ SELECT i.id, i.cid, i.text, i.status, i.person_id, i.updated_at, @@ -247,9 +249,13 @@ defmodule App.Item do list_person_items(person_id) |> Enum.reduce(%{}, fn i, acc -> Map.put(acc, i.id, i) end) + # dbg(items_tags) + accumulate_item_timers(values, seq) |> Enum.map(fn t -> - Map.put(t, :tags, items_tags[t.id].tags) + if t != nil do + Map.put(t, :tags, items_tags[t.id].tags) + end end) end @@ -388,16 +394,17 @@ defmodule App.Item do end) # creates a nested map: %{ item.cid: %{id: 1, text: "my item", etc.}} - cid_item_map = Map.new(items_with_timers, fn item -> - time_elapsed = Map.get(item_id_timer_diff_map, item.id) + cid_item_map = + Map.new(items_with_timers, fn item -> + time_elapsed = Map.get(item_id_timer_diff_map, item.id) - start = - if is_nil(item.start), - do: nil, - else: NaiveDateTime.add(item.start, -time_elapsed) + start = + if is_nil(item.start), + do: nil, + else: NaiveDateTime.add(item.start, -time_elapsed) - {item.cid, %{item | start: start}} - end) + {item.cid, %{item | start: start}} + end) # return the list of items in the order of seq Enum.map(seq, fn cid -> cid_item_map[cid] end) @@ -416,16 +423,21 @@ defmodule App.Item do items = list_items() Enum.each(items, fn i -> - item = %{ - person_id: i.person_id, - status: i.status, - text: i.text, - id: i.id - } - - i - |> changeset(Map.put(item, :cid, Cid.cid(item))) - |> Repo.update() + # coveralls-ignore-start + unless Map.has_key?(i, :cid) do + item = %{ + person_id: i.person_id, + status: i.status, + text: i.text, + id: i.id + } + + i + |> changeset(Map.put(item, :cid, Cid.cid(item))) + |> Repo.update() + end + + # coveralls-ignore-stop end) end end diff --git a/lib/app/list_items.ex b/lib/app/list_items.ex index b8585960..2eb5c2a9 100644 --- a/lib/app/list_items.ex +++ b/lib/app/list_items.ex @@ -37,6 +37,8 @@ defmodule App.ListItems do seq: seq }) |> Repo.insert() + + # |> dbg() end def list_items_seq_sql do @@ -53,13 +55,13 @@ defmodule App.ListItems do `get_list_items/2` retrieves the *latest* `list_items` record for a given `list_cid`. """ def get_list_items(list_cid) do - # dbg(list_cid) - # dbg(list_items_seq_sql()) + # IO.puts(" = = = = = = = = = = = = = = = > get_list_items(#{list_cid})") result = Ecto.Adapters.SQL.query!(Repo, list_items_seq_sql(), [list_cid]) # dbg(result.rows) if is_nil(result.rows) or result.rows == [] do [] else + # dbg(result.rows) result.rows |> List.first() |> List.first() |> String.split(",") end end diff --git a/lib/app_web/controllers/init_controller.ex b/lib/app_web/controllers/init_controller.ex index 60a93cb0..3d0d1aa5 100644 --- a/lib/app_web/controllers/init_controller.ex +++ b/lib/app_web/controllers/init_controller.ex @@ -5,6 +5,9 @@ defmodule AppWeb.InitController do @env_required ~w/AUTH_API_KEY ENCRYPTION_KEYS SECRET_KEY_BASE DATABASE_URL/ def index(conn, _params) do + Logger.info("attempeting to update cid of all items ...") + App.Item.update_all_items_cid() + Logger.info("init attempting to check environment variables ... ") conn diff --git a/lib/app_web/live/app_live.ex b/lib/app_web/live/app_live.ex index 959fcf74..3c350c5a 100644 --- a/lib/app_web/live/app_live.ex +++ b/lib/app_web/live/app_live.ex @@ -25,8 +25,9 @@ defmodule AppWeb.AppLive do person_id = get_person_id(socket.assigns) # Create the "all" list for the person_id all_list = App.List.get_all_list_for_person(person_id) + # dbg(all_list) # Temporary function to add All *existing* items to the "All" list: - App.ListItems.add_all_items_to_all_list_for_person_id(person_id) + # App.ListItems.add_all_items_to_all_list_for_person_id(person_id) items = Item.items_with_timers(person_id) tags = Tag.list_person_tags(person_id) @@ -305,9 +306,9 @@ defmodule AppWeb.AppLive do list_cid = get_list_cid(socket.assigns) person_id = get_person_id(socket.assigns) - IO.puts( - "updateIndexes -> seq: #{seq} | list_id: #{list_cid} | person_id: #{person_id}" - ) + # IO.puts( + # "updateIndexes -> seq: #{seq} | list_cid: #{list_cid} | person_id: #{person_id}" + # ) App.ListItems.create_list_items_seq(list_cid, person_id, seq) {:noreply, socket} @@ -321,9 +322,9 @@ defmodule AppWeb.AppLive do }, socket ) do - IO.puts( - "cur_item_id: #{current_item_id}, selected_item_id: #{selected_item_id}" - ) + # IO.puts( + # "cur_item_id: #{current_item_id}, selected_item_id: #{selected_item_id}" + # ) {:noreply, push_event(socket, "dragover-item", %{ diff --git a/priv/repo/migrations/20230824153220_add_cid.exs b/priv/repo/migrations/20230824153220_add_cid.exs index d677e27e..e3fa8fe9 100644 --- a/priv/repo/migrations/20230824153220_add_cid.exs +++ b/priv/repo/migrations/20230824153220_add_cid.exs @@ -1,14 +1,12 @@ defmodule App.Repo.Migrations.AddCid do use Ecto.Migration - - def up do alter table(:items) do add(:cid, :string) end # https://stackoverflow.com/questions/36723407/how-to-run-updating-in-migration-for-ecto flush() - + App.Item.update_all_items_cid() end end diff --git a/test/app_web/live/app_live_test.exs b/test/app_web/live/app_live_test.exs index 8bfd1c03..0adb1bf0 100644 --- a/test/app_web/live/app_live_test.exs +++ b/test/app_web/live/app_live_test.exs @@ -685,16 +685,17 @@ defmodule AppWeb.AppLiveTest do end test "filter items by tag name", %{conn: conn} do + person_id = 0 {:ok, tag1} = - Tag.create_tag(%{person_id: 0, text: "tag1", color: "#FCA5A5"}) + Tag.create_tag(%{person_id: person_id, text: "tag1", color: "#FCA5A5"}) {:ok, tag2} = - Tag.create_tag(%{person_id: 0, text: "tag2", color: "#FCA5A5"}) + Tag.create_tag(%{person_id: person_id, text: "tag2", color: "#FCA5A5"}) {:ok, tag3} = - Tag.create_tag(%{person_id: 0, text: "tag3", color: "#FCA5A5"}) + Tag.create_tag(%{person_id: person_id, text: "tag3", color: "#FCA5A5"}) - {:ok, %{model: _item}} = + {:ok, %{model: item1}} = Item.create_item_with_tags(%{ text: "Item1 to do", person_id: 0, @@ -702,7 +703,7 @@ defmodule AppWeb.AppLiveTest do tags: [tag1, tag2] }) - {:ok, %{model: _item}} = + {:ok, %{model: item2}} = Item.create_item_with_tags(%{ text: "Item2 to do", person_id: 0, @@ -710,6 +711,11 @@ defmodule AppWeb.AppLiveTest do tags: [tag1, tag3] }) + # The items need to be in the latest seq to appear on the page: + list = App.List.get_all_list_for_person(person_id) + App.ListItems.create_list_items_seq(list.cid, person_id, "#{item1.cid},#{item2.cid}") + + {:ok, view, _html} = live(conn, "/?filter_by=all") assert render(view) =~ "Item1 to do" assert render(view) =~ "Item2 to do" @@ -815,19 +821,20 @@ defmodule AppWeb.AppLiveTest do end test "Drag and Drop item", %{conn: conn} do - person_id = 42 + person_id = 0 # Creating Three items {:ok, %{model: item}} = - Item.create_item(%{text: "Learn Elixir", person_id: person_id}) + Item.create_item(%{text: "Learn Elixir", person_id: person_id, status: 2}) {:ok, %{model: item2}} = - Item.create_item(%{ text: "Build Awesome App", person_id: person_id}) + Item.create_item(%{ text: "Build Awesome App", person_id: person_id, status: 2}) {:ok, %{model: item3}} = - Item.create_item(%{ text: "Profit", person_id: person_id}) + Item.create_item(%{ text: "Profit", person_id: person_id, status: 2}) # Create "all" list for this person_id: list = App.List.get_all_list_for_person(person_id) + # Add all items to "all" list: App.ListItems.add_all_items_to_all_list_for_person_id(person_id) @@ -848,24 +855,27 @@ defmodule AppWeb.AppLiveTest do assert render_hook(view, "removeHighlight", %{"id" => item.id}) - # Switch items (update indexes) + # reorder items: render_hook(view, "updateIndexes", %{ - "sec" => "#{item.id},#{item2.id},#{item3.id}" + "seq" => "#{item.cid},#{item2.cid},#{item3.cid}" }) - sec = App.ListItems.get_list_items(list.id) - dbg(sec) + seq = App.ListItems.get_list_items(list.cid) + pos1 = Enum.find_index(seq, fn x -> x == "#{item.cid}" end) + pos2 = Enum.find_index(seq, fn x -> x == "#{item2.cid}" end) + # IO.puts("#{pos1}: #{item.cid}") + # IO.puts("#{pos2}: #{item2.cid}") - {pos1, _} = :binary.match sec, "#{item.id}" - {pos2, _} = :binary.match sec, "#{item2.id}" assert pos1 < pos2 # Update list_item.seq: - App.ListItems.create_list_items_seq(list.id, person_id, "#{item.id},#{item3.id},#{item2.id}") - new_sec = App.ListItems.get_list_items(list.id) - dbg(new_sec) - {pos2, _} = :binary.match new_sec, "#{item2.id}" - {pos3, _} = :binary.match new_sec, "#{item3.id}" + App.ListItems.create_list_items_seq(list.cid, person_id, "#{item.cid},#{item3.cid},#{item2.cid}") + new_seq = App.ListItems.get_list_items(list.cid) + # dbg(new_seq) + pos2 = Enum.find_index(new_seq, fn x -> x == "#{item2.cid}" end) + pos3 = Enum.find_index(new_seq, fn x -> x == "#{item3.cid}" end) + # IO.puts("#{pos2}: #{item2.cid}") + # IO.puts("#{pos3}: #{item3.cid}") assert pos3 < pos2 end @@ -883,8 +893,8 @@ defmodule AppWeb.AppLiveTest do color: "#FCA5A5" }) - assert render_submit(view, :create, %{text: "tag enter pressed"}) - assert render(view) =~ "tag enter pressed" + assert render_submit(view, :create, %{text: "baking"}) + assert render(view) =~ "baking" end test "don't select tag if other keydown is pressed", %{conn: conn} do