1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 defmacro clear_config_all(config_path) do
31 clear_config_all(unquote(config_path)) do
36 defmacro clear_config_all(config_path, do: yield) do
39 initial_setting = Config.get(unquote(config_path))
41 on_exit(fn -> Config.put(unquote(config_path), initial_setting) end)
47 defmacro __using__(_opts) do
49 import Pleroma.Tests.Helpers,
57 def to_datetime(naive_datetime) do
59 |> DateTime.from_naive!("Etc/UTC")
60 |> DateTime.truncate(:second)
63 def collect_ids(collection) do
69 def refresh_record(%{id: id, __struct__: model} = _),
70 do: refresh_record(model, %{id: id})
72 def refresh_record(model, %{id: id} = _) do
73 Pleroma.Repo.get_by(model, id: id)
76 # Used for comparing json rendering during tests.
77 def render_json(view, template, assigns) do
78 assigns = Map.new(assigns)
80 view.render(template, assigns)
85 def stringify_keys(nil), do: nil
87 def stringify_keys(key) when key in [true, false], do: key
88 def stringify_keys(key) when is_atom(key), do: Atom.to_string(key)
90 def stringify_keys(map) when is_map(map) do
92 |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
96 def stringify_keys([head | rest] = list) when is_list(list) do
97 [stringify_keys(head) | stringify_keys(rest)]
100 def stringify_keys(key), do: key
102 defmacro guards_config(config_path) do
104 initial_setting = Config.get(config_path)
106 Config.put(config_path, true)
107 on_exit(fn -> Config.put(config_path, initial_setting) end)