use strict context boundaries and remove all n+1 queries
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-03-18 21:06:00 -04:00
parent 0b7146ba32
commit a54cf8b87d
84 changed files with 4345 additions and 3090 deletions

View File

@ -53,7 +53,7 @@ defmodule Cannery.InvitesTest do
test "get_use_count/2 returns the correct invite usage",
%{invite: %{token: token} = invite, current_user: current_user} do
assert 0 == Invites.get_use_count(invite, current_user)
assert Invites.get_use_count(invite, current_user) |> is_nil()
assert {:ok, _user} =
Accounts.register_user(
@ -72,6 +72,40 @@ defmodule Cannery.InvitesTest do
assert 2 == Invites.get_use_count(invite, current_user)
end
test "get_use_counts/2 returns the correct invite usage",
%{invite: %{id: invite_id, token: token} = invite, current_user: current_user} do
{:ok, %{id: another_invite_id, token: another_token} = another_invite} =
Invites.create_invite(current_user, @valid_attrs)
assert [invite, another_invite] |> Invites.get_use_counts(current_user) == %{}
assert {:ok, _user} =
Accounts.register_user(
%{"email" => unique_user_email(), "password" => valid_user_password()},
token
)
assert {:ok, _user} =
Accounts.register_user(
%{"email" => unique_user_email(), "password" => valid_user_password()},
another_token
)
use_counts = [invite, another_invite] |> Invites.get_use_counts(current_user)
assert %{^invite_id => 1} = use_counts
assert %{^another_invite_id => 1} = use_counts
assert {:ok, _user} =
Accounts.register_user(
%{"email" => unique_user_email(), "password" => valid_user_password()},
token
)
use_counts = [invite, another_invite] |> Invites.get_use_counts(current_user)
assert %{^invite_id => 2} = use_counts
assert %{^another_invite_id => 1} = use_counts
end
test "use_invite/1 successfully uses an unlimited invite",
%{invite: %{token: token} = invite, current_user: current_user} do
{:ok, invite} = Invites.update_invite(invite, %{uses_left: nil}, current_user)

View File

@ -217,5 +217,158 @@ defmodule Cannery.ActivityLogTest do
ActivityLog.get_shot_group!(shot_group.id, current_user)
end
end
test "get_used_count/2 returns accurate used count", %{
ammo_group: ammo_group,
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
{1, [another_ammo_group]} = ammo_group_fixture(ammo_type, container, current_user)
assert 0 = another_ammo_group |> ActivityLog.get_used_count(current_user)
assert 5 = ammo_group |> ActivityLog.get_used_count(current_user)
shot_group_fixture(%{"count" => 15}, current_user, ammo_group)
assert 20 = ammo_group |> ActivityLog.get_used_count(current_user)
shot_group_fixture(%{"count" => 10}, current_user, ammo_group)
assert 30 = ammo_group |> ActivityLog.get_used_count(current_user)
{1, [another_ammo_group]} = ammo_group_fixture(ammo_type, container, current_user)
assert 0 = another_ammo_group |> ActivityLog.get_used_count(current_user)
end
test "get_used_counts/2 returns accurate used counts", %{
ammo_group: %{id: ammo_group_id} = ammo_group,
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
ammo_group_fixture(ammo_type, container, current_user)
assert %{ammo_group_id => 5} ==
[ammo_group, another_ammo_group] |> ActivityLog.get_used_counts(current_user)
shot_group_fixture(%{"count" => 5}, current_user, another_ammo_group)
used_counts = [ammo_group, another_ammo_group] |> ActivityLog.get_used_counts(current_user)
assert %{^ammo_group_id => 5} = used_counts
assert %{^another_ammo_group_id => 5} = used_counts
shot_group_fixture(%{"count" => 15}, current_user, ammo_group)
used_counts = [ammo_group, another_ammo_group] |> ActivityLog.get_used_counts(current_user)
assert %{^ammo_group_id => 20} = used_counts
assert %{^another_ammo_group_id => 5} = used_counts
shot_group_fixture(%{"count" => 10}, current_user, ammo_group)
used_counts = [ammo_group, another_ammo_group] |> ActivityLog.get_used_counts(current_user)
assert %{^ammo_group_id => 30} = used_counts
assert %{^another_ammo_group_id => 5} = used_counts
end
test "get_last_used_date/2 returns accurate used count", %{
ammo_group: ammo_group,
ammo_type: ammo_type,
container: container,
shot_group: %{date: date},
current_user: current_user
} do
{1, [another_ammo_group]} = ammo_group_fixture(ammo_type, container, current_user)
assert another_ammo_group |> ActivityLog.get_last_used_date(current_user) |> is_nil()
assert ^date = ammo_group |> ActivityLog.get_last_used_date(current_user)
%{date: date} = shot_group_fixture(%{"date" => ~D[2022-11-10]}, current_user, ammo_group)
assert ^date = ammo_group |> ActivityLog.get_last_used_date(current_user)
%{date: date} = shot_group_fixture(%{"date" => ~D[2022-11-11]}, current_user, ammo_group)
assert ^date = ammo_group |> ActivityLog.get_last_used_date(current_user)
end
test "get_last_used_dates/2 returns accurate used counts", %{
ammo_group: %{id: ammo_group_id} = ammo_group,
ammo_type: ammo_type,
container: container,
shot_group: %{date: date},
current_user: current_user
} do
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
ammo_group_fixture(ammo_type, container, current_user)
# unset date
assert %{ammo_group_id => date} ==
[ammo_group, another_ammo_group] |> ActivityLog.get_last_used_dates(current_user)
shot_group_fixture(%{"date" => ~D[2022-11-09]}, current_user, another_ammo_group)
# setting initial date
last_used_shot_groups =
[ammo_group, another_ammo_group] |> ActivityLog.get_last_used_dates(current_user)
assert %{^ammo_group_id => ^date} = last_used_shot_groups
assert %{^another_ammo_group_id => ~D[2022-11-09]} = last_used_shot_groups
# setting another date
shot_group_fixture(%{"date" => ~D[2022-11-10]}, current_user, ammo_group)
last_used_shot_groups =
[ammo_group, another_ammo_group] |> ActivityLog.get_last_used_dates(current_user)
assert %{^ammo_group_id => ~D[2022-11-10]} = last_used_shot_groups
assert %{^another_ammo_group_id => ~D[2022-11-09]} = last_used_shot_groups
# setting yet another date
shot_group_fixture(%{"date" => ~D[2022-11-11]}, current_user, ammo_group)
last_used_shot_groups =
[ammo_group, another_ammo_group] |> ActivityLog.get_last_used_dates(current_user)
assert %{^ammo_group_id => ~D[2022-11-11]} = last_used_shot_groups
assert %{^another_ammo_group_id => ~D[2022-11-09]} = last_used_shot_groups
end
test "get_used_count_for_ammo_type/2 gets accurate used round count for ammo type",
%{ammo_type: ammo_type, ammo_group: ammo_group, current_user: current_user} do
another_ammo_type = ammo_type_fixture(current_user)
assert 0 = another_ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user)
assert 5 = ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user)
shot_group_fixture(%{"count" => 5}, current_user, ammo_group)
assert 10 = ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user)
shot_group_fixture(%{"count" => 1}, current_user, ammo_group)
assert 11 = ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user)
end
test "get_used_count_for_ammo_types/2 gets accurate used round count for ammo types", %{
ammo_type: %{id: ammo_type_id} = ammo_type,
container: container,
current_user: current_user
} do
# testing unused ammo type
%{id: another_ammo_type_id} = another_ammo_type = ammo_type_fixture(current_user)
{1, [ammo_group]} = ammo_group_fixture(another_ammo_type, container, current_user)
assert %{ammo_type_id => 5} ==
[ammo_type, another_ammo_type]
|> ActivityLog.get_used_count_for_ammo_types(current_user)
# use generated ammo group
shot_group_fixture(%{"count" => 5}, current_user, ammo_group)
used_counts =
[ammo_type, another_ammo_type] |> ActivityLog.get_used_count_for_ammo_types(current_user)
assert %{^ammo_type_id => 5} = used_counts
assert %{^another_ammo_type_id => 5} = used_counts
# use generated ammo group again
shot_group_fixture(%{"count" => 1}, current_user, ammo_group)
used_counts =
[ammo_type, another_ammo_type] |> ActivityLog.get_used_count_for_ammo_types(current_user)
assert %{^ammo_type_id => 5} = used_counts
assert %{^another_ammo_type_id => 6} = used_counts
end
end
end

