add selectable ammo types

This commit is contained in:
2023-03-23 22:07:25 -04:00
parent d9251c7e4c
commit 8c95536ffd
56 changed files with 4306 additions and 2077 deletions

View File

@ -38,50 +38,6 @@ defmodule Cannery.ActivityLogTest do
]
end
test "list_shot_groups/1 returns all shot_groups",
%{shot_group: shot_group, current_user: current_user} do
assert ActivityLog.list_shot_groups(current_user) == [shot_group]
end
test "list_shot_groups/2 returns relevant shot_groups for a user", %{
ammo_type: ammo_type,
ammo_group: ammo_group,
container: container,
current_user: current_user
} do
shot_group_a = shot_group_fixture(%{"notes" => "amazing"}, current_user, ammo_group)
{1, [another_ammo_group]} =
ammo_group_fixture(%{"notes" => "stupendous"}, ammo_type, container, current_user)
shot_group_b = shot_group_fixture(current_user, another_ammo_group)
another_ammo_type = ammo_type_fixture(%{"name" => "fabulous ammo"}, current_user)
{1, [yet_another_ammo_group]} =
ammo_group_fixture(another_ammo_type, container, current_user)
shot_group_c = shot_group_fixture(current_user, yet_another_ammo_group)
random_user = user_fixture()
random_container = container_fixture(random_user)
random_ammo_type = ammo_type_fixture(random_user)
{1, [random_ammo_group]} =
ammo_group_fixture(random_ammo_type, random_container, random_user)
_shouldnt_return = shot_group_fixture(random_user, random_ammo_group)
# notes
assert ActivityLog.list_shot_groups("amazing", current_user) == [shot_group_a]
# ammo group attributes
assert ActivityLog.list_shot_groups("stupendous", current_user) == [shot_group_b]
# ammo type attributes
assert ActivityLog.list_shot_groups("fabulous", current_user) == [shot_group_c]
end
test "get_shot_group!/2 returns the shot_group with given id",
%{shot_group: shot_group, current_user: current_user} do
assert ActivityLog.get_shot_group!(shot_group.id, current_user) == shot_group
@ -371,4 +327,99 @@ defmodule Cannery.ActivityLogTest do
assert %{^another_ammo_type_id => 6} = used_counts
end
end
describe "list_shot_groups/3" do
setup do
current_user = user_fixture()
container = container_fixture(current_user)
ammo_type = ammo_type_fixture(current_user)
{1, [ammo_group]} = ammo_group_fixture(ammo_type, container, current_user)
[
current_user: current_user,
container: container,
ammo_type: ammo_type,
ammo_group: ammo_group
]
end
test "list_shot_groups/3 returns relevant shot_groups for a type",
%{current_user: current_user, container: container} do
other_user = user_fixture()
other_container = container_fixture(other_user)
for type <- ["rifle", "shotgun", "pistol"] do
other_ammo_type = ammo_type_fixture(%{"type" => type}, other_user)
{1, [other_ammo_group]} = ammo_group_fixture(other_ammo_type, other_container, other_user)
shot_group_fixture(other_user, other_ammo_group)
end
rifle_ammo_type = ammo_type_fixture(%{"type" => "rifle"}, current_user)
{1, [rifle_ammo_group]} = ammo_group_fixture(rifle_ammo_type, container, current_user)
rifle_shot_group = shot_group_fixture(current_user, rifle_ammo_group)
shotgun_ammo_type = ammo_type_fixture(%{"type" => "shotgun"}, current_user)
{1, [shotgun_ammo_group]} = ammo_group_fixture(shotgun_ammo_type, container, current_user)
shotgun_shot_group = shot_group_fixture(current_user, shotgun_ammo_group)
pistol_ammo_type = ammo_type_fixture(%{"type" => "pistol"}, current_user)
{1, [pistol_ammo_group]} = ammo_group_fixture(pistol_ammo_type, container, current_user)
pistol_shot_group = shot_group_fixture(current_user, pistol_ammo_group)
assert [^rifle_shot_group] = ActivityLog.list_shot_groups(:rifle, current_user)
assert [^shotgun_shot_group] = ActivityLog.list_shot_groups(:shotgun, current_user)
assert [^pistol_shot_group] = ActivityLog.list_shot_groups(:pistol, current_user)
shot_groups = ActivityLog.list_shot_groups(:all, current_user)
assert Enum.count(shot_groups) == 3
assert rifle_shot_group in shot_groups
assert shotgun_shot_group in shot_groups
assert pistol_shot_group in shot_groups
shot_groups = ActivityLog.list_shot_groups(nil, current_user)
assert Enum.count(shot_groups) == 3
assert rifle_shot_group in shot_groups
assert shotgun_shot_group in shot_groups
assert pistol_shot_group in shot_groups
end
test "list_shot_groups/3 returns relevant shot_groups for a search", %{
ammo_type: ammo_type,
ammo_group: ammo_group,
container: container,
current_user: current_user
} do
shot_group_a = shot_group_fixture(%{"notes" => "amazing"}, current_user, ammo_group)
{1, [another_ammo_group]} =
ammo_group_fixture(%{"notes" => "stupendous"}, ammo_type, container, current_user)
shot_group_b = shot_group_fixture(current_user, another_ammo_group)
another_ammo_type = ammo_type_fixture(%{"name" => "fabulous ammo"}, current_user)
{1, [yet_another_ammo_group]} =
ammo_group_fixture(another_ammo_type, container, current_user)
shot_group_c = shot_group_fixture(current_user, yet_another_ammo_group)
another_user = user_fixture()
another_container = container_fixture(another_user)
another_ammo_type = ammo_type_fixture(another_user)
{1, [another_ammo_group]} =
ammo_group_fixture(another_ammo_type, another_container, another_user)
_shouldnt_return = shot_group_fixture(another_user, another_ammo_group)
# notes
assert ActivityLog.list_shot_groups("amazing", :all, current_user) == [shot_group_a]
# ammo group attributes
assert ActivityLog.list_shot_groups("stupendous", :all, current_user) == [shot_group_b]
# ammo type attributes
assert ActivityLog.list_shot_groups("fabulous", :all, current_user) == [shot_group_c]
end
end
end

