You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
decentralised_book_index/test/support/helpers.ex

62 lines
1.5 KiB

defmodule DecentralisedBookIndex.Helpers do
@moduledoc "Helpers for tests"
@doc """
Return a new map with same keys as in map_with_keys.
"""
def get_submap(map, map_with_keys) when is_map(map_with_keys) do
Map.take(map, Map.keys(map_with_keys))
end
def get_submap(map, keys) when is_list(keys) do
Map.take(map, keys)
end
def get_submaps(map_list, keys) do
Enum.map(map_list, fn map ->
get_submap(map, keys)
end)
end
def datetime_from_iso_8601(datetime_string) do
case DateTime.from_iso8601(datetime_string) do
{:ok, datetime, 0} -> datetime
_ -> nil
end
end
@doc """
Return a new map with updated keys :inserted_at and :updated_at.
Those keys will be transformed from string to DateTime.
"""
def datetime_from_iso8601_for_map(map) do
map
|> Map.update(:inserted_at, nil, &datetime_from_iso_8601/1)
|> Map.update(:updated_at, nil, &datetime_from_iso_8601/1)
end
def datetime_add_second_as_string(datetime, seconds) do
datetime
|> DateTime.add(seconds)
|> DateTime.to_iso8601()
end
def get_ids(records) do
Enum.map(records, fn record -> get_id(record) end)
end
def get_id(record) when is_struct(record), do: record.id
def get_id(record) when is_map(record), do: record[:id]
@doc """
Logs the given `user` into the `conn`.
It returns an updated `conn`.
"""
def log_in_user(conn, user) do
conn
|> Phoenix.ConnTest.init_test_session(%{})
|> AshAuthentication.Plug.Helpers.store_in_session(user)
end
end