mirror of
https://github.com/teslamate-org/teslamate.git
synced 2026-01-24 21:06:08 +08:00
56 lines
1.5 KiB
Elixir
56 lines
1.5 KiB
Elixir
defmodule TeslaMate.DataCase do
|
|
@moduledoc """
|
|
This module defines the setup for tests requiring
|
|
access to the application's data layer.
|
|
|
|
You may define functions here to be used as helpers in
|
|
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.
|
|
"""
|
|
|
|
use ExUnit.CaseTemplate
|
|
|
|
using do
|
|
quote do
|
|
alias TeslaMate.Repo
|
|
|
|
import Ecto
|
|
import Ecto.Changeset
|
|
import Ecto.Query
|
|
import TeslaMate.DataCase
|
|
end
|
|
end
|
|
|
|
setup tags do
|
|
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(TeslaMate.Repo, shared: not tags[:async])
|
|
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
|
|
:ok
|
|
rescue
|
|
e in [MatchError] ->
|
|
case e.term do
|
|
{:error, {{:badmatch, :already_shared}, _}} -> :ok
|
|
_ -> reraise e, __STACKTRACE__
|
|
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
|
|
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
|