View File

@ -9,54 +9,55 @@ defmodule Cannery.AmmoTest do
@moduletag :ammo_test
describe "ammo_types" do
@valid_attrs %{
"bullet_type" => "some bullet_type",
"case_material" => "some case_material",
"desc" => "some desc",
"manufacturer" => "some manufacturer",
"name" => "some name",
"grains" => 120
}
@update_attrs %{
"bullet_type" => "some updated bullet_type",
"case_material" => "some updated case_material",
"desc" => "some updated desc",
"manufacturer" => "some updated manufacturer",
"name" => "some updated name",
"grains" => 456
}
@invalid_attrs %{
"bullet_type" => nil,
"case_material" => nil,
"desc" => nil,
"manufacturer" => nil,
"name" => nil,
"grains" => nil
}
@valid_attrs %{
"bullet_type" => "some bullet_type",
"case_material" => "some case_material",
"desc" => "some desc",
"manufacturer" => "some manufacturer",
"name" => "some name",
"grains" => 120
}
@update_attrs %{
"bullet_type" => "some updated bullet_type",
"case_material" => "some updated case_material",
"desc" => "some updated desc",
"manufacturer" => "some updated manufacturer",
"name" => "some updated name",
"grains" => 456
}
@invalid_attrs %{
"bullet_type" => nil,
"case_material" => nil,
"desc" => nil,
"manufacturer" => nil,
"name" => nil,
"grains" => nil
}
describe "list_ammo_types/2" do
setup do
current_user = user_fixture()
[ammo_type: ammo_type_fixture(current_user), current_user: current_user]
end
test "list_ammo_types/1 returns all ammo_types",
%{ammo_type: ammo_type, current_user: current_user} do
assert Ammo.list_ammo_types(current_user) == [ammo_type]
end
test "list_ammo_types/2 returns relevant ammo_types for a user",
%{current_user: current_user} do
ammo_type_a =
%{"name" => "bullets", "desc" => "has some pews in it", "grains" => 5}
|> ammo_type_fixture(current_user)
ammo_type_b =
%{"name" => "hollows", "grains" => 3}
|> ammo_type_fixture(current_user)
ammo_type_c =
rifle_ammo_type =
%{
"name" => "bullets",
"type" => "rifle",
"desc" => "has some pews in it",
"grains" => 5
}
|> ammo_type_fixture(current_user)
shotgun_ammo_type =
%{
"name" => "hollows",
"type" => "shotgun",
"grains" => 3
}
|> ammo_type_fixture(current_user)
pistol_ammo_type =
%{
"type" => "pistol",
"name" => "jackets",
"desc" => "brass shell",
"tracer" => true
@ -70,23 +71,78 @@ defmodule Cannery.AmmoTest do
}
|> ammo_type_fixture(user_fixture())
[
rifle_ammo_type: rifle_ammo_type,
shotgun_ammo_type: shotgun_ammo_type,
pistol_ammo_type: pistol_ammo_type,
current_user: current_user
]
end
test "list_ammo_types/2 returns all ammo_types", %{
rifle_ammo_type: rifle_ammo_type,
shotgun_ammo_type: shotgun_ammo_type,
pistol_ammo_type: pistol_ammo_type,
current_user: current_user
} do
results = Ammo.list_ammo_types(current_user, :all)
assert results |> Enum.count() == 3
assert rifle_ammo_type in results
assert shotgun_ammo_type in results
assert pistol_ammo_type in results
end
test "list_ammo_types/2 returns rifle ammo_types", %{
rifle_ammo_type: rifle_ammo_type,
current_user: current_user
} do
assert [^rifle_ammo_type] = Ammo.list_ammo_types(current_user, :rifle)
end
test "list_ammo_types/2 returns shotgun ammo_types", %{
shotgun_ammo_type: shotgun_ammo_type,
current_user: current_user
} do
assert [^shotgun_ammo_type] = Ammo.list_ammo_types(current_user, :shotgun)
end
test "list_ammo_types/2 returns pistol ammo_types", %{
pistol_ammo_type: pistol_ammo_type,
current_user: current_user
} do
assert [^pistol_ammo_type] = Ammo.list_ammo_types(current_user, :pistol)
end
test "list_ammo_types/2 returns relevant ammo_types for a user", %{
rifle_ammo_type: rifle_ammo_type,
shotgun_ammo_type: shotgun_ammo_type,
pistol_ammo_type: pistol_ammo_type,
current_user: current_user
} do
# name
assert Ammo.list_ammo_types("bullet", current_user) == [ammo_type_a]
assert Ammo.list_ammo_types("bullets", current_user) == [ammo_type_a]
assert Ammo.list_ammo_types("hollow", current_user) == [ammo_type_b]
assert Ammo.list_ammo_types("jacket", current_user) == [ammo_type_c]
assert Ammo.list_ammo_types("bullet", current_user, :all) == [rifle_ammo_type]
assert Ammo.list_ammo_types("bullets", current_user, :all) == [rifle_ammo_type]
assert Ammo.list_ammo_types("hollow", current_user, :all) == [shotgun_ammo_type]
assert Ammo.list_ammo_types("jacket", current_user, :all) == [pistol_ammo_type]
# desc
assert Ammo.list_ammo_types("pew", current_user) == [ammo_type_a]
assert Ammo.list_ammo_types("brass", current_user) == [ammo_type_c]
assert Ammo.list_ammo_types("shell", current_user) == [ammo_type_c]
assert Ammo.list_ammo_types("pew", current_user, :all) == [rifle_ammo_type]
assert Ammo.list_ammo_types("brass", current_user, :all) == [pistol_ammo_type]
assert Ammo.list_ammo_types("shell", current_user, :all) == [pistol_ammo_type]
# grains (integer)
assert Ammo.list_ammo_types("5", current_user) == [ammo_type_a]
assert Ammo.list_ammo_types("3", current_user) == [ammo_type_b]
assert Ammo.list_ammo_types("5", current_user, :all) == [rifle_ammo_type]
assert Ammo.list_ammo_types("3", current_user, :all) == [shotgun_ammo_type]
# tracer (boolean)
assert Ammo.list_ammo_types("tracer", current_user) == [ammo_type_c]
assert Ammo.list_ammo_types("tracer", current_user, :all) == [pistol_ammo_type]
end
end
describe "ammo types" do
setup do
current_user = user_fixture()
[ammo_type: ammo_type_fixture(current_user), current_user: current_user]
end
test "get_ammo_type!/2 returns the ammo_type with given id",
@ -94,6 +150,23 @@ defmodule Cannery.AmmoTest do
assert Ammo.get_ammo_type!(ammo_type.id, current_user) == ammo_type
end
test "get_ammo_types_count!/1 returns the correct amount of ammo",
%{current_user: current_user} do
assert Ammo.get_ammo_types_count!(current_user) == 1
ammo_type_fixture(current_user)
assert Ammo.get_ammo_types_count!(current_user) == 2
ammo_type_fixture(current_user)
assert Ammo.get_ammo_types_count!(current_user) == 3
other_user = user_fixture()
assert Ammo.get_ammo_types_count!(other_user) == 0
ammo_type_fixture(other_user)
assert Ammo.get_ammo_types_count!(other_user) == 1
end
test "create_ammo_type/2 with valid data creates a ammo_type",
%{current_user: current_user} do
assert {:ok, %AmmoType{} = ammo_type} = Ammo.create_ammo_type(@valid_attrs, current_user)
@ -653,32 +726,85 @@ defmodule Cannery.AmmoTest do
]
end
test "list_ammo_groups/3 returns all ammo_groups",
%{
ammo_type: ammo_type,
ammo_group: ammo_group,
container: container,
current_user: current_user
} do
test "get_ammo_groups_count!/2 returns the correct amount of ammo",
%{ammo_type: ammo_type, container: container, current_user: current_user} do
assert Ammo.get_ammo_groups_count!(current_user) == 1
ammo_group_fixture(ammo_type, container, current_user)
assert Ammo.get_ammo_groups_count!(current_user) == 2
ammo_group_fixture(ammo_type, container, current_user)
assert Ammo.get_ammo_groups_count!(current_user) == 3
other_user = user_fixture()
assert Ammo.get_ammo_groups_count!(other_user) == 0
assert Ammo.get_ammo_groups_count!(other_user, true) == 0
other_ammo_type = ammo_type_fixture(other_user)
other_container = container_fixture(other_user)
{1, [another_ammo_group]} =
ammo_group_fixture(%{"count" => 30}, other_ammo_type, other_container, other_user)
shot_group_fixture(%{"count" => 30}, other_user, another_ammo_group)
assert Ammo.get_ammo_groups_count!(other_user) == 0
assert Ammo.get_ammo_groups_count!(other_user, true) == 1
end
test "list_ammo_groups/4 returns all ammo_groups for a type" do
current_user = user_fixture()
container = container_fixture(current_user)
rifle_ammo_type = ammo_type_fixture(%{"type" => "rifle"}, current_user)
{1, [rifle_ammo_group]} = ammo_group_fixture(rifle_ammo_type, container, current_user)
shotgun_ammo_type = ammo_type_fixture(%{"type" => "shotgun"}, current_user)
{1, [shotgun_ammo_group]} = ammo_group_fixture(shotgun_ammo_type, container, current_user)
pistol_ammo_type = ammo_type_fixture(%{"type" => "pistol"}, current_user)
{1, [pistol_ammo_group]} = ammo_group_fixture(pistol_ammo_type, container, current_user)
assert [^rifle_ammo_group] = Ammo.list_ammo_groups(nil, :rifle, current_user, false)
assert [^shotgun_ammo_group] = Ammo.list_ammo_groups(nil, :shotgun, current_user, false)
assert [^pistol_ammo_group] = Ammo.list_ammo_groups(nil, :pistol, current_user, false)
ammo_groups = Ammo.list_ammo_groups(nil, :all, current_user, false)
assert Enum.count(ammo_groups) == 3
assert rifle_ammo_group in ammo_groups
assert shotgun_ammo_group in ammo_groups
assert pistol_ammo_group in ammo_groups
ammo_groups = Ammo.list_ammo_groups(nil, nil, current_user, false)
assert Enum.count(ammo_groups) == 3
assert rifle_ammo_group in ammo_groups
assert shotgun_ammo_group in ammo_groups
assert pistol_ammo_group in ammo_groups
end
test "list_ammo_groups/4 returns all relevant ammo_groups including used", %{
ammo_type: ammo_type,
ammo_group: ammo_group,
container: container,
current_user: current_user
} do
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
ammo_group_fixture(%{"count" => 30}, ammo_type, container, current_user)
shot_group_fixture(%{"count" => 30}, current_user, another_ammo_group)
another_ammo_group = Ammo.get_ammo_group!(another_ammo_group_id, current_user)
assert Ammo.list_ammo_groups(nil, false, current_user) == [ammo_group]
assert Ammo.list_ammo_groups(nil, :all, current_user, false) == [ammo_group]
assert Ammo.list_ammo_groups(nil, true, current_user)
|> Enum.sort_by(fn %{count: count} -> count end) == [another_ammo_group, ammo_group]
ammo_groups = Ammo.list_ammo_groups(nil, :all, current_user, true)
assert Enum.count(ammo_groups) == 2
assert another_ammo_group in ammo_groups
assert ammo_group in ammo_groups
end
test "list_ammo_groups/3 returns relevant ammo groups when searched",
%{
ammo_type: ammo_type,
ammo_group: ammo_group,
container: container,
current_user: current_user
} do
test "list_ammo_groups/4 returns relevant ammo groups when searched", %{
ammo_type: ammo_type,
ammo_group: ammo_group,
container: container,
current_user: current_user
} do
{1, [another_ammo_group]} =
%{"count" => 49, "notes" => "cool ammo group"}
|> ammo_group_fixture(ammo_type, container, current_user)
@ -695,49 +821,80 @@ defmodule Cannery.AmmoTest do
{1, [fantastic_ammo_group]} =
ammo_group_fixture(%{"count" => 47}, ammo_type, another_container, current_user)
assert Ammo.list_ammo_groups(nil, false, current_user)
|> Enum.sort_by(fn %{count: count} -> count end) ==
[fantastic_ammo_group, amazing_ammo_group, another_ammo_group, ammo_group]
ammo_groups = Ammo.list_ammo_groups(nil, :all, current_user, false)
assert Enum.count(ammo_groups) == 4
assert fantastic_ammo_group in ammo_groups
assert amazing_ammo_group in ammo_groups
assert another_ammo_group in ammo_groups
assert ammo_group in ammo_groups
# search works for ammo group attributes
assert Ammo.list_ammo_groups("cool", true, current_user) == [another_ammo_group]
assert Ammo.list_ammo_groups("cool", :all, current_user, true) == [another_ammo_group]
# search works for ammo type attributes
assert Ammo.list_ammo_groups("amazing", true, current_user) == [amazing_ammo_group]
assert Ammo.list_ammo_groups("amazing", :all, current_user, true) == [amazing_ammo_group]
# search works for container attributes
assert Ammo.list_ammo_groups("fantastic", true, current_user) == [fantastic_ammo_group]
assert Ammo.list_ammo_groups("fantastic", :all, current_user, true) ==
[fantastic_ammo_group]
# search works for container tag attributes
assert Ammo.list_ammo_groups("stupendous", true, current_user) == [fantastic_ammo_group]
assert Ammo.list_ammo_groups("stupendous", :all, current_user, true) ==
[fantastic_ammo_group]
assert Ammo.list_ammo_groups("random", true, current_user) == []
assert Ammo.list_ammo_groups("random", :all, current_user, true) == []
end
test "list_ammo_groups_for_type/2 returns all ammo_groups for a type",
%{
ammo_type: ammo_type,
container: container,
ammo_group: ammo_group,
current_user: current_user
} do
another_ammo_type = ammo_type_fixture(current_user)
{1, [_another]} = ammo_group_fixture(another_ammo_type, container, current_user)
test "list_ammo_groups_for_type/3 returns all ammo_groups for a type", %{
container: container,
current_user: current_user
} do
ammo_type = ammo_type_fixture(current_user)
{1, [ammo_group]} = ammo_group_fixture(ammo_type, container, current_user)
assert [^ammo_group] = Ammo.list_ammo_groups_for_type(ammo_type, current_user)
assert Ammo.list_ammo_groups_for_type(ammo_type, current_user) == [ammo_group]
shot_group_fixture(current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group.id, current_user)
assert [] == Ammo.list_ammo_groups_for_type(ammo_type, current_user)
assert [^ammo_group] = Ammo.list_ammo_groups_for_type(ammo_type, current_user, true)
end
test "list_ammo_groups_for_container/2 returns all ammo_groups for a container",
%{
ammo_type: ammo_type,
container: container,
ammo_group: ammo_group,
current_user: current_user
} do
test "list_ammo_groups_for_container/3 returns all ammo_groups for a container" do
current_user = user_fixture()
container = container_fixture(current_user)
rifle_ammo_type = ammo_type_fixture(%{"type" => "rifle"}, current_user)
{1, [rifle_ammo_group]} = ammo_group_fixture(rifle_ammo_type, container, current_user)
shotgun_ammo_type = ammo_type_fixture(%{"type" => "shotgun"}, current_user)
{1, [shotgun_ammo_group]} = ammo_group_fixture(shotgun_ammo_type, container, current_user)
pistol_ammo_type = ammo_type_fixture(%{"type" => "pistol"}, current_user)
{1, [pistol_ammo_group]} = ammo_group_fixture(pistol_ammo_type, container, current_user)
another_container = container_fixture(current_user)
{1, [_another]} = ammo_group_fixture(ammo_type, another_container, current_user)
ammo_group_fixture(rifle_ammo_type, another_container, current_user)
ammo_group_fixture(shotgun_ammo_type, another_container, current_user)
ammo_group_fixture(pistol_ammo_type, another_container, current_user)
assert Ammo.list_ammo_groups_for_container(container, current_user) == [ammo_group]
assert [^rifle_ammo_group] =
Ammo.list_ammo_groups_for_container(container, :rifle, current_user)
assert [^shotgun_ammo_group] =
Ammo.list_ammo_groups_for_container(container, :shotgun, current_user)
assert [^pistol_ammo_group] =
Ammo.list_ammo_groups_for_container(container, :pistol, current_user)
ammo_groups = Ammo.list_ammo_groups_for_container(container, :all, current_user)
assert Enum.count(ammo_groups) == 3
assert rifle_ammo_group in ammo_groups
assert shotgun_ammo_group in ammo_groups
assert pistol_ammo_group in ammo_groups
ammo_groups = Ammo.list_ammo_groups_for_container(container, nil, current_user)
assert Enum.count(ammo_groups) == 3
assert rifle_ammo_group in ammo_groups
assert shotgun_ammo_group in ammo_groups
assert pistol_ammo_group in ammo_groups
end
test "get_ammo_groups_count_for_type/2 returns count of ammo_groups for a type", %{
@ -785,12 +942,11 @@ defmodule Cannery.AmmoTest do
assert %{^another_ammo_type_id => 1} = ammo_groups_count
end
test "list_staged_ammo_groups/1 returns all ammo_groups that are staged",
%{
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
test "list_staged_ammo_groups/1 returns all ammo_groups that are staged", %{
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
{1, [another_ammo_group]} =
ammo_group_fixture(%{"staged" => true}, ammo_type, container, current_user)
@ -816,12 +972,11 @@ defmodule Cannery.AmmoTest do
assert %{^another_ammo_group_id => ^another_ammo_group} = ammo_groups
end
test "create_ammo_groups/3 with valid data creates a ammo_group",
%{
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
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, {1, [%AmmoGroup{} = ammo_group]}} =
@valid_attrs
|> Map.merge(%{"ammo_type_id" => ammo_type.id, "container_id" => container.id})
@ -832,12 +987,11 @@ defmodule Cannery.AmmoTest do
assert ammo_group.price_paid == 120.5
end
test "create_ammo_groups/3 with valid data creates multiple ammo_groups",
%{
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
test "create_ammo_groups/3 with valid data creates multiple ammo_groups", %{
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
assert {:ok, {3, ammo_groups}} =
@valid_attrs
|> Map.merge(%{"ammo_type_id" => ammo_type.id, "container_id" => container.id})

View File

@ -66,6 +66,60 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
assert html =~ ammo_group.ammo_type.name
end
test "can sort by type",
%{conn: conn, container: container, current_user: current_user} do
rifle_type = ammo_type_fixture(%{"type" => "rifle"}, current_user)
{1, [rifle_ammo_group]} = ammo_group_fixture(rifle_type, container, current_user)
shotgun_type = ammo_type_fixture(%{"type" => "shotgun"}, current_user)
{1, [shotgun_ammo_group]} = ammo_group_fixture(shotgun_type, container, current_user)
pistol_type = ammo_type_fixture(%{"type" => "pistol"}, current_user)
{1, [pistol_ammo_group]} = ammo_group_fixture(pistol_type, container, current_user)
{:ok, index_live, html} = live(conn, Routes.ammo_group_index_path(conn, :index))
assert html =~ "All"
assert html =~ rifle_ammo_group.ammo_type.name
assert html =~ shotgun_ammo_group.ammo_type.name
assert html =~ pistol_ammo_group.ammo_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :rifle})
assert html =~ rifle_ammo_group.ammo_type.name
refute html =~ shotgun_ammo_group.ammo_type.name
refute html =~ pistol_ammo_group.ammo_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :shotgun})
refute html =~ rifle_ammo_group.ammo_type.name
assert html =~ shotgun_ammo_group.ammo_type.name
refute html =~ pistol_ammo_group.ammo_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :pistol})
refute html =~ rifle_ammo_group.ammo_type.name
refute html =~ shotgun_ammo_group.ammo_type.name
assert html =~ pistol_ammo_group.ammo_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :all})
assert html =~ rifle_ammo_group.ammo_type.name
assert html =~ shotgun_ammo_group.ammo_type.name
assert html =~ pistol_ammo_group.ammo_type.name
end
test "can search for ammo_groups", %{conn: conn, ammo_group: ammo_group} do
{:ok, index_live, html} = live(conn, Routes.ammo_group_index_path(conn, :index))
@ -142,7 +196,7 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
|> follow_redirect(conn, Routes.ammo_group_index_path(conn, :index))
assert html =~ dgettext("prompts", "Ammo added successfully")
assert Ammo.list_ammo_groups(nil, false, current_user) |> Enum.count() == multiplier + 1
assert Ammo.list_ammo_groups(nil, :all, current_user) |> Enum.count() == multiplier + 1
end
test "does not save invalid number of new ammo_groups", %{conn: conn} do

