-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for profile schema and controller
Add more tests for profile schema and controller
- Loading branch information
Showing
5 changed files
with
152 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
defmodule App.PersonTest do | ||
use App.DataCase | ||
alias App.Person | ||
|
||
describe "Test constraints and requirements for Person schema" do | ||
test "valid person changeset" do | ||
changeset = Person.changeset(%Person{}, %{person_id: 1, name: "person1"}) | ||
|
||
assert changeset.valid? | ||
end | ||
|
||
test "invalid person changeset when name value missing" do | ||
changeset = Person.changeset(%Person{}, %{person_id: 1, name: ""}) | ||
refute changeset.valid? | ||
end | ||
|
||
test "invalid person changeset when person_id value missing" do | ||
changeset = Person.changeset(%Person{}, %{name: "person1"}) | ||
refute changeset.valid? | ||
end | ||
end | ||
|
||
describe "Save person in Postgres" do | ||
@valid_attrs %{person_id: 1, name: "person 1"} | ||
@invalid_attrs %{name: nil} | ||
|
||
test "get_person!/1 returns the person with given id" do | ||
{:ok, person} = Person.create_person(@valid_attrs) | ||
assert Person.get_person!(person.person_id).name == person.name | ||
end | ||
|
||
test "create_person/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = Person.create_person(@invalid_attrs) | ||
end | ||
|
||
test "create_person/1 returns invalid changeset when trying to insert a duplicate name" do | ||
assert {:ok, _person} = Person.create_person(@valid_attrs) | ||
|
||
assert {:error, _changeset} = | ||
Person.create_person(%{person_id: 2, name: "person 1"}) | ||
end | ||
end | ||
|
||
describe "Update person in Postgres" do | ||
@valid_attrs %{person_id: 1, name: "person 1"} | ||
@valid_update_attrs %{person_id: 1, name: "person 1 updated"} | ||
|
||
test "create_person/1 returns invalid changeset when trying to insert a duplicate name" do | ||
assert {:ok, person} = Person.create_person(@valid_attrs) | ||
|
||
assert {:ok, person_updated} = | ||
Person.update_person(person, @valid_update_attrs) | ||
|
||
assert person_updated.name == "person 1 updated" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
defmodule AppWeb.ProfileControllerTest do | ||
use AppWeb.ConnCase | ||
|
||
alias App.{Person, Repo} | ||
|
||
setup [:create_profile] | ||
|
||
@create_attrs %{person_id: 1, name: "person 1"} | ||
@update_attrs %{person_id: 1, name: "person 1 udpated"} | ||
@invalid_attrs %{person_id: 1, name: ""} | ||
|
||
describe "edit profile" do | ||
test "renders form for editing chosen profile", %{ | ||
conn: conn, | ||
person: profile | ||
} do | ||
conn = | ||
conn | ||
|> assign(:person, %{id: 1}) | ||
|> get(Routes.profile_path(conn, :edit, profile)) | ||
|
||
assert html_response(conn, 200) =~ "Edit Profile" | ||
end | ||
|
||
test "redirect to index when missing permission to edit the profile", %{ | ||
conn: conn, | ||
person: profile | ||
} do | ||
conn = get(conn, Routes.profile_path(conn, :edit, profile)) | ||
|
||
assert redirected_to(conn) == "/" | ||
end | ||
end | ||
|
||
describe "update profile" do | ||
test "redirects to home page when data is valid", %{ | ||
conn: conn, | ||
person: person | ||
} do | ||
conn = | ||
conn | ||
|> assign(:person, %{id: 1}) | ||
|> put(Routes.profile_path(conn, :update, person), person: @update_attrs) | ||
|
||
assert redirected_to(conn) == "/" | ||
end | ||
|
||
test "renders errors when data is invalid", %{conn: conn, person: person} do | ||
conn = | ||
conn | ||
|> assign(:person, %{id: 1}) | ||
|> put(Routes.profile_path(conn, :update, person), | ||
person: @invalid_attrs | ||
) | ||
|
||
assert html_response(conn, 200) =~ "Edit Profile" | ||
end | ||
end | ||
|
||
describe "redirect to edit profile when profile name not define" do | ||
setup :create_profile_with_no_name | ||
|
||
test "redirect to edit profile", %{conn: conn, person: person} do | ||
conn = | ||
conn | ||
|> assign(:person, %{id: person.person_id}) | ||
|> get("/") | ||
|
||
assert redirected_to(conn) == "/profile/#{person.person_id}/edit" | ||
end | ||
end | ||
|
||
def fixture(:person) do | ||
{:ok, person} = Person.create_person(@create_attrs) | ||
person | ||
end | ||
|
||
def fixture(:person_no_name) do | ||
{:ok, person} = Repo.insert(%Person{person_id: 404}) | ||
person | ||
end | ||
|
||
defp create_profile(_) do | ||
person = fixture(:person) | ||
%{person: person} | ||
end | ||
|
||
defp create_profile_with_no_name(_) do | ||
person = fixture(:person_no_name) | ||
%{person: person} | ||
end | ||
end |