Skip to content

Commit

Permalink
fix: Fixing tests and API with Papertrail integration. #256
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Jan 13, 2023
1 parent d4133aa commit fe061d1
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 36 deletions.
70 changes: 47 additions & 23 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -152,15 +154,15 @@ 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
assert json_response(conn, 200)["text"] == Map.get(@update_attrs, :text)
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
Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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`,
Expand Down
8 changes: 4 additions & 4 deletions lib/app_web/controllers/api/item_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/app/timer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions test/app_web/controllers/api/item_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,15 +52,15 @@ 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
assert json_response(conn, 200)["text"] == Map.get(@update_attrs, :text)
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
Expand Down
10 changes: 5 additions & 5 deletions test/app_web/controllers/api/timer_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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
Expand All @@ -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 =
Expand All @@ -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))
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit fe061d1

Please sign in to comment.