improve Ammo.get_grouped_packs_count
This commit is contained in:
parent
a94d2eebf4
commit
f0536f3030
@ -573,114 +573,103 @@ defmodule Cannery.Ammo do
|
||||
|
||||
defp get_packs_count_container_id(query, _nil), do: query
|
||||
|
||||
@type get_grouped_packs_count_opt ::
|
||||
{:group_by, atom()}
|
||||
| {:containers, [Container.t()] | nil}
|
||||
| {:types, [Type.t()] | nil}
|
||||
| {:show_used, :only_used | boolean() | nil}
|
||||
@type get_grouped_packs_counts_opts :: [get_grouped_packs_count_opt()]
|
||||
|
||||
@doc """
|
||||
Returns the count of packs for multiple types.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_packs_count_for_types(
|
||||
...> [%Type{id: 123, user_id: 456}],
|
||||
...> %User{id: 456}
|
||||
iex> get_grouped_packs_count(
|
||||
...> %User{id: 456},
|
||||
...> group_by: :type_id,
|
||||
...> types: [%Type{id: 123, user_id: 456}]
|
||||
...> )
|
||||
3
|
||||
|
||||
iex> get_packs_count_for_types(
|
||||
...> [%Type{id: 123, user_id: 456}],
|
||||
iex> get_grouped_packs_count(
|
||||
...> %User{id: 456},
|
||||
...> true
|
||||
...> group_by: :type_id,
|
||||
...> types: [%Type{id: 123, user_id: 456}],
|
||||
...> show_used: true
|
||||
...> )
|
||||
5
|
||||
|
||||
"""
|
||||
@spec get_packs_count_for_types([Type.t()], User.t()) ::
|
||||
%{optional(Type.id()) => non_neg_integer()}
|
||||
@spec get_packs_count_for_types([Type.t()], User.t(), show_used :: boolean()) ::
|
||||
%{optional(Type.id()) => non_neg_integer()}
|
||||
def get_packs_count_for_types(types, %User{id: user_id}, show_used \\ false) do
|
||||
type_ids =
|
||||
types
|
||||
|> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end)
|
||||
iex> get_grouped_packs_count(
|
||||
...> %User{id: 456},
|
||||
...> group_by: :type_id,
|
||||
...> types: [%Type{id: 123, user_id: 456}],
|
||||
...> show_used: :only_used
|
||||
...> )
|
||||
2
|
||||
|
||||
iex> get_grouped_packs_count(
|
||||
...> %User{id: 456},
|
||||
...> group_by: :container_id,
|
||||
...> containers: [%Container{id: 123, user_id: 456}]
|
||||
...> )
|
||||
7
|
||||
|
||||
"""
|
||||
@spec get_grouped_packs_count(User.t(), get_grouped_packs_counts_opts()) ::
|
||||
%{optional(Type.id() | Container.id()) => non_neg_integer()}
|
||||
def get_grouped_packs_count(%User{id: user_id}, opts) do
|
||||
from(p in Pack,
|
||||
as: :p,
|
||||
where: p.user_id == ^user_id,
|
||||
where: p.type_id in ^type_ids,
|
||||
group_by: p.type_id,
|
||||
select: {p.type_id, count(p.id)}
|
||||
where: p.user_id == ^user_id
|
||||
)
|
||||
|> get_packs_count_for_types_maybe_show_used(show_used)
|
||||
|> get_grouped_packs_count_group_by(Keyword.fetch!(opts, :group_by))
|
||||
|> get_grouped_packs_count_filter_ids(
|
||||
Keyword.fetch!(opts, :group_by),
|
||||
Keyword.get(opts, :types)
|
||||
)
|
||||
|> get_grouped_packs_count_filter_ids(
|
||||
Keyword.fetch!(opts, :group_by),
|
||||
Keyword.get(opts, :containers)
|
||||
)
|
||||
|> get_grouped_packs_count_show_used(Keyword.get(opts, :show_used))
|
||||
|> Repo.all()
|
||||
|> Map.new()
|
||||
end
|
||||
|
||||
@spec get_packs_count_for_types_maybe_show_used(Queryable.t(), show_used :: boolean()) ::
|
||||
Queryable.t()
|
||||
defp get_packs_count_for_types_maybe_show_used(query, true), do: query
|
||||
@spec get_grouped_packs_count_group_by(Queryable.t(), :type_id | :container_id) :: Queryable.t()
|
||||
defp get_grouped_packs_count_group_by(query, group_key) when group_key |> is_atom() do
|
||||
query
|
||||
|> group_by([p: p], field(p, ^group_key))
|
||||
|> select([p: p], {field(p, ^group_key), count(p.id)})
|
||||
end
|
||||
|
||||
defp get_packs_count_for_types_maybe_show_used(query, _false) do
|
||||
@spec get_grouped_packs_count_filter_ids(
|
||||
Queryable.t(),
|
||||
:type_id | :container_id,
|
||||
[Type.t()] | [Container.t()] | nil
|
||||
) :: Queryable.t()
|
||||
defp get_grouped_packs_count_filter_ids(query, group_key, items) when items |> is_list() do
|
||||
item_ids = items |> Enum.map(fn %{id: id} -> id end)
|
||||
query |> where([p: p], field(p, ^group_key) in ^item_ids)
|
||||
end
|
||||
|
||||
defp get_grouped_packs_count_filter_ids(query, _filter_key, _nil), do: query
|
||||
|
||||
@spec get_grouped_packs_count_show_used(
|
||||
Queryable.t(),
|
||||
show_used :: :only_used | boolean() | nil
|
||||
) :: Queryable.t()
|
||||
defp get_grouped_packs_count_show_used(query, true), do: query
|
||||
|
||||
defp get_grouped_packs_count_show_used(query, :only_used) do
|
||||
query |> where([p: p], p.count == 0)
|
||||
end
|
||||
|
||||
defp get_grouped_packs_count_show_used(query, _false) do
|
||||
query |> where([p: p], not (p.count == 0))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the count of used packs for multiple types.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_used_packs_count_for_types(
|
||||
...> [%Type{id: 123, user_id: 456}],
|
||||
...> %User{id: 456}
|
||||
...> )
|
||||
%{123 => 3}
|
||||
|
||||
"""
|
||||
@spec get_used_packs_count_for_types([Type.t()], User.t()) ::
|
||||
%{optional(Type.id()) => non_neg_integer()}
|
||||
def get_used_packs_count_for_types(types, %User{id: user_id}) do
|
||||
type_ids =
|
||||
types
|
||||
|> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end)
|
||||
|
||||
Repo.all(
|
||||
from p in Pack,
|
||||
where: p.user_id == ^user_id,
|
||||
where: p.type_id in ^type_ids,
|
||||
where: p.count == 0,
|
||||
group_by: p.type_id,
|
||||
select: {p.type_id, count(p.id)}
|
||||
)
|
||||
|> Map.new()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns number of ammo packs in multiple containers.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_packs_count_for_containers(
|
||||
...> [%Container{id: 123, user_id: 456}],
|
||||
...> %User{id: 456}
|
||||
...> )
|
||||
%{123 => 3}
|
||||
|
||||
"""
|
||||
@spec get_packs_count_for_containers([Container.t()], User.t()) :: %{
|
||||
Container.id() => non_neg_integer()
|
||||
}
|
||||
def get_packs_count_for_containers(containers, %User{id: user_id}) do
|
||||
container_ids =
|
||||
containers
|
||||
|> Enum.map(fn %Container{id: container_id, user_id: ^user_id} -> container_id end)
|
||||
|
||||
Repo.all(
|
||||
from p in Pack,
|
||||
where: p.container_id in ^container_ids,
|
||||
where: p.count > 0,
|
||||
group_by: p.container_id,
|
||||
select: {p.container_id, count(p.id)}
|
||||
)
|
||||
|> Map.new()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns number of rounds in a container.
|
||||
|
||||
|
@ -71,7 +71,8 @@ defmodule CanneryWeb.Components.ContainerTableComponent do
|
||||
current_user: current_user,
|
||||
tag_actions: tag_actions,
|
||||
actions: actions,
|
||||
pack_count: Ammo.get_packs_count_for_containers(containers, current_user),
|
||||
pack_count:
|
||||
Ammo.get_grouped_packs_count(current_user, containers: containers, group_by: :container_id),
|
||||
round_count: Ammo.get_round_count_for_containers(containers, current_user)
|
||||
}
|
||||
|
||||
|
@ -152,7 +152,7 @@ defmodule CanneryWeb.Components.TypeTableComponent do
|
||||
|> TableComponent.maybe_compose_columns(%{label: gettext("Name"), key: :name, type: :name})
|
||||
|
||||
round_counts = types |> Ammo.get_round_count_for_types(current_user)
|
||||
packs_count = types |> Ammo.get_packs_count_for_types(current_user)
|
||||
packs_count = Ammo.get_grouped_packs_count(current_user, types: types, group_by: :type_id)
|
||||
average_costs = types |> Ammo.get_average_cost_for_types(current_user)
|
||||
|
||||
[used_counts, historical_round_counts, historical_pack_counts, used_pack_counts] =
|
||||
@ -160,8 +160,16 @@ defmodule CanneryWeb.Components.TypeTableComponent do
|
||||
[
|
||||
types |> ActivityLog.get_used_count_for_types(current_user),
|
||||
types |> Ammo.get_historical_count_for_types(current_user),
|
||||
types |> Ammo.get_packs_count_for_types(current_user, true),
|
||||
types |> Ammo.get_used_packs_count_for_types(current_user)
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
types: types,
|
||||
group_by: :type_id,
|
||||
show_used: true
|
||||
),
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
types: types,
|
||||
group_by: :type_id,
|
||||
show_used: :only_used
|
||||
)
|
||||
]
|
||||
else
|
||||
[nil, nil, nil, nil]
|
||||
|
@ -6,9 +6,10 @@ defmodule CanneryWeb.ExportController do
|
||||
types = Ammo.list_types(current_user)
|
||||
used_counts = types |> ActivityLog.get_used_count_for_types(current_user)
|
||||
round_counts = types |> Ammo.get_round_count_for_types(current_user)
|
||||
pack_counts = types |> Ammo.get_packs_count_for_types(current_user)
|
||||
pack_counts = Ammo.get_grouped_packs_count(current_user, types: types, group_by: :type_id)
|
||||
|
||||
total_pack_counts = types |> Ammo.get_packs_count_for_types(current_user, true)
|
||||
total_pack_counts =
|
||||
Ammo.get_grouped_packs_count(current_user, types: types, group_by: :type_id, show_used: true)
|
||||
|
||||
average_costs = types |> Ammo.get_average_cost_for_types(current_user)
|
||||
|
||||
|
2
mix.exs
2
mix.exs
@ -4,7 +4,7 @@ defmodule Cannery.MixProject do
|
||||
def project do
|
||||
[
|
||||
app: :cannery,
|
||||
version: "0.9.3",
|
||||
version: "0.9.4",
|
||||
elixir: "1.14.4",
|
||||
elixirc_paths: elixirc_paths(Mix.env()),
|
||||
start_permanent: Mix.env() == :prod,
|
||||
|
@ -502,7 +502,7 @@ msgstr "Schießkladde"
|
||||
#: lib/cannery_web/components/core_components/pack_card.html.heex:47
|
||||
#: lib/cannery_web/components/pack_table_component.ex:181
|
||||
#: lib/cannery_web/components/pack_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:265
|
||||
#: lib/cannery_web/components/type_table_component.ex:273
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:37
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:42
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:150
|
||||
@ -594,7 +594,7 @@ msgstr "Patronen:"
|
||||
|
||||
#: lib/cannery_web/components/pack_table_component.ex:178
|
||||
#: lib/cannery_web/components/pack_table_component.ex:260
|
||||
#: lib/cannery_web/components/type_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:272
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:154
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "No cost information"
|
||||
|
@ -170,7 +170,7 @@ msgstr ""
|
||||
"Ungültige Nummer an Kopien. Muss zwischen 1 and %{max} liegen. War "
|
||||
"%{multiplier}"
|
||||
|
||||
#: lib/cannery/ammo.ex:990
|
||||
#: lib/cannery/ammo.ex:979
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Invalid multiplier"
|
||||
msgstr ""
|
||||
|
@ -496,7 +496,7 @@ msgstr ""
|
||||
#: lib/cannery_web/components/core_components/pack_card.html.heex:47
|
||||
#: lib/cannery_web/components/pack_table_component.ex:181
|
||||
#: lib/cannery_web/components/pack_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:265
|
||||
#: lib/cannery_web/components/type_table_component.ex:273
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:37
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:42
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:150
|
||||
@ -588,7 +588,7 @@ msgstr ""
|
||||
|
||||
#: lib/cannery_web/components/pack_table_component.ex:178
|
||||
#: lib/cannery_web/components/pack_table_component.ex:260
|
||||
#: lib/cannery_web/components/type_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:272
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:154
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "No cost information"
|
||||
|
@ -496,7 +496,7 @@ msgstr ""
|
||||
#: lib/cannery_web/components/core_components/pack_card.html.heex:47
|
||||
#: lib/cannery_web/components/pack_table_component.ex:181
|
||||
#: lib/cannery_web/components/pack_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:265
|
||||
#: lib/cannery_web/components/type_table_component.ex:273
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:37
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:42
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:150
|
||||
@ -588,7 +588,7 @@ msgstr ""
|
||||
|
||||
#: lib/cannery_web/components/pack_table_component.ex:178
|
||||
#: lib/cannery_web/components/pack_table_component.ex:260
|
||||
#: lib/cannery_web/components/type_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:272
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:154
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "No cost information"
|
||||
|
@ -153,7 +153,7 @@ msgstr ""
|
||||
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
|
||||
msgstr ""
|
||||
|
||||
#: lib/cannery/ammo.ex:990
|
||||
#: lib/cannery/ammo.ex:979
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Invalid multiplier"
|
||||
msgstr ""
|
||||
|
@ -152,7 +152,7 @@ msgstr ""
|
||||
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
|
||||
msgstr ""
|
||||
|
||||
#: lib/cannery/ammo.ex:990
|
||||
#: lib/cannery/ammo.ex:979
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Invalid multiplier"
|
||||
msgstr ""
|
||||
|
@ -503,7 +503,7 @@ msgstr "Registro de tiros"
|
||||
#: lib/cannery_web/components/core_components/pack_card.html.heex:47
|
||||
#: lib/cannery_web/components/pack_table_component.ex:181
|
||||
#: lib/cannery_web/components/pack_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:265
|
||||
#: lib/cannery_web/components/type_table_component.ex:273
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:37
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:42
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:150
|
||||
@ -595,7 +595,7 @@ msgstr "Balas:"
|
||||
|
||||
#: lib/cannery_web/components/pack_table_component.ex:178
|
||||
#: lib/cannery_web/components/pack_table_component.ex:260
|
||||
#: lib/cannery_web/components/type_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:272
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:154
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "No cost information"
|
||||
|
@ -168,7 +168,7 @@ msgstr "No se ha podido procesar el número de copias"
|
||||
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
|
||||
msgstr "Número inválido de copias, debe ser entre 1 y %{max}. Fue %{multiplier"
|
||||
|
||||
#: lib/cannery/ammo.ex:990
|
||||
#: lib/cannery/ammo.ex:979
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Invalid multiplier"
|
||||
msgstr "Multiplicador inválido"
|
||||
|
@ -504,7 +504,7 @@ msgstr "Évènements de tir"
|
||||
#: lib/cannery_web/components/core_components/pack_card.html.heex:47
|
||||
#: lib/cannery_web/components/pack_table_component.ex:181
|
||||
#: lib/cannery_web/components/pack_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:265
|
||||
#: lib/cannery_web/components/type_table_component.ex:273
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:37
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:42
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:150
|
||||
@ -596,7 +596,7 @@ msgstr "Cartouches :"
|
||||
|
||||
#: lib/cannery_web/components/pack_table_component.ex:178
|
||||
#: lib/cannery_web/components/pack_table_component.ex:260
|
||||
#: lib/cannery_web/components/type_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:272
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:154
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "No cost information"
|
||||
|
@ -169,7 +169,7 @@ msgstr "Impossible d'analyser le nombre de copies"
|
||||
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
|
||||
msgstr "Nombre de copies invalide, doit être 1 et %{max}. Été %{multiplier}"
|
||||
|
||||
#: lib/cannery/ammo.ex:990
|
||||
#: lib/cannery/ammo.ex:979
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Invalid multiplier"
|
||||
msgstr "Multiplicateur invalide"
|
||||
|
@ -498,7 +498,7 @@ msgstr ""
|
||||
#: lib/cannery_web/components/core_components/pack_card.html.heex:47
|
||||
#: lib/cannery_web/components/pack_table_component.ex:181
|
||||
#: lib/cannery_web/components/pack_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:265
|
||||
#: lib/cannery_web/components/type_table_component.ex:273
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:37
|
||||
#: lib/cannery_web/live/pack_live/show.html.heex:42
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:150
|
||||
@ -590,7 +590,7 @@ msgstr ""
|
||||
|
||||
#: lib/cannery_web/components/pack_table_component.ex:178
|
||||
#: lib/cannery_web/components/pack_table_component.ex:260
|
||||
#: lib/cannery_web/components/type_table_component.ex:264
|
||||
#: lib/cannery_web/components/type_table_component.ex:272
|
||||
#: lib/cannery_web/live/type_live/show.html.heex:154
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "No cost information"
|
||||
|
@ -168,7 +168,7 @@ msgstr ""
|
||||
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
|
||||
msgstr ""
|
||||
|
||||
#: lib/cannery/ammo.ex:990
|
||||
#: lib/cannery/ammo.ex:979
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Invalid multiplier"
|
||||
msgstr ""
|
||||
|
@ -490,28 +490,39 @@ defmodule Cannery.AmmoTest do
|
||||
assert 2 = Ammo.get_packs_count(current_user, type_id: type.id, show_used: :only_used)
|
||||
end
|
||||
|
||||
test "get_used_packs_count_for_types/2 gets accurate total ammo counts for types",
|
||||
test "get_grouped_packs_count/2 gets accurate total ammo counts for types",
|
||||
%{
|
||||
type: %{id: type_id} = type,
|
||||
current_user: current_user,
|
||||
container: container
|
||||
} do
|
||||
# testing empty type
|
||||
assert %{} == [type] |> Ammo.get_used_packs_count_for_types(current_user)
|
||||
assert %{} ==
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
types: [type],
|
||||
group_by: :type_id,
|
||||
show_used: :only_used
|
||||
)
|
||||
|
||||
# testing two empty types
|
||||
%{id: another_type_id} = another_type = type_fixture(current_user)
|
||||
|
||||
assert %{} ==
|
||||
[type, another_type]
|
||||
|> Ammo.get_used_packs_count_for_types(current_user)
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
types: [type, another_type],
|
||||
group_by: :type_id,
|
||||
show_used: :only_used
|
||||
)
|
||||
|
||||
# testing type with pack
|
||||
{1, [first_pack]} = pack_fixture(%{count: 1}, type, container, current_user)
|
||||
|
||||
assert %{} ==
|
||||
[type, another_type]
|
||||
|> Ammo.get_used_packs_count_for_types(current_user)
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
types: [type, another_type],
|
||||
group_by: :type_id,
|
||||
show_used: :only_used
|
||||
)
|
||||
|
||||
# testing type with used pack
|
||||
{1, [another_pack]} = pack_fixture(%{count: 50}, another_type, container, current_user)
|
||||
@ -519,14 +530,22 @@ defmodule Cannery.AmmoTest do
|
||||
shot_record_fixture(%{count: 50}, current_user, another_pack)
|
||||
|
||||
assert %{another_type_id => 1} ==
|
||||
[type, another_type]
|
||||
|> Ammo.get_used_packs_count_for_types(current_user)
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
types: [type, another_type],
|
||||
group_by: :type_id,
|
||||
show_used: :only_used
|
||||
)
|
||||
|
||||
# testing two types with zero and one used packs
|
||||
{1, [pack]} = pack_fixture(%{count: 50}, type, container, current_user)
|
||||
shot_record_fixture(%{count: 50}, current_user, pack)
|
||||
|
||||
used_counts = [type, another_type] |> Ammo.get_used_packs_count_for_types(current_user)
|
||||
used_counts =
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
types: [type, another_type],
|
||||
group_by: :type_id,
|
||||
show_used: :only_used
|
||||
)
|
||||
|
||||
assert %{^type_id => 1} = used_counts
|
||||
assert %{^another_type_id => 1} = used_counts
|
||||
@ -534,7 +553,12 @@ defmodule Cannery.AmmoTest do
|
||||
# testing two type with one and two used packs
|
||||
shot_record_fixture(%{count: 1}, current_user, first_pack)
|
||||
|
||||
used_counts = [type, another_type] |> Ammo.get_used_packs_count_for_types(current_user)
|
||||
used_counts =
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
types: [type, another_type],
|
||||
group_by: :type_id,
|
||||
show_used: :only_used
|
||||
)
|
||||
|
||||
assert %{^type_id => 2} = used_counts
|
||||
assert %{^another_type_id => 1} = used_counts
|
||||
@ -557,7 +581,7 @@ defmodule Cannery.AmmoTest do
|
||||
assert 25 = Ammo.get_packs_count(current_user, container_id: container.id)
|
||||
end
|
||||
|
||||
test "get_packs_count_for_containers/2 gets accurate ammo count for containers", %{
|
||||
test "get_grouped_packs_count/2 gets accurate ammo count for containers", %{
|
||||
type: type,
|
||||
current_user: current_user,
|
||||
container: %{id: container_id} = container
|
||||
@ -569,8 +593,10 @@ defmodule Cannery.AmmoTest do
|
||||
{1, [_first_pack]} = pack_fixture(%{count: 5}, type, another_container, current_user)
|
||||
|
||||
packs_count =
|
||||
[container, another_container]
|
||||
|> Ammo.get_packs_count_for_containers(current_user)
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
containers: [container, another_container],
|
||||
group_by: :container_id
|
||||
)
|
||||
|
||||
assert %{^container_id => 1} = packs_count
|
||||
assert %{^another_container_id => 1} = packs_count
|
||||
@ -578,8 +604,10 @@ defmodule Cannery.AmmoTest do
|
||||
{25, _packs} = pack_fixture(%{count: 5}, 25, type, container, current_user)
|
||||
|
||||
packs_count =
|
||||
[container, another_container]
|
||||
|> Ammo.get_packs_count_for_containers(current_user)
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
containers: [container, another_container],
|
||||
group_by: :container_id
|
||||
)
|
||||
|
||||
assert %{^container_id => 26} = packs_count
|
||||
assert %{^another_container_id => 1} = packs_count
|
||||
@ -587,8 +615,10 @@ defmodule Cannery.AmmoTest do
|
||||
shot_record_fixture(%{count: 1}, current_user, first_pack)
|
||||
|
||||
packs_count =
|
||||
[container, another_container]
|
||||
|> Ammo.get_packs_count_for_containers(current_user)
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
containers: [container, another_container],
|
||||
group_by: :container_id
|
||||
)
|
||||
|
||||
assert %{^container_id => 26} = packs_count
|
||||
assert %{^another_container_id => 1} = packs_count
|
||||
@ -596,8 +626,10 @@ defmodule Cannery.AmmoTest do
|
||||
shot_record_fixture(%{count: 4}, current_user, first_pack)
|
||||
|
||||
packs_count =
|
||||
[container, another_container]
|
||||
|> Ammo.get_packs_count_for_containers(current_user)
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
containers: [container, another_container],
|
||||
group_by: :container_id
|
||||
)
|
||||
|
||||
assert %{^container_id => 25} = packs_count
|
||||
assert %{^another_container_id => 1} = packs_count
|
||||
@ -888,30 +920,34 @@ defmodule Cannery.AmmoTest do
|
||||
assert 6 = Ammo.get_packs_count(current_user, type_id: type.id)
|
||||
end
|
||||
|
||||
test "get_packs_count_for_types/2 returns counts of packs for types", %{
|
||||
test "get_grouped_packs_count/2 returns counts of packs for types", %{
|
||||
type: %{id: type_id} = type,
|
||||
container: container,
|
||||
current_user: current_user
|
||||
} do
|
||||
assert %{type_id => 1} ==
|
||||
[type] |> Ammo.get_packs_count_for_types(current_user)
|
||||
Ammo.get_grouped_packs_count(current_user, types: [type], group_by: :type_id)
|
||||
|
||||
%{id: another_type_id} = another_type = type_fixture(current_user)
|
||||
|
||||
assert %{type_id => 1} ==
|
||||
[type, another_type]
|
||||
|> Ammo.get_packs_count_for_types(current_user)
|
||||
Ammo.get_grouped_packs_count(current_user,
|
||||
types: [type, another_type],
|
||||
group_by: :type_id
|
||||
)
|
||||
|
||||
{1, [_pack]} = pack_fixture(another_type, container, current_user)
|
||||
|
||||
packs_count = [type, another_type] |> Ammo.get_packs_count_for_types(current_user)
|
||||
packs_count =
|
||||
Ammo.get_grouped_packs_count(current_user, types: [type, another_type], group_by: :type_id)
|
||||
|
||||
assert %{^type_id => 1} = packs_count
|
||||
assert %{^another_type_id => 1} = packs_count
|
||||
|
||||
{5, _packs} = pack_fixture(%{}, 5, type, container, current_user)
|
||||
|
||||
packs_count = [type, another_type] |> Ammo.get_packs_count_for_types(current_user)
|
||||
packs_count =
|
||||
Ammo.get_grouped_packs_count(current_user, types: [type, another_type], group_by: :type_id)
|
||||
|
||||
assert %{^type_id => 6} = packs_count
|
||||
assert %{^another_type_id => 1} = packs_count
|
||||
|
Loading…
Reference in New Issue
Block a user