cannery/test/support/fixtures.ex

163 lines
4.4 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
"""
alias Cannery.{
Accounts,
Accounts.User,
ActivityLog.ShotGroup,
Ammo,
Ammo.AmmoGroup,
Ammo.AmmoType,
Containers,
2022-02-16 20:42:33 -05:00
Containers.Container,
Email,
2022-11-23 21:14:29 -05:00
Repo,
2022-02-16 20:42:33 -05:00
Tags,
Tags.Tag
2022-02-16 19:46:25 -05:00
}
def unique_user_email, do: "user#{System.unique_integer()}@example.com"
def valid_user_password, do: "hello world!"
@spec user_fixture() :: Accounts.User.t()
@spec user_fixture(attrs :: map()) :: Accounts.User.t()
def user_fixture(attrs \\ %{}) do
2022-02-16 20:42:33 -05:00
attrs
|> Enum.into(%{
"email" => unique_user_email(),
"password" => valid_user_password()
})
|> Accounts.register_user()
|> unwrap_ok_tuple()
2022-02-16 19:46:25 -05:00
end
@spec admin_fixture() :: Accounts.User.t()
@spec admin_fixture(attrs :: map()) :: Accounts.User.t()
def admin_fixture(attrs \\ %{}) do
2022-02-16 20:42:33 -05:00
attrs
|> Enum.into(%{
"email" => unique_user_email(),
2022-11-23 21:14:29 -05:00
"password" => valid_user_password()
2022-02-16 20:42:33 -05:00
})
|> Accounts.register_user()
|> unwrap_ok_tuple()
2022-11-23 21:14:29 -05:00
|> User.role_changeset("admin")
|> 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
"""
@spec shot_group_fixture(User.t(), AmmoGroup.t()) :: ShotGroup.t()
@spec shot_group_fixture(attrs :: map(), User.t(), AmmoGroup.t()) :: ShotGroup.t()
def shot_group_fixture(attrs \\ %{}, %User{} = user, %AmmoGroup{} = ammo_group) do
2022-02-16 20:42:33 -05:00
attrs
|> Enum.into(%{
"count" => 20,
2022-02-16 20:42:33 -05:00
"date" => ~N[2022-02-13 03:17:00],
"notes" => "some notes"
})
|> Cannery.ActivityLog.create_shot_group(user, ammo_group)
|> 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
|> Enum.into(%{"name" => "My container", "type" => "Ammo can"})
|> 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
|> Enum.into(%{"name" => "ammo_type"})
|> Ammo.create_ammo_type(user)
|> unwrap_ok_tuple()
2022-02-16 19:46:25 -05:00
end
@doc """
Generate a AmmoGroup
"""
2022-02-24 00:16:23 -05:00
@spec ammo_group_fixture(AmmoType.t(), Container.t(), User.t()) ::
{count :: non_neg_integer(), [AmmoGroup.t()]}
@spec ammo_group_fixture(attrs :: map(), AmmoType.t(), Container.t(), User.t()) ::
{count :: non_neg_integer(), [AmmoGroup.t()]}
@spec ammo_group_fixture(
attrs :: map(),
multiplier :: non_neg_integer(),
AmmoType.t(),
Container.t(),
User.t()
) :: {count :: non_neg_integer(), [AmmoGroup.t()]}
2022-02-16 19:46:25 -05:00
def ammo_group_fixture(
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(%{
"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
})
2022-02-24 00:16:23 -05:00
|> Ammo.create_ammo_groups(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(%{
"bg_color" => "some bg-color",
"name" => "some name",
"text_color" => "some text-color"
})
|> Tags.create_tag(user)
|> 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