From 76c0a50266ea9256fb1f03ab584653be0528d07f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Arteiro?= Date: Fri, 11 Nov 2022 17:18:51 +0000 Subject: [PATCH] fix: Formatting files. #195 --- lib/app/timer.ex | 3 +- lib/app_web/live/app_live.ex | 69 +++++----- test/app_web/live/app_live_test.exs | 191 +++++++++++++++++----------- 3 files changed, 157 insertions(+), 106 deletions(-) diff --git a/lib/app/timer.ex b/lib/app/timer.ex index c6dc3788..a4014d4c 100644 --- a/lib/app/timer.ex +++ b/lib/app/timer.ex @@ -98,7 +98,8 @@ defmodule App.Timer do start: t.start, stop: t.stop, item_id: t.item_id - }) end) + }) + end) end @doc """ diff --git a/lib/app_web/live/app_live.ex b/lib/app_web/live/app_live.ex index 35728ae2..be81fe66 100644 --- a/lib/app_web/live/app_live.ex +++ b/lib/app_web/live/app_live.ex @@ -121,16 +121,16 @@ defmodule AppWeb.AppLive do @impl true def handle_event( - "update-item-timer", - %{ - "timer_id" => id, - "index" => index, - "timer_start" => timer_start, - "timer_stop" => timer_stop - }, - socket - ) when timer_stop == "" do - + "update-item-timer", + %{ + "timer_id" => id, + "index" => index, + "timer_start" => timer_start, + "timer_stop" => timer_stop + }, + socket + ) + when timer_stop == "" do timer_changeset_list = socket.assigns.editing_timers index = String.to_integer(index) changeset_obj = Enum.at(timer_changeset_list, index) @@ -140,15 +140,16 @@ defmodule AppWeb.AppLive do App.DateTimeParser.parse!(timer_start, "%Y-%m-%dT%H:%M:%S") |> DateTime.to_naive() - other_timers_list = - List.delete_at(socket.assigns.editing_timers, index) + other_timers_list = List.delete_at(socket.assigns.editing_timers, index) - max_end = other_timers_list |> Enum.map(fn chs -> chs.data.stop end) |> Enum.max() + max_end = + other_timers_list |> Enum.map(fn chs -> chs.data.stop end) |> Enum.max() case NaiveDateTime.compare(start, max_end) do :gt -> Timer.update_timer(%{id: id, start: start, stop: nil}) {:noreply, assign(socket, editing: nil, editing_timers: [])} + _ -> updated_changeset_timers_list = error_timer_changeset( @@ -161,22 +162,20 @@ defmodule AppWeb.AppLive do {:noreply, assign(socket, editing_timers: updated_changeset_timers_list)} - end - rescue - _e -> - updated_changeset_timers_list = - error_timer_changeset( - timer_changeset_list, - changeset_obj, - index, - :id, - "Date format invalid on either start or stop." - ) + _e -> + updated_changeset_timers_list = + error_timer_changeset( + timer_changeset_list, + changeset_obj, + index, + :id, + "Date format invalid on either start or stop." + ) - {:noreply, - assign(socket, editing_timers: updated_changeset_timers_list)} + {:noreply, + assign(socket, editing_timers: updated_changeset_timers_list)} end end @@ -321,7 +320,6 @@ defmodule AppWeb.AppLive do @impl true def handle_info(%Broadcast{event: "update", payload: payload}, socket) do - person_id = get_person_id(socket.assigns) items = Item.items_with_timers(person_id) @@ -332,18 +330,29 @@ defmodule AppWeb.AppLive do case payload do {:start, item_id} -> timers_list_changeset = Timer.list_timers_changesets(item_id) - {:noreply, assign(socket, items: items, editing: item_id, editing_timers: timers_list_changeset)} + {:noreply, + assign(socket, + items: items, + editing: item_id, + editing_timers: timers_list_changeset + )} {:stop, item_id} -> timers_list_changeset = Timer.list_timers_changesets(item_id) - {:noreply, assign(socket, items: items, editing: item_id, editing_timers: timers_list_changeset)} + + {:noreply, + assign(socket, + items: items, + editing: item_id, + editing_timers: timers_list_changeset + )} _ -> {:noreply, assign(socket, items: items)} end - # If not, just update the item list. + # If not, just update the item list. else {:noreply, assign(socket, items: items)} end diff --git a/test/app_web/live/app_live_test.exs b/test/app_web/live/app_live_test.exs index 5286a23b..b0e49db2 100644 --- a/test/app_web/live/app_live_test.exs +++ b/test/app_web/live/app_live_test.exs @@ -104,14 +104,20 @@ defmodule AppWeb.AppLiveTest do {:ok, item} = Item.create_item(%{text: "Always Learning", person_id: 0, status: 2}) - {:ok, now} = - NaiveDateTime.new(Date.utc_today(), Time.utc_now()) + {:ok, now} = NaiveDateTime.new(Date.utc_today(), Time.utc_now()) - now_string = NaiveDateTime.truncate(now, :second) + now_string = + NaiveDateTime.truncate(now, :second) |> NaiveDateTime.to_string() |> String.graphemes() |> Enum.with_index() - |> Enum.map(fn {value, index} -> if index == 10 do "T" else value end end) + |> Enum.map(fn {value, index} -> + if index == 10 do + "T" + else + value + end + end) |> List.to_string() render_click(view, "edit-item", %{"id" => Integer.to_string(item.id)}) @@ -127,7 +133,6 @@ defmodule AppWeb.AppLiveTest do {:ok, item} = Item.create_item(%{text: "Always Learning", person_id: 0, status: 2}) - {:ok, seven_seconds_ago} = NaiveDateTime.new(Date.utc_today(), Time.add(Time.utc_now(), -7)) @@ -142,7 +147,8 @@ defmodule AppWeb.AppLiveTest do render_click(view, "start", %{"id" => Integer.to_string(item.id)}) render_click(view, "stop", %{"timerid" => timer.id, "id" => item.id}) - num_timers_rendered = (render(view) |> String.split("Update") |> length()) - 1 + num_timers_rendered = + (render(view) |> String.split("Update") |> length()) - 1 # Checking if two timers were rendered assert num_timers_rendered = 2 @@ -259,15 +265,13 @@ defmodule AppWeb.AppLiveTest do end test "update timer timer with ongoing timer ", %{conn: conn} do - {:ok, item} = Item.create_item(%{text: "Learn Elixir", person_id: 0, status: 2}) {:ok, seven_seconds_ago} = NaiveDateTime.new(Date.utc_today(), Time.add(Time.utc_now(), -7)) - {:ok, now} = - NaiveDateTime.new(Date.utc_today(), Time.utc_now()) + {:ok, now} = NaiveDateTime.new(Date.utc_today(), Time.utc_now()) {:ok, four_seconds_ago} = NaiveDateTime.new(Date.utc_today(), Time.add(Time.utc_now(), -4)) @@ -283,8 +287,7 @@ defmodule AppWeb.AppLiveTest do Timer.stop_timer_for_item_id(item.id) # Start a second timer - {:ok, timer2} = - Timer.start(%{item_id: item.id, person_id: 1, start: now}) + {:ok, timer2} = Timer.start(%{item_id: item.id, person_id: 1, start: now}) # Stop the timer based on its item_id Timer.stop_timer_for_item_id(item.id) @@ -294,57 +297,84 @@ defmodule AppWeb.AppLiveTest do # Update fails because of overlap timer ----------- render_click(view, "edit-item", %{"id" => Integer.to_string(item.id)}) - four_seconds_ago_string = NaiveDateTime.truncate(four_seconds_ago, :second) - |> NaiveDateTime.to_string() - |> String.graphemes() - |> Enum.with_index() - |> Enum.map(fn {value, index} -> if index == 10 do "T" else value end end) - |> List.to_string() - - - now_string = NaiveDateTime.truncate(now, :second) - |> NaiveDateTime.to_string() - |> String.graphemes() - |> Enum.with_index() - |> Enum.map(fn {value, index} -> if index == 10 do "T" else value end end) - |> List.to_string() - - error_view = render_submit(view, "update-item-timer", %{ - "timer_id" => timer2.id, - "index" => 1, - "timer_start" => four_seconds_ago_string, - "timer_stop" => "" - }) + four_seconds_ago_string = + NaiveDateTime.truncate(four_seconds_ago, :second) + |> NaiveDateTime.to_string() + |> String.graphemes() + |> Enum.with_index() + |> Enum.map(fn {value, index} -> + if index == 10 do + "T" + else + value + end + end) + |> List.to_string() + + now_string = + NaiveDateTime.truncate(now, :second) + |> NaiveDateTime.to_string() + |> String.graphemes() + |> Enum.with_index() + |> Enum.map(fn {value, index} -> + if index == 10 do + "T" + else + value + end + end) + |> List.to_string() + + error_view = + render_submit(view, "update-item-timer", %{ + "timer_id" => timer2.id, + "index" => 1, + "timer_start" => four_seconds_ago_string, + "timer_stop" => "" + }) + assert error_view =~ "When editing an ongoing timer" # Update fails because of format ----------- render_click(view, "edit-item", %{"id" => Integer.to_string(item.id)}) - error_format_view = render_submit(view, "update-item-timer", %{ - "timer_id" => timer2.id, - "index" => 1, - "timer_start" => "invalidformat", - "timer_stop" => "" - }) + error_format_view = + render_submit(view, "update-item-timer", %{ + "timer_id" => timer2.id, + "index" => 1, + "timer_start" => "invalidformat", + "timer_stop" => "" + }) + assert error_format_view =~ "Date format invalid on either start or stop." # Update successful ----------- - ten_seconds_after_string = NaiveDateTime.truncate(ten_seconds_after, :second) - |> NaiveDateTime.to_string() - |> String.graphemes() - |> Enum.with_index() - |> Enum.map(fn {value, index} -> if index == 10 do "T" else value end end) - |> List.to_string() - ten_seconds_after_datetime = NaiveDateTime.truncate(ten_seconds_after, :second) + ten_seconds_after_string = + NaiveDateTime.truncate(ten_seconds_after, :second) + |> NaiveDateTime.to_string() + |> String.graphemes() + |> Enum.with_index() + |> Enum.map(fn {value, index} -> + if index == 10 do + "T" + else + value + end + end) + |> List.to_string() + + ten_seconds_after_datetime = + NaiveDateTime.truncate(ten_seconds_after, :second) render_click(view, "edit-item", %{"id" => Integer.to_string(item.id)}) - view = assert render_submit(view, "update-item-timer", %{ - "timer_id" => timer2.id, - "index" => 1, - "timer_start" => ten_seconds_after_string, - "timer_stop" => "" - }) + view = + assert render_submit(view, "update-item-timer", %{ + "timer_id" => timer2.id, + "index" => 1, + "timer_start" => ten_seconds_after_string, + "timer_stop" => "" + }) updated_timer2 = Timer.get_timer!(timer2.id) @@ -352,15 +382,13 @@ defmodule AppWeb.AppLiveTest do end test "timer overlap error when updating timer", %{conn: conn} do - {:ok, item} = Item.create_item(%{text: "Learn Elixir", person_id: 0, status: 2}) {:ok, seven_seconds_ago} = NaiveDateTime.new(Date.utc_today(), Time.add(Time.utc_now(), -7)) - {:ok, now} = - NaiveDateTime.new(Date.utc_today(), Time.utc_now()) + {:ok, now} = NaiveDateTime.new(Date.utc_today(), Time.utc_now()) {:ok, four_seconds_ago} = NaiveDateTime.new(Date.utc_today(), Time.add(Time.utc_now(), -4)) @@ -373,8 +401,7 @@ defmodule AppWeb.AppLiveTest do Timer.stop_timer_for_item_id(item.id) # Start a second timer - {:ok, timer2} = - Timer.start(%{item_id: item.id, person_id: 1, start: now}) + {:ok, timer2} = Timer.start(%{item_id: item.id, person_id: 1, start: now}) # Stop the timer based on its item_id Timer.stop_timer_for_item_id(item.id) @@ -384,27 +411,40 @@ defmodule AppWeb.AppLiveTest do # Update fails because of overlap ----------- render_click(view, "edit-item", %{"id" => Integer.to_string(item.id)}) - four_seconds_ago_string = NaiveDateTime.truncate(four_seconds_ago, :second) - |> NaiveDateTime.to_string() - |> String.graphemes() - |> Enum.with_index() - |> Enum.map(fn {value, index} -> if index == 10 do "T" else value end end) - |> List.to_string() - + four_seconds_ago_string = + NaiveDateTime.truncate(four_seconds_ago, :second) + |> NaiveDateTime.to_string() + |> String.graphemes() + |> Enum.with_index() + |> Enum.map(fn {value, index} -> + if index == 10 do + "T" + else + value + end + end) + |> List.to_string() - now_string = NaiveDateTime.truncate(now, :second) - |> NaiveDateTime.to_string() - |> String.graphemes() - |> Enum.with_index() - |> Enum.map(fn {value, index} -> if index == 10 do "T" else value end end) - |> List.to_string() + now_string = + NaiveDateTime.truncate(now, :second) + |> NaiveDateTime.to_string() + |> String.graphemes() + |> Enum.with_index() + |> Enum.map(fn {value, index} -> + if index == 10 do + "T" + else + value + end + end) + |> List.to_string() assert render_submit(view, "update-item-timer", %{ - "timer_id" => timer2.id, - "index" => 0, - "timer_start" => four_seconds_ago_string, - "timer_stop" => now_string - }) =~ "This timer interval overlaps with other timers." + "timer_id" => timer2.id, + "index" => 0, + "timer_start" => four_seconds_ago_string, + "timer_stop" => now_string + }) =~ "This timer interval overlaps with other timers." end test "timer_text(start, stop)" do @@ -534,7 +574,8 @@ defmodule AppWeb.AppLiveTest do test "test login link redirect to auth.dwyl.com", %{conn: conn} do conn = get(conn, "/login") - assert redirected_to(conn, 302) =~ "https://dwylauth.herokuapp.com?referer=https://www.example.com/&auth_client_id=88SwQGzxQEvo6S9Pu7FZGp9btNo52rVkwtrhyub9i6K6UxVqho9A" + assert redirected_to(conn, 302) =~ + "https://dwylauth.herokuapp.com?referer=https://www.example.com/&auth_client_id=88SwQGzxQEvo6S9Pu7FZGp9btNo52rVkwtrhyub9i6K6UxVqho9A" end test "tags_to_string/1" do