Skip to content

Commit

Permalink
Update test to work with Person schema/table
Browse files Browse the repository at this point in the history
Update test to avoid ecto foreign key error with new people table
  • Loading branch information
SimonLab committed Oct 5, 2022
1 parent c1e015c commit cbb765b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 14 deletions.
5 changes: 4 additions & 1 deletion lib/app_web/controllers/init_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ defmodule AppWeb.InitController do

conn
|> assign(:loggedin, true)
|> assign(:person, %{picture: "https://dwyl.com/img/favicon-32x32.png"})
|> assign(:person, %{
picture: "https://dwyl.com/img/favicon-32x32.png",
id: 0
})
|> render(:index,
env: check_env(@env_required),
api_key_set: api_key_set?()
Expand Down
7 changes: 6 additions & 1 deletion lib/app_web/controllers/tag_controller.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule AppWeb.TagController do
use AppWeb, :controller
alias App.Tag
alias App.{Person, Tag}
plug :permission_tag when action in [:edit, :update, :delete]

def index(conn, _params) do
Expand Down Expand Up @@ -54,4 +54,9 @@ defmodule AppWeb.TagController do
|> halt()
end
end

defp create_person(_) do
person = Person.create_person(%{"person_id" => 1, "name" => "guest"})
%{person: person}
end
end
4 changes: 0 additions & 4 deletions lib/app_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ defmodule AppWeb.Router do
resources "/tags", TagController, except: [:show]
get "/login", AuthController, :login
get "/logout", AuthController, :logout

resources "/profile", ProfileController,
except: [:index, :delete],
param: "person_id"
end

# assign to conn the loggedin value used in templates
Expand Down
9 changes: 8 additions & 1 deletion test/app/item_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule App.ItemTest do
use App.DataCase
alias App.{Item, Timer}
alias App.{Item, Person, Timer}

setup [:create_person]

describe "items" do
@valid_attrs %{text: "some text", person_id: 1, status: 2}
Expand Down Expand Up @@ -151,4 +153,9 @@ defmodule App.ItemTest do
assert length(item_timers) > 0
end
end

defp create_person(_) do
person = Person.create_person(%{"person_id" => 1, "name" => "guest"})
%{person: person}
end
end
9 changes: 8 additions & 1 deletion test/app/tag_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule App.TagTest do
use App.DataCase
alias App.Tag
alias App.{Person, Tag}

setup [:create_person]

describe "Test constraints and requirements for Tag schema" do
test "valid tag changeset" do
Expand Down Expand Up @@ -54,4 +56,9 @@ defmodule App.TagTest do
assert length(tags) == 0
end
end

defp create_person(_) do
person = Person.create_person(%{"person_id" => 1, "name" => "guest"})
%{person: person}
end
end
9 changes: 8 additions & 1 deletion test/app/timer_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule App.TimerTest do
use App.DataCase
alias App.{Item, Timer}
alias App.{Item, Person, Timer}

setup [:create_person]

describe "timers" do
@valid_item_attrs %{text: "some text", person_id: 1}
Expand Down Expand Up @@ -66,4 +68,9 @@ defmodule App.TimerTest do
assert "Keep on truckin'"
end
end

defp create_person(_) do
person = Person.create_person(%{"person_id" => 1, "name" => "guest"})
%{person: person}
end
end
10 changes: 9 additions & 1 deletion test/app_web/controllers/tag_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule AppWeb.TagControllerTest do
use AppWeb.ConnCase

alias App.Tag
alias App.{Tag, Person}

setup [:create_person]

@create_attrs %{text: "tag1", person_id: 1, color: "#FCA5A5"}
@update_attrs %{text: "tag1 updated", color: "#F87171"}
Expand Down Expand Up @@ -90,4 +92,10 @@ defmodule AppWeb.TagControllerTest do
tag = fixture(:tag)
%{tag: tag}
end

defp create_person(_) do
person = Person.create_person(%{"person_id" => 0, "name" => "guest"})
person = Person.create_person(%{"person_id" => 1, "name" => "Person1"})
%{person: person}
end
end
15 changes: 11 additions & 4 deletions test/app_web/live/app_live_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule AppWeb.AppLiveTest do
use AppWeb.ConnCase
alias App.{Item, Timer, Tag}
alias App.{Item, Person, Timer, Tag}
import Phoenix.LiveViewTest
alias Phoenix.Socket.Broadcast

setup [:create_person]

test "disconnected and connected render", %{conn: conn} do
{:ok, page_live, disconnected_html} = live(conn, "/")
assert disconnected_html =~ "done"
Expand Down Expand Up @@ -203,7 +205,7 @@ defmodule AppWeb.AppLiveTest do
givenName: "Alex",
picture: "this",
auth_provider: "GitHub",
id: 2
id: 0
}

jwt = AuthPlug.Token.generate_jwt!(data)
Expand All @@ -218,7 +220,7 @@ defmodule AppWeb.AppLiveTest do
givenName: "Alex",
picture: "this",
auth_provider: "GitHub",
id: 2
id: 0
}

jwt = AuthPlug.Token.generate_jwt!(data)
Expand All @@ -234,7 +236,7 @@ defmodule AppWeb.AppLiveTest do
picture: "this",
auth_provider: "GitHub",
sid: 1,
id: 2
id: 0
}

jwt = AuthPlug.Token.generate_jwt!(data)
Expand All @@ -258,4 +260,9 @@ defmodule AppWeb.AppLiveTest do
%Tag{text: "Elixir"}
]) == "Learn, Elixir"
end

defp create_person(_) do
person = Person.create_person(%{"person_id" => 0, "name" => "guest"})
%{person: person}
end
end

0 comments on commit cbb765b

Please sign in to comment.