Skip to content

Commit

Permalink
Merge pull request #4 from erickmp07/fifth-class
Browse files Browse the repository at this point in the history
Fifth class
  • Loading branch information
erickmp07 authored Oct 22, 2021
2 parents 8fa1774 + 60812a2 commit 580d050
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions test/inmana/restaurant_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Inmana.RestaurantTest do
use Inmana.DataCase, async: true

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: "[email protected]"}

response = Restaurant.changeset(params)

assert %Changeset{
changes: %{
email: "[email protected]",
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
35 changes: 35 additions & 0 deletions test/inmana_web/controllers/restaurants_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule InmanaWeb.RestaurantsControllerTest do
use InmanaWeb.ConnCase, async: true

describe "create/2" do
test "when all params are valid, creates the restaurant", %{conn: connection} do
params = %{name: "Restaurant name", email: "[email protected]"}

response =
connection
|> post(Routes.restaurants_path(connection, :create, params))
|> json_response(:created)

assert %{
"message" => "Restaurant created!",
"restaurant" => %{
"email" => "[email protected]",
"id" => _id,
"name" => "Restaurant name"
}
} = response
end

test "when there are invalid params, returns an error", %{conn: connection} do
params = %{email: "[email protected]"}
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
26 changes: 26 additions & 0 deletions test/inmana_web/views/restaurants_view_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule InmanaWeb.RestaurantsViewTest do
use InmanaWeb.ConnCase, async: true

import Phoenix.View

alias Inmana.Restaurant
alias InmanaWeb.RestaurantsView

describe "render/2" do
test "renders create.json" do
params = %{name: "Restaurant name", email: "[email protected]"}
{:ok, restaurant} = Inmana.create_restaurant(params)

response = render(RestaurantsView, "create.json", restaurant: restaurant)

assert %{
message: "Restaurant created!",
restaurant: %Restaurant{
email: "[email protected]",
id: _id,
name: "Restaurant name"
}
} = response
end
end
end

0 comments on commit 580d050

Please sign in to comment.