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
20 initial_setting = Config.get(unquote(config_path))
22 on_exit(fn -> Config.put(unquote(config_path), initial_setting) end)
27 defmacro clear_config(config_path, temp_setting) do
29 clear_config(unquote(config_path)) do
30 Config.put(unquote(config_path), unquote(temp_setting))
35 defmacro __using__(_opts) do
37 import Pleroma.Tests.Helpers,
43 def to_datetime(%NaiveDateTime{} = naive_datetime) do
45 |> DateTime.from_naive!("Etc/UTC")
46 |> DateTime.truncate(:second)
49 def to_datetime(datetime) when is_binary(datetime) do
51 |> NaiveDateTime.from_iso8601!()
55 def collect_ids(collection) do
61 def refresh_record(%{id: id, __struct__: model} = _),
62 do: refresh_record(model, %{id: id})
64 def refresh_record(model, %{id: id} = _) do
65 Pleroma.Repo.get_by(model, id: id)
68 # Used for comparing json rendering during tests.
69 def render_json(view, template, assigns) do
70 assigns = Map.new(assigns)
72 view.render(template, assigns)
77 def stringify_keys(nil), do: nil
79 def stringify_keys(key) when key in [true, false], do: key
80 def stringify_keys(key) when is_atom(key), do: Atom.to_string(key)
82 def stringify_keys(map) when is_map(map) do
84 |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
88 def stringify_keys([head | rest] = list) when is_list(list) do
89 [stringify_keys(head) | stringify_keys(rest)]
92 def stringify_keys(key), do: key
94 defmacro guards_config(config_path) do
96 initial_setting = Config.get(config_path)
98 Config.put(config_path, true)
99 on_exit(fn -> Config.put(config_path, initial_setting) end)