pass ammo test

This commit is contained in:
shibao 2022-02-16 20:42:33 -05:00
parent eede6c225d
commit 0721702804
2 changed files with 175 additions and 128 deletions

View File

@ -1,19 +1,22 @@
defmodule Cannery.AmmoTest do defmodule Cannery.AmmoTest do
use Cannery.DataCase @moduledoc """
Tests the Ammo context
"""
alias Cannery.Ammo use Cannery.DataCase
alias Cannery.{Ammo, Ammo.AmmoType, Ammo.AmmoGroup}
alias Ecto.Changeset alias Ecto.Changeset
describe "ammo_types" do @moduletag :ammo_test
alias Cannery.Ammo.AmmoType
describe "ammo_types" do
@valid_attrs %{ @valid_attrs %{
"bullet_type" => "some bullet_type", "bullet_type" => "some bullet_type",
"case_material" => "some case_material", "case_material" => "some case_material",
"desc" => "some desc", "desc" => "some desc",
"manufacturer" => "some manufacturer", "manufacturer" => "some manufacturer",
"name" => "some name", "name" => "some name",
"weight" => 120.5 "grains" => 120
} }
@update_attrs %{ @update_attrs %{
"bullet_type" => "some updated bullet_type", "bullet_type" => "some updated bullet_type",
@ -21,7 +24,7 @@ defmodule Cannery.AmmoTest do
"desc" => "some updated desc", "desc" => "some updated desc",
"manufacturer" => "some updated manufacturer", "manufacturer" => "some updated manufacturer",
"name" => "some updated name", "name" => "some updated name",
"weight" => 456.7 "grains" => 456
} }
@invalid_attrs %{ @invalid_attrs %{
"bullet_type" => nil, "bullet_type" => nil,
@ -29,130 +32,155 @@ defmodule Cannery.AmmoTest do
"desc" => nil, "desc" => nil,
"manufacturer" => nil, "manufacturer" => nil,
"name" => nil, "name" => nil,
"weight" => nil "grains" => nil
} }
def ammo_type_fixture(attrs \\ %{}) do setup do
{:ok, ammo_type} = current_user = user_fixture()
attrs [ammo_type: ammo_type_fixture(current_user), current_user: current_user]
|> Enum.into(@valid_attrs)
|> Ammo.create_ammo_type()
ammo_type
end end
test "list_ammo_types/0 returns all ammo_types" do test "list_ammo_types/0 returns all ammo_types",
ammo_type = ammo_type_fixture() %{ammo_type: ammo_type, current_user: current_user} do
assert Ammo.list_ammo_types() == [ammo_type] assert Ammo.list_ammo_types(current_user) == [ammo_type]
end end
test "get_ammo_type!/1 returns the ammo_type with given id" do test "get_ammo_type!/1 returns the ammo_type with given id",
ammo_type = ammo_type_fixture() %{ammo_type: ammo_type, current_user: current_user} do
assert Ammo.get_ammo_type!(ammo_type.id) == ammo_type assert Ammo.get_ammo_type!(ammo_type.id, current_user) == ammo_type
end end
test "create_ammo_type/1 with valid data creates a ammo_type" do test "create_ammo_type/1 with valid data creates a ammo_type",
assert {:ok, %AmmoType{} = ammo_type} = Ammo.create_ammo_type(@valid_attrs) %{ammo_type: ammo_type, current_user: current_user} do
assert {:ok, %AmmoType{} = ammo_type} = Ammo.create_ammo_type(@valid_attrs, current_user)
assert ammo_type.bullet_type == "some bullet_type" assert ammo_type.bullet_type == "some bullet_type"
assert ammo_type.case_material == "some case_material" assert ammo_type.case_material == "some case_material"
assert ammo_type.desc == "some desc" assert ammo_type.desc == "some desc"
assert ammo_type.manufacturer == "some manufacturer" assert ammo_type.manufacturer == "some manufacturer"
assert ammo_type.name == "some name" assert ammo_type.name == "some name"
assert ammo_type.weight == 120.5 assert ammo_type.grains == 120
end end
test "create_ammo_type/1 with invalid data returns error changeset" do test "create_ammo_type/1 with invalid data returns error changeset",
assert {:error, %Changeset{}} = Ammo.create_ammo_type(@invalid_attrs) %{ammo_type: ammo_type, current_user: current_user} do
assert {:error, %Changeset{}} = Ammo.create_ammo_type(@invalid_attrs, current_user)
end end
test "update_ammo_type/2 with valid data updates the ammo_type" do test "update_ammo_type/2 with valid data updates the ammo_type",
ammo_type = ammo_type_fixture() %{ammo_type: ammo_type, current_user: current_user} do
assert {:ok, %AmmoType{} = ammo_type} = Ammo.update_ammo_type(ammo_type, @update_attrs) assert {:ok, %AmmoType{} = ammo_type} =
Ammo.update_ammo_type(ammo_type, @update_attrs, current_user)
assert ammo_type.bullet_type == "some updated bullet_type" assert ammo_type.bullet_type == "some updated bullet_type"
assert ammo_type.case_material == "some updated case_material" assert ammo_type.case_material == "some updated case_material"
assert ammo_type.desc == "some updated desc" assert ammo_type.desc == "some updated desc"
assert ammo_type.manufacturer == "some updated manufacturer" assert ammo_type.manufacturer == "some updated manufacturer"
assert ammo_type.name == "some updated name" assert ammo_type.name == "some updated name"
assert ammo_type.weight == 456.7 assert ammo_type.grains == 456
end end
test "update_ammo_type/2 with invalid data returns error changeset" do test "update_ammo_type/2 with invalid data returns error changeset",
ammo_type = ammo_type_fixture() %{ammo_type: ammo_type, current_user: current_user} do
assert {:error, %Changeset{}} = Ammo.update_ammo_type(ammo_type, @invalid_attrs) assert {:error, %Changeset{}} =
assert ammo_type == Ammo.get_ammo_type!(ammo_type.id) Ammo.update_ammo_type(ammo_type, @invalid_attrs, current_user)
assert ammo_type == Ammo.get_ammo_type!(ammo_type.id, current_user)
end end
test "delete_ammo_type/1 deletes the ammo_type" do test "delete_ammo_type/1 deletes the ammo_type",
ammo_type = ammo_type_fixture() %{ammo_type: ammo_type, current_user: current_user} do
assert {:ok, %AmmoType{}} = Ammo.delete_ammo_type(ammo_type) assert {:ok, %AmmoType{}} = Ammo.delete_ammo_type(ammo_type, current_user)
assert_raise Ecto.NoResultsError, fn -> Ammo.get_ammo_type!(ammo_type.id) end assert_raise Ecto.NoResultsError, fn -> Ammo.get_ammo_type!(ammo_type.id, current_user) end
end end
test "change_ammo_type/1 returns a ammo_type changeset" do test "change_ammo_type/1 returns a ammo_type changeset",
ammo_type = ammo_type_fixture() %{ammo_type: ammo_type} do
assert %Changeset{} = Ammo.change_ammo_type(ammo_type) assert %Changeset{} = Ammo.change_ammo_type(ammo_type)
end end
end end
describe "ammo_groups" do describe "ammo_groups" do
alias Cannery.Ammo.AmmoGroup @valid_attrs %{"count" => 42, "notes" => "some notes", "price_paid" => 120.5}
@update_attrs %{"count" => 43, "notes" => "some updated notes", "price_paid" => 456.7}
@invalid_attrs %{"count" => nil, "notes" => nil, "price_paid" => nil}
@valid_attrs %{count: 42, notes: "some notes", price_paid: 120.5} setup do
@update_attrs %{count: 43, notes: "some updated notes", price_paid: 456.7} current_user = user_fixture()
@invalid_attrs %{count: nil, notes: nil, price_paid: nil} ammo_type = ammo_type_fixture(current_user)
container = container_fixture(current_user)
ammo_group = ammo_group_fixture(ammo_type, container, current_user)
def ammo_group_fixture(attrs \\ %{}) do [
{:ok, ammo_group} = ammo_type: ammo_type,
attrs ammo_group: ammo_group,
|> Enum.into(@valid_attrs) container: container,
|> Ammo.create_ammo_group() current_user: current_user
]
ammo_group
end end
test "list_ammo_groups/0 returns all ammo_groups" do test "list_ammo_groups/0 returns all ammo_groups",
ammo_group = ammo_group_fixture() %{ammo_group: ammo_group, current_user: current_user} do
assert Ammo.list_ammo_groups() == [ammo_group] assert Ammo.list_ammo_groups(current_user) == [ammo_group]
end end
test "get_ammo_group!/1 returns the ammo_group with given id" do test "get_ammo_group!/1 returns the ammo_group with given id",
ammo_group = ammo_group_fixture() %{ammo_group: ammo_group, current_user: current_user} do
assert Ammo.get_ammo_group!(ammo_group.id) == ammo_group assert Ammo.get_ammo_group!(ammo_group.id, current_user) == ammo_group
end end
test "create_ammo_group/1 with valid data creates a ammo_group" do test "create_ammo_group/1 with valid data creates a ammo_group",
assert {:ok, %AmmoGroup{} = ammo_group} = Ammo.create_ammo_group(@valid_attrs) %{
ammo_type: ammo_type,
ammo_group: ammo_group,
container: container,
current_user: current_user
} do
assert {:ok, %AmmoGroup{} = ammo_group} =
@valid_attrs
|> Map.merge(%{"ammo_type_id" => ammo_type.id, "container_id" => container.id})
|> Ammo.create_ammo_group(current_user)
assert ammo_group.count == 42 assert ammo_group.count == 42
assert ammo_group.notes == "some notes" assert ammo_group.notes == "some notes"
assert ammo_group.price_paid == 120.5 assert ammo_group.price_paid == 120.5
end end
test "create_ammo_group/1 with invalid data returns error changeset" do test "create_ammo_group/1 with invalid data returns error changeset",
assert {:error, %Changeset{}} = Ammo.create_ammo_group(@invalid_attrs) %{ammo_type: ammo_type, container: container, current_user: current_user} do
assert {:error, %Changeset{}} =
@invalid_attrs
|> Map.merge(%{"ammo_type_id" => ammo_type.id, "container_id" => container.id})
|> Ammo.create_ammo_group(current_user)
end end
test "update_ammo_group/2 with valid data updates the ammo_group" do test "update_ammo_group/2 with valid data updates the ammo_group",
ammo_group = ammo_group_fixture() %{ammo_group: ammo_group, current_user: current_user} do
assert {:ok, %AmmoGroup{} = ammo_group} = Ammo.update_ammo_group(ammo_group, @update_attrs) assert {:ok, %AmmoGroup{} = ammo_group} =
Ammo.update_ammo_group(ammo_group, @update_attrs, current_user)
assert ammo_group.count == 43 assert ammo_group.count == 43
assert ammo_group.notes == "some updated notes" assert ammo_group.notes == "some updated notes"
assert ammo_group.price_paid == 456.7 assert ammo_group.price_paid == 456.7
end end
test "update_ammo_group/2 with invalid data returns error changeset" do test "update_ammo_group/2 with invalid data returns error changeset",
ammo_group = ammo_group_fixture() %{ammo_group: ammo_group, current_user: current_user} do
assert {:error, %Changeset{}} = Ammo.update_ammo_group(ammo_group, @invalid_attrs) assert {:error, %Changeset{}} =
assert ammo_group == Ammo.get_ammo_group!(ammo_group.id) Ammo.update_ammo_group(ammo_group, @invalid_attrs, current_user)
assert ammo_group == Ammo.get_ammo_group!(ammo_group.id, current_user)
end end
test "delete_ammo_group/1 deletes the ammo_group" do test "delete_ammo_group/1 deletes the ammo_group",
ammo_group = ammo_group_fixture() %{ammo_group: ammo_group, current_user: current_user} do
assert {:ok, %AmmoGroup{}} = Ammo.delete_ammo_group(ammo_group) assert {:ok, %AmmoGroup{}} = Ammo.delete_ammo_group(ammo_group, current_user)
assert_raise Ecto.NoResultsError, fn -> Ammo.get_ammo_group!(ammo_group.id) end
assert_raise Ecto.NoResultsError, fn ->
Ammo.get_ammo_group!(ammo_group.id, current_user)
end
end end
test "change_ammo_group/1 returns a ammo_group changeset" do test "change_ammo_group/1 returns a ammo_group changeset", %{ammo_group: ammo_group} do
ammo_group = ammo_group_fixture()
assert %Changeset{} = Ammo.change_ammo_group(ammo_group) assert %Changeset{} = Ammo.change_ammo_group(ammo_group)
end end
end end

