forked from shibao/cannery
		
	use strict context boundaries and remove all n+1 queries
This commit is contained in:
		@@ -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 = %{
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user