From 48b3f6c83202eb53ddc4acf0e84f4ab4edf8ab77 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Mon, 4 Nov 2024 10:29:52 -0500 Subject: [PATCH] improvement: add tests for calculations through from_many? relationships test: improve test QOL with ecto dev logger --- config/config.exs | 3 ++ mix.exs | 3 +- mix.lock | 1 + test/complex_calculations_test.exs | 23 +++++++++++++++ .../resources/channel_member.ex | 8 +++++ test/test_helper.exs | 29 +++++++++++++++++++ 6 files changed, 66 insertions(+), 1 deletion(-) diff --git a/config/config.exs b/config/config.exs index 2a17cfd8..62650332 100644 --- a/config/config.exs +++ b/config/config.exs @@ -18,6 +18,9 @@ if Mix.env() == :dev do end if Mix.env() == :test do + config :ash_postgres, AshPostgres.TestRepo, log: false + config :ash_postgres, AshPostgres.TestNoSandboxRepo, log: false + config :ash, :validate_domain_resource_inclusion?, false config :ash, :validate_domain_config_inclusion?, false diff --git a/mix.exs b/mix.exs index 5c37a0f7..4ed1dc33 100644 --- a/mix.exs +++ b/mix.exs @@ -165,7 +165,7 @@ defmodule AshPostgres.MixProject do defp deps do [ {:ash, ash_version("~> 3.4 and >= 3.4.37")}, - {:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.37")}, + {:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.39")}, {:igniter, "~> 0.4 and >= 0.4.4"}, {:ecto_sql, "~> 3.12"}, {:ecto, "~> 3.12 and >= 3.12.1"}, @@ -174,6 +174,7 @@ defmodule AshPostgres.MixProject do {:inflex, "~> 2.1"}, {:owl, "~> 0.11"}, # dev/test dependencies + {:ecto_dev_logger, "~> 0.14", only: :test}, {:eflame, "~> 1.0", only: [:dev, :test]}, {:simple_sat, "~> 0.1", only: [:dev, :test]}, {:benchee, "~> 1.1", only: [:dev, :test]}, diff --git a/mix.lock b/mix.lock index 763e41ae..fc77391b 100644 --- a/mix.lock +++ b/mix.lock @@ -10,6 +10,7 @@ "dialyxir": {:hex, :dialyxir, "1.4.4", "fb3ce8741edeaea59c9ae84d5cec75da00fa89fe401c72d6e047d11a61f65f70", [:mix], [{:erlex, ">= 0.2.7", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "cd6111e8017ccd563e65621a4d9a4a1c5cd333df30cebc7face8029cacb4eff6"}, "earmark_parser": {:hex, :earmark_parser, "1.4.41", "ab34711c9dc6212dda44fcd20ecb87ac3f3fce6f0ca2f28d4a00e4154f8cd599", [:mix], [], "hexpm", "a81a04c7e34b6617c2792e291b5a2e57ab316365c2644ddc553bb9ed863ebefa"}, "ecto": {:hex, :ecto, "3.12.4", "267c94d9f2969e6acc4dd5e3e3af5b05cdae89a4d549925f3008b2b7eb0b93c3", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ef04e4101688a67d061e1b10d7bc1fbf00d1d13c17eef08b71d070ff9188f747"}, + "ecto_dev_logger": {:hex, :ecto_dev_logger, "0.14.0", "048e20ea5e3b9c744efda9ed838b8f1e4c180020163afeec102b95143dfdce0a", [:mix], [{:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:geo, "~> 3.5.1", [hex: :geo, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "44691ba46409be073448764e3557694e5a022dcaa94b11a3d0b73b9066e1b224"}, "ecto_sql": {:hex, :ecto_sql, "3.12.1", "c0d0d60e85d9ff4631f12bafa454bc392ce8b9ec83531a412c12a0d415a3a4d0", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.12", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "aff5b958a899762c5f09028c847569f7dfb9cc9d63bdb8133bff8a5546de6bf5"}, "eflame": {:hex, :eflame, "1.0.1", "0664d287e39eef3c413749254b3af5f4f8b00be71c1af67d325331c4890be0fc", [:mix], [], "hexpm", "e0b08854a66f9013129de0b008488f3411ae9b69b902187837f994d7a99cf04e"}, "erlex": {:hex, :erlex, "0.2.7", "810e8725f96ab74d17aac676e748627a07bc87eb950d2b83acd29dc047a30595", [:mix], [], "hexpm", "3ed95f79d1a844c3f6bf0cea61e0d5612a42ce56da9c03f01df538685365efb0"}, diff --git a/test/complex_calculations_test.exs b/test/complex_calculations_test.exs index dfa5e13d..383a0ac9 100644 --- a/test/complex_calculations_test.exs +++ b/test/complex_calculations_test.exs @@ -226,6 +226,29 @@ defmodule AshPostgres.Test.ComplexCalculationsTest do assert channel.dm_name == user_2.name end + test "calculations through `from_many?` relationships properly join" do + dm_channel = + AshPostgres.Test.ComplexCalculations.DMChannel + |> Ash.Changeset.new() + |> Ash.create!() + + user_1 = + AshPostgres.Test.User + |> Ash.Changeset.for_create(:create, %{name: "User 1"}) + |> Ash.create!() + + AshPostgres.Test.ComplexCalculations.ChannelMember + |> Ash.Changeset.for_create(:create, %{channel_id: dm_channel.id, user_id: user_1.id}) + |> Ash.create!() + + assert channel_member = + AshPostgres.Test.ComplexCalculations.ChannelMember + |> Ash.Query.load(:first_member_recently_created) + |> Ash.read_one!() + + assert channel_member.first_member_recently_created + end + test "calculations with parent filters can be filtered on themselves" do AshPostgres.Test.ComplexCalculations.DMChannel |> Ash.Changeset.new() diff --git a/test/support/complex_calculations/resources/channel_member.ex b/test/support/complex_calculations/resources/channel_member.ex index 69e631e4..c9d99997 100644 --- a/test/support/complex_calculations/resources/channel_member.ex +++ b/test/support/complex_calculations/resources/channel_member.ex @@ -18,6 +18,14 @@ defmodule AshPostgres.Test.ComplexCalculations.ChannelMember do update_timestamp(:updated_at, public?: true) end + calculations do + calculate( + :first_member_recently_created, + :boolean, + expr(channel.first_member.created_at > ago(1, :day)) + ) + end + postgres do table "complex_calculations_certifications_channel_members" repo(AshPostgres.TestRepo) diff --git a/test/test_helper.exs b/test/test_helper.exs index 5b02474b..531e4ba8 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -22,3 +22,32 @@ ExUnit.configure(stacktrace_depth: 100, exclude: exclude_tags) AshPostgres.TestRepo.start_link() AshPostgres.TestNoSandboxRepo.start_link() + +format_sql_query = + try do + case System.shell("which pg_format") do + {_, 0} -> + fn query -> + try do + case System.shell("echo $SQL_QUERY | pg_format -", + env: [{"SQL_QUERY", query}], + stderr_to_stdout: true + ) do + {formatted_query, 0} -> String.trim_trailing(formatted_query) + _ -> query + end + rescue + _ -> query + end + end + + _ -> + & &1 + end + rescue + _ -> + & &1 + end + +Ecto.DevLogger.install(AshPostgres.TestRepo, before_inline_callback: format_sql_query) +Ecto.DevLogger.install(AshPostgres.TestNoSandboxRepo, before_inline_callback: format_sql_query)