View File

@ -74,28 +74,77 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
assert html =~ ammo_type.bullet_type
end
test "can sort by type", %{conn: conn, current_user: current_user} do
rifle_type = ammo_type_fixture(%{"type" => "rifle"}, current_user)
shotgun_type = ammo_type_fixture(%{"type" => "shotgun"}, current_user)
pistol_type = ammo_type_fixture(%{"type" => "pistol"}, current_user)
{:ok, index_live, html} = live(conn, Routes.ammo_type_index_path(conn, :index))
assert html =~ "All"
assert html =~ rifle_type.name
assert html =~ shotgun_type.name
assert html =~ pistol_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :rifle})
assert html =~ rifle_type.name
refute html =~ shotgun_type.name
refute html =~ pistol_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :shotgun})
refute html =~ rifle_type.name
assert html =~ shotgun_type.name
refute html =~ pistol_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :pistol})
refute html =~ rifle_type.name
refute html =~ shotgun_type.name
assert html =~ pistol_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :all})
assert html =~ rifle_type.name
assert html =~ shotgun_type.name
assert html =~ pistol_type.name
end
test "can search for ammo_type", %{conn: conn, ammo_type: ammo_type} do
{:ok, index_live, html} = live(conn, Routes.ammo_type_index_path(conn, :index))
assert html =~ ammo_type.bullet_type
assert index_live
|> form(~s/form[phx-change="search"]/,
search: %{search_term: ammo_type.bullet_type}
)
|> render_change() =~ ammo_type.bullet_type
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: ammo_type.bullet_type}) =~
ammo_type.bullet_type
assert_patch(index_live, Routes.ammo_type_index_path(conn, :search, ammo_type.bullet_type))
refute index_live
|> form(~s/form[phx-change="search"]/, search: %{search_term: "something_else"})
|> render_change() =~ ammo_type.bullet_type
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: "something_else"}) =~ ammo_type.bullet_type
assert_patch(index_live, Routes.ammo_type_index_path(conn, :search, "something_else"))
assert index_live
|> form(~s/form[phx-change="search"]/, search: %{search_term: ""})
|> render_change() =~ ammo_type.bullet_type
|> form(~s/form[phx-change="search"]/)
|> render_change(search: %{search_term: ""}) =~ ammo_type.bullet_type
assert_patch(index_live, Routes.ammo_type_index_path(conn, :index))
end
@ -114,8 +163,8 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
{:ok, _view, html} =
index_live
|> form("#ammo_type-form", ammo_type: @create_attrs)
|> render_submit()
|> form("#ammo_type-form")
|> render_submit(ammo_type: @create_attrs)
|> follow_redirect(conn, Routes.ammo_type_index_path(conn, :index))
ammo_type = ammo_type.id |> Ammo.get_ammo_type!(current_user)
@ -138,8 +187,8 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
{:ok, _view, html} =
index_live
|> form("#ammo_type-form", ammo_type: @update_attrs)
|> render_submit()
|> form("#ammo_type-form")
|> render_submit(ammo_type: @update_attrs)
|> follow_redirect(conn, Routes.ammo_type_index_path(conn, :index))
ammo_type = ammo_type.id |> Ammo.get_ammo_type!(current_user)
@ -163,8 +212,8 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
{:ok, _view, html} =
index_live
|> form("#ammo_type-form", ammo_type: @create_attrs)
|> render_submit()
|> form("#ammo_type-form")
|> render_submit(ammo_type: @create_attrs)
|> follow_redirect(conn, Routes.ammo_type_index_path(conn, :index))
ammo_type = ammo_type.id |> Ammo.get_ammo_type!(current_user)
@ -188,10 +237,10 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
{:ok, _view, html} =
index_live
|> form("#ammo_type-form",
|> form("#ammo_type-form")
|> render_submit(
ammo_type: Map.merge(@create_attrs, %{"bullet_type" => "some updated bullet_type"})
)
|> render_submit()
|> follow_redirect(conn, Routes.ammo_type_index_path(conn, :index))
ammo_type = ammo_type.id |> Ammo.get_ammo_type!(current_user)
@ -276,8 +325,8 @@ defmodule CanneryWeb.AmmoTypeLiveTest do
{:ok, _view, html} =
show_live
|> form("#ammo_type-form", ammo_type: @update_attrs)
|> render_submit()
|> form("#ammo_type-form")
|> render_submit(ammo_type: @update_attrs)
|> follow_redirect(conn, Routes.ammo_type_show_path(conn, :show, ammo_type))
ammo_type = ammo_type.id |> Ammo.get_ammo_type!(current_user)

View File

@ -6,7 +6,7 @@ defmodule CanneryWeb.ContainerLiveTest do
use CanneryWeb.ConnCase
import Phoenix.LiveViewTest
import CanneryWeb.Gettext
alias Cannery.{Containers, Repo}
alias Cannery.Containers
@moduletag :container_live_test
@ -34,10 +34,6 @@ defmodule CanneryWeb.ContainerLiveTest do
"notes" => "some ammo group",
"count" => 20
}
@shot_group_attrs %{
"notes" => "some shot group",
"count" => 20
}
# @invalid_attrs %{desc: nil, location: nil, name: nil, type: nil}
@ -53,15 +49,6 @@ defmodule CanneryWeb.ContainerLiveTest do
%{ammo_type: ammo_type, ammo_group: ammo_group}
end
defp create_empty_ammo_group(%{container: container, current_user: current_user}) do
ammo_type = ammo_type_fixture(@ammo_type_attrs, current_user)
{1, [ammo_group]} = ammo_group_fixture(@ammo_group_attrs, ammo_type, container, current_user)
shot_group = shot_group_fixture(@shot_group_attrs, current_user, ammo_group)
ammo_group = ammo_group |> Repo.reload!()
%{ammo_type: ammo_type, ammo_group: ammo_group, shot_group: shot_group}
end
describe "Index" do
setup [:register_and_log_in_user, :create_container]
@ -263,6 +250,60 @@ defmodule CanneryWeb.ContainerLiveTest do
assert html =~ dgettext("prompts", "%{name} updated successfully", name: container.name)
assert html =~ "some updated location"
end
test "can sort by type",
%{conn: conn, container: container, current_user: current_user} do
rifle_type = ammo_type_fixture(%{"type" => "rifle"}, current_user)
{1, [rifle_ammo_group]} = ammo_group_fixture(rifle_type, container, current_user)
shotgun_type = ammo_type_fixture(%{"type" => "shotgun"}, current_user)
{1, [shotgun_ammo_group]} = ammo_group_fixture(shotgun_type, container, current_user)
pistol_type = ammo_type_fixture(%{"type" => "pistol"}, current_user)
{1, [pistol_ammo_group]} = ammo_group_fixture(pistol_type, container, current_user)
{:ok, index_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
assert html =~ "All"
assert html =~ rifle_ammo_group.ammo_type.name
assert html =~ shotgun_ammo_group.ammo_type.name
assert html =~ pistol_ammo_group.ammo_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :rifle})
assert html =~ rifle_ammo_group.ammo_type.name
refute html =~ shotgun_ammo_group.ammo_type.name
refute html =~ pistol_ammo_group.ammo_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :shotgun})
refute html =~ rifle_ammo_group.ammo_type.name
assert html =~ shotgun_ammo_group.ammo_type.name
refute html =~ pistol_ammo_group.ammo_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :pistol})
refute html =~ rifle_ammo_group.ammo_type.name
refute html =~ shotgun_ammo_group.ammo_type.name
assert html =~ pistol_ammo_group.ammo_type.name
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :all})
assert html =~ rifle_ammo_group.ammo_type.name
assert html =~ shotgun_ammo_group.ammo_type.name
assert html =~ pistol_ammo_group.ammo_type.name
end
end
describe "Show with ammo group" do
@ -289,47 +330,4 @@ defmodule CanneryWeb.ContainerLiveTest do
assert html =~ "\n20\n"
end
end
describe "Show with empty ammo group" do
setup [:register_and_log_in_user, :create_container, :create_empty_ammo_group]
test "hides empty ammo groups by default",
%{conn: conn, ammo_type: %{name: ammo_type_name}, container: container} do
{:ok, show_live, html} = live(conn, Routes.container_show_path(conn, :show, container))
assert html =~ dgettext("actions", "Show used")
refute html =~ "\n20\n"
html =
show_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_show_used-label"}]/)
|> render_click()
assert html =~ ammo_type_name
assert html =~ "\n20\n"
assert html =~ "Empty"
end
test "displays empty ammo groups in table on toggle",
%{conn: conn, ammo_type: %{name: ammo_type_name}, container: container} do
{:ok, show_live, _html} = live(conn, Routes.container_show_path(conn, :show, container))
html =
show_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_table-label"}]/)
|> render_click()
assert html =~ dgettext("actions", "Show used")
refute html =~ "\n20\n"
html =
show_live
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_show_used-label"}]/)
|> render_click()
assert html =~ ammo_type_name
assert html =~ "\n20\n"
assert html =~ "Empty"
end
end
end

