From 76e23b186f279f7dc411e2b7e2d00303211573d5 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Wed, 10 Jan 2024 07:22:25 -0500 Subject: [PATCH] test: add some tests for aggregate refs in calcs --- .formatter.exs | 1 - mix.exs | 2 +- mix.lock | 2 +- test/calculation_test.exs | 36 ++++++++++++++++++++++++++++++++++ test/support/resources/post.ex | 12 +++++++++++- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/.formatter.exs b/.formatter.exs index 95e0deb..372d59d 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -26,7 +26,6 @@ spark_locals_without_parens = [ on_delete: 1, on_update: 1, polymorphic?: 1, - polymorphic_name: 1, polymorphic_on_delete: 1, polymorphic_on_update: 1, prefix: 1, diff --git a/mix.exs b/mix.exs index b1e88c2..9cb45ae 100644 --- a/mix.exs +++ b/mix.exs @@ -204,7 +204,7 @@ defmodule AshPostgres.MixProject do {:ecto, "~> 3.9"}, {:jason, "~> 1.0"}, {:postgrex, ">= 0.0.0"}, - {:ash, ash_version("~> 2.17 and >= 2.17.20")}, + {:ash, ash_version("~> 2.17 and >= 2.17.23")}, {:benchee, "~> 1.1", only: [:dev, :test]}, {:git_ops, "~> 2.5", only: [:dev, :test]}, {:ex_doc, github: "elixir-lang/ex_doc", only: [:dev, :test], runtime: false}, diff --git a/mix.lock b/mix.lock index 1c1a780..4d8ae64 100644 --- a/mix.lock +++ b/mix.lock @@ -1,5 +1,5 @@ %{ - "ash": {:hex, :ash, "2.17.20", "8b201335fac2f9ec8eb89c71c7c9007d11a09089dd82aa070ed4214c7ae02400", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: false]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:spark, ">= 1.1.50 and < 2.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:stream_data, "~> 0.6", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c89da37cf7464803b09cdd6f20c0b944764ea124b782cdfc72eeb9ac43a11445"}, + "ash": {:hex, :ash, "2.17.23", "a37aed1fa1f8a98f9d323c8fabf0eabd2fe24f4ccaea368f332bccf2fda21e9b", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: false]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:spark, ">= 1.1.50 and < 2.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:stream_data, "~> 0.6", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "72ea3b60ecb39e10fbb862184f5361b8bc28465ebb72da9f961afa0abdc9d27e"}, "benchee": {:hex, :benchee, "1.1.0", "f3a43817209a92a1fade36ef36b86e1052627fd8934a8b937ac9ab3a76c43062", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}], "hexpm", "7da57d545003165a012b587077f6ba90b89210fd88074ce3c60ce239eb5e6d93"}, "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, "certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"}, diff --git a/test/calculation_test.exs b/test/calculation_test.exs index 4b9e1c9..9058f01 100644 --- a/test/calculation_test.exs +++ b/test/calculation_test.exs @@ -244,6 +244,42 @@ defmodule AshPostgres.CalculationTest do |> Api.read!() end + test "calculations that refer to aggregates can be authorized" do + post = + Post + |> Ash.Changeset.new(%{title: "title"}) + |> Api.create!() + + Comment + |> Ash.Changeset.new(%{title: "comment"}) + |> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove) + |> Api.create!() + + assert %{has_future_comment: false} = + Post + |> Ash.Query.load([:has_future_comment, :latest_comment_created_at]) + |> Ash.Query.for_read(:allow_any, %{}) + |> Api.read_one!(authorize?: true) + + assert %{has_future_comment: true} = + Post + |> Ash.Query.load([:has_future_comment, :latest_comment_created_at]) + |> Ash.Query.for_read(:allow_any, %{}) + |> Api.read_one!(authorize?: false) + + assert %{has_future_comment: false} = + Post + |> Ash.Query.for_read(:allow_any, %{}) + |> Api.read_one!() + |> Api.load!([:has_future_comment, :latest_comment_created_at], authorize?: true) + + assert %{has_future_comment: true} = + Post + |> Ash.Query.for_read(:allow_any, %{}) + |> Api.read_one!() + |> Api.load!([:has_future_comment, :latest_comment_created_at], authorize?: false) + end + test "conditional calculations can be filtered on" do author = Author diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index 2774a91..f0110b5 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -11,6 +11,10 @@ defmodule AshPostgres.Test.Post do # Check that the post is in the same org as actor authorize_if(relates_to_actor_via([:organization, :users])) end + + policy action(:allow_any) do + authorize_if(always()) + end end postgres do @@ -45,6 +49,8 @@ defmodule AshPostgres.Test.Post do primary?(true) end + read(:allow_any) + read :paginated do pagination(offset?: true, required?: true, countable: true) end @@ -227,7 +233,11 @@ defmodule AshPostgres.Test.Post do expr(latest_arbitrary_timestamp > fragment("now()")) ) - calculate(:has_future_comment, :boolean, expr(latest_comment_created_at > fragment("now()"))) + calculate( + :has_future_comment, + :boolean, + expr(latest_comment_created_at > fragment("now()") || type(false, :boolean)) + ) calculate( :was_created_in_the_last_month,