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,
|
2023-03-30 20:43:30 -04:00
|
|
|
ActivityLog.ShotRecord,
|
2022-02-16 19:46:25 -05:00
|
|
|
Ammo,
|
2023-03-29 23:49:45 -04:00
|
|
|
Ammo.Pack,
|
2023-03-30 21:53:52 -04:00
|
|
|
Ammo.Type,
|
2022-02-16 19:46:25 -05:00
|
|
|
Containers,
|
2022-02-16 20:42:33 -05:00
|
|
|
Containers.Container,
|
2023-03-18 21:06:00 -04:00
|
|
|
Containers.Tag,
|
2022-02-16 20:42:33 -05:00
|
|
|
Email,
|
2023-03-18 21:06:00 -04:00
|
|
|
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]")
|
|
|
|
|
2024-07-28 11:35:45 -04:00
|
|
|
attrs =
|
|
|
|
attrs
|
|
|
|
|> Map.new(fn {key, value} -> {Atom.to_string(key), value} end)
|
|
|
|
|
2022-02-16 20:42:33 -05:00
|
|
|
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 """
|
2023-03-30 20:43:30 -04:00
|
|
|
Generate a ShotRecord
|
2022-02-16 19:46:25 -05:00
|
|
|
"""
|
2023-03-30 20:43:30 -04:00
|
|
|
@spec shot_record_fixture(User.t(), Pack.t()) :: ShotRecord.t()
|
|
|
|
@spec shot_record_fixture(attrs :: map(), User.t(), Pack.t()) :: ShotRecord.t()
|
|
|
|
def shot_record_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-30 20:43:30 -04:00
|
|
|
|> Cannery.ActivityLog.create_shot_record(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-30 00:07:38 -04:00
|
|
|
|> Enum.into(%{
|
|
|
|
name: random_string(),
|
|
|
|
type: "Ammo can",
|
|
|
|
location: random_string(),
|
|
|
|
desc: random_string()
|
|
|
|
})
|
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 """
|
2023-03-30 21:53:52 -04:00
|
|
|
Generate a Type
|
2022-02-16 19:46:25 -05:00
|
|
|
"""
|
2023-03-30 21:53:52 -04:00
|
|
|
@spec type_fixture(User.t()) :: Type.t()
|
|
|
|
@spec type_fixture(attrs :: map(), User.t()) :: Type.t()
|
|
|
|
def type_fixture(attrs \\ %{}, %User{} = user) do
|
2022-02-16 20:42:33 -05:00
|
|
|
attrs
|
2023-03-30 00:07:38 -04:00
|
|
|
|> Enum.into(%{
|
|
|
|
name: random_string(),
|
|
|
|
class: :rifle,
|
|
|
|
desc: random_string(),
|
|
|
|
bullet_type: random_string(),
|
|
|
|
bullet_core: random_string(),
|
|
|
|
cartridge: random_string(),
|
|
|
|
caliber: random_string(),
|
|
|
|
case_material: random_string(),
|
|
|
|
jacket_type: random_string(),
|
|
|
|
muzzle_velocity: 3,
|
|
|
|
powder_type: random_string(),
|
|
|
|
powder_grains_per_charge: 5,
|
|
|
|
grains: 7,
|
|
|
|
pressure: random_string(),
|
|
|
|
primer_type: random_string(),
|
|
|
|
firing_type: random_string(),
|
|
|
|
wadding: random_string(),
|
|
|
|
shot_type: random_string(),
|
|
|
|
shot_material: random_string(),
|
|
|
|
shot_size: random_string(),
|
|
|
|
unfired_length: random_string(),
|
|
|
|
brass_height: random_string(),
|
|
|
|
chamber_size: random_string(),
|
|
|
|
load_grains: 9,
|
|
|
|
shot_charge_weight: random_string(),
|
|
|
|
dram_equivalent: random_string(),
|
|
|
|
tracer: false,
|
|
|
|
incendiary: false,
|
|
|
|
blank: false,
|
|
|
|
corrosive: false,
|
|
|
|
manufacturer: random_string(),
|
|
|
|
upc: random_string()
|
|
|
|
})
|
2023-03-30 21:53:52 -04:00
|
|
|
|> Ammo.create_type(user)
|
2022-02-16 20:42:33 -05:00
|
|
|
|> 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-30 21:53:52 -04:00
|
|
|
@spec pack_fixture(Type.t(), Container.t(), User.t()) ::
|
2023-03-29 22:54:55 -04:00
|
|
|
{count :: non_neg_integer(), [Pack.t()]}
|
2023-03-30 21:53:52 -04:00
|
|
|
@spec pack_fixture(attrs :: map(), Type.t(), Container.t(), User.t()) ::
|
2023-03-29 22:54:55 -04:00
|
|
|
{count :: non_neg_integer(), [Pack.t()]}
|
|
|
|
@spec pack_fixture(
|
2022-02-24 00:16:23 -05:00
|
|
|
attrs :: map(),
|
|
|
|
multiplier :: non_neg_integer(),
|
2023-03-30 21:53:52 -04:00
|
|
|
Type.t(),
|
2022-02-24 00:16:23 -05:00
|
|
|
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,
|
2023-03-30 21:53:52 -04:00
|
|
|
%Type{id: type_id},
|
2022-02-16 19:46:25 -05:00
|
|
|
%Container{id: container_id},
|
|
|
|
%User{} = user
|
|
|
|
) do
|
2022-02-16 20:42:33 -05:00
|
|
|
attrs
|
|
|
|
|> Enum.into(%{
|
2023-03-30 21:53:52 -04:00
|
|
|
type_id: type_id,
|
2023-03-28 21:57:29 -04:00
|
|
|
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
|
|
|
})
|
2023-03-18 21:06:00 -04: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
|