View File

@ -11,7 +11,10 @@ defmodule Cannery.Fixtures do
Ammo.AmmoGroup, Ammo.AmmoGroup,
Ammo.AmmoType, Ammo.AmmoType,
Containers, Containers,
Containers.Container Containers.Container,
Email,
Tags,
Tags.Tag
} }
def unique_user_email, do: "user#{System.unique_integer()}@example.com" def unique_user_email, do: "user#{System.unique_integer()}@example.com"
@ -20,21 +23,18 @@ defmodule Cannery.Fixtures do
@spec user_fixture() :: Accounts.User.t() @spec user_fixture() :: Accounts.User.t()
@spec user_fixture(attrs :: map()) :: Accounts.User.t() @spec user_fixture(attrs :: map()) :: Accounts.User.t()
def user_fixture(attrs \\ %{}) do def user_fixture(attrs \\ %{}) do
{:ok, user} =
attrs attrs
|> Enum.into(%{ |> Enum.into(%{
"email" => unique_user_email(), "email" => unique_user_email(),
"password" => valid_user_password() "password" => valid_user_password()
}) })
|> Accounts.register_user() |> Accounts.register_user()
|> unwrap_ok_tuple()
user
end end
@spec admin_fixture() :: Accounts.User.t() @spec admin_fixture() :: Accounts.User.t()
@spec admin_fixture(attrs :: map()) :: Accounts.User.t() @spec admin_fixture(attrs :: map()) :: Accounts.User.t()
def admin_fixture(attrs \\ %{}) do def admin_fixture(attrs \\ %{}) do
{:ok, user} =
attrs attrs
|> Enum.into(%{ |> Enum.into(%{
"email" => unique_user_email(), "email" => unique_user_email(),
@ -42,14 +42,23 @@ defmodule Cannery.Fixtures do
"role" => "admin" "role" => "admin"
}) })
|> Accounts.register_user() |> Accounts.register_user()
|> unwrap_ok_tuple()
user
end end
def extract_user_token(fun) do def extract_user_token(fun) do
{:ok, captured} = fun.(&"[TOKEN]#{&1}[TOKEN]") %{args: %{attrs: attrs, email: email_key, user_id: user_id}} = fun.(&"[TOKEN]#{&1}[TOKEN]")
[_, token, _] = String.split(captured.body, "[TOKEN]")
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)
[_, html_token, _] = email.html_body |> String.split("[TOKEN]")
[_, text_token, _] = email.text_body |> String.split("[TOKEN]")
text_token = html_token
end end
def valid_user_attributes(attrs \\ %{}) do def valid_user_attributes(attrs \\ %{}) do
@ -65,7 +74,6 @@ defmodule Cannery.Fixtures do
@spec shot_group_fixture(User.t(), AmmoGroup.t()) :: ShotGroup.t() @spec shot_group_fixture(User.t(), AmmoGroup.t()) :: ShotGroup.t()
@spec shot_group_fixture(attrs :: map(), 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 def shot_group_fixture(attrs \\ %{}, %User{} = user, %AmmoGroup{} = ammo_group) do
{:ok, shot_group} =
attrs attrs
|> Enum.into(%{ |> Enum.into(%{
"count" => 25, "count" => 25,
@ -73,8 +81,7 @@ defmodule Cannery.Fixtures do
"notes" => "some notes" "notes" => "some notes"
}) })
|> Cannery.ActivityLog.create_shot_group(user, ammo_group) |> Cannery.ActivityLog.create_shot_group(user, ammo_group)
|> unwrap_ok_tuple()
shot_group
end end
@doc """ @doc """
@ -83,12 +90,10 @@ defmodule Cannery.Fixtures do
@spec container_fixture(User.t()) :: Container.t() @spec container_fixture(User.t()) :: Container.t()
@spec container_fixture(attrs :: map(), User.t()) :: Container.t() @spec container_fixture(attrs :: map(), User.t()) :: Container.t()
def container_fixture(attrs \\ %{}, %User{} = user) do def container_fixture(attrs \\ %{}, %User{} = user) do
{:ok, container} =
attrs attrs
|> Enum.into(%{"name" => "My container", "type" => "Ammo can"}) |> Enum.into(%{"name" => "My container", "type" => "Ammo can"})
|> Containers.create_container(user) |> Containers.create_container(user)
|> unwrap_ok_tuple()
container
end end
@doc """ @doc """
@ -97,12 +102,10 @@ defmodule Cannery.Fixtures do
@spec ammo_type_fixture(User.t()) :: AmmoType.t() @spec ammo_type_fixture(User.t()) :: AmmoType.t()
@spec ammo_type_fixture(attrs :: map(), User.t()) :: AmmoType.t() @spec ammo_type_fixture(attrs :: map(), User.t()) :: AmmoType.t()
def ammo_type_fixture(attrs \\ %{}, %User{} = user) do def ammo_type_fixture(attrs \\ %{}, %User{} = user) do
{:ok, ammo_type} =
attrs attrs
|> Enum.into(%{"name" => "ammo_type"}) |> Enum.into(%{"name" => "ammo_type"})
|> Ammo.create_ammo_type(user) |> Ammo.create_ammo_type(user)
|> unwrap_ok_tuple()
ammo_type
end end
@doc """ @doc """
@ -116,7 +119,6 @@ defmodule Cannery.Fixtures do
%Container{id: container_id}, %Container{id: container_id},
%User{} = user %User{} = user
) do ) do
{:ok, ammo_group} =
attrs attrs
|> Enum.into(%{ |> Enum.into(%{
"ammo_type_id" => ammo_type_id, "ammo_type_id" => ammo_type_id,
@ -124,7 +126,24 @@ defmodule Cannery.Fixtures do
"count" => 20 "count" => 20
}) })
|> Ammo.create_ammo_group(user) |> Ammo.create_ammo_group(user)
|> unwrap_ok_tuple()
end
ammo_group @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()
end end
defp unwrap_ok_tuple({:ok, value}), do: value
end end