From fe061d1a455ae1240a2267fd7882cd03b9f8c678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Arteiro?= Date: Fri, 13 Jan 2023 11:19:18 +0000 Subject: [PATCH] fix: Fixing tests and API with Papertrail integration. #256 --- api.md | 70 +++++++++++++------ .../controllers/api/item_controller.ex | 8 +-- test/app/timer_test.exs | 2 +- .../controllers/api/item_controller_test.exs | 6 +- .../controllers/api/timer_controller_test.exs | 10 +-- 5 files changed, 60 insertions(+), 36 deletions(-) diff --git a/api.md b/api.md index 2d1b003f..fdf7753d 100644 --- a/api.md +++ b/api.md @@ -110,7 +110,7 @@ defmodule AppWeb.API.ItemControllerTest do describe "show" do test "specific item", %{conn: conn} do - {:ok, item} = Item.create_item(@create_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = get(conn, Routes.item_path(conn, :show, item.id)) assert conn.status == 200 @@ -137,9 +137,11 @@ defmodule AppWeb.API.ItemControllerTest do assert conn.status == 200 assert json_response(conn, 200)["text"] == Map.get(@create_attrs, "text") - assert json_response(conn, 200)["status"] == Map.get(@create_attrs, "status") + assert json_response(conn, 200)["status"] == + Map.get(@create_attrs, "status") - assert json_response(conn, 200)["person_id"] == Map.get(@create_attrs, "person_id") + assert json_response(conn, 200)["person_id"] == + Map.get(@create_attrs, "person_id") end test "an invalid item", %{conn: conn} do @@ -152,7 +154,7 @@ defmodule AppWeb.API.ItemControllerTest do describe "update" do test "item with valid attributes", %{conn: conn} do - {:ok, item} = Item.create_item(@create_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = put(conn, Routes.item_path(conn, :update, item.id, @update_attrs)) assert conn.status == 200 @@ -160,7 +162,7 @@ defmodule AppWeb.API.ItemControllerTest do end test "item with invalid attributes", %{conn: conn} do - {:ok, item} = Item.create_item(@create_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = put(conn, Routes.item_path(conn, :update, item.id, @invalid_attrs)) assert conn.status == 400 @@ -217,7 +219,7 @@ defmodule AppWeb.API.TimerControllerTest do test "not found timer", %{conn: conn} do # Create item - {:ok, item} = Item.create_item(@create_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_item_attrs) conn = get(conn, Routes.timer_path(conn, :show, item.id, -1)) @@ -226,7 +228,7 @@ defmodule AppWeb.API.TimerControllerTest do test "invalid id (not being an integer)", %{conn: conn} do # Create item - {:ok, item} = Item.create_item(@create_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_item_attrs) conn = get(conn, Routes.timer_path(conn, :show, item.id, "invalid")) assert conn.status == 400 @@ -235,22 +237,25 @@ defmodule AppWeb.API.TimerControllerTest do describe "create" do test "a valid timer", %{conn: conn} do - # Create item - {:ok, item} = Item.create_item(@create_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_item_attrs) # Create timer - conn = post(conn, Routes.timer_path(conn, :create, item.id, @create_attrs)) + conn = + post(conn, Routes.timer_path(conn, :create, item.id, @create_attrs)) assert conn.status == 200 - assert json_response(conn, 200)["start"] == Map.get(@create_attrs, "start") + + assert json_response(conn, 200)["start"] == + Map.get(@create_attrs, "start") end test "an invalid timer", %{conn: conn} do # Create item - {:ok, item} = Item.create_item(@create_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_item_attrs) - conn = post(conn, Routes.timer_path(conn, :create, item.id, @invalid_attrs)) + conn = + post(conn, Routes.timer_path(conn, :create, item.id, @invalid_attrs)) assert conn.status == 400 assert length(json_response(conn, 400)["errors"]["start"]) > 0 @@ -262,7 +267,11 @@ defmodule AppWeb.API.TimerControllerTest do # Create item and timer {item, timer} = item_and_timer_fixture() - conn = put(conn, Routes.timer_path(conn, :update, item.id, timer.id, @update_attrs)) + conn = + put( + conn, + Routes.timer_path(conn, :update, item.id, timer.id, @update_attrs) + ) assert conn.status == 200 assert json_response(conn, 200)["start"] == Map.get(@update_attrs, :start) @@ -272,7 +281,11 @@ defmodule AppWeb.API.TimerControllerTest do # Create item and timer {item, timer} = item_and_timer_fixture() - conn = put(conn, Routes.timer_path(conn, :update, item.id, timer.id, @invalid_attrs)) + conn = + put( + conn, + Routes.timer_path(conn, :update, item.id, timer.id, @invalid_attrs) + ) assert conn.status == 400 assert length(json_response(conn, 400)["errors"]["start"]) > 0 @@ -281,7 +294,7 @@ defmodule AppWeb.API.TimerControllerTest do defp item_and_timer_fixture() do # Create item - {:ok, item} = Item.create_item(@create_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_item_attrs) # Create timer started = NaiveDateTime.utc_now() @@ -320,10 +333,11 @@ defmodule AppWeb.API.ItemController do code: 404, message: "No item found with the given \'id\'." } + json(conn |> put_status(404), errors) - timer -> - json(conn, timer) + item -> + json(conn, item) end # ID is not an integer @@ -347,9 +361,8 @@ defmodule AppWeb.API.ItemController do } case Item.create_item(attrs) do - # Successfully creates item - {:ok, item} -> + {:ok, %{model: item, version: _version}} -> id_item = Map.take(item, [:id]) json(conn, id_item) @@ -371,9 +384,8 @@ defmodule AppWeb.API.ItemController do item = Item.get_item!(id) case Item.update_item(item, %{text: new_text}) do - # Successfully updates item - {:ok, item} -> + {:ok, %{model: item, version: _version}} -> json(conn, item) # Error creating item @@ -390,7 +402,7 @@ defmodule AppWeb.API.ItemController do defp make_changeset_errors_readable(changeset) do errors = %{ code: 400, - message: "Malformed request", + message: "Malformed request" } changeset_errors = traverse_errors(changeset, fn {msg, _opts} -> msg end) @@ -408,6 +420,18 @@ and yields the `id` of the newly created `item` object. - `:update` refers to `PUT or PATCH api/items/:id` and returns the updated `item` object. +Do notice that, since we are using +[`PaperTrail`](https://github.com/izelnakri/paper_trail) +to record changes to the `items`, +database operations return +a map with `"model"` and `"version"`, +hence why we are pattern-matching it when +updating and create items. + +```elixir +{:ok, %{model: item, version: _version}} -> Item.create_item(attrs) +``` + In cases where, for example, `:id` is invalid when creating an `item`; or there are missing fields when creating an `item`, diff --git a/lib/app_web/controllers/api/item_controller.ex b/lib/app_web/controllers/api/item_controller.ex index d9f2862f..c271d1d3 100644 --- a/lib/app_web/controllers/api/item_controller.ex +++ b/lib/app_web/controllers/api/item_controller.ex @@ -16,8 +16,8 @@ defmodule AppWeb.API.ItemController do json(conn |> put_status(404), errors) - timer -> - json(conn, timer) + item -> + json(conn, item) end # ID is not an integer @@ -42,7 +42,7 @@ defmodule AppWeb.API.ItemController do case Item.create_item(attrs) do # Successfully creates item - {:ok, item} -> + {:ok, %{model: item, version: _version}} -> id_item = Map.take(item, [:id]) json(conn, id_item) @@ -65,7 +65,7 @@ defmodule AppWeb.API.ItemController do case Item.update_item(item, %{text: new_text}) do # Successfully updates item - {:ok, item} -> + {:ok, %{model: item, version: _version}} -> json(conn, item) # Error creating item diff --git a/test/app/timer_test.exs b/test/app/timer_test.exs index 1b39ec69..da320baa 100644 --- a/test/app/timer_test.exs +++ b/test/app/timer_test.exs @@ -101,7 +101,7 @@ defmodule App.TimerTest do start = ~N[2022-10-27 05:00:00] stop = ~N[2022-10-27 00:00:00] - {:ok, item} = Item.create_item(@valid_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@valid_item_attrs) {:ok, seven_seconds_ago} = NaiveDateTime.new(Date.utc_today(), Time.add(Time.utc_now(), -7)) diff --git a/test/app_web/controllers/api/item_controller_test.exs b/test/app_web/controllers/api/item_controller_test.exs index fded3848..5dcf0eb5 100644 --- a/test/app_web/controllers/api/item_controller_test.exs +++ b/test/app_web/controllers/api/item_controller_test.exs @@ -8,7 +8,7 @@ defmodule AppWeb.API.ItemControllerTest do describe "show" do test "specific item", %{conn: conn} do - {:ok, item} = Item.create_item(@create_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = get(conn, Routes.item_path(conn, :show, item.id)) assert conn.status == 200 @@ -52,7 +52,7 @@ defmodule AppWeb.API.ItemControllerTest do describe "update" do test "item with valid attributes", %{conn: conn} do - {:ok, item} = Item.create_item(@create_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = put(conn, Routes.item_path(conn, :update, item.id, @update_attrs)) assert conn.status == 200 @@ -60,7 +60,7 @@ defmodule AppWeb.API.ItemControllerTest do end test "item with invalid attributes", %{conn: conn} do - {:ok, item} = Item.create_item(@create_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = put(conn, Routes.item_path(conn, :update, item.id, @invalid_attrs)) assert conn.status == 400 diff --git a/test/app_web/controllers/api/timer_controller_test.exs b/test/app_web/controllers/api/timer_controller_test.exs index b38d1bc3..40397351 100644 --- a/test/app_web/controllers/api/timer_controller_test.exs +++ b/test/app_web/controllers/api/timer_controller_test.exs @@ -34,7 +34,7 @@ defmodule AppWeb.API.TimerControllerTest do test "not found timer", %{conn: conn} do # Create item - {:ok, item} = Item.create_item(@create_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_item_attrs) conn = get(conn, Routes.timer_path(conn, :show, item.id, -1)) @@ -43,7 +43,7 @@ defmodule AppWeb.API.TimerControllerTest do test "invalid id (not being an integer)", %{conn: conn} do # Create item - {:ok, item} = Item.create_item(@create_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_item_attrs) conn = get(conn, Routes.timer_path(conn, :show, item.id, "invalid")) assert conn.status == 400 @@ -53,7 +53,7 @@ defmodule AppWeb.API.TimerControllerTest do describe "create" do test "a valid timer", %{conn: conn} do # Create item - {:ok, item} = Item.create_item(@create_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_item_attrs) # Create timer conn = @@ -67,7 +67,7 @@ defmodule AppWeb.API.TimerControllerTest do test "an invalid timer", %{conn: conn} do # Create item - {:ok, item} = Item.create_item(@create_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_item_attrs) conn = post(conn, Routes.timer_path(conn, :create, item.id, @invalid_attrs)) @@ -109,7 +109,7 @@ defmodule AppWeb.API.TimerControllerTest do defp item_and_timer_fixture() do # Create item - {:ok, item} = Item.create_item(@create_item_attrs) + {:ok, %{model: item, version: _version}} = Item.create_item(@create_item_attrs) # Create timer started = NaiveDateTime.utc_now()