remove all n+1 queries for real this time

This commit is contained in:
2023-03-19 15:05:09 -04:00
parent 071eb1b3c9
commit 03f8a2e8a7
24 changed files with 240 additions and 131 deletions

View File

@ -45,7 +45,7 @@ defmodule Cannery.AmmoTest do
assert Ammo.list_ammo_types(current_user) == [ammo_type]
end
test "list_ammo_types/1 returns relevant ammo_types for a user",
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}
@ -89,12 +89,12 @@ defmodule Cannery.AmmoTest do
assert Ammo.list_ammo_types("tracer", current_user) == [ammo_type_c]
end
test "get_ammo_type!/1 returns the ammo_type with given id",
test "get_ammo_type!/2 returns the ammo_type with given id",
%{ammo_type: ammo_type, current_user: current_user} do
assert Ammo.get_ammo_type!(ammo_type.id, current_user) == ammo_type
end
test "create_ammo_type/1 with valid data creates a ammo_type",
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)
assert ammo_type.bullet_type == "some bullet_type"
@ -105,12 +105,12 @@ defmodule Cannery.AmmoTest do
assert ammo_type.grains == 120
end
test "create_ammo_type/1 with invalid data returns error changeset",
test "create_ammo_type/2 with invalid data returns error changeset",
%{current_user: current_user} do
assert {:error, %Changeset{}} = Ammo.create_ammo_type(@invalid_attrs, current_user)
end
test "update_ammo_type/2 with valid data updates the ammo_type",
test "update_ammo_type/3 with valid data updates the ammo_type",
%{ammo_type: ammo_type, current_user: current_user} do
assert {:ok, %AmmoType{} = ammo_type} =
Ammo.update_ammo_type(ammo_type, @update_attrs, current_user)
@ -123,7 +123,7 @@ defmodule Cannery.AmmoTest do
assert ammo_type.grains == 456
end
test "update_ammo_type/2 with invalid data returns error changeset",
test "update_ammo_type/3 with invalid data returns error changeset",
%{ammo_type: ammo_type, current_user: current_user} do
assert {:error, %Changeset{}} =
Ammo.update_ammo_type(ammo_type, @invalid_attrs, current_user)
@ -131,7 +131,7 @@ defmodule Cannery.AmmoTest do
assert ammo_type == Ammo.get_ammo_type!(ammo_type.id, current_user)
end
test "delete_ammo_type/1 deletes the ammo_type",
test "delete_ammo_type/2 deletes the ammo_type",
%{ammo_type: ammo_type, current_user: current_user} do
assert {:ok, %AmmoType{}} = Ammo.delete_ammo_type(ammo_type, current_user)
assert_raise Ecto.NoResultsError, fn -> Ammo.get_ammo_type!(ammo_type.id, current_user) end
@ -785,7 +785,7 @@ defmodule Cannery.AmmoTest do
assert %{^another_ammo_type_id => 1} = ammo_groups_count
end
test "list_staged_ammo_groups/2 returns all ammo_groups that are staged",
test "list_staged_ammo_groups/1 returns all ammo_groups that are staged",
%{
ammo_type: ammo_type,
container: container,
@ -797,7 +797,7 @@ defmodule Cannery.AmmoTest do
assert Ammo.list_staged_ammo_groups(current_user) == [another_ammo_group]
end
test "get_ammo_group!/1 returns the ammo_group with given id",
test "get_ammo_group!/2 returns the ammo_group with given id",
%{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
@ -861,7 +861,7 @@ defmodule Cannery.AmmoTest do
|> Ammo.create_ammo_groups(1, current_user)
end
test "update_ammo_group/2 with valid data updates the ammo_group",
test "update_ammo_group/3 with valid data updates the ammo_group",
%{ammo_group: ammo_group, current_user: current_user} do
assert {:ok, %AmmoGroup{} = ammo_group} =
Ammo.update_ammo_group(ammo_group, @update_attrs, current_user)
@ -871,7 +871,7 @@ defmodule Cannery.AmmoTest do
assert ammo_group.price_paid == 456.7
end
test "update_ammo_group/2 with invalid data returns error changeset",
test "update_ammo_group/3 with invalid data returns error changeset",
%{ammo_group: ammo_group, current_user: current_user} do
assert {:error, %Changeset{}} =
Ammo.update_ammo_group(ammo_group, @invalid_attrs, current_user)
@ -879,13 +879,13 @@ defmodule Cannery.AmmoTest do
assert ammo_group == Ammo.get_ammo_group!(ammo_group.id, current_user)
end
test "delete_ammo_group/1 deletes the ammo_group",
test "delete_ammo_group/2 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 KeyError, fn -> Ammo.get_ammo_group!(ammo_group.id, current_user) end
end
test "get_percentage_remaining/1 gets accurate total round count",
test "get_percentage_remaining/2 gets accurate total round count",
%{ammo_group: %{id: ammo_group_id} = ammo_group, current_user: current_user} do
assert 100 = ammo_group |> Ammo.get_percentage_remaining(current_user)
@ -902,6 +902,53 @@ defmodule Cannery.AmmoTest do
assert 0 = ammo_group |> Ammo.get_percentage_remaining(current_user)
end
test "get_percentages_remaining/2 gets accurate total round count", %{
ammo_group: %{id: ammo_group_id} = ammo_group,
ammo_type: ammo_type,
container: container,
current_user: current_user
} do
assert %{ammo_group_id => 100} ==
[ammo_group] |> Ammo.get_percentages_remaining(current_user)
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
%{"count" => 50, "price_paid" => 36.1}
|> ammo_group_fixture(ammo_type, container, current_user)
percentages =
[ammo_group, another_ammo_group] |> Ammo.get_percentages_remaining(current_user)
assert %{^ammo_group_id => 100} = percentages
assert %{^another_ammo_group_id => 100} = percentages
shot_group_fixture(%{"count" => 14}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
percentages =
[ammo_group, another_ammo_group] |> Ammo.get_percentages_remaining(current_user)
assert %{^ammo_group_id => 72} = percentages
assert %{^another_ammo_group_id => 100} = percentages
shot_group_fixture(%{"count" => 11}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
percentages =
[ammo_group, another_ammo_group] |> Ammo.get_percentages_remaining(current_user)
assert %{^ammo_group_id => 50} = percentages
assert %{^another_ammo_group_id => 100} = percentages
shot_group_fixture(%{"count" => 25}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
percentages =
[ammo_group, another_ammo_group] |> Ammo.get_percentages_remaining(current_user)
assert %{^ammo_group_id => 0} = percentages
assert %{^another_ammo_group_id => 100} = percentages
end
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)

View File

@ -90,12 +90,24 @@ defmodule Cannery.ContainersTest do
assert Containers.list_containers("asajslkdflskdf", current_user) == []
end
test "get_container!/1 returns the container with given id",
test "get_container!/2 returns the container with given id",
%{current_user: current_user, container: container} do
assert Containers.get_container!(container.id, current_user) == container
assert_raise KeyError, fn -> Containers.get_container!(current_user.id, current_user) end
end
test "create_container/1 with valid data creates a container", %{current_user: current_user} do
test "get_containers/2 returns the container with given id",
%{current_user: current_user, container: %{id: container_id} = container} do
assert %{container_id => container} ==
Containers.get_containers([container_id], current_user)
%{id: another_container_id} = another_container = container_fixture(current_user)
containers = [container_id, another_container_id] |> Containers.get_containers(current_user)
assert %{^container_id => ^container} = containers
assert %{^another_container_id => ^another_container} = containers
end
test "create_container/2 with valid data creates a container", %{current_user: current_user} do
assert {:ok, %Container{} = container} =
@valid_attrs |> Containers.create_container(current_user)
@ -106,12 +118,12 @@ defmodule Cannery.ContainersTest do
assert container.user_id == current_user.id
end
test "create_container/1 with invalid data returns error changeset",
test "create_container/2 with invalid data returns error changeset",
%{current_user: current_user} do
assert {:error, %Changeset{}} = @invalid_attrs |> Containers.create_container(current_user)
end
test "update_container/2 with valid data updates the container",
test "update_container/3 with valid data updates the container",
%{current_user: current_user, container: container} do
assert {:ok, %Container{} = container} =
Containers.update_container(container, current_user, @update_attrs)
@ -122,7 +134,7 @@ defmodule Cannery.ContainersTest do
assert container.type == "some updated type"
end
test "update_container/2 with invalid data returns error changeset",
test "update_container/3 with invalid data returns error changeset",
%{current_user: current_user, container: container} do
assert {:error, %Changeset{}} =
Containers.update_container(container, current_user, @invalid_attrs)
@ -130,11 +142,11 @@ defmodule Cannery.ContainersTest do
assert container == Containers.get_container!(container.id, current_user)
end
test "delete_container/1 deletes the container",
test "delete_container/2 deletes the container",
%{current_user: current_user, container: container} do
assert {:ok, %Container{}} = Containers.delete_container(container, current_user)
assert_raise Ecto.NoResultsError, fn ->
assert_raise KeyError, fn ->
Containers.get_container!(container.id, current_user)
end
end
@ -168,36 +180,36 @@ defmodule Cannery.ContainersTest do
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
test "get_tag!/2 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
test "create_tag/2 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",
test "create_tag/2 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
test "update_tag/3 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",
test "update_tag/3 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
test "delete_tag/2 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

View File

@ -309,12 +309,8 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
assert html =~ dgettext("actions", "Show used")
refute html =~ gettext("$%{amount}", amount: display_currency(50.00))
refute html =~
"\n" <>
gettext("%{percentage}%",
percentage: ammo_group |> Ammo.get_percentage_remaining(current_user)
) <>
"\n"
percentage = ammo_group |> Ammo.get_percentage_remaining(current_user)
refute html =~ "\n#{gettext("%{percentage}%", percentage: percentage)}\n"
html =
show_live
@ -323,12 +319,8 @@ defmodule CanneryWeb.AmmoGroupLiveTest do
assert html =~ gettext("$%{amount}", amount: display_currency(50.00))
assert html =~
"\n" <>
gettext("%{percentage}%",
percentage: ammo_group |> Ammo.get_percentage_remaining(current_user)
) <>
"\n"
percentage = ammo_group |> Ammo.get_percentage_remaining(current_user)
assert html =~ "\n#{gettext("%{percentage}%", percentage: percentage)}\n"
end
end