data_case.ex 1.94 KB
Newer Older
lain's avatar
lain committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
defmodule Pleroma.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 Pleroma.Repo

      import Ecto
      import Ecto.Changeset
      import Ecto.Query
      import Pleroma.DataCase
Maksim's avatar
Maksim committed
25
      use Pleroma.Tests.Helpers
lain's avatar
lain committed
26
27
28
29
    end
  end

  setup tags do
lain's avatar
lain committed
30
    Cachex.clear(:user_cache)
lain's avatar
lain committed
31
32
33
34
35
36
37
38
39
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)

    unless tags[:async] do
      Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
    end

    :ok
  end

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  def ensure_local_uploader(_context) do
    uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
    filters = Pleroma.Config.get([Pleroma.Upload, :filters])

    unless uploader == Pleroma.Uploaders.Local || filters != [] do
      Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
      Pleroma.Config.put([Pleroma.Upload, :filters], [])

      on_exit(fn ->
        Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
        Pleroma.Config.put([Pleroma.Upload, :filters], filters)
      end)
    end

    :ok
  end

lain's avatar
lain committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  @doc """
  A helper that transform changeset errors to a map of messages.

      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} ->
      Enum.reduce(opts, message, fn {key, value}, acc ->
        String.replace(acc, "%{#{key}}", to_string(value))
      end)
    end)
  end
end