Skip to content

Commit 76e23b1

Browse files
committed
test: add some tests for aggregate refs in calcs
1 parent 8976ab6 commit 76e23b1

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

.formatter.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ spark_locals_without_parens = [
2626
on_delete: 1,
2727
on_update: 1,
2828
polymorphic?: 1,
29-
polymorphic_name: 1,
3029
polymorphic_on_delete: 1,
3130
polymorphic_on_update: 1,
3231
prefix: 1,

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ defmodule AshPostgres.MixProject do
204204
{:ecto, "~> 3.9"},
205205
{:jason, "~> 1.0"},
206206
{:postgrex, ">= 0.0.0"},
207-
{:ash, ash_version("~> 2.17 and >= 2.17.20")},
207+
{:ash, ash_version("~> 2.17 and >= 2.17.23")},
208208
{:benchee, "~> 1.1", only: [:dev, :test]},
209209
{:git_ops, "~> 2.5", only: [:dev, :test]},
210210
{:ex_doc, github: "elixir-lang/ex_doc", only: [:dev, :test], runtime: false},

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%{
2-
"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"},
2+
"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"},
33
"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"},
44
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
55
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},

test/calculation_test.exs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,42 @@ defmodule AshPostgres.CalculationTest do
244244
|> Api.read!()
245245
end
246246

247+
test "calculations that refer to aggregates can be authorized" do
248+
post =
249+
Post
250+
|> Ash.Changeset.new(%{title: "title"})
251+
|> Api.create!()
252+
253+
Comment
254+
|> Ash.Changeset.new(%{title: "comment"})
255+
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
256+
|> Api.create!()
257+
258+
assert %{has_future_comment: false} =
259+
Post
260+
|> Ash.Query.load([:has_future_comment, :latest_comment_created_at])
261+
|> Ash.Query.for_read(:allow_any, %{})
262+
|> Api.read_one!(authorize?: true)
263+
264+
assert %{has_future_comment: true} =
265+
Post
266+
|> Ash.Query.load([:has_future_comment, :latest_comment_created_at])
267+
|> Ash.Query.for_read(:allow_any, %{})
268+
|> Api.read_one!(authorize?: false)
269+
270+
assert %{has_future_comment: false} =
271+
Post
272+
|> Ash.Query.for_read(:allow_any, %{})
273+
|> Api.read_one!()
274+
|> Api.load!([:has_future_comment, :latest_comment_created_at], authorize?: true)
275+
276+
assert %{has_future_comment: true} =
277+
Post
278+
|> Ash.Query.for_read(:allow_any, %{})
279+
|> Api.read_one!()
280+
|> Api.load!([:has_future_comment, :latest_comment_created_at], authorize?: false)
281+
end
282+
247283
test "conditional calculations can be filtered on" do
248284
author =
249285
Author

test/support/resources/post.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ defmodule AshPostgres.Test.Post do
1111
# Check that the post is in the same org as actor
1212
authorize_if(relates_to_actor_via([:organization, :users]))
1313
end
14+
15+
policy action(:allow_any) do
16+
authorize_if(always())
17+
end
1418
end
1519

1620
postgres do
@@ -45,6 +49,8 @@ defmodule AshPostgres.Test.Post do
4549
primary?(true)
4650
end
4751

52+
read(:allow_any)
53+
4854
read :paginated do
4955
pagination(offset?: true, required?: true, countable: true)
5056
end
@@ -227,7 +233,11 @@ defmodule AshPostgres.Test.Post do
227233
expr(latest_arbitrary_timestamp > fragment("now()"))
228234
)
229235

230-
calculate(:has_future_comment, :boolean, expr(latest_comment_created_at > fragment("now()")))
236+
calculate(
237+
:has_future_comment,
238+
:boolean,
239+
expr(latest_comment_created_at > fragment("now()") || type(false, :boolean))
240+
)
231241

232242
calculate(
233243
:was_created_in_the_last_month,

0 commit comments

Comments
 (0)