1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Tests.Helpers do
7 Helpers for use in tests.
11 defmacro clear_config(config_path) do
13 clear_config(unquote(config_path)) do
18 defmacro clear_config(config_path, do: yield) do
21 initial_setting = Config.get(unquote(config_path))
23 on_exit(fn -> Config.put(unquote(config_path), initial_setting) end)
29 @doc "Stores initial config value and restores it after *all* test examples are executed."
30 defmacro clear_config_all(config_path) do
32 clear_config_all(unquote(config_path)) do
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).
42 defmacro clear_config_all(config_path, do: yield) do
45 initial_setting = Config.get(unquote(config_path))
47 on_exit(fn -> Config.put(unquote(config_path), initial_setting) end)
53 defmacro __using__(_opts) do
55 import Pleroma.Tests.Helpers,
63 def to_datetime(naive_datetime) do
65 |> DateTime.from_naive!("Etc/UTC")
66 |> DateTime.truncate(:second)
69 def collect_ids(collection) do
75 def refresh_record(%{id: id, __struct__: model} = _),
76 do: refresh_record(model, %{id: id})
78 def refresh_record(model, %{id: id} = _) do
79 Pleroma.Repo.get_by(model, id: id)
82 # Used for comparing json rendering during tests.
83 def render_json(view, template, assigns) do
84 assigns = Map.new(assigns)
86 view.render(template, assigns)
91 def stringify_keys(nil), do: nil
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)
96 def stringify_keys(map) when is_map(map) do
98 |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
102 def stringify_keys([head | rest] = list) when is_list(list) do
103 [stringify_keys(head) | stringify_keys(rest)]
106 def stringify_keys(key), do: key
108 defmacro guards_config(config_path) do
110 initial_setting = Config.get(config_path)
112 Config.put(config_path, true)
113 on_exit(fn -> Config.put(config_path, initial_setting) end)