Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gcauchon committed Feb 8, 2023
1 parent 886bec1 commit 05374bf
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 144 deletions.
3 changes: 3 additions & 0 deletions lib/elixir_boilerplate_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ defmodule ElixirBoilerplateWeb do
@moduledoc """
The entrypoint for defining your web interface, such
as controllers, components, channels, and so on.
This can be used in your application as:
use ElixirBoilerplateWeb, :controller
use ElixirBoilerplateWeb, :html
The definitions below will be executed for every controller,
component, etc, so keep them short and clean, focused
on imports, uses and aliases.
Do NOT define functions inside the quoted expressions
below. Instead, define additional modules and import
those modules here.
Expand Down
14 changes: 14 additions & 0 deletions test/elixir_boilerplate_web/controllers/error_html_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule ElixirBoilerplateWeb.ErrorHTMLTest do
use ElixirBoilerplateWeb.ConnCase, async: true

# Bring render_to_string/4 for testing custom views
import Phoenix.Template

test "renders 404.html" do
assert render_to_string(ElixirBoilerplateWeb.ErrorHTML, "404", "html", []) == "Not Found"
end

test "renders 500.html" do
assert render_to_string(ElixirBoilerplateWeb.ErrorHTML, "500", "html", []) == "Internal Server Error"
end
end
12 changes: 12 additions & 0 deletions test/elixir_boilerplate_web/controllers/error_json_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule ElixirBoilerplateWeb.ErrorJSONTest do
use ElixirBoilerplateWeb.ConnCase, async: true

test "renders 404" do
assert ElixirBoilerplateWeb.ErrorJSON.render("404.json", %{}) == %{errors: %{detail: "Not Found"}}
end

test "renders 500" do
assert ElixirBoilerplateWeb.ErrorJSON.render("500.json", %{}) ==
%{errors: %{detail: "Internal Server Error"}}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule ElixirBoilerplateWeb.PageControllerTest do
use ElixirBoilerplateWeb.ConnCase

test "GET /", %{conn: conn} do
conn = get(conn, "/")
assert html_response(conn, 200) =~ "stable base upon which"
end
end
17 changes: 0 additions & 17 deletions test/elixir_boilerplate_web/errors/view_test.exs

This file was deleted.

80 changes: 0 additions & 80 deletions test/elixir_boilerplate_web/errors_test.exs

This file was deleted.

8 changes: 0 additions & 8 deletions test/elixir_boilerplate_web/home/controller_test.exs

This file was deleted.

35 changes: 14 additions & 21 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,35 @@ defmodule ElixirBoilerplateWeb.ConnCase do
Such tests rely on `Phoenix.ConnTest` and also
import other functionality to make it easier
to build common datastructures and query the data layer.
to build common data structures and query the data layer.
Finally, if the test case interacts with the database,
it cannot be async. For this reason, every test runs
inside a transaction which is reset at the beginning
of the test unless the test case is marked as async.
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
PostgreSQL, you can even run database tests asynchronously
by setting `use ElixirBoilerplateWeb.ConnCase, async: true`, although
this option is not recommended for other databases.
"""

use ExUnit.CaseTemplate

alias Ecto.Adapters.SQL.Sandbox
alias ElixirBoilerplate.Repo
alias ElixirBoilerplateWeb.Endpoint
alias Phoenix.ConnTest

using do
quote do
# The default endpoint for testing
@endpoint ElixirBoilerplateWeb.Endpoint

use ElixirBoilerplateWeb, :verified_routes

# Import conveniences for testing with connections
import Plug.Conn
import Phoenix.ConnTest

import ElixirBoilerplateWeb.Router.Helpers

# The default endpoint for testing
@endpoint Endpoint
import ElixirBoilerplateWeb.ConnCase
end
end

setup tags do
:ok = Sandbox.checkout(Repo)

unless tags[:async] do
Sandbox.mode(Repo, {:shared, self()})
end

{:ok, conn: %{ConnTest.build_conn() | host: host()}}
ElixirBoilerplate.DataCase.setup_sandbox(tags)
{:ok, conn: %{Phoenix.ConnTest.build_conn() | host: host()}}
end

defp host, do: Application.get_env(:elixir_boilerplate, :canonical_host)
Expand Down
35 changes: 17 additions & 18 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ defmodule ElixirBoilerplate.DataCase do
your tests.
Finally, if the test case interacts with the database,
it cannot be async. For this reason, every test runs
inside a transaction which is reset at the beginning
of the test unless the test case is marked as async.
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
PostgreSQL, you can even run database tests asynchronously
by setting `use ElixirBoilerplate.DataCase, async: true`, although
this option is not recommended for other databases.
"""

use ExUnit.CaseTemplate

alias Ecto.Adapters.SQL.Sandbox
alias Ecto.Changeset
alias ElixirBoilerplate.Repo

using do
quote do
alias ElixirBoilerplate.Repo
Expand All @@ -30,27 +28,28 @@ defmodule ElixirBoilerplate.DataCase do
end

setup tags do
:ok = Sandbox.checkout(Repo)

unless tags[:async] do
Sandbox.mode(Repo, {:shared, self()})
end

ElixirBoilerplate.DataCase.setup_sandbox(tags)
:ok
end

@doc """
A helper that transform changeset errors to a map of messages.
Sets up the sandbox based on the test tags.
"""
def setup_sandbox(tags) do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(ElixirBoilerplate.Repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
end

@doc """
A helper that transforms changeset errors into a map of messages.
assert {:error, changeset} = Accounts.create_user(%{password: "short"})
assert "password is too short" in errors_on(changeset).password
assert %{password: ["password is too short"]} = errors_on(changeset)
"""
def errors_on(changeset) do
Changeset.traverse_errors(changeset, fn {message, opts} ->
Enum.reduce(opts, message, fn {key, value}, acc ->
String.replace(acc, "%{#{key}}", to_string(value))
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
end
Expand Down

0 comments on commit 05374bf

Please sign in to comment.