cannery/test/support/fixtures.ex

161 lines
4.1 KiB
Elixir
Raw Normal View History

2022-02-16 19:46:25 -05:00
defmodule Cannery.Fixtures do
@moduledoc """
This module defines test helpers for creating entities
"""
2023-03-23 22:07:25 -04:00
import Cannery.DataCase
2022-02-16 19:46:25 -05:00
alias Cannery.{
Accounts,
Accounts.User,
ActivityLog.ShotGroup,
Ammo,
Ammo.AmmoType,
2023-03-29 23:49:45 -04:00
Ammo.Pack,
2022-02-16 19:46:25 -05:00
Containers,
2022-02-16 20:42:33 -05:00
Containers.Container,
Containers.Tag,
2022-02-16 20:42:33 -05:00
Email,
Repo
2022-02-16 19:46:25 -05:00
}
2023-01-29 13:05:01 -05:00
@spec user_fixture() :: User.t()
@spec user_fixture(attrs :: map()) :: User.t()
2022-02-16 19:46:25 -05:00
def user_fixture(attrs \\ %{}) do
2022-02-16 20:42:33 -05:00
attrs
|> Enum.into(%{
2023-03-28 21:57:29 -04:00
email: unique_user_email(),
password: valid_user_password()
2022-02-16 20:42:33 -05:00
})
|> Accounts.register_user()
|> unwrap_ok_tuple()
2022-02-16 19:46:25 -05:00
end
2023-01-29 13:05:01 -05:00
@spec admin_fixture() :: User.t()
@spec admin_fixture(attrs :: map()) :: User.t()
2022-02-16 19:46:25 -05:00
def admin_fixture(attrs \\ %{}) do
2022-02-16 20:42:33 -05:00
attrs
|> Enum.into(%{
2023-03-28 21:57:29 -04:00
email: unique_user_email(),
password: valid_user_password()
2022-02-16 20:42:33 -05:00
})
|> Accounts.register_user()
|> unwrap_ok_tuple()
2023-01-29 12:57:07 -05:00
|> User.role_changeset(:admin)
2022-11-23 21:14:29 -05:00
|> Repo.update!()
2022-02-16 19:46:25 -05:00
end
def extract_user_token(fun) do
2022-02-16 20:42:33 -05:00
%{args: %{attrs: attrs, email: email_key, user_id: user_id}} = fun.(&"[TOKEN]#{&1}[TOKEN]")
# convert atoms to string keys
attrs = attrs |> Map.new(fn {atom_key, value} -> {atom_key |> Atom.to_string(), value} end)
email =
email_key
|> Atom.to_string()
|> Email.generate_email(Accounts.get_user!(user_id), attrs)
2022-02-18 18:25:28 -05:00
[_, html_token | _] = email.html_body |> String.split("[TOKEN]")
[_, text_token | _] = email.text_body |> String.split("[TOKEN]")
2022-02-17 21:30:05 -05:00
^text_token = html_token
2022-02-16 19:46:25 -05:00
end
def valid_user_attributes(attrs \\ %{}) do
Enum.into(attrs, %{
email: unique_user_email(),
password: valid_user_password()
})
end
@doc """
Generate a ShotGroup
"""
2023-03-29 22:54:55 -04:00
@spec shot_group_fixture(User.t(), Pack.t()) :: ShotGroup.t()
@spec shot_group_fixture(attrs :: map(), User.t(), Pack.t()) :: ShotGroup.t()
def shot_group_fixture(attrs \\ %{}, %User{} = user, %Pack{} = pack) do
2022-02-16 20:42:33 -05:00
attrs
|> Enum.into(%{
2023-03-28 21:57:29 -04:00
count: 20,
date: ~N[2022-02-13 03:17:00],
notes: random_string()
2022-02-16 20:42:33 -05:00
})
2023-03-29 22:54:55 -04:00
|> Cannery.ActivityLog.create_shot_group(user, pack)
2022-02-16 20:42:33 -05:00
|> unwrap_ok_tuple()
2022-02-16 19:46:25 -05:00
end
@doc """
Generate a Container
"""
@spec container_fixture(User.t()) :: Container.t()
@spec container_fixture(attrs :: map(), User.t()) :: Container.t()
def container_fixture(attrs \\ %{}, %User{} = user) do
2022-02-16 20:42:33 -05:00
attrs
2023-03-28 21:57:29 -04:00
|> Enum.into(%{name: random_string(), type: "Ammo can"})
2022-02-16 20:42:33 -05:00
|> Containers.create_container(user)
|> unwrap_ok_tuple()
2022-02-16 19:46:25 -05:00
end
@doc """
Generate a AmmoType
"""
@spec ammo_type_fixture(User.t()) :: AmmoType.t()
@spec ammo_type_fixture(attrs :: map(), User.t()) :: AmmoType.t()
def ammo_type_fixture(attrs \\ %{}, %User{} = user) do
2022-02-16 20:42:33 -05:00
attrs
2023-03-28 23:08:40 -04:00
|> Enum.into(%{name: random_string(), class: :rifle})
2022-02-16 20:42:33 -05:00
|> Ammo.create_ammo_type(user)
|> unwrap_ok_tuple()
2022-02-16 19:46:25 -05:00
end
@doc """
2023-03-29 22:54:55 -04:00
Generate a Pack
2022-02-16 19:46:25 -05:00
"""
2023-03-29 22:54:55 -04:00
@spec pack_fixture(AmmoType.t(), Container.t(), User.t()) ::
{count :: non_neg_integer(), [Pack.t()]}
@spec pack_fixture(attrs :: map(), AmmoType.t(), Container.t(), User.t()) ::
{count :: non_neg_integer(), [Pack.t()]}
@spec pack_fixture(
2022-02-24 00:16:23 -05:00
attrs :: map(),
multiplier :: non_neg_integer(),
AmmoType.t(),
Container.t(),
User.t()
2023-03-29 22:54:55 -04:00
) :: {count :: non_neg_integer(), [Pack.t()]}
def pack_fixture(
2022-02-16 19:46:25 -05:00
attrs \\ %{},
2022-02-24 00:16:23 -05:00
multiplier \\ 1,
2022-02-16 19:46:25 -05:00
%AmmoType{id: ammo_type_id},
%Container{id: container_id},
%User{} = user
) do
2022-02-16 20:42:33 -05:00
attrs
|> Enum.into(%{
2023-03-28 21:57:29 -04:00
ammo_type_id: ammo_type_id,
container_id: container_id,
count: 20,
purchased_on: Date.utc_today()
2022-02-16 20:42:33 -05:00
})
2023-03-29 22:54:55 -04:00
|> Ammo.create_packs(multiplier, user)
2022-02-16 20:42:33 -05:00
|> unwrap_ok_tuple()
end
2022-02-16 19:46:25 -05:00
2022-02-16 20:42:33 -05:00
@doc """
Generates a Tag
"""
@spec tag_fixture(User.t()) :: Tag.t()
@spec tag_fixture(attrs :: map(), User.t()) :: Tag.t()
def tag_fixture(attrs \\ %{}, %User{} = user) do
attrs
|> Enum.into(%{
2023-03-28 21:57:29 -04:00
bg_color: "#100000",
name: random_string(),
text_color: "#000000"
2022-02-16 20:42:33 -05:00
})
|> Containers.create_tag(user)
2022-02-16 20:42:33 -05:00
|> unwrap_ok_tuple()
2022-02-16 19:46:25 -05:00
end
2022-02-16 20:42:33 -05:00
defp unwrap_ok_tuple({:ok, value}), do: value
2022-02-16 19:46:25 -05:00
end