Skip to content

Commit

Permalink
improvement: add tests for calculations through from_many? relationships
Browse files Browse the repository at this point in the history
test: improve test QOL with ecto dev logger
  • Loading branch information
zachdaniel committed Nov 4, 2024
1 parent d8e88fa commit 48b3f6c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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]},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
23 changes: 23 additions & 0 deletions test/complex_calculations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions test/support/complex_calculations/resources/channel_member.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 48b3f6c

Please sign in to comment.