Skip to content

Commit

Permalink
refactor:test(ex): use DataCase.setup_sandbox acording to phx.new
Browse files Browse the repository at this point in the history
… template

feat:test(ex/test): add database `setup_sandbox` function to `DataCase`

This allows other test cases to configure the database via a shared function
  • Loading branch information
firestack committed Oct 25, 2024
1 parent 611f99c commit 7a5a742
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
16 changes: 7 additions & 9 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ defmodule SkateWeb.ConnCase do
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 SkateWeb.ConnCase, async: true`, although
this option is not recommended for other databases.
"""

use ExUnit.CaseTemplate
Expand All @@ -32,15 +34,11 @@ defmodule SkateWeb.ConnCase do
end

setup tags do
alias Ecto.Adapters.SQL.Sandbox
:ok = Sandbox.checkout(Skate.Repo)
Skate.DataCase.setup_sandbox(tags)

username = "test_user"
email = "[email protected]"

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

user = User.upsert(username, email)
resource = %{id: user.id}

Expand Down
27 changes: 14 additions & 13 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ defmodule Skate.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 Skate.DataCase, async: true`, although
this option is not recommended for other databases.
"""

use ExUnit.CaseTemplate
Expand All @@ -26,19 +28,18 @@ defmodule Skate.DataCase do
end

setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Skate.Repo)

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

on_exit(fn ->
Ecto.Adapters.SQL.Sandbox.checkin(Skate.Repo)
end)

Skate.DataCase.setup_sandbox(tags)
:ok
end

@doc """
Sets up the sandbox based on the test tags.
"""
def setup_sandbox(tags) do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Skate.Repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
end

@doc """
A helper that transform changeset errors to a map of messages.
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ ExUnit.start(
]
)

Ecto.Adapters.SQL.Sandbox.mode(Skate.Repo, :manual)

Mox.defmock(Skate.OpenRouteServiceAPI.MockClient, for: Skate.OpenRouteServiceAPI.Client)

Application.put_env(:skate, Skate.OpenRouteServiceAPI,
Expand Down

0 comments on commit 7a5a742

Please sign in to comment.