-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor:test(ex): use
DataCase.setup_sandbox
acording to phx.new
…
… 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
Showing
3 changed files
with
23 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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} | ||
|
||
|
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