From 58fbc6505a12765c4a411ca15ea232f5ed7c2a58 Mon Sep 17 00:00:00 2001 From: nelsonic <nelson+github@dwyl.io> Date: Sat, 30 May 2020 20:39:03 +0100 Subject: [PATCH 1/5] mix format --- test/app_web/controllers/item_controller_test.exs | 3 ++- test/app_web/views/item_view_test.exs | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/test/app_web/controllers/item_controller_test.exs b/test/app_web/controllers/item_controller_test.exs index 0bee10eb..4ace8a2b 100644 --- a/test/app_web/controllers/item_controller_test.exs +++ b/test/app_web/controllers/item_controller_test.exs @@ -75,7 +75,8 @@ defmodule AppWeb.ItemControllerTest do test "deletes chosen item", %{conn: conn, item: item} do conn = delete(conn, Routes.item_path(conn, :delete, item)) conn = get(conn, Routes.item_path(conn, :show, item)) - assert html_response(conn, 200) =~ "2" # Status: 2 + # Status: 2 + assert html_response(conn, 200) =~ "2" end end diff --git a/test/app_web/views/item_view_test.exs b/test/app_web/views/item_view_test.exs index 75d629e7..898e5126 100644 --- a/test/app_web/views/item_view_test.exs +++ b/test/app_web/views/item_view_test.exs @@ -35,10 +35,12 @@ defmodule AppWeb.ItemViewTest do test "pluralise/1 returns item for 1 item and items for < 1 <" do assert ItemView.pluralise([%{text: "one", status: 0}]) == "item" + assert ItemView.pluralise([ - %{text: "one", status: 0}, - %{text: "two", status: 0} - ]) == "items" + %{text: "one", status: 0}, + %{text: "two", status: 0} + ]) == "items" + assert ItemView.pluralise([%{text: "one", status: 1}]) == "items" end end From ab4a45b494ee0b0f89becbc425f57ceb55a34eac Mon Sep 17 00:00:00 2001 From: nelsonic <nelson+github@dwyl.io> Date: Sat, 30 May 2020 20:39:35 +0100 Subject: [PATCH 2/5] add /ping route, controller and test to wake heroku dyno. see: https://github.com/dwyl/ping/issues/1 --- README.md | 3 +++ lib/app_web/controllers/ping_controller.ex | 8 ++++++++ lib/app_web/router.ex | 3 +++ lib/app_web/views/item_view.ex | 2 +- mix.exs | 3 +++ mix.lock | 1 + test/app_web/controllers/ping_controller_test.exs | 11 +++++++++++ 7 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 lib/app_web/controllers/ping_controller.ex create mode 100644 test/app_web/controllers/ping_controller_test.exs diff --git a/README.md b/README.md index 94deb897..a927a8a1 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,9 @@ from scratch. ### Try it on Heroku: [phxtodo.herokuapp.com](https://phxtodo.herokuapp.com) +<!-- wake heroku app before visit. see: https://github.com/dwyl/ping --> + + Try the Heroku version. Add a few items to the list and test the functionality. diff --git a/lib/app_web/controllers/ping_controller.ex b/lib/app_web/controllers/ping_controller.ex new file mode 100644 index 00000000..a811b6f4 --- /dev/null +++ b/lib/app_web/controllers/ping_controller.ex @@ -0,0 +1,8 @@ +defmodule AppWeb.PingController do + use AppWeb, :controller + + # see: github.com/dwyl/ping + def ping(conn, params) do + Ping.render_pixel(conn, params) + end +end diff --git a/lib/app_web/router.ex b/lib/app_web/router.ex index 6983e82a..c10342b8 100644 --- a/lib/app_web/router.ex +++ b/lib/app_web/router.ex @@ -21,6 +21,9 @@ defmodule AppWeb.Router do get "/items/toggle/:id", ItemController, :toggle get "/clear", ItemController, :clear_completed get "/:filter", ItemController, :index + + # see: https://github.com/dwyl/ping + get "/ping", PingController, :ping end # Other scopes may use custom stacks. diff --git a/lib/app_web/views/item_view.ex b/lib/app_web/views/item_view.ex index f7f388d8..71c682a0 100644 --- a/lib/app_web/views/item_view.ex +++ b/lib/app_web/views/item_view.ex @@ -51,6 +51,6 @@ defmodule AppWeb.ItemView do # check if there are items with status 0 or 1, used to hide/show footer def got_items?(items) do - Enum.filter(items, fn i -> i.status < 2 end) |> Enum.count > 0 + Enum.filter(items, fn i -> i.status < 2 end) |> Enum.count() > 0 end end diff --git a/mix.exs b/mix.exs index 93329b5c..363d06bf 100644 --- a/mix.exs +++ b/mix.exs @@ -53,6 +53,9 @@ defmodule App.MixProject do {:jason, "~> 1.0"}, {:plug_cowboy, "~> 2.0"}, + # ping to wake the ("Free") Heroku dyno: https://github.com/dwyl/ping + {:ping, "~> 1.0.0"}, + # Test Code Coverage: {:excoveralls, "~> 0.12.2", only: :test} ] diff --git a/mix.lock b/mix.lock index 75da7a65..79d2f7dc 100644 --- a/mix.lock +++ b/mix.lock @@ -24,6 +24,7 @@ "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.2.2", "38d94c30df5e2ef11000697a4fbe2b38d0fbf79239d492ff1be87bbc33bc3a84", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "a3dec3d28ddb5476c96a7c8a38ea8437923408bc88da43e5c45d97037b396280"}, "phoenix_live_view": {:hex, :phoenix_live_view, "0.12.1", "42f591c781edbf9fab921319076b7ac635d43aa23e6748d2644563326236d7e4", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.4.16 or ~> 1.5.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm", "585321e98df1cd5943e370b9784e950a37ca073744eb534660c9048967c52ab6"}, "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.0.0", "a1ae76717bb168cdeb10ec9d92d1480fec99e3080f011402c0a2d68d47395ffb", [:mix], [], "hexpm", "c52d948c4f261577b9c6fa804be91884b381a7f8f18450c5045975435350f771"}, + "ping": {:hex, :ping, "1.0.0", "d692c1b8b45fd8ad188c75dfc871639d1e528bcca46b36dccf5cb04c35d7f460", [:mix], [], "hexpm", "e2b2c63e5741827e786674ce7083c3784a51f03bd34237a31364670b51421675"}, "plug": {:hex, :plug, "1.10.1", "c56a6d9da7042d581159bcbaef873ba9d87f15dce85420b0d287bca19f40f9bd", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "b5cd52259817eb8a31f2454912ba1cff4990bca7811918878091cb2ab9e52cb8"}, "plug_cowboy": {:hex, :plug_cowboy, "2.2.2", "7a09aa5d10e79b92d332a288f21cc49406b1b994cbda0fde76160e7f4cc890ea", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e82364b29311dbad3753d588febd7e5ef05062cd6697d8c231e0e007adab3727"}, "plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"}, diff --git a/test/app_web/controllers/ping_controller_test.exs b/test/app_web/controllers/ping_controller_test.exs new file mode 100644 index 00000000..fe9dd930 --- /dev/null +++ b/test/app_web/controllers/ping_controller_test.exs @@ -0,0 +1,11 @@ +defmodule AppWeb.PingControllerTest do + use AppWeb.ConnCase + + # see: https://github.com/dwyl/ping + test "GET /ping (GIF) renders 1x1 pixel", %{conn: conn} do + conn = get(conn, Routes.ping_path(conn, :ping)) + assert conn.status == 200 + assert conn.state == :sent + assert conn.resp_body =~ <<71, 73, 70, 56, 57>> + end +end From ea94ee9777f86da5b8f7427e21721541cc6c64d1 Mon Sep 17 00:00:00 2001 From: nelsonic <nelson+github@dwyl.io> Date: Sun, 31 May 2020 00:06:29 +0100 Subject: [PATCH 3/5] use ping v1.0.1 which includes :plug https://github.com/dwyl/phoenix-todo-list-tutorial/pull/39#issuecomment-636377763 --- mix.exs | 2 +- mix.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mix.exs b/mix.exs index 363d06bf..6f75f8cd 100644 --- a/mix.exs +++ b/mix.exs @@ -54,7 +54,7 @@ defmodule App.MixProject do {:plug_cowboy, "~> 2.0"}, # ping to wake the ("Free") Heroku dyno: https://github.com/dwyl/ping - {:ping, "~> 1.0.0"}, + {:ping, "~> 1.0.1"}, # Test Code Coverage: {:excoveralls, "~> 0.12.2", only: :test} diff --git a/mix.lock b/mix.lock index 79d2f7dc..bb63c090 100644 --- a/mix.lock +++ b/mix.lock @@ -24,7 +24,7 @@ "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.2.2", "38d94c30df5e2ef11000697a4fbe2b38d0fbf79239d492ff1be87bbc33bc3a84", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "a3dec3d28ddb5476c96a7c8a38ea8437923408bc88da43e5c45d97037b396280"}, "phoenix_live_view": {:hex, :phoenix_live_view, "0.12.1", "42f591c781edbf9fab921319076b7ac635d43aa23e6748d2644563326236d7e4", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.4.16 or ~> 1.5.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm", "585321e98df1cd5943e370b9784e950a37ca073744eb534660c9048967c52ab6"}, "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.0.0", "a1ae76717bb168cdeb10ec9d92d1480fec99e3080f011402c0a2d68d47395ffb", [:mix], [], "hexpm", "c52d948c4f261577b9c6fa804be91884b381a7f8f18450c5045975435350f771"}, - "ping": {:hex, :ping, "1.0.0", "d692c1b8b45fd8ad188c75dfc871639d1e528bcca46b36dccf5cb04c35d7f460", [:mix], [], "hexpm", "e2b2c63e5741827e786674ce7083c3784a51f03bd34237a31364670b51421675"}, + "ping": {:hex, :ping, "1.0.1", "1f44c7b7151af18627edbd7946a376935871f285fc9c11e9f16ddb1555ea65b5", [:mix], [{:plug, "~> 1.10.1", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "c1421bebcb6601e7fb1158700ca99eb2dc0976b45fe2a26023db5a9318aadfa6"}, "plug": {:hex, :plug, "1.10.1", "c56a6d9da7042d581159bcbaef873ba9d87f15dce85420b0d287bca19f40f9bd", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "b5cd52259817eb8a31f2454912ba1cff4990bca7811918878091cb2ab9e52cb8"}, "plug_cowboy": {:hex, :plug_cowboy, "2.2.2", "7a09aa5d10e79b92d332a288f21cc49406b1b994cbda0fde76160e7f4cc890ea", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e82364b29311dbad3753d588febd7e5ef05062cd6697d8c231e0e007adab3727"}, "plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"}, From d4ed703c560860e9f263c2b84b4b58aca19e35e4 Mon Sep 17 00:00:00 2001 From: nelsonic <nelson+github@dwyl.io> Date: Sun, 31 May 2020 00:07:13 +0100 Subject: [PATCH 4/5] move /ping above /:filter to avoid override https://github.com/dwyl/phoenix-todo-list-tutorial/pull/39 --- lib/app_web/router.ex | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/app_web/router.ex b/lib/app_web/router.ex index c10342b8..9840b9cf 100644 --- a/lib/app_web/router.ex +++ b/lib/app_web/router.ex @@ -9,28 +9,20 @@ defmodule AppWeb.Router do plug :put_secure_browser_headers end - pipeline :api do - plug :accepts, ["json"] - end - scope "/", AppWeb do pipe_through :browser + # see: https://github.com/dwyl/ping + get "/ping", PingController, :ping + get "/", ItemController, :index resources "/items", ItemController get "/items/toggle/:id", ItemController, :toggle get "/clear", ItemController, :clear_completed + # this route will "catch all" so has to be last: get "/:filter", ItemController, :index - - # see: https://github.com/dwyl/ping - get "/ping", PingController, :ping end - # Other scopes may use custom stacks. - # scope "/api", AppWeb do - # pipe_through :api - # end - # Enables LiveDashboard only for development # # If you want to use the LiveDashboard in production, you should put From fa72cc76e2b6a7a1c8cc2ae8f6d09facf2ce87aa Mon Sep 17 00:00:00 2001 From: nelsonic <nelson+github@dwyl.io> Date: Sun, 31 May 2020 00:23:02 +0100 Subject: [PATCH 5/5] create test "clear_completed/2" to bump coverage back up to 100% https://github.com/dwyl/phoenix-todo-list-tutorial/pull/35#issuecomment-632682466 --- test/app_web/controllers/item_controller_test.exs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/app_web/controllers/item_controller_test.exs b/test/app_web/controllers/item_controller_test.exs index 4ace8a2b..ad867d38 100644 --- a/test/app_web/controllers/item_controller_test.exs +++ b/test/app_web/controllers/item_controller_test.exs @@ -100,6 +100,17 @@ defmodule AppWeb.ItemControllerTest do end end + describe "clear_completed/2" do + setup [:create_item] + + test "clear_completed/2", %{conn: conn, item: item} do + get(conn, Routes.item_path(conn, :toggle, item.id)) + conn = get(conn, Routes.item_path(conn, :clear_completed)) + updated_item = App.Todo.get_item!(item.id) + assert updated_item.status == 2 + end + end + defp create_item(_) do item = fixture(:item) %{item: item}