From 8cf85255a686b1d35ad56c05d9c4c120e8d20f48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Arteiro?= Date: Tue, 13 Dec 2022 16:58:42 +0000 Subject: [PATCH] feat: Adding testing to API Controller. #53 --- .../controllers/api_controller_test.exs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 test/app_web/controllers/api_controller_test.exs diff --git a/test/app_web/controllers/api_controller_test.exs b/test/app_web/controllers/api_controller_test.exs new file mode 100644 index 00000000..9847d46d --- /dev/null +++ b/test/app_web/controllers/api_controller_test.exs @@ -0,0 +1,71 @@ +defmodule AppWeb.ApiControllerTest do + use AppWeb.ConnCase + alias App.Todo + + @create_attrs %{person_id: 42, status: 0, text: "some text"} + @update_attrs %{person_id: 43, status: 0, text: "some updated text"} + @update_status_attrs %{status: 1} + + @invalid_attrs %{person_id: nil, status: nil, text: nil} + @invalid_status_attrs %{status: 6} + + describe "create" do + test "a valid item", %{conn: conn} do + conn = post(conn, ~p"/api/items", @create_attrs) + + assert conn.status == 200 + assert Map.get(Jason.decode!(response(conn, 200)), :text) == Map.get(@create_attrs, "text") + assert Map.get(Jason.decode!(response(conn, 200)), :status) == Map.get(@create_attrs, "status") + assert Map.get(Jason.decode!(response(conn, 200)), :person_id) == Map.get(@create_attrs, "person_id") + end + + test "an invalid item", %{conn: conn} do + conn = post(conn, ~p"/api/items", @invalid_attrs) + + assert conn.status == 400 + + error_text = response(conn, 400) |> Jason.decode!() |> Map.get("text") + assert error_text == ["can't be blank"] + end + end + + describe "update" do + test "item with valid attributes", %{conn: conn} do + {:ok, item} = Todo.create_item(@create_attrs) + + conn = put(conn, ~p"/api/items/#{item.id}", @update_attrs) + assert conn.status == 200 + assert Map.get(Jason.decode!(response(conn, 200)), :text) == Map.get(@update_attrs, "text") + end + + test "item with invalid attributes", %{conn: conn} do + {:ok, item} = Todo.create_item(@create_attrs) + + conn = put(conn, ~p"/api/items/#{item.id}", @invalid_attrs) + + assert conn.status == 400 + error_text = response(conn, 400) |> Jason.decode!() |> Map.get("text") + assert error_text == ["can't be blank"] + end + end + + describe "update item status" do + test "with valid attributes", %{conn: conn} do + {:ok, item} = Todo.create_item(@create_attrs) + + conn = put(conn, ~p"/api/items/#{item.id}/status", @update_status_attrs) + assert conn.status == 200 + assert Map.get(Jason.decode!(response(conn, 200)), :status) == Map.get(@update_status_attrs, "status") + end + + test "with invalid attributes", %{conn: conn} do + {:ok, item} = Todo.create_item(@create_attrs) + + conn = put(conn, ~p"/api/items/#{item.id}/status", @invalid_status_attrs) + + assert conn.status == 400 + error_text = response(conn, 400) |> Jason.decode!() |> Map.get("status") + assert error_text == ["must be less than or equal to 2"] + end + end +end