add multiple ammo groups at one time
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-02-24 00:16:23 -05:00
parent d79d0fa179
commit a6b2c6181e
13 changed files with 257 additions and 54 deletions

View File

@ -20,8 +20,8 @@ defmodule Cannery.ActivityLogTest do
container = container_fixture(current_user)
ammo_type = ammo_type_fixture(current_user)
%{id: ammo_group_id} =
ammo_group = ammo_group_fixture(%{"count" => 25}, ammo_type, container, current_user)
{1, [%{id: ammo_group_id} = ammo_group]} =
ammo_group_fixture(%{"count" => 25}, ammo_type, container, current_user)
shot_group =
%{"count" => 5, "date" => ~N[2022-02-13 03:17:00], "notes" => "some notes"}

View File

@ -108,7 +108,7 @@ defmodule Cannery.AmmoTest do
current_user = user_fixture()
ammo_type = ammo_type_fixture(current_user)
container = container_fixture(current_user)
ammo_group = ammo_group_fixture(ammo_type, container, current_user)
{1, [ammo_group]} = ammo_group_fixture(ammo_type, container, current_user)
[
ammo_type: ammo_type,
@ -129,28 +129,28 @@ defmodule Cannery.AmmoTest do
ammo_group |> Repo.preload(:shot_groups)
end
test "create_ammo_group/1 with valid data creates a ammo_group",
test "create_ammo_groups/3 with valid data creates a ammo_group",
%{
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
assert {:ok, %AmmoGroup{} = ammo_group} =
assert {:ok, {1, [%AmmoGroup{} = ammo_group]}} =
@valid_attrs
|> Map.merge(%{"ammo_type_id" => ammo_type.id, "container_id" => container.id})
|> Ammo.create_ammo_group(current_user)
|> Ammo.create_ammo_groups(1, current_user)
assert ammo_group.count == 42
assert ammo_group.notes == "some notes"
assert ammo_group.price_paid == 120.5
end
test "create_ammo_group/1 with invalid data returns error changeset",
test "create_ammo_groups/3 with invalid data returns error changeset",
%{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)
|> Ammo.create_ammo_groups(1, current_user)
end
test "update_ammo_group/2 with valid data updates the ammo_group",

View File

@ -6,19 +6,20 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
use CanneryWeb.ConnCase
import Phoenix.LiveViewTest
import CanneryWeb.Gettext
alias Cannery.Repo
alias Cannery.{Ammo, Repo}
@moduletag :ammo_group_live_test
@shot_group_create_attrs %{"ammo_left" => 5, "notes" => "some notes"}
@shot_group_update_attrs %{"count" => 5, "notes" => "some updated notes"}
@create_attrs %{count: 42, notes: "some notes", price_paid: 120.5}
@update_attrs %{count: 43, notes: "some updated notes", price_paid: 456.7}
@create_attrs %{"count" => 42, "notes" => "some notes", "price_paid" => 120.5}
@update_attrs %{"count" => 43, "notes" => "some updated notes", "price_paid" => 456.7}
@ammo_group_create_limit 10_000
# @invalid_attrs %{count: -1, notes: nil, price_paid: nil}
defp create_ammo_group(%{current_user: current_user}) do
ammo_type = ammo_type_fixture(current_user)
container = container_fixture(current_user)
ammo_group = ammo_group_fixture(ammo_type, container, current_user)
{1, [ammo_group]} = ammo_group_fixture(ammo_type, container, current_user)
shot_group =
%{"count" => 5, "date" => ~N[2022-02-13 03:17:00], "notes" => "some notes"}
@ -38,7 +39,7 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
assert html =~ ammo_group.ammo_type.name
end
test "saves new ammo_group", %{conn: conn} do
test "saves a single new ammo_group", %{conn: conn} do
{:ok, index_live, _html} = live(conn, Routes.ammo_group_index_path(conn, :index))
assert index_live |> element("a", dgettext("actions", "New Ammo group")) |> render_click() =~
@ -60,6 +61,68 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
assert html =~ "42"
end
test "saves multiple new ammo_groups", %{conn: conn, current_user: current_user} do
multiplier = 25
{:ok, index_live, _html} = live(conn, Routes.ammo_group_index_path(conn, :index))
assert index_live |> element("a", dgettext("actions", "New Ammo group")) |> render_click() =~
gettext("New Ammo group")
assert_patch(index_live, Routes.ammo_group_index_path(conn, :new))
# assert index_live
# |> form("#ammo_group-form", ammo_group: @invalid_attrs)
# |> render_change() =~ dgettext("errors", "can't be blank")
{:ok, _, html} =
index_live
|> form("#ammo_group-form",
ammo_group: @create_attrs |> Map.put("multiplier", to_string(multiplier))
)
|> render_submit()
|> follow_redirect(conn, Routes.ammo_group_index_path(conn, :index))
assert html =~ dgettext("prompts", "Ammo groups created successfully")
assert Ammo.list_ammo_groups(current_user) |> Enum.count() == multiplier + 1
end
test "does not save invalid number of new ammo_groups", %{conn: conn} do
{:ok, index_live, _html} = live(conn, Routes.ammo_group_index_path(conn, :index))
assert index_live |> element("a", dgettext("actions", "New Ammo group")) |> render_click() =~
gettext("New Ammo group")
assert_patch(index_live, Routes.ammo_group_index_path(conn, :new))
# assert index_live
# |> form("#ammo_group-form", ammo_group: @invalid_attrs)
# |> render_change() =~ dgettext("errors", "can't be blank")
assert index_live
|> form("#ammo_group-form", ammo_group: @create_attrs |> Map.put("multiplier", "0"))
|> render_submit() =~
dgettext(
"errors",
"Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}",
multiplier: 0,
max: @ammo_group_create_limit
)
assert index_live
|> form("#ammo_group-form",
ammo_group:
@create_attrs |> Map.put("multiplier", to_string(@ammo_group_create_limit + 1))
)
|> render_submit() =~
dgettext(
"errors",
"Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}",
multiplier: @ammo_group_create_limit + 1,
max: @ammo_group_create_limit
)
end
test "saves new shot_group", %{conn: conn, ammo_group: ammo_group} do
{:ok, index_live, _html} = live(conn, Routes.ammo_group_index_path(conn, :index))

View File

@ -16,7 +16,9 @@ defmodule CanneryWeb.RangeLiveTest do
defp create_shot_group(%{current_user: current_user}) do
container = container_fixture(%{"staged" => true}, current_user)
ammo_type = ammo_type_fixture(current_user)
ammo_group = ammo_group_fixture(%{"staged" => true}, ammo_type, container, current_user)
{1, [ammo_group]} =
ammo_group_fixture(%{"staged" => true}, ammo_type, container, current_user)
shot_group =
%{"count" => 5, "date" => ~N[2022-02-13 03:17:00], "notes" => "some notes"}

View File

@ -111,10 +111,20 @@ defmodule Cannery.Fixtures do
@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()
@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()]}
def ammo_group_fixture(
attrs \\ %{},
multiplier \\ 1,
%AmmoType{id: ammo_type_id},
%Container{id: container_id},
%User{} = user
@ -125,7 +135,7 @@ defmodule Cannery.Fixtures do
"container_id" => container_id,
"count" => 20
})
|> Ammo.create_ammo_group(user)
|> Ammo.create_ammo_groups(multiplier, user)
|> unwrap_ok_tuple()
end