I have multiple tests in one file. I want to create users at the beginning of each test and roll back those db changes at the end of each test.
At the top of my file I have this:
use Teiserver.DataCase, async: true
which references this module
defmodule Teiserver.DataCase do
use ExUnit.CaseTemplate
using do
quote do
alias Teiserver.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import Teiserver.DataCase
end
end
@doc """
Sets up the sandbox based on the test tags.
"""
def setup_sandbox(tags) do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Teiserver.Repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
end
setup tags do
setup_sandbox(tags)
:ok
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
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
end
However it seems like my changes are persisted between tests.
Also test_helper.exs has this
ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(Teiserver.Repo, :manual)
alias Teiserver.Repo
alias Central.Helpers.GeneralTestLib
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo, sandbox: false)
if not GeneralTestLib.seeded?() do
GeneralTestLib.seed()
Teiserver.TeiserverTestLib.seed()
end
Ecto.Adapters.SQL.Sandbox.checkin(Repo)
How do I make it so any db changes are rolled back after each test?
You can see the video in this link where I run the same test multiple times and it will just randomly fail eventually.