Skip to content

Commit

Permalink
Add test for profile schema and controller
Browse files Browse the repository at this point in the history
Add more tests for profile schema and controller
  • Loading branch information
SimonLab committed Oct 5, 2022
1 parent cbb765b commit a1673b8
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 13 deletions.
6 changes: 1 addition & 5 deletions lib/app/person.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule App.Person do
person
|> cast(attrs, [:person_id, :name])
|> validate_required([:person_id, :name])
|> unique_constraint([:name], name: :people_name_index)
|> unique_constraint(:name, name: :people_name_index)
end

def create_person(attrs) do
Expand All @@ -36,8 +36,4 @@ defmodule App.Person do
|> Person.changeset(attrs)
|> Repo.update()
end

def delete_person(%Person{} = person) do
Repo.delete(person)
end
end
8 changes: 1 addition & 7 deletions lib/app_web/controllers/profile_controller.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
defmodule AppWeb.ProfileController do
use AppWeb, :controller
alias App.Person
plug :permission_profile when action in [:show, :edit, :update]

def show(conn, %{"personid" => person_id}) do
profile = Person.get_person!(person_id)

render(conn, "show.html", profile: profile)
end
plug :permission_profile when action in [:edit, :update]

def edit(conn, %{"person_id" => person_id}) do
profile = Person.get_person!(person_id)
Expand Down
2 changes: 1 addition & 1 deletion lib/app_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule AppWeb.Router do
pipe_through [:browser, :authOptional, :verify_loggedin]

resources "/profile", ProfileController,
except: [:index, :delete],
only: [:edit, :update],
param: "person_id"

pipe_through [:check_profile_name]
Expand Down
57 changes: 57 additions & 0 deletions test/app/person_test.exs
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
92 changes: 92 additions & 0 deletions test/app_web/controllers/profile_controller_test.exs
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

0 comments on commit a1673b8

Please sign in to comment.