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.
10 defmacro clear_config(config_path) do
12 clear_config(unquote(config_path)) do
17 defmacro clear_config(config_path, do: yield) do
20 initial_setting = Pleroma.Config.get(unquote(config_path))
22 on_exit(fn -> Pleroma.Config.put(unquote(config_path), initial_setting) end)
28 defmacro clear_config_all(config_path) do
30 clear_config_all(unquote(config_path)) do
35 defmacro clear_config_all(config_path, do: yield) do
38 initial_setting = Pleroma.Config.get(unquote(config_path))
40 on_exit(fn -> Pleroma.Config.put(unquote(config_path), initial_setting) end)
46 defmacro __using__(_opts) do
48 import Pleroma.Tests.Helpers,
56 def to_datetime(naive_datetime) do
58 |> DateTime.from_naive!("Etc/UTC")
59 |> DateTime.truncate(:second)
62 def collect_ids(collection) do
68 def refresh_record(%{id: id, __struct__: model} = _),
69 do: refresh_record(model, %{id: id})
71 def refresh_record(model, %{id: id} = _) do
72 Pleroma.Repo.get_by(model, id: id)
75 # Used for comparing json rendering during tests.
76 def render_json(view, template, assigns) do
77 assigns = Map.new(assigns)
79 view.render(template, assigns)
84 defmacro guards_config(config_path) do
86 initial_setting = Pleroma.Config.get(config_path)
88 Pleroma.Config.put(config_path, true)
89 on_exit(fn -> Pleroma.Config.put(config_path, initial_setting) end)