Merge branch 'remake-remodel' into develop
[akkoma] / test / support / helpers.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Tests.Helpers do
6 @moduledoc """
7 Helpers for use in tests.
8 """
9 alias Pleroma.Config
10
11 defmacro clear_config(config_path) do
12 quote do
13 clear_config(unquote(config_path)) do
14 end
15 end
16 end
17
18 defmacro clear_config(config_path, do: yield) do
19 quote do
20 setup do
21 initial_setting = Config.get(unquote(config_path))
22 unquote(yield)
23 on_exit(fn -> Config.put(unquote(config_path), initial_setting) end)
24 :ok
25 end
26 end
27 end
28
29 @doc "Stores initial config value and restores it after *all* test examples are executed."
30 defmacro clear_config_all(config_path) do
31 quote do
32 clear_config_all(unquote(config_path)) do
33 end
34 end
35 end
36
37 @doc """
38 Stores initial config value and restores it after *all* test examples are executed.
39 Only use if *all* test examples should work with the same stubbed value
40 (*no* examples set a different value).
41 """
42 defmacro clear_config_all(config_path, do: yield) do
43 quote do
44 setup_all do
45 initial_setting = Config.get(unquote(config_path))
46 unquote(yield)
47 on_exit(fn -> Config.put(unquote(config_path), initial_setting) end)
48 :ok
49 end
50 end
51 end
52
53 defmacro __using__(_opts) do
54 quote do
55 import Pleroma.Tests.Helpers,
56 only: [
57 clear_config: 1,
58 clear_config: 2,
59 clear_config_all: 1,
60 clear_config_all: 2
61 ]
62
63 def to_datetime(naive_datetime) do
64 naive_datetime
65 |> DateTime.from_naive!("Etc/UTC")
66 |> DateTime.truncate(:second)
67 end
68
69 def collect_ids(collection) do
70 collection
71 |> Enum.map(& &1.id)
72 |> Enum.sort()
73 end
74
75 def refresh_record(%{id: id, __struct__: model} = _),
76 do: refresh_record(model, %{id: id})
77
78 def refresh_record(model, %{id: id} = _) do
79 Pleroma.Repo.get_by(model, id: id)
80 end
81
82 # Used for comparing json rendering during tests.
83 def render_json(view, template, assigns) do
84 assigns = Map.new(assigns)
85
86 view.render(template, assigns)
87 |> Poison.encode!()
88 |> Poison.decode!()
89 end
90
91 def stringify_keys(nil), do: nil
92
93 def stringify_keys(key) when key in [true, false], do: key
94 def stringify_keys(key) when is_atom(key), do: Atom.to_string(key)
95
96 def stringify_keys(map) when is_map(map) do
97 map
98 |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
99 |> Enum.into(%{})
100 end
101
102 def stringify_keys([head | rest] = list) when is_list(list) do
103 [stringify_keys(head) | stringify_keys(rest)]
104 end
105
106 def stringify_keys(key), do: key
107
108 defmacro guards_config(config_path) do
109 quote do
110 initial_setting = Config.get(config_path)
111
112 Config.put(config_path, true)
113 on_exit(fn -> Config.put(config_path, initial_setting) end)
114 end
115 end
116 end
117 end
118 end