Skip to content

Commit

Permalink
only 2 tests faling ... #410
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 27, 2023
1 parent 14a2110 commit 22d354e
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 73 deletions.
8 changes: 4 additions & 4 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Hooks.Items = {

this.el.addEventListener("update-indexes", e => {
const item_id = e.detail.fromItemId
const list_ids = get_list_item_ids()
const list_ids = get_list_item_cids()
console.log("update-indexes", e.detail, "list: ", list_ids)
// Check if both "from" and "to" are defined
if(item_id && itemId_to && item_id != itemId_to) {
Expand All @@ -53,11 +53,11 @@ Hooks.Items = {
* and returns a String containing the IDs as a space-separated list e.g: "1 2 3 42 71 93"
* This is used to determine the `position` of the `item` that has been moved.
*/
function get_list_item_ids() {
function get_list_item_cids() {
console.log("invoke get_list_item_ids")
const lis = document.querySelectorAll("label[phx-value-id]");
const lis = document.querySelectorAll("label[phx-value-cid]");
return Object.values(lis).map(li => {
return li.attributes["phx-value-id"].nodeValue
return li.attributes["phx-value-cid"].nodeValue
}).join(",")
}

Expand Down
2 changes: 1 addition & 1 deletion lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ defmodule App.Item do
%Item{}
|> changeset(attrs)
|> PaperTrail.insert(originator: %{id: Map.get(attrs, :person_id, 0)})
|> App.ListItems.add_papertrail_item_to_all_list()
end

@doc """
Expand Down Expand Up @@ -226,7 +227,6 @@ defmodule App.Item do
# dbg(all_list)
# |> Enum.join(",")
item_ids = App.ListItems.get_list_items(all_list.cid)
# dbg(item_ids)

sql = """
SELECT i.id, i.cid, i.text, i.status, i.person_id, i.updated_at,
Expand Down
67 changes: 46 additions & 21 deletions lib/app/list_items.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
defmodule App.ListItems do
require Logger
use Ecto.Schema
import Ecto.Changeset
alias App.{Repo}
alias __MODULE__

schema "list_items" do
field :list_cid, :string
field :list_id, :id
field :person_id, :integer
field :seq, :string
Expand All @@ -15,8 +17,8 @@ defmodule App.ListItems do
@doc false
def changeset(list_items, attrs) do
list_items
|> cast(attrs, [:list_id, :person_id, :seq])
|> validate_required([:list_id, :person_id, :seq])
|> cast(attrs, [:list_cid, :list_id, :person_id, :seq])
|> validate_required([:list_cid, :person_id, :seq])
end

@doc """
Expand All @@ -26,11 +28,11 @@ defmodule App.ListItems do
and `seq` is the String of ids currently visible in the interface.
e.g: "1,2,3,42,71,93". so we can easily determine the precise position of an `item_id`.
"""
def create_list_items_seq(list_id, person_id, seq) do
# IO.inspect("create_list_itemix ms_seq(list_id: #{list_id}, person_id: #{person_id}, seq: #{seq})")
def create_list_items_seq(list_cid, person_id, seq) do
# IO.inspect("create_list_item_seq(list_id: #{list_cid}, person_id: #{person_id}, seq: #{seq})")
%ListItems{}
|> changeset(%{
list_id: list_id,
list_cid: list_cid,
person_id: person_id,
seq: seq
})
Expand All @@ -41,21 +43,19 @@ defmodule App.ListItems do
"""
SELECT li.seq
FROM list_items li
WHERE li.list_id = $1
ORDER BY li.inserted_at DESC
WHERE li.list_cid = $1
ORDER BY li.id DESC
LIMIT 1
"""
end

@doc """
`get_list_items/2` retrieves the *latest* `list_items` record for a given `list_id`.
`get_list_items/2` retrieves the *latest* `list_items` record for a given `list_cid`.
"""
def get_list_items(list_id) do
# IO.inspect("get_list_items(list_id: #{list_id})")

sql = list_items_seq_sql()

result = Ecto.Adapters.SQL.query!(Repo, sql, [list_id])
def get_list_items(list_cid) do
# dbg(list_cid)
# dbg(list_items_seq_sql())
result = Ecto.Adapters.SQL.query!(Repo, list_items_seq_sql(), [list_cid])
# dbg(result.rows)
if is_nil(result.rows) or result.rows == [] do
[]
Expand All @@ -67,12 +67,30 @@ defmodule App.ListItems do
@doc """
`add_list_item/3` adds an `item` to a `list` for the given `person_id`.
"""
def add_list_item(item_id, list_id, person_id) do
def add_list_item(item_cid, list_cid, person_id) do
# Get latest list_items.seq for this list.id and person_id combo.
prev_seq = get_list_items(list_id)
prev_seq = get_list_items(list_cid)
# dbg(prev_seq)
# Add the `item.id` to the sequence
seq = [item_id | prev_seq] |> Enum.join(",")
create_list_items_seq(list_id, person_id, seq)
seq = [item_cid | prev_seq] |> Enum.join(",")
# dbg(seq)
create_list_items_seq(list_cid, person_id, seq)
end

# feel free to refactor this to use pattern matching:
def add_papertrail_item_to_all_list(tuple) do
# extract the item from the tuple:
try do
{:ok, %{model: item}} = tuple
all_list = App.List.get_all_list_for_person(item.person_id)
App.ListItems.add_list_item(item.cid, all_list.cid, item.person_id)
rescue
e ->
Logger.error(Exception.format(:error, e, __STACKTRACE__))
end

# return the original tuple as expected downstream:
tuple
end

@doc """
Expand All @@ -81,16 +99,23 @@ defmodule App.ListItems do
"""
def add_all_items_to_all_list_for_person_id(person_id) do
all_list = App.List.get_all_list_for_person(person_id)
# dbg(all_list)
all_items = App.Item.all_items_for_person(person_id)
# dbg(all_items)
# The previous sequence of items if there is any:
prev_seq = get_list_items(all_list.id)
prev_seq = get_list_items(all_list.cid)
# Add add each `item.id` to the sequence of item ids:
seq =
Enum.reduce(all_items, prev_seq, fn i, acc ->
[i.id | acc]
if Enum.member?(acc, i.cid) do
acc
else
[i.cid | acc]
end
end)
|> Enum.join(",")
# |> dbg()

create_list_items_seq(all_list.id, person_id, seq)
create_list_items_seq(all_list.cid, person_id, seq)
end
end
10 changes: 9 additions & 1 deletion lib/app_web/live/app_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
type="checkbox"
checked
phx-value-id={item.id}
phx-value-cid={item.cid}
phx-click="toggle"
class="flex-none p-4 m-2 form-checkbox text-slate-400 cursor-not-allowed"
/>
Expand Down Expand Up @@ -227,6 +228,7 @@
<input
type="checkbox"
phx-value-id={item.id}
phx-value-cid={item.cid}
phx-click="toggle"
class="flex-none p-4 m-2 form-checkbox text-slate-400"
checked
Expand All @@ -244,6 +246,7 @@
bg-gray-200 hover:bg-gray-300 text-gray-800 rounded-md"
phx-click="delete"
phx-value-id={item.id}
phx-value-cid={item.cid}
>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -278,6 +281,7 @@
<input
type="checkbox"
phx-value-id={item.id}
phx-value-cid={item.cid}
phx-click="toggle"
class="flex-none p-4 m-2 form-checkbox hover:text-slate-600"
/>
Expand Down Expand Up @@ -425,14 +429,15 @@
class="w-full flex-auto text-slate-800 m-2"
phx-click="edit-item"
phx-value-id={item.id}
phx-value-cid={item.cid}
>
<%= item.text %>
</label>
<code class="font-mono p-2 text-lime-400 bg-black rounded-md mr-2 max-h-10">
<%= item.id %>
</code>
<code class="font-mono p-2 text-lime-400 bg-black rounded-md mr-2 max-h-10">
<%= item.position %>
<%= item.cid %>
</code>
<% end %>

Expand All @@ -445,6 +450,7 @@
<button
phx-click="start"
phx-value-id={item.id}
phx-value-cid={item.cid}
class="inline-flex items-center px-2 py-2 h-9
bg-teal-600 hover:bg-teal-800 text-white rounded-md"
>
Expand Down Expand Up @@ -497,6 +503,7 @@
<button
phx-click="stop"
phx-value-id={item.id}
phx-value-cid={item.cid}
phx-value-timerid={item.timer_id}
class="inline-flex items-center px-2 py-2 h-9 mr-1
bg-red-500 hover:bg-red-700 text-white rounded-md"
Expand Down Expand Up @@ -536,6 +543,7 @@
<button
phx-click="start"
phx-value-id={item.id}
phx-value-cid={item.cid}
class="inline-flex items-center px-2 py-2 h-9 mr-1
bg-teal-700 hover:bg-teal-800 text-white rounded-md"
>
Expand Down
3 changes: 2 additions & 1 deletion priv/repo/migrations/20230416001045_create_list_items.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ defmodule App.Repo.Migrations.CreateListItems do

def change do
create table(:list_items) do
add :list_cid, :string
add :list_id, references(:lists, on_delete: :nothing)
add :person_id, :integer
add :seq, :string
add :seq, :text

timestamps()
end
Expand Down
27 changes: 14 additions & 13 deletions test/app/item_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ defmodule App.ItemTest do
@invalid_attrs %{text: nil}

test "get_item!/2 returns the item with given id" do
{:ok, %{model: item, version: _version}} = Item.create_item(@valid_attrs)
{:ok, %{model: item}} = Item.create_item(@valid_attrs)
assert Item.get_item!(item.id).text == item.text
end

test "get_item/2 returns the item with given id with tags" do
{:ok, %{model: item, version: _version}} = Item.create_item(@valid_attrs)
{:ok, %{model: item}} = Item.create_item(@valid_attrs)

tags = Map.get(Item.get_item(item.id, true), :tags)

Expand All @@ -22,9 +22,10 @@ defmodule App.ItemTest do
end

test "create_item/1 with valid data creates a item" do
assert {:ok, %{model: item, version: _version}} =
assert {:ok, %{model: item}} =
Item.create_item(@valid_attrs)

# dbg(item)
assert item.text == @valid_attrs.text

inserted_item = List.first(Item.list_items())
Expand All @@ -44,7 +45,7 @@ defmodule App.ItemTest do
status: 2
}

assert {:ok, %{model: item, version: _version}} = Item.create_item(attrs)
assert {:ok, %{model: item}} = Item.create_item(attrs)

assert item.text == attrs.text
end
Expand All @@ -68,19 +69,19 @@ defmodule App.ItemTest do
end

# test "list_items/0 returns a list of items stored in the DB" do
# {:ok, %{model: _item1, version: _version}} =
# {:ok, %{model: _item1}} =
# Item.create_item(@valid_attrs)

# {:ok, %{model: _item2, version: _version}} =
# {:ok, %{model: _item2}} =
# Item.create_item(@valid_attrs)

# assert Enum.count(Item.list_items()) == 2
# end

test "update_item/2 with valid data updates the item" do
{:ok, %{model: item, version: _version}} = Item.create_item(@valid_attrs)
{:ok, %{model: item}} = Item.create_item(@valid_attrs)

assert {:ok, %{model: item, version: _version}} =
assert {:ok, %{model: item}} =
Item.update_item(item, @update_attrs)

assert item.text == "some updated text"
Expand All @@ -96,7 +97,7 @@ defmodule App.ItemTest do
}

test "get_item!/1 returns the item with given id" do
{:ok, %{model: item, version: _version}} =
{:ok, %{model: item}} =
Item.create_item_with_tags(@valid_attrs)

assert length(item.tags) == 0
Expand Down Expand Up @@ -193,8 +194,8 @@ defmodule App.ItemTest do
end

test "Item.items_with_timers/1 returns a list of items with timers" do
{:ok, %{model: item1, version: _version}} = Item.create_item(@valid_attrs)
{:ok, %{model: item2, version: _version}} = Item.create_item(@valid_attrs)
{:ok, %{model: item1}} = Item.create_item(@valid_attrs)
{:ok, %{model: item2}} = Item.create_item(@valid_attrs)

assert Item.get_item!(item1.id).text == item1.text

Expand Down Expand Up @@ -222,8 +223,8 @@ defmodule App.ItemTest do
end

test "Item.person_with_item_and_timer_count/0 returns a list of count of timers and items for each given person" do
{:ok, %{model: item1, version: _version}} = Item.create_item(@valid_attrs)
{:ok, %{model: item2, version: _version}} = Item.create_item(@valid_attrs)
{:ok, %{model: item1}} = Item.create_item(@valid_attrs)
{:ok, %{model: item2}} = Item.create_item(@valid_attrs)

started = NaiveDateTime.utc_now()

Expand Down
23 changes: 13 additions & 10 deletions test/app/list_items_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ defmodule App.ListItemsTest do
@valid_list_attrs %{name: "Health", person_id: @person_id, sort: 1, status: 2}

test "add_list_item/3 adds a list_item & get_list_items/1 retrieves the list of items (seq)" do
all_list = App.List.get_all_list_for_person(@person_id)
# dbg(all_list)
# No list No list_items:
assert App.ListItems.get_list_items(0) == []
assert App.ListItems.get_list_items(all_list.cid) == []

# Create an item
assert {:ok, %{model: item}} = Item.create_item(@valid_item_attrs)
# Create list
assert {:ok, %{model: list}} = List.create_list(@valid_list_attrs)

# add the item to the lists_items:
ListItems.add_list_item(item.id, list.id, @person_id)
ListItems.add_list_item(item.cid, list.cid, @person_id)

# Confirm the item.id is in the list_items.seq:
seq = ListItems.get_list_items(list.id)
assert Enum.member?(seq, "#{item.id}")
seq = ListItems.get_list_items(list.cid)
# dbg(seq)
assert Enum.member?(seq, "#{item.cid}")
end

test "add_all_items_to_all_list_for_person_id/1 adds all items to all list for person_id" do
Expand All @@ -34,16 +37,16 @@ defmodule App.ListItemsTest do
assert {:ok, %{model: item3}} =
Item.create_item(%{text: "live best life", person_id: person_id, status: 2})

assert {:ok, %{model: all_list}} =
List.create_list(%{name: "all", person_id: person_id, status: 2})
all_list = App.List.get_all_list_for_person(person_id)
# Invoke the biz logic:
App.ListItems.add_all_items_to_all_list_for_person_id(person_id)

# Confirm that the item.id is in the squence of item ids for the "all" list:
all_items_seq = ListItems.get_list_items(all_list.id)
assert Enum.member?(all_items_seq, "#{item1.id}")
assert Enum.member?(all_items_seq, "#{item2.id}")
assert Enum.member?(all_items_seq, "#{item3.id}")
all_items_seq = ListItems.get_list_items(all_list.cid)
# dbg(all_items_seq)
assert Enum.member?(all_items_seq, "#{item1.cid}")
assert Enum.member?(all_items_seq, "#{item2.cid}")
assert Enum.member?(all_items_seq, "#{item3.cid}")
end


Expand Down
Loading

0 comments on commit 22d354e

Please sign in to comment.