Skip to content

Commit

Permalink
fix: Fix failing tests. #53
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Dec 13, 2022
1 parent 034df47 commit 9134601
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
7 changes: 2 additions & 5 deletions lib/app_web/controllers/api_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule AppWeb.ApiController do

def create(conn, params) do
case Todo.create_item(params) do

# Successfully creates item
{:ok, item} ->
json(conn, item)
Expand All @@ -26,8 +25,8 @@ defmodule AppWeb.ApiController do
text = Map.get(params, "text", "")

item = Todo.get_item!(id)
case Todo.update_item(item, %{text: text}) do

case Todo.update_item(item, %{text: text}) do
# Successfully updates item
{:ok, item} ->
json(conn, item)
Expand All @@ -48,8 +47,8 @@ defmodule AppWeb.ApiController do
status = Map.get(params, "status", "")

item = Todo.get_item!(id)
case Todo.update_item(item, %{status: status}) do

case Todo.update_item(item, %{status: status}) do
# Successfully updates item
{:ok, item} ->
json(conn, item)
Expand All @@ -65,13 +64,11 @@ defmodule AppWeb.ApiController do
end
end


defp make_errors_readable(changeset) do
traverse_errors(changeset, fn {msg, opts} ->
Regex.replace(~r"%{(\w+)}", msg, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
end

end
11 changes: 5 additions & 6 deletions test/app/todo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule App.TodoTest do
import App.TodoFixtures

@invalid_attrs %{person_id: nil, status: nil, text: nil}
@valid_attrs %{person_id: 42, status: 0, text: "some text"}

test "list_items/0 returns all items" do
item = item_fixture()
Expand All @@ -21,11 +22,9 @@ defmodule App.TodoTest do
end

test "create_item/1 with valid data creates a item" do
valid_attrs = %{person_id: 42, status: 42, text: "some text"}

assert {:ok, %Item{} = item} = Todo.create_item(valid_attrs)
assert {:ok, %Item{} = item} = Todo.create_item(@valid_attrs)
assert item.person_id == 42
assert item.status == 42
assert item.status == 0
assert item.text == "some text"
end

Expand All @@ -35,11 +34,11 @@ defmodule App.TodoTest do

test "update_item/2 with valid data updates the item" do
item = item_fixture()
update_attrs = %{person_id: 43, status: 43, text: "some updated text"}
update_attrs = %{person_id: 43, status: 2, text: "some updated text"}

assert {:ok, %Item{} = item} = Todo.update_item(item, update_attrs)
assert item.person_id == 43
assert item.status == 43
assert item.status == 2
assert item.text == "some updated text"
end

Expand Down
2 changes: 1 addition & 1 deletion test/support/fixtures/todo_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule App.TodoFixtures do
attrs
|> Enum.into(%{
person_id: 42,
status: 42,
status: 0,
text: "some text"
})
|> App.Todo.create_item()
Expand Down

0 comments on commit 9134601

Please sign in to comment.