fix test fixtures

This commit is contained in:
shibao 2022-02-16 19:46:25 -05:00
parent d162a7d5c3
commit e5b80f0338
5 changed files with 134 additions and 62 deletions

View File

@ -16,6 +16,7 @@ defmodule CanneryWeb.ConnCase do
""" """
use ExUnit.CaseTemplate use ExUnit.CaseTemplate
import Cannery.Fixtures
alias Ecto.Adapters.SQL.Sandbox alias Ecto.Adapters.SQL.Sandbox
using do using do
@ -23,6 +24,7 @@ defmodule CanneryWeb.ConnCase do
# Import conveniences for testing with connections # Import conveniences for testing with connections
import Plug.Conn import Plug.Conn
import Phoenix.ConnTest import Phoenix.ConnTest
import Cannery.Fixtures
import CanneryWeb.ConnCase import CanneryWeb.ConnCase
alias CanneryWeb.Router.Helpers, as: Routes alias CanneryWeb.Router.Helpers, as: Routes
@ -47,7 +49,7 @@ defmodule CanneryWeb.ConnCase do
test context. test context.
""" """
def register_and_log_in_user(%{conn: conn}) do def register_and_log_in_user(%{conn: conn}) do
user = Cannery.AccountsFixtures.user_fixture() user = user_fixture()
%{conn: log_in_user(conn, user), user: user} %{conn: log_in_user(conn, user), user: user}
end end

View File

@ -25,7 +25,7 @@ defmodule Cannery.DataCase do
import Ecto.Changeset import Ecto.Changeset
import Ecto.Query import Ecto.Query
import Cannery.DataCase import Cannery.DataCase
import Cannery.AccountsFixtures import Cannery.Fixtures
end end
end end

130
test/support/fixtures.ex Normal file
View File

@ -0,0 +1,130 @@
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,
Containers.Container
}
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
{:ok, user} =
attrs
|> Enum.into(%{
"email" => unique_user_email(),
"password" => valid_user_password()
})
|> Accounts.register_user()
user
end
@spec admin_fixture() :: Accounts.User.t()
@spec admin_fixture(attrs :: map()) :: Accounts.User.t()
def admin_fixture(attrs \\ %{}) do
{:ok, user} =
attrs
|> Enum.into(%{
"email" => unique_user_email(),
"password" => valid_user_password(),
"role" => "admin"
})
|> Accounts.register_user()
user
end
def extract_user_token(fun) do
{:ok, captured} = fun.(&"[TOKEN]#{&1}[TOKEN]")
[_, token, _] = String.split(captured.body, "[TOKEN]")
token
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
{:ok, shot_group} =
attrs
|> Enum.into(%{
"count" => 25,
"date" => ~N[2022-02-13 03:17:00],
"notes" => "some notes"
})
|> Cannery.ActivityLog.create_shot_group(user, ammo_group)
shot_group
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
{:ok, container} =
attrs
|> Enum.into(%{"name" => "My container", "type" => "Ammo can"})
|> Containers.create_container(user)
container
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
{:ok, ammo_type} =
attrs
|> Enum.into(%{"name" => "ammo_type"})
|> Ammo.create_ammo_type(user)
ammo_type
end
@doc """
Generate a AmmoGroup
"""
@spec ammo_group_fixture(AmmoType.t(), Container.t(), User.t()) :: AmmoGroup.t()
@spec ammo_group_fixture(attrs :: map(), AmmoType.t(), Container.t(), User.t()) :: AmmoGroup.t()
def ammo_group_fixture(
attrs \\ %{},
%AmmoType{id: ammo_type_id},
%Container{id: container_id},
%User{} = user
) do
{:ok, ammo_group} =
attrs
|> Enum.into(%{
"ammo_type_id" => ammo_type_id,
"container_id" => container_id,
"count" => 20
})
|> Ammo.create_ammo_group(user)
ammo_group
end
end

View File

@ -1,38 +0,0 @@
defmodule Cannery.AccountsFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Cannery.Accounts` context.
"""
alias Cannery.Accounts
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
{:ok, user} =
attrs
|> Enum.into(%{
"email" => unique_user_email(),
"password" => valid_user_password()
})
|> Accounts.register_user()
user
end
def extract_user_token(fun) do
{:ok, captured} = fun.(&"[TOKEN]#{&1}[TOKEN]")
[_, token, _] = String.split(captured.body, "[TOKEN]")
token
end
def valid_user_attributes(attrs \\ %{}) do
Enum.into(attrs, %{
email: unique_user_email(),
password: valid_user_password()
})
end
end

View File

@ -1,22 +0,0 @@
defmodule Cannery.ActivityLogFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Cannery.ActivityLog` context.
"""
@doc """
Generate a shot_group.
"""
def shot_group_fixture(attrs \\ %{}) do
{:ok, shot_group} =
attrs
|> Enum.into(%{
count: 42,
date: ~N[2022-02-13 03:17:00],
notes: "some notes"
})
|> Cannery.ActivityLog.create_shot_group()
shot_group
end
end