data_case.ex 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. defmodule Openpod.DataCase do
  2. @moduledoc """
  3. This module defines the setup for tests requiring
  4. access to the application's data layer.
  5. You may define functions here to be used as helpers in
  6. your tests.
  7. Finally, if the test case interacts with the database,
  8. we enable the SQL sandbox, so changes done to the database
  9. are reverted at the end of every test. If you are using
  10. PostgreSQL, you can even run database tests asynchronously
  11. by setting `use Openpod.DataCase, async: true`, although
  12. this option is not recommended for other databases.
  13. """
  14. use ExUnit.CaseTemplate
  15. using do
  16. quote do
  17. alias Openpod.Repo
  18. # import Ecto
  19. # import Ecto.Changeset
  20. # import Ecto.Query
  21. import Openpod.DataCase
  22. end
  23. end
  24. setup tags do
  25. # :ok = Ecto.Adapters.SQL.Sandbox.checkout(Openpod.Repo)
  26. # unless tags[:async] do
  27. # Ecto.Adapters.SQL.Sandbox.mode(Openpod.Repo, {:shared, self()})
  28. # end
  29. :ok
  30. end
  31. @doc """
  32. A helper that transforms changeset errors into a map of messages.
  33. assert {:error, changeset} = Accounts.create_user(%{password: "short"})
  34. assert "password is too short" in errors_on(changeset).password
  35. assert %{password: ["password is too short"]} = errors_on(changeset)
  36. """
  37. def errors_on(changeset) do
  38. # Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
  39. # Regex.replace(~r"%{(\w+)}", message, fn _, key ->
  40. # opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
  41. # end)
  42. # end)
  43. end
  44. end