From e5b80f03389dc170952b31c0d9fb22dc48dd0aa9 Mon Sep 17 00:00:00 2001 From: shibao Date: Wed, 16 Feb 2022 19:46:25 -0500 Subject: [PATCH] fix test fixtures --- test/support/conn_case.ex | 4 +- test/support/data_case.ex | 2 +- test/support/fixtures.ex | 130 ++++++++++++++++++ test/support/fixtures/accounts_fixtures.ex | 38 ----- .../support/fixtures/activity_log_fixtures.ex | 22 --- 5 files changed, 134 insertions(+), 62 deletions(-) create mode 100644 test/support/fixtures.ex delete mode 100644 test/support/fixtures/accounts_fixtures.ex delete mode 100644 test/support/fixtures/activity_log_fixtures.ex diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 3c8d17e..1d87d4d 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -16,6 +16,7 @@ defmodule CanneryWeb.ConnCase do """ use ExUnit.CaseTemplate + import Cannery.Fixtures alias Ecto.Adapters.SQL.Sandbox using do @@ -23,6 +24,7 @@ defmodule CanneryWeb.ConnCase do # Import conveniences for testing with connections import Plug.Conn import Phoenix.ConnTest + import Cannery.Fixtures import CanneryWeb.ConnCase alias CanneryWeb.Router.Helpers, as: Routes @@ -47,7 +49,7 @@ defmodule CanneryWeb.ConnCase do test context. """ 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} end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 784d2cc..a902260 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -25,7 +25,7 @@ defmodule Cannery.DataCase do import Ecto.Changeset import Ecto.Query import Cannery.DataCase - import Cannery.AccountsFixtures + import Cannery.Fixtures end end diff --git a/test/support/fixtures.ex b/test/support/fixtures.ex new file mode 100644 index 0000000..18ae42c --- /dev/null +++ b/test/support/fixtures.ex @@ -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 diff --git a/test/support/fixtures/accounts_fixtures.ex b/test/support/fixtures/accounts_fixtures.ex deleted file mode 100644 index b0e6dad..0000000 --- a/test/support/fixtures/accounts_fixtures.ex +++ /dev/null @@ -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 diff --git a/test/support/fixtures/activity_log_fixtures.ex b/test/support/fixtures/activity_log_fixtures.ex deleted file mode 100644 index 0105b47..0000000 --- a/test/support/fixtures/activity_log_fixtures.ex +++ /dev/null @@ -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