test: add some tests for aggregate refs in calcs

This commit is contained in:
Zach Daniel 2024-01-10 07:22:25 -05:00
parent 8976ab6722
commit 76e23b186f
5 changed files with 49 additions and 4 deletions

View file

@ -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,

View file

@ -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},

View file

@ -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"},

View file

@ -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

View file

@ -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,