View File

@ -24,7 +24,12 @@ defmodule CanneryWeb.RangeLiveTest do
%{"count" => 5, "date" => ~N[2022-02-13 03:17:00], "notes" => "some notes"}
|> shot_group_fixture(current_user, ammo_group)
%{shot_group: shot_group, ammo_group: ammo_group}
[
container: container,
ammo_type: ammo_type,
ammo_group: ammo_group,
shot_group: shot_group
]
end
describe "Index" do
@ -37,6 +42,71 @@ defmodule CanneryWeb.RangeLiveTest do
assert html =~ shot_group.notes
end
test "can sort by type",
%{conn: conn, container: container, current_user: current_user} do
rifle_ammo_type = ammo_type_fixture(%{"type" => "rifle"}, current_user)
{1, [rifle_ammo_group]} = ammo_group_fixture(rifle_ammo_type, container, current_user)
rifle_shot_group =
shot_group_fixture(%{"notes" => "group_one"}, current_user, rifle_ammo_group)
shotgun_ammo_type = ammo_type_fixture(%{"type" => "shotgun"}, current_user)
{1, [shotgun_ammo_group]} = ammo_group_fixture(shotgun_ammo_type, container, current_user)
shotgun_shot_group =
shot_group_fixture(%{"notes" => "group_two"}, current_user, shotgun_ammo_group)
pistol_ammo_type = ammo_type_fixture(%{"type" => "pistol"}, current_user)
{1, [pistol_ammo_group]} = ammo_group_fixture(pistol_ammo_type, container, current_user)
pistol_shot_group =
shot_group_fixture(%{"notes" => "group_three"}, current_user, pistol_ammo_group)
{:ok, index_live, html} = live(conn, Routes.range_index_path(conn, :index))
assert html =~ "All"
assert html =~ rifle_shot_group.notes
assert html =~ shotgun_shot_group.notes
assert html =~ pistol_shot_group.notes
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :rifle})
assert html =~ rifle_shot_group.notes
refute html =~ shotgun_shot_group.notes
refute html =~ pistol_shot_group.notes
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :shotgun})
refute html =~ rifle_shot_group.notes
assert html =~ shotgun_shot_group.notes
refute html =~ pistol_shot_group.notes
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :pistol})
refute html =~ rifle_shot_group.notes
refute html =~ shotgun_shot_group.notes
assert html =~ pistol_shot_group.notes
html =
index_live
|> form(~s/form[phx-change="change_type"]/)
|> render_change(ammo_type: %{type: :all})
assert html =~ rifle_shot_group.notes
assert html =~ shotgun_shot_group.notes
assert html =~ pistol_shot_group.notes
end
test "can search for shot_group", %{conn: conn, shot_group: shot_group} do
{:ok, index_live, html} = live(conn, Routes.range_index_path(conn, :index))