View File

@ -151,7 +151,7 @@ defmodule Cannery.AmmoTest do
]
end
test "get_average_cost_for_ammo_type!/2 gets average cost for ammo type",
test "get_average_cost_for_ammo_type/2 gets average cost for ammo type",
%{ammo_type: ammo_type, current_user: current_user, container: container} do
{1, [_ammo_group]} =
ammo_group_fixture(
@ -161,7 +161,7 @@ defmodule Cannery.AmmoTest do
current_user
)
assert 25.0 = Ammo.get_average_cost_for_ammo_type!(ammo_type, current_user)
assert 25.0 = Ammo.get_average_cost_for_ammo_type(ammo_type, current_user)
{1, [_ammo_group]} =
ammo_group_fixture(
@ -171,7 +171,7 @@ defmodule Cannery.AmmoTest do
current_user
)
assert 25.0 = Ammo.get_average_cost_for_ammo_type!(ammo_type, current_user)
assert 25.0 = Ammo.get_average_cost_for_ammo_type(ammo_type, current_user)
{1, [_ammo_group]} =
ammo_group_fixture(
@ -181,7 +181,7 @@ defmodule Cannery.AmmoTest do
current_user
)
assert 40.0 = Ammo.get_average_cost_for_ammo_type!(ammo_type, current_user)
assert 40.0 = Ammo.get_average_cost_for_ammo_type(ammo_type, current_user)
{1, [_ammo_group]} =
ammo_group_fixture(
@ -191,11 +191,96 @@ defmodule Cannery.AmmoTest do
current_user
)
assert 37.5 = Ammo.get_average_cost_for_ammo_type!(ammo_type, current_user)
assert 37.5 = Ammo.get_average_cost_for_ammo_type(ammo_type, current_user)
end
test "get_average_cost_for_ammo_types/2 gets average costs for ammo types", %{
ammo_type: %{id: ammo_type_id} = ammo_type,
current_user: current_user,
container: container
} do
assert %{} == [ammo_type] |> Ammo.get_average_cost_for_ammo_types(current_user)
%{id: another_ammo_type_id} = another_ammo_type = ammo_type_fixture(current_user)
assert %{} ==
[ammo_type, another_ammo_type]
|> Ammo.get_average_cost_for_ammo_types(current_user)
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 25.00, "count" => 1},
another_ammo_type,
container,
current_user
)
assert %{another_ammo_type_id => 25.0} ==
[ammo_type, another_ammo_type]
|> Ammo.get_average_cost_for_ammo_types(current_user)
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 25.00, "count" => 1},
ammo_type,
container,
current_user
)
average_costs =
[ammo_type, another_ammo_type] |> Ammo.get_average_cost_for_ammo_types(current_user)
assert %{^ammo_type_id => 25.0} = average_costs
assert %{^another_ammo_type_id => 25.0} = average_costs
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 25.00, "count" => 1},
ammo_type,
container,
current_user
)
average_costs =
[ammo_type, another_ammo_type] |> Ammo.get_average_cost_for_ammo_types(current_user)
assert %{^ammo_type_id => 25.0} = average_costs
assert %{^another_ammo_type_id => 25.0} = average_costs
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 70.00, "count" => 1},
ammo_type,
container,
current_user
)
average_costs =
[ammo_type, another_ammo_type] |> Ammo.get_average_cost_for_ammo_types(current_user)
assert %{^ammo_type_id => 40.0} = average_costs
assert %{^another_ammo_type_id => 25.0} = average_costs
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 30.00, "count" => 1},
ammo_type,
container,
current_user
)
average_costs =
[ammo_type, another_ammo_type] |> Ammo.get_average_cost_for_ammo_types(current_user)
assert %{^ammo_type_id => 37.5} = average_costs
assert %{^another_ammo_type_id => 25.0} = average_costs
end
test "get_round_count_for_ammo_type/2 gets accurate round count for ammo type",
%{ammo_type: ammo_type, current_user: current_user, container: container} do
another_ammo_type = ammo_type_fixture(current_user)
assert 0 = Ammo.get_round_count_for_ammo_type(another_ammo_type, current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
@ -212,26 +297,57 @@ defmodule Cannery.AmmoTest do
assert 24 = Ammo.get_round_count_for_ammo_type(ammo_type, current_user)
end
test "get_used_count_for_ammo_type/2 gets accurate used round count for ammo type",
%{ammo_type: ammo_type, current_user: current_user, container: container} do
test "get_round_count_for_ammo_types/2 gets accurate round counts for ammo types", %{
ammo_type: %{id: ammo_type_id} = ammo_type,
current_user: current_user,
container: container
} do
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
assert 0 = Ammo.get_used_count_for_ammo_type(ammo_type, current_user)
assert %{ammo_type_id => 1} ==
[ammo_type] |> Ammo.get_round_count_for_ammo_types(current_user)
%{id: another_ammo_type_id} = another_ammo_type = ammo_type_fixture(current_user)
{1, [_another_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, another_ammo_type, container, current_user)
round_counts =
[ammo_type, another_ammo_type] |> Ammo.get_round_count_for_ammo_types(current_user)
assert %{^ammo_type_id => 1} = round_counts
assert %{^another_ammo_type_id => 1} = round_counts
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 50}, ammo_type, container, current_user)
assert 0 = Ammo.get_used_count_for_ammo_type(ammo_type, current_user)
round_counts =
[ammo_type, another_ammo_type] |> Ammo.get_round_count_for_ammo_types(current_user)
assert %{^ammo_type_id => 51} = round_counts
assert %{^another_ammo_type_id => 1} = round_counts
shot_group_fixture(%{"count" => 26}, current_user, ammo_group)
assert 26 = Ammo.get_used_count_for_ammo_type(ammo_type, current_user)
round_counts =
[ammo_type, another_ammo_type] |> Ammo.get_round_count_for_ammo_types(current_user)
assert %{^ammo_type_id => 25} = round_counts
assert %{^another_ammo_type_id => 1} = round_counts
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
assert 27 = Ammo.get_used_count_for_ammo_type(ammo_type, current_user)
round_counts =
[ammo_type, another_ammo_type] |> Ammo.get_round_count_for_ammo_types(current_user)
assert %{^ammo_type_id => 24} = round_counts
assert %{^another_ammo_type_id => 1} = round_counts
end
test "get_historical_count_for_ammo_type/2 gets accurate total round count for ammo type",
%{ammo_type: ammo_type, current_user: current_user, container: container} do
assert 0 = Ammo.get_historical_count_for_ammo_type(ammo_type, current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
@ -248,8 +364,60 @@ defmodule Cannery.AmmoTest do
assert 51 = Ammo.get_historical_count_for_ammo_type(ammo_type, current_user)
end
test "get_historical_count_for_ammo_types/2 gets accurate total round counts for ammo types",
%{
ammo_type: %{id: ammo_type_id} = ammo_type,
current_user: current_user,
container: container
} do
assert %{} == [ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
assert %{ammo_type_id => 1} ==
[ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
%{id: another_ammo_type_id} = another_ammo_type = ammo_type_fixture(current_user)
{1, [_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, another_ammo_type, container, current_user)
historical_counts =
[ammo_type, another_ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
assert %{^ammo_type_id => 1} = historical_counts
assert %{^another_ammo_type_id => 1} = historical_counts
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 50}, ammo_type, container, current_user)
historical_counts =
[ammo_type, another_ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
assert %{^ammo_type_id => 51} = historical_counts
assert %{^another_ammo_type_id => 1} = historical_counts
shot_group_fixture(%{"count" => 26}, current_user, ammo_group)
historical_counts =
[ammo_type, another_ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
assert %{^ammo_type_id => 51} = historical_counts
assert %{^another_ammo_type_id => 1} = historical_counts
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
historical_counts =
[ammo_type, another_ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
assert %{^ammo_type_id => 51} = historical_counts
assert %{^another_ammo_type_id => 1} = historical_counts
end
test "get_used_ammo_groups_count_for_type/2 gets accurate total ammo count for ammo type",
%{ammo_type: ammo_type, current_user: current_user, container: container} do
assert 0 = Ammo.get_used_ammo_groups_count_for_type(ammo_type, current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
@ -265,6 +433,182 @@ defmodule Cannery.AmmoTest do
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
assert 2 = Ammo.get_used_ammo_groups_count_for_type(ammo_type, current_user)
end
test "get_used_ammo_groups_count_for_types/2 gets accurate total ammo counts for ammo types",
%{
ammo_type: %{id: ammo_type_id} = ammo_type,
current_user: current_user,
container: container
} do
# testing empty ammo type
assert %{} == [ammo_type] |> Ammo.get_used_ammo_groups_count_for_types(current_user)
# testing two empty ammo types
%{id: another_ammo_type_id} = another_ammo_type = ammo_type_fixture(current_user)
assert %{} ==
[ammo_type, another_ammo_type]
|> Ammo.get_used_ammo_groups_count_for_types(current_user)
# testing ammo type with ammo group
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
assert %{} ==
[ammo_type, another_ammo_type]
|> Ammo.get_used_ammo_groups_count_for_types(current_user)
# testing ammo type with used ammo group
{1, [another_ammo_group]} =
ammo_group_fixture(%{"count" => 50}, another_ammo_type, container, current_user)
shot_group_fixture(%{"count" => 50}, current_user, another_ammo_group)
assert %{another_ammo_type_id => 1} ==
[ammo_type, another_ammo_type]
|> Ammo.get_used_ammo_groups_count_for_types(current_user)
# testing two ammo types with zero and one used ammo groups
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 50}, ammo_type, container, current_user)
shot_group_fixture(%{"count" => 50}, current_user, ammo_group)
used_counts =
[ammo_type, another_ammo_type] |> Ammo.get_used_ammo_groups_count_for_types(current_user)
assert %{^ammo_type_id => 1} = used_counts
assert %{^another_ammo_type_id => 1} = used_counts
# testing two ammo type with one and two used ammo groups
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
used_counts =
[ammo_type, another_ammo_type] |> Ammo.get_used_ammo_groups_count_for_types(current_user)
assert %{^ammo_type_id => 2} = used_counts
assert %{^another_ammo_type_id => 1} = used_counts
end
test "get_ammo_groups_count_for_container!/2 gets accurate ammo count for container",
%{ammo_type: ammo_type, current_user: current_user, container: container} do
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, container, current_user)
assert 1 = Ammo.get_ammo_groups_count_for_container!(container, current_user)
{25, _ammo_groups} =
ammo_group_fixture(%{"count" => 5}, 25, ammo_type, container, current_user)
assert 26 = Ammo.get_ammo_groups_count_for_container!(container, current_user)
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
assert 26 = Ammo.get_ammo_groups_count_for_container!(container, current_user)
shot_group_fixture(%{"count" => 4}, current_user, first_ammo_group)
assert 25 = Ammo.get_ammo_groups_count_for_container!(container, current_user)
end
test "get_ammo_groups_count_for_containers/2 gets accurate ammo count for containers", %{
ammo_type: ammo_type,
current_user: current_user,
container: %{id: container_id} = container
} do
%{id: another_container_id} = another_container = container_fixture(current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, container, current_user)
{1, [_first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, another_container, current_user)
ammo_groups_count =
[container, another_container]
|> Ammo.get_ammo_groups_count_for_containers(current_user)
assert %{^container_id => 1} = ammo_groups_count
assert %{^another_container_id => 1} = ammo_groups_count
{25, _ammo_groups} =
ammo_group_fixture(%{"count" => 5}, 25, ammo_type, container, current_user)
ammo_groups_count =
[container, another_container]
|> Ammo.get_ammo_groups_count_for_containers(current_user)
assert %{^container_id => 26} = ammo_groups_count
assert %{^another_container_id => 1} = ammo_groups_count
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
ammo_groups_count =
[container, another_container]
|> Ammo.get_ammo_groups_count_for_containers(current_user)
assert %{^container_id => 26} = ammo_groups_count
assert %{^another_container_id => 1} = ammo_groups_count
shot_group_fixture(%{"count" => 4}, current_user, first_ammo_group)
ammo_groups_count =
[container, another_container]
|> Ammo.get_ammo_groups_count_for_containers(current_user)
assert %{^container_id => 25} = ammo_groups_count
assert %{^another_container_id => 1} = ammo_groups_count
end
test "get_round_count_for_container!/2 gets accurate total round count for container",
%{ammo_type: ammo_type, current_user: current_user, container: container} do
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, container, current_user)
assert 5 = Ammo.get_round_count_for_container!(container, current_user)
{25, _ammo_groups} =
ammo_group_fixture(%{"count" => 5}, 25, ammo_type, container, current_user)
assert 130 = Ammo.get_round_count_for_container!(container, current_user)
shot_group_fixture(%{"count" => 5}, current_user, first_ammo_group)
assert 125 = Ammo.get_round_count_for_container!(container, current_user)
end
test "get_round_count_for_containers/2 gets accurate total round count for containers",
%{
ammo_type: ammo_type,
current_user: current_user,
container: %{id: container_id} = container
} do
%{id: another_container_id} = another_container = container_fixture(current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, container, current_user)
{1, [_first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, another_container, current_user)
round_counts =
[container, another_container] |> Ammo.get_round_count_for_containers(current_user)
assert %{^container_id => 5} = round_counts
assert %{^another_container_id => 5} = round_counts
{25, _ammo_groups} =
ammo_group_fixture(%{"count" => 5}, 25, ammo_type, container, current_user)
round_counts =
[container, another_container] |> Ammo.get_round_count_for_containers(current_user)
assert %{^container_id => 130} = round_counts
assert %{^another_container_id => 5} = round_counts
shot_group_fixture(%{"count" => 5}, current_user, first_ammo_group)
round_counts =
[container, another_container] |> Ammo.get_round_count_for_containers(current_user)
assert %{^container_id => 125} = round_counts
assert %{^another_container_id => 5} = round_counts
end
end
describe "ammo_groups" do
@ -316,18 +660,16 @@ defmodule Cannery.AmmoTest do
container: container,
current_user: current_user
} do
{1, [another_ammo_group]} =
{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 = another_ammo_group |> Repo.reload!()
another_ammo_group = Ammo.get_ammo_group!(another_ammo_group_id, current_user)
assert Ammo.list_ammo_groups(nil, false, current_user) ==
[ammo_group] |> preload_ammo_group()
assert Ammo.list_ammo_groups(nil, false, current_user) == [ammo_group]
assert Ammo.list_ammo_groups(nil, true, current_user)
|> Enum.sort_by(fn %{count: count} -> count end) ==
[another_ammo_group, ammo_group] |> preload_ammo_group()
|> Enum.sort_by(fn %{count: count} -> count end) == [another_ammo_group, ammo_group]
end
test "list_ammo_groups/3 returns relevant ammo groups when searched",
@ -356,30 +698,22 @@ defmodule Cannery.AmmoTest do
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]
|> preload_ammo_group()
# search works for ammo group attributes
assert Ammo.list_ammo_groups("cool", true, current_user) ==
[another_ammo_group] |> preload_ammo_group()
assert Ammo.list_ammo_groups("cool", true, current_user) == [another_ammo_group]
# search works for ammo type attributes
assert Ammo.list_ammo_groups("amazing", true, current_user) ==
[amazing_ammo_group] |> preload_ammo_group()
assert Ammo.list_ammo_groups("amazing", true, current_user) == [amazing_ammo_group]
# search works for container attributes
assert Ammo.list_ammo_groups("fantastic", true, current_user) ==
[fantastic_ammo_group] |> preload_ammo_group()
assert Ammo.list_ammo_groups("fantastic", true, current_user) == [fantastic_ammo_group]
# search works for container tag attributes
assert Ammo.list_ammo_groups("stupendous", true, current_user) ==
[fantastic_ammo_group] |> preload_ammo_group()
assert Ammo.list_ammo_groups("stupendous", true, current_user) == [fantastic_ammo_group]
assert Ammo.list_ammo_groups("random", true, current_user) == []
end
defp preload_ammo_group(ammo_group),
do: ammo_group |> Repo.preload([:ammo_type, :shot_groups, container: :tags])
test "list_ammo_groups_for_type/2 returns all ammo_groups for a type",
%{
ammo_type: ammo_type,
@ -390,8 +724,7 @@ defmodule Cannery.AmmoTest do
another_ammo_type = ammo_type_fixture(current_user)
{1, [_another]} = ammo_group_fixture(another_ammo_type, container, current_user)
assert Ammo.list_ammo_groups_for_type(ammo_type, current_user) ==
[ammo_group] |> Repo.preload(:shot_groups)
assert Ammo.list_ammo_groups_for_type(ammo_type, current_user) == [ammo_group]
end
test "list_ammo_groups_for_container/2 returns all ammo_groups for a container",
@ -404,20 +737,52 @@ defmodule Cannery.AmmoTest do
another_container = container_fixture(current_user)
{1, [_another]} = ammo_group_fixture(ammo_type, another_container, current_user)
assert Ammo.list_ammo_groups_for_container(container, current_user) ==
[ammo_group] |> Repo.preload(:shot_groups)
assert Ammo.list_ammo_groups_for_container(container, current_user) == [ammo_group]
end
test "get_ammo_groups_count_for_type/2 returns count of ammo_groups for a type",
%{
ammo_type: ammo_type,
container: container,
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 "get_ammo_groups_count_for_type/2 returns count of ammo_groups for a type", %{
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
assert 1 = Ammo.get_ammo_groups_count_for_type(ammo_type, current_user)
another_ammo_type = ammo_type_fixture(current_user)
assert 0 = Ammo.get_ammo_groups_count_for_type(another_ammo_type, current_user)
{5, _ammo_groups} = ammo_group_fixture(%{}, 5, ammo_type, container, current_user)
assert 6 = Ammo.get_ammo_groups_count_for_type(ammo_type, current_user)
end
test "get_ammo_groups_count_for_types/2 returns counts of ammo_groups for types", %{
ammo_type: %{id: ammo_type_id} = ammo_type,
container: container,
current_user: current_user
} do
assert %{ammo_type_id => 1} ==
[ammo_type] |> Ammo.get_ammo_groups_count_for_types(current_user)
%{id: another_ammo_type_id} = another_ammo_type = ammo_type_fixture(current_user)
assert %{ammo_type_id => 1} ==
[ammo_type, another_ammo_type]
|> Ammo.get_ammo_groups_count_for_types(current_user)
{1, [_ammo_group]} = ammo_group_fixture(another_ammo_type, container, current_user)
ammo_groups_count =
[ammo_type, another_ammo_type] |> Ammo.get_ammo_groups_count_for_types(current_user)
assert %{^ammo_type_id => 1} = ammo_groups_count
assert %{^another_ammo_type_id => 1} = ammo_groups_count
{5, _ammo_groups} = ammo_group_fixture(%{}, 5, ammo_type, container, current_user)
ammo_groups_count =
[ammo_type, another_ammo_type] |> Ammo.get_ammo_groups_count_for_types(current_user)
assert %{^ammo_type_id => 6} = ammo_groups_count
assert %{^another_ammo_type_id => 1} = ammo_groups_count
end
test "list_staged_ammo_groups/2 returns all ammo_groups that are staged",
@ -429,14 +794,26 @@ defmodule Cannery.AmmoTest do
{1, [another_ammo_group]} =
ammo_group_fixture(%{"staged" => true}, ammo_type, container, current_user)
assert Ammo.list_staged_ammo_groups(current_user) ==
[another_ammo_group] |> Repo.preload(:shot_groups)
assert Ammo.list_staged_ammo_groups(current_user) == [another_ammo_group]
end
test "get_ammo_group!/1 returns the ammo_group with given id",
%{ammo_group: ammo_group, current_user: current_user} do
assert Ammo.get_ammo_group!(ammo_group.id, current_user) ==
ammo_group |> Repo.preload(:shot_groups)
%{ammo_group: %{id: ammo_group_id} = ammo_group, current_user: current_user} do
assert Ammo.get_ammo_group!(ammo_group_id, current_user) == ammo_group
end
test "get_ammo_groups/2 returns the ammo_groups with given id", %{
ammo_group: %{id: ammo_group_id} = ammo_group,
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
ammo_group_fixture(ammo_type, container, current_user)
ammo_groups = Ammo.get_ammo_groups([ammo_group_id, another_ammo_group_id], current_user)
assert %{^ammo_group_id => ^ammo_group} = ammo_groups
assert %{^another_ammo_group_id => ^another_ammo_group} = ammo_groups
end
test "create_ammo_groups/3 with valid data creates a ammo_group",
@ -499,124 +876,174 @@ defmodule Cannery.AmmoTest do
assert {:error, %Changeset{}} =
Ammo.update_ammo_group(ammo_group, @invalid_attrs, current_user)
assert ammo_group |> Repo.preload(:shot_groups) ==
Ammo.get_ammo_group!(ammo_group.id, current_user)
assert ammo_group == Ammo.get_ammo_group!(ammo_group.id, current_user)
end
test "delete_ammo_group/1 deletes the ammo_group",
%{ammo_group: ammo_group, current_user: current_user} do
assert {:ok, %AmmoGroup{}} = Ammo.delete_ammo_group(ammo_group, current_user)
assert_raise Ecto.NoResultsError, fn ->
Ammo.get_ammo_group!(ammo_group.id, current_user)
end
end
test "get_used_count/1 returns accurate used count",
%{ammo_group: ammo_group, current_user: current_user} do
assert 0 = Ammo.get_used_count(ammo_group)
shot_group_fixture(%{"count" => 15}, current_user, ammo_group)
assert 15 = ammo_group |> Repo.preload(:shot_groups, force: true) |> Ammo.get_used_count()
shot_group_fixture(%{"count" => 10}, current_user, ammo_group)
assert 25 = ammo_group |> Repo.preload(:shot_groups, force: true) |> Ammo.get_used_count()
end
test "get_last_used_shot_group/1 returns accurate used count",
%{ammo_group: ammo_group, current_user: current_user} do
assert ammo_group
|> Repo.preload(:shot_groups, force: true)
|> Ammo.get_last_used_shot_group()
|> is_nil()
shot_group = shot_group_fixture(%{"date" => ~D[2022-11-10]}, current_user, ammo_group)
assert ^shot_group =
ammo_group
|> Repo.preload(:shot_groups, force: true)
|> Ammo.get_last_used_shot_group()
shot_group = shot_group_fixture(%{"date" => ~D[2022-11-11]}, current_user, ammo_group)
assert ^shot_group =
ammo_group
|> Repo.preload(:shot_groups, force: true)
|> Ammo.get_last_used_shot_group()
assert_raise KeyError, fn -> Ammo.get_ammo_group!(ammo_group.id, current_user) end
end
test "get_percentage_remaining/1 gets accurate total round count",
%{ammo_group: ammo_group, current_user: current_user} do
assert 100 = Ammo.get_percentage_remaining(ammo_group)
%{ammo_group: %{id: ammo_group_id} = ammo_group, current_user: current_user} do
assert 100 = ammo_group |> Ammo.get_percentage_remaining(current_user)
shot_group_fixture(%{"count" => 14}, current_user, ammo_group)
assert 72 =
ammo_group
|> Repo.reload!()
|> Repo.preload(:shot_groups, force: true)
|> Ammo.get_percentage_remaining()
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 72 = ammo_group |> Ammo.get_percentage_remaining(current_user)
shot_group_fixture(%{"count" => 11}, current_user, ammo_group)
assert 50 =
ammo_group
|> Repo.reload!()
|> Repo.preload(:shot_groups, force: true)
|> Ammo.get_percentage_remaining()
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 50 = ammo_group |> Ammo.get_percentage_remaining(current_user)
shot_group_fixture(%{"count" => 25}, current_user, ammo_group)
assert 0 =
ammo_group
|> Repo.reload!()
|> Repo.preload(:shot_groups, force: true)
|> Ammo.get_percentage_remaining()
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 0 = ammo_group |> Ammo.get_percentage_remaining(current_user)
end
test "get_cpr/1 gets accurate cpr",
%{ammo_group: ammo_group, current_user: current_user} do
assert %AmmoGroup{price_paid: nil} |> Ammo.get_cpr() |> is_nil()
assert %AmmoGroup{count: 1, price_paid: nil} |> Ammo.get_cpr() |> is_nil()
assert 1.0 = %AmmoGroup{count: 1, price_paid: 1.0} |> Ammo.get_cpr()
assert 1.5 = %AmmoGroup{count: 2, price_paid: 3.0} |> Ammo.get_cpr()
assert 0.722 = %AmmoGroup{count: 50, price_paid: 36.1} |> Ammo.get_cpr()
test "get_cpr/2 gets accurate cpr",
%{ammo_type: ammo_type, container: container, current_user: current_user} do
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
assert ammo_group |> Ammo.get_cpr(current_user) |> is_nil()
{1, [ammo_group]} =
ammo_group_fixture(
%{"count" => 1, "price_paid" => 1.0},
ammo_type,
container,
current_user
)
assert 1.0 = ammo_group |> Ammo.get_cpr(current_user)
{1, [ammo_group]} =
ammo_group_fixture(
%{"count" => 2, "price_paid" => 3.0},
ammo_type,
container,
current_user
)
assert 1.5 = ammo_group |> Ammo.get_cpr(current_user)
{1, [ammo_group]} =
ammo_group_fixture(
%{"count" => 50, "price_paid" => 36.1},
ammo_type,
container,
current_user
)
assert 0.722 = ammo_group |> Ammo.get_cpr(current_user)
# with shot group, maintains total
shot_group_fixture(%{"count" => 14}, current_user, ammo_group)
assert 0.722 =
ammo_group
|> Repo.reload!()
|> Repo.preload(:shot_groups, force: true)
|> Ammo.get_cpr()
ammo_group = Ammo.get_ammo_group!(ammo_group.id, current_user)
assert 0.722 = ammo_group |> Ammo.get_cpr(current_user)
end
test "get_original_count/1 gets accurate original count",
%{ammo_group: ammo_group, current_user: current_user} do
assert 50 = Ammo.get_original_count(ammo_group)
test "get_cprs/2 gets accurate cprs",
%{ammo_type: ammo_type, container: container, current_user: current_user} do
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
assert %{} == [ammo_group] |> Ammo.get_cprs(current_user)
{1, [%{id: ammo_group_id} = ammo_group]} =
ammo_group_fixture(
%{"count" => 1, "price_paid" => 1.0},
ammo_type,
container,
current_user
)
assert %{ammo_group_id => 1.0} == [ammo_group] |> Ammo.get_cprs(current_user)
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
ammo_group_fixture(
%{"count" => 2, "price_paid" => 3.0},
ammo_type,
container,
current_user
)
cprs = [ammo_group, another_ammo_group] |> Ammo.get_cprs(current_user)
assert %{^ammo_group_id => 1.0} = cprs
assert %{^another_ammo_group_id => 1.5} = cprs
{1, [%{id: yet_another_ammo_group_id} = yet_another_ammo_group]} =
ammo_group_fixture(
%{"count" => 50, "price_paid" => 36.1},
ammo_type,
container,
current_user
)
cprs =
[ammo_group, another_ammo_group, yet_another_ammo_group] |> Ammo.get_cprs(current_user)
assert %{^ammo_group_id => 1.0} = cprs
assert %{^another_ammo_group_id => 1.5} = cprs
assert %{^yet_another_ammo_group_id => 0.722} = cprs
# with shot group, maintains total
shot_group_fixture(%{"count" => 14}, current_user, yet_another_ammo_group)
yet_another_ammo_group = Ammo.get_ammo_group!(yet_another_ammo_group.id, current_user)
cprs =
[ammo_group, another_ammo_group, yet_another_ammo_group] |> Ammo.get_cprs(current_user)
assert %{^ammo_group_id => 1.0} = cprs
assert %{^another_ammo_group_id => 1.5} = cprs
assert %{^yet_another_ammo_group_id => 0.722} = cprs
end
test "get_original_count/2 gets accurate original count",
%{ammo_group: %{id: ammo_group_id} = ammo_group, current_user: current_user} do
assert 50 = ammo_group |> Ammo.get_original_count(current_user)
shot_group_fixture(%{"count" => 14}, current_user, ammo_group)
assert 50 =
ammo_group
|> Repo.reload!()
|> Repo.preload(:shot_groups, force: true)
|> Ammo.get_original_count()
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 50 = ammo_group |> Ammo.get_original_count(current_user)
shot_group_fixture(%{"count" => 11}, current_user, ammo_group)
assert 50 =
ammo_group
|> Repo.reload!()
|> Repo.preload(:shot_groups, force: true)
|> Ammo.get_original_count()
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 50 = ammo_group |> Ammo.get_original_count(current_user)
shot_group_fixture(%{"count" => 25}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 50 = ammo_group |> Ammo.get_original_count(current_user)
end
assert 50 =
ammo_group
|> Repo.reload!()
|> Repo.preload(:shot_groups, force: true)
|> Ammo.get_original_count()
test "get_original_counts/2 gets accurate original counts", %{
ammo_group: %{id: ammo_group_id} = ammo_group,
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
ammo_group_fixture(%{"count" => 25}, ammo_type, container, current_user)
original_counts = [ammo_group, another_ammo_group] |> Ammo.get_original_counts(current_user)
assert %{^ammo_group_id => 50} = original_counts
assert %{^another_ammo_group_id => 25} = original_counts
shot_group_fixture(%{"count" => 14}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
original_counts = [ammo_group, another_ammo_group] |> Ammo.get_original_counts(current_user)
assert %{^ammo_group_id => 50} = original_counts
assert %{^another_ammo_group_id => 25} = original_counts
shot_group_fixture(%{"count" => 11}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
original_counts = [ammo_group, another_ammo_group] |> Ammo.get_original_counts(current_user)
assert %{^ammo_group_id => 50} = original_counts
assert %{^another_ammo_group_id => 25} = original_counts
shot_group_fixture(%{"count" => 25}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
original_counts = [ammo_group, another_ammo_group] |> Ammo.get_original_counts(current_user)
assert %{^ammo_group_id => 50} = original_counts
assert %{^another_ammo_group_id => 25} = original_counts
end
end
end

View File

@ -4,8 +4,7 @@ defmodule Cannery.ContainersTest do
"""
use Cannery.DataCase
alias Cannery.Containers
alias Cannery.{Containers.Container}
alias Cannery.{Containers, Containers.Container, Containers.Tag}
alias Ecto.Changeset
@moduletag :containers_test
@ -28,6 +27,21 @@ defmodule Cannery.ContainersTest do
"name" => nil,
"type" => nil
}
@valid_tag_attrs %{
"bg_color" => "some bg-color",
"name" => "some name",
"text_color" => "some text-color"
}
@update_tag_attrs %{
"bg_color" => "some updated bg-color",
"name" => "some updated name",
"text_color" => "some updated text-color"
}
@invalid_tag_attrs %{
"bg_color" => nil,
"name" => nil,
"text_color" => nil
}
describe "containers" do
setup do
@ -38,28 +52,27 @@ defmodule Cannery.ContainersTest do
test "list_containers/1 returns all containers",
%{current_user: current_user, container: container} do
assert Containers.list_containers(current_user) ==
[container] |> preload_containers()
assert Containers.list_containers(current_user) == [container]
end
test "list_containers/2 returns relevant containers for a user",
%{current_user: current_user} do
container_a =
container_fixture(%{"name" => "my cool container"}, current_user) |> preload_containers()
container_a = container_fixture(%{"name" => "my cool container"}, current_user)
container_b = container_fixture(%{"desc" => "a fascinating description"}, current_user)
container_b =
container_fixture(%{"desc" => "a fascinating description"}, current_user)
|> preload_containers()
%{id: container_c_id} =
container_c = container_fixture(%{"location" => "a secret place"}, current_user)
container_c = container_fixture(%{"location" => "a secret place"}, current_user)
tag = tag_fixture(%{"name" => "stupendous tag"}, current_user)
Containers.add_tag!(container_c, tag, current_user)
container_c = container_c |> preload_containers()
container_c = container_c_id |> Containers.get_container!(current_user)
%{id: container_d_id} =
container_d = container_fixture(%{"type" => "musty old box"}, current_user)
container_d = container_fixture(%{"type" => "musty old box"}, current_user)
tag = tag_fixture(%{"name" => "amazing tag"}, current_user)
Containers.add_tag!(container_d, tag, current_user)
container_d = container_d |> preload_containers()
container_d = container_d_id |> Containers.get_container!(current_user)
_shouldnt_return =
container_fixture(%{"name" => "another person's container"}, user_fixture())
@ -77,13 +90,9 @@ defmodule Cannery.ContainersTest do
assert Containers.list_containers("asajslkdflskdf", current_user) == []
end
defp preload_containers(containers),
do: containers |> Repo.preload([:ammo_groups, :tags])
test "get_container!/1 returns the container with given id",
%{current_user: current_user, container: container} do
assert Containers.get_container!(container.id, current_user) ==
container |> Repo.preload([:ammo_groups, :tags])
assert Containers.get_container!(container.id, current_user) == container
end
test "create_container/1 with valid data creates a container", %{current_user: current_user} do
@ -118,8 +127,7 @@ defmodule Cannery.ContainersTest do
assert {:error, %Changeset{}} =
Containers.update_container(container, current_user, @invalid_attrs)
assert container |> Repo.preload([:ammo_groups, :tags]) ==
Containers.get_container!(container.id, current_user)
assert container == Containers.get_container!(container.id, current_user)
end
test "delete_container/1 deletes the container",
@ -131,4 +139,67 @@ defmodule Cannery.ContainersTest do
end
end
end
describe "tags" do
setup do
current_user = user_fixture()
[tag: tag_fixture(current_user), current_user: current_user]
end
test "list_tags/1 returns all tags", %{tag: tag, current_user: current_user} do
assert Containers.list_tags(current_user) == [tag]
end
test "list_tags/2 returns relevant tags for a user", %{current_user: current_user} do
tag_a = tag_fixture(%{"name" => "bullets"}, current_user)
tag_b = tag_fixture(%{"name" => "hollows"}, current_user)
_shouldnt_return =
%{
"name" => "bullet",
"desc" => "pews brass shell"
}
|> tag_fixture(user_fixture())
# name
assert Containers.list_tags("bullet", current_user) == [tag_a]
assert Containers.list_tags("bullets", current_user) == [tag_a]
assert Containers.list_tags("hollow", current_user) == [tag_b]
assert Containers.list_tags("hollows", current_user) == [tag_b]
end
test "get_tag!/1 returns the tag with given id", %{tag: tag, current_user: current_user} do
assert Containers.get_tag!(tag.id, current_user) == tag
end
test "create_tag/1 with valid data creates a tag", %{current_user: current_user} do
assert {:ok, %Tag{} = tag} = Containers.create_tag(@valid_tag_attrs, current_user)
assert tag.bg_color == "some bg-color"
assert tag.name == "some name"
assert tag.text_color == "some text-color"
end
test "create_tag/1 with invalid data returns error changeset",
%{current_user: current_user} do
assert {:error, %Changeset{}} = Containers.create_tag(@invalid_tag_attrs, current_user)
end
test "update_tag/2 with valid data updates the tag", %{tag: tag, current_user: current_user} do
assert {:ok, %Tag{} = tag} = Containers.update_tag(tag, @update_tag_attrs, current_user)
assert tag.bg_color == "some updated bg-color"
assert tag.name == "some updated name"
assert tag.text_color == "some updated text-color"
end
test "update_tag/2 with invalid data returns error changeset",
%{tag: tag, current_user: current_user} do
assert {:error, %Changeset{}} = Containers.update_tag(tag, @invalid_tag_attrs, current_user)
assert tag == Containers.get_tag!(tag.id, current_user)
end
test "delete_tag/1 deletes the tag", %{tag: tag, current_user: current_user} do
assert {:ok, %Tag{}} = Containers.delete_tag(tag, current_user)
assert_raise Ecto.NoResultsError, fn -> Containers.get_tag!(tag.id, current_user) end
end
end
end

View File

@ -1,90 +0,0 @@
defmodule Cannery.TagsTest do
@moduledoc """
Tests the Tags context
"""
use Cannery.DataCase
alias Cannery.{Tags, Tags.Tag}
alias Ecto.Changeset
@moduletag :tags_test
@valid_attrs %{
"bg_color" => "some bg-color",
"name" => "some name",
"text_color" => "some text-color"
}
@update_attrs %{
"bg_color" => "some updated bg-color",
"name" => "some updated name",
"text_color" => "some updated text-color"
}
@invalid_attrs %{
"bg_color" => nil,
"name" => nil,
"text_color" => nil
}
describe "tags" do
setup do
current_user = user_fixture()
[tag: tag_fixture(current_user), current_user: current_user]
end
test "list_tags/1 returns all tags", %{tag: tag, current_user: current_user} do
assert Tags.list_tags(current_user) == [tag]
end
test "list_tags/2 returns relevant tags for a user", %{current_user: current_user} do
tag_a = tag_fixture(%{"name" => "bullets"}, current_user)
tag_b = tag_fixture(%{"name" => "hollows"}, current_user)
_shouldnt_return =
%{
"name" => "bullet",
"desc" => "pews brass shell"
}
|> tag_fixture(user_fixture())
# name
assert Tags.list_tags("bullet", current_user) == [tag_a]
assert Tags.list_tags("bullets", current_user) == [tag_a]
assert Tags.list_tags("hollow", current_user) == [tag_b]
assert Tags.list_tags("hollows", current_user) == [tag_b]
end
test "get_tag!/1 returns the tag with given id", %{tag: tag, current_user: current_user} do
assert Tags.get_tag!(tag.id, current_user) == tag
end
test "create_tag/1 with valid data creates a tag", %{current_user: current_user} do
assert {:ok, %Tag{} = tag} = Tags.create_tag(@valid_attrs, current_user)
assert tag.bg_color == "some bg-color"
assert tag.name == "some name"
assert tag.text_color == "some text-color"
end
test "create_tag/1 with invalid data returns error changeset",
%{current_user: current_user} do
assert {:error, %Changeset{}} = Tags.create_tag(@invalid_attrs, current_user)
end
test "update_tag/2 with valid data updates the tag", %{tag: tag, current_user: current_user} do
assert {:ok, %Tag{} = tag} = Tags.update_tag(tag, @update_attrs, current_user)
assert tag.bg_color == "some updated bg-color"
assert tag.name == "some updated name"
assert tag.text_color == "some updated text-color"
end
test "update_tag/2 with invalid data returns error changeset",
%{tag: tag, current_user: current_user} do
assert {:error, %Changeset{}} = Tags.update_tag(tag, @invalid_attrs, current_user)
assert tag == Tags.get_tag!(tag.id, current_user)
end
test "delete_tag/1 deletes the tag", %{tag: tag, current_user: current_user} do
assert {:ok, %Tag{}} = Tags.delete_tag(tag, current_user)
assert_raise Ecto.NoResultsError, fn -> Tags.get_tag!(tag.id, current_user) end
end
end
end

View File

@ -4,7 +4,7 @@ defmodule CanneryWeb.ExportControllerTest do
"""
use CanneryWeb.ConnCase
alias Cannery.{Ammo, Containers, Repo}
alias Cannery.{ActivityLog, Ammo, Containers, Repo}
@moduletag :export_controller_test
@ -50,10 +50,10 @@ defmodule CanneryWeb.ExportControllerTest do
"notes" => ammo_group.notes,
"price_paid" => ammo_group.price_paid,
"staged" => ammo_group.staged,
"used_count" => ammo_group |> Ammo.get_used_count(),
"original_count" => ammo_group |> Ammo.get_original_count(),
"cpr" => ammo_group |> Ammo.get_cpr(),
"percentage_remaining" => ammo_group |> Ammo.get_percentage_remaining()
"used_count" => ammo_group |> ActivityLog.get_used_count(current_user),
"original_count" => ammo_group |> Ammo.get_original_count(current_user),
"cpr" => ammo_group |> Ammo.get_cpr(current_user),
"percentage_remaining" => ammo_group |> Ammo.get_percentage_remaining(current_user)
}
ideal_ammo_type = %{
@ -79,10 +79,12 @@ defmodule CanneryWeb.ExportControllerTest do
"primer_type" => ammo_type.primer_type,
"tracer" => ammo_type.tracer,
"upc" => ammo_type.upc,
"average_cost" => ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user),
"average_cost" => ammo_type |> Ammo.get_average_cost_for_ammo_type(current_user),
"round_count" => ammo_type |> Ammo.get_round_count_for_ammo_type(current_user),
"used_count" => ammo_type |> Ammo.get_used_count_for_ammo_type(current_user),
"ammo_group_count" => ammo_type |> Ammo.get_ammo_groups_count_for_type(current_user, true)
"used_count" => ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user),
"ammo_group_count" => ammo_type |> Ammo.get_ammo_groups_count_for_type(current_user),
"total_ammo_group_count" =>
ammo_type |> Ammo.get_ammo_groups_count_for_type(current_user, true)
}
ideal_container = %{
@ -99,8 +101,9 @@ defmodule CanneryWeb.ExportControllerTest do
}
],
"type" => container.type,
"ammo_group_count" => container |> Containers.get_container_ammo_group_count!(),
"round_count" => container |> Containers.get_container_rounds!()
"ammo_group_count" =>
container |> Ammo.get_ammo_groups_count_for_container!(current_user),
"round_count" => container |> Ammo.get_round_count_for_container!(current_user)
}
ideal_shot_group = %{

View File

@ -214,7 +214,7 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
|> render_click()
assert html =~ dgettext("actions", "Add Ammo")
assert html =~ gettext("$%{amount}", amount: 120.5 |> :erlang.float_to_binary(decimals: 2))
assert html =~ gettext("$%{amount}", amount: display_currency(120.5))
assert_patch(index_live, Routes.ammo_group_index_path(conn, :clone, ammo_group))
@ -230,7 +230,7 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
assert html =~ dgettext("prompts", "Ammo added successfully")
assert html =~ "42"
assert html =~ gettext("$%{amount}", amount: 120.5 |> :erlang.float_to_binary(decimals: 2))
assert html =~ gettext("$%{amount}", amount: display_currency(120.5))
end
test "clones ammo_group in listing with updates", %{conn: conn, ammo_group: ammo_group} do
@ -242,7 +242,7 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
|> render_click()
assert html =~ dgettext("actions", "Add Ammo")
assert html =~ gettext("$%{amount}", amount: 120.5 |> :erlang.float_to_binary(decimals: 2))
assert html =~ gettext("$%{amount}", amount: display_currency(120.5))
assert_patch(index_live, Routes.ammo_group_index_path(conn, :clone, ammo_group))
@ -258,7 +258,7 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
assert html =~ dgettext("prompts", "Ammo added successfully")
assert html =~ "43"
assert html =~ gettext("$%{amount}", amount: 120.5 |> :erlang.float_to_binary(decimals: 2))
assert html =~ gettext("$%{amount}", amount: display_currency(120.5))
end
test "deletes ammo_group in listing", %{conn: conn, ammo_group: ammo_group} do
@ -291,21 +291,28 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
assert html =~ dgettext("prompts", "Shots recorded successfully")
end
@spec display_currency(float()) :: String.t()
defp display_currency(float), do: :erlang.float_to_binary(float, decimals: 2)
end
describe "Index of empty ammo group" do
setup [:register_and_log_in_user, :create_ammo_group, :create_empty_ammo_group]
test "hides empty ammo groups by default", %{conn: conn, empty_ammo_group: ammo_group} do
test "hides empty ammo groups by default", %{
conn: conn,
empty_ammo_group: ammo_group,
current_user: current_user
} do
{:ok, show_live, html} = live(conn, Routes.ammo_group_index_path(conn, :index))
assert html =~ dgettext("actions", "Show used")
refute html =~ gettext("$%{amount}", amount: 50.00 |> :erlang.float_to_binary(decimals: 2))
refute html =~ gettext("$%{amount}", amount: display_currency(50.00))
refute html =~
"\n" <>
gettext("%{percentage}%",
percentage: ammo_group |> Ammo.get_percentage_remaining()
percentage: ammo_group |> Ammo.get_percentage_remaining(current_user)
) <>
"\n"
@ -314,12 +321,12 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
|> element(~s/input[type="checkbox"][aria-labelledby="toggle_show_used-label"}]/)
|> render_click()
assert html =~ gettext("$%{amount}", amount: 50.00 |> :erlang.float_to_binary(decimals: 2))
assert html =~ gettext("$%{amount}", amount: display_currency(50.00))
assert html =~
"\n" <>
gettext("%{percentage}%",
percentage: ammo_group |> Ammo.get_percentage_remaining()
percentage: ammo_group |> Ammo.get_percentage_remaining(current_user)
) <>
"\n"
end

View File

@ -12,10 +12,9 @@ defmodule Cannery.Fixtures do
Ammo.AmmoType,
Containers,
Containers.Container,
Containers.Tag,
Email,
Repo,
Tags,
Tags.Tag
Repo
}
def unique_user_email, do: "user#{System.unique_integer()}@example.com"
@ -154,7 +153,7 @@ defmodule Cannery.Fixtures do
"name" => "some name",
"text_color" => "some text-color"
})
|> Tags.create_tag(user)
|> Containers.create_tag(user)
|> unwrap_ok_tuple()
end