1 defmodule TechRadarEditor.ModelCase do
3 This module defines the test case to be used by
6 You may define functions here to be used as helpers in
7 your model tests. See `errors_on/2`'s definition as reference.
9 Finally, if the test case interacts with the database,
10 it cannot be async. For this reason, every test runs
11 inside a transaction which is reset at the beginning
12 of the test unless the test case is marked as async.
15 use ExUnit.CaseTemplate
19 alias TechRadarEditor.Repo
24 import TechRadarEditor.ModelCase
29 :ok = Ecto.Adapters.SQL.Sandbox.checkout(TechRadarEditor.Repo)
31 unless tags[:async] do
32 Ecto.Adapters.SQL.Sandbox.mode(TechRadarEditor.Repo, {:shared, self()})
39 Helper for returning list of errors in a struct when given certain data.
43 Given a User schema that lists `:name` as a required field and validates
44 `:password` to be safe, it would return:
46 iex> errors_on(%User{}, %{password: "password"})
47 [password: "is unsafe", name: "is blank"]
49 You could then write your assertion like:
51 assert {:password, "is unsafe"} in errors_on(%User{}, %{password: "password"})
53 You can also create the changeset manually and retrieve the errors
56 iex> changeset = User.changeset(%User{}, password: "password")
57 iex> {:password, "is unsafe"} in changeset.errors
60 def errors_on(struct, data) do
61 struct.__struct__.changeset(struct, data)
62 |> Ecto.Changeset.traverse_errors(&TechRadarEditor.ErrorHelpers.translate_error/1)
63 |> Enum.flat_map(fn {key, errors} -> for msg <- errors, do: {key, msg} end)