View File

@ -26,7 +26,7 @@ defmodule CanneryWeb.ConnCase do
import Plug.Conn
import Phoenix.ConnTest
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
import Cannery.Fixtures
import Cannery.{DataCase, Fixtures}
import CanneryWeb.ConnCase
alias CanneryWeb.Router.Helpers, as: Routes

View File

@ -48,4 +48,15 @@ defmodule Cannery.DataCase do
end)
end)
end
@doc """
Generates a random string of any length, default of 12
"""
@spec random_string(length :: non_neg_integer()) :: String.t()
def random_string(length \\ 12) do
:crypto.strong_rand_bytes(length) |> Base.url_encode64() |> binary_part(0, length)
end
def unique_user_email, do: "user#{System.unique_integer()}@example.com"
def valid_user_password, do: "hello world!"
end

View File

@ -3,6 +3,8 @@ defmodule Cannery.Fixtures do
This module defines test helpers for creating entities
"""
import Cannery.DataCase
alias Cannery.{
Accounts,
Accounts.User,
@ -17,9 +19,6 @@ defmodule Cannery.Fixtures do
Repo
}
def unique_user_email, do: "user#{System.unique_integer()}@example.com"
def valid_user_password, do: "hello world!"
@spec user_fixture() :: User.t()
@spec user_fixture(attrs :: map()) :: User.t()
def user_fixture(attrs \\ %{}) do
@ -79,7 +78,7 @@ defmodule Cannery.Fixtures do
|> Enum.into(%{
"count" => 20,
"date" => ~N[2022-02-13 03:17:00],
"notes" => "some notes"
"notes" => random_string()
})
|> Cannery.ActivityLog.create_shot_group(user, ammo_group)
|> unwrap_ok_tuple()
@ -92,7 +91,7 @@ defmodule Cannery.Fixtures do
@spec container_fixture(attrs :: map(), User.t()) :: Container.t()
def container_fixture(attrs \\ %{}, %User{} = user) do
attrs
|> Enum.into(%{"name" => "My container", "type" => "Ammo can"})
|> Enum.into(%{"name" => random_string(), "type" => "Ammo can"})
|> Containers.create_container(user)
|> unwrap_ok_tuple()
end
@ -104,7 +103,7 @@ defmodule Cannery.Fixtures do
@spec ammo_type_fixture(attrs :: map(), User.t()) :: AmmoType.t()
def ammo_type_fixture(attrs \\ %{}, %User{} = user) do
attrs
|> Enum.into(%{"name" => "ammo_type"})
|> Enum.into(%{"name" => random_string(), "type" => "rifle"})
|> Ammo.create_ammo_type(user)
|> unwrap_ok_tuple()
end
@ -150,7 +149,7 @@ defmodule Cannery.Fixtures do
attrs
|> Enum.into(%{
"bg_color" => "#100000",
"name" => "some name",
"name" => random_string(),
"text_color" => "#000000"
})
|> Containers.create_tag(user)