From edb8c2d7b0b549dab8da4933c53b4d4c1daf5eab Mon Sep 17 00:00:00 2001 From: Erick Macedo Pinto Date: Fri, 22 Oct 2021 02:03:21 -0300 Subject: [PATCH 1/5] Create changeset test --- test/inmana/restaurant_test.exs | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/inmana/restaurant_test.exs diff --git a/test/inmana/restaurant_test.exs b/test/inmana/restaurant_test.exs new file mode 100644 index 0000000..fb7bab0 --- /dev/null +++ b/test/inmana/restaurant_test.exs @@ -0,0 +1,36 @@ +defmodule Inmana.RestaurantTest do + use Inmana.DataCase + + alias Ecto.Changeset + alias Inmana.Restaurant + + describe "changeset/1" do + test "when all params are valid, returns a valid changeset" do + params = %{name: "Restaurant name", email: "restaurant@restaurant.com"} + + response = Restaurant.changeset(params) + + assert %Changeset{ + changes: %{ + email: "restaurant@restaurant.com", + name: "Restaurant name" + }, + valid?: true + } = response + end + + test "when there are invalid params, returns an invalid changeset" do + params = %{name: "R", email: ""} + expected_response = %{ + email: ["can't be blank"], + name: ["should be at least 2 character(s)"] + } + + response = Restaurant.changeset(params) + + assert %Changeset{valid?: false} = response + + assert errors_on(response) == expected_response + end + end +end From cf85037e113baa8fab0665d9b452ff288ce5f871 Mon Sep 17 00:00:00 2001 From: Erick Macedo Pinto Date: Fri, 22 Oct 2021 02:05:24 -0300 Subject: [PATCH 2/5] Add note with command to run the tests --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 1ec01b3..b1a69ee 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,11 @@ Start Phoenix endpoint: mix phx.server ``` +NOTE: To run the tests: +```bash +mix test +``` + The application can be accessed at [`localhost:4000`](http://localhost:4000). ## Technologies From c1222323f5099fa61008565285b78c4ad9d9dacc Mon Sep 17 00:00:00 2001 From: Erick Macedo Pinto Date: Fri, 22 Oct 2021 02:22:47 -0300 Subject: [PATCH 3/5] Create controller test --- .../restaurants_controller_test.exs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test/inmana_web/controllers/restaurants_controller_test.exs diff --git a/test/inmana_web/controllers/restaurants_controller_test.exs b/test/inmana_web/controllers/restaurants_controller_test.exs new file mode 100644 index 0000000..021ee6f --- /dev/null +++ b/test/inmana_web/controllers/restaurants_controller_test.exs @@ -0,0 +1,35 @@ +defmodule InmanaWeb.RestaurantsControllerTest do + use InmanaWeb.ConnCase + + describe "create/2" do + test "when all params are valid, creates the restaurant", %{conn: connection} do + params = %{name: "Restaurant name", email: "restaurant@restaurant.com"} + + response = + connection + |> post(Routes.restaurants_path(connection, :create, params)) + |> json_response(:created) + + assert %{ + "message" => "Restaurant created!", + "restaurant" => %{ + "email" => "restaurant@restaurant.com", + "id" => _id, + "name" => "Restaurant name" + } + } = response + end + + test "when there are invalid params, returns an error", %{conn: connection} do + params = %{email: "restaurant@restaurant.com"} + expected_response = %{"message" => %{"name" => ["can't be blank"]}} + + response = + connection + |> post(Routes.restaurants_path(connection, :create, params)) + |> json_response(:bad_request) + + assert response == expected_response + end + end +end From 3175cb9abaf699a2b3fcba943cd4e976682270bf Mon Sep 17 00:00:00 2001 From: Erick Macedo Pinto Date: Fri, 22 Oct 2021 02:33:40 -0300 Subject: [PATCH 4/5] Create view test --- .../views/restaurants_view_test.exs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test/inmana_web/views/restaurants_view_test.exs diff --git a/test/inmana_web/views/restaurants_view_test.exs b/test/inmana_web/views/restaurants_view_test.exs new file mode 100644 index 0000000..ad2270b --- /dev/null +++ b/test/inmana_web/views/restaurants_view_test.exs @@ -0,0 +1,26 @@ +defmodule InmanaWeb.RestaurantsViewTest do + use InmanaWeb.ConnCase + + import Phoenix.View + + alias Inmana.Restaurant + alias InmanaWeb.RestaurantsView + + describe "render/2" do + test "renders create.json" do + params = %{name: "Restaurant name", email: "restaurant@restaurant.com"} + {:ok, restaurant} = Inmana.create_restaurant(params) + + response = render(RestaurantsView, "create.json", restaurant: restaurant) + + assert %{ + message: "Restaurant created!", + restaurant: %Restaurant{ + email: "restaurant@restaurant.com", + id: _id, + name: "Restaurant name" + } + } = response + end + end +end From 60812a2fe65bf691bb4bb62720149f4b54e4bf86 Mon Sep 17 00:00:00 2001 From: Erick Macedo Pinto Date: Fri, 22 Oct 2021 02:36:10 -0300 Subject: [PATCH 5/5] Set the tests to run async --- test/inmana/restaurant_test.exs | 2 +- test/inmana_web/controllers/restaurants_controller_test.exs | 2 +- test/inmana_web/views/restaurants_view_test.exs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/inmana/restaurant_test.exs b/test/inmana/restaurant_test.exs index fb7bab0..3371420 100644 --- a/test/inmana/restaurant_test.exs +++ b/test/inmana/restaurant_test.exs @@ -1,5 +1,5 @@ defmodule Inmana.RestaurantTest do - use Inmana.DataCase + use Inmana.DataCase, async: true alias Ecto.Changeset alias Inmana.Restaurant diff --git a/test/inmana_web/controllers/restaurants_controller_test.exs b/test/inmana_web/controllers/restaurants_controller_test.exs index 021ee6f..fbe33cf 100644 --- a/test/inmana_web/controllers/restaurants_controller_test.exs +++ b/test/inmana_web/controllers/restaurants_controller_test.exs @@ -1,5 +1,5 @@ defmodule InmanaWeb.RestaurantsControllerTest do - use InmanaWeb.ConnCase + use InmanaWeb.ConnCase, async: true describe "create/2" do test "when all params are valid, creates the restaurant", %{conn: connection} do diff --git a/test/inmana_web/views/restaurants_view_test.exs b/test/inmana_web/views/restaurants_view_test.exs index ad2270b..224814c 100644 --- a/test/inmana_web/views/restaurants_view_test.exs +++ b/test/inmana_web/views/restaurants_view_test.exs @@ -1,5 +1,5 @@ defmodule InmanaWeb.RestaurantsViewTest do - use InmanaWeb.ConnCase + use InmanaWeb.ConnCase, async: true import Phoenix.View