Skip to content

Commit

Permalink
test: add some tests for aggregate refs in calcs
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jan 10, 2024
1 parent 8976ab6 commit 76e23b1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
1 change: 0 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -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"},
Expand Down
36 changes: 36 additions & 0 deletions test/calculation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 76e23b1

Please sign in to comment.