ash_postgres/test/support/resources/comment.ex
Alan Heywood 1dbd7f619d Update test to demo issue with policy on aggregate
The issue occurs when an aggregate is loaded, and the aggregated
resource itself has a relates_to_actor_via policy.

The following error is produced at test/aggregate_test.exs:42

  1) test relates to actor via has_many and with an aggregate (AshPostgres.AggregateTest)
     test/aggregate_test.exs:8
     ** (RuntimeError) Could not determined related for `exists/2` expression.

     Context Resource: %{aggregates: %{count_of_comments: #count<comments from #Ash.Query<resource: AshPostgres.Test.Comment>>}, calculations: %{}, data_layer: AshPostgres.DataLayer, public?: false, relationship_path: [], resource: AshPostgres.Test.Post, root_resource: AshPostgres.Test.Post}
     Context Relationship Path: []
     At Path: []
     Path: [:post, :organization, :users]
     Related: nil
     Expression: exists(post.organization.users, [id: "236f08bc-6a3a-4a78-9911-2c24189dafac"])

     code: |> Api.read_one!()
     stacktrace:
       (ash 2.5.12) lib/ash/filter/filter.ex:2121: Ash.Filter.add_expression_part/3
       (ash 2.5.12) lib/ash/filter/filter.ex:2037: anonymous fn/3 in Ash.Filter.parse_expression/2
       (elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
       (elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
       (ash 2.5.12) lib/ash/filter/filter.ex:295: Ash.Filter.parse/5
       (ash 2.5.12) lib/ash/filter/filter.ex:995: Ash.Filter.add_to_filter/6
       (ash 2.5.12) lib/ash/query/query.ex:1797: Ash.Query.do_filter/2
       (elixir 1.14.2) lib/map.ex:883: Map.update!/3
       (ash 2.5.12) lib/ash/engine/request.ex:650: Ash.Engine.Request.apply_filter/4
       (ash 2.5.12) lib/ash/engine/request.ex:557: Ash.Engine.Request.do_strict_check/3
       (ash 2.5.12) lib/ash/engine/request.ex:518: anonymous fn/2 in Ash.Engine.Request.strict_check/2
       (elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
       (elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
       (ash 2.5.12) lib/ash/engine/request.ex:255: Ash.Engine.Request.do_next/1
       (ash 2.5.12) lib/ash/engine/request.ex:211: Ash.Engine.Request.next/1
       (ash 2.5.12) lib/ash/engine/engine.ex:650: Ash.Engine.advance_request/2
       (ash 2.5.12) lib/ash/engine/engine.ex:556: Ash.Engine.fully_advance_request/2
       (ash 2.5.12) lib/ash/engine/engine.ex:497: Ash.Engine.do_run_iteration/2
       (elixir 1.14.2) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ash 2.5.12) lib/ash/engine/engine.ex:257: Ash.Engine.run_to_completion/1
       (ash 2.5.12) lib/ash/engine/engine.ex:202: Ash.Engine.do_run/2
       (ash 2.5.12) lib/ash/engine/engine.ex:141: Ash.Engine.run/2
       (ash 2.5.12) lib/ash/actions/read.ex:170: Ash.Actions.Read.do_run/3
       (ash 2.5.12) lib/ash/actions/read.ex:90: Ash.Actions.Read.run/3
       (ash 2.5.12) lib/ash/api/api.ex:1005: Ash.Api.read_one/3
       (ash 2.5.12) lib/ash/api/api.ex:998: Ash.Api.read_one!/3
       test/aggregate_test.exs:43: (test)
2023-01-30 16:09:48 +10:00

66 lines
1.8 KiB
Elixir

defmodule AshPostgres.Test.Comment do
@moduledoc false
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
authorizers: [
Ash.Policy.Authorizer
]
policies do
bypass action_type(:read) do
# Check that the comment is in the same org (via post) as actor
authorize_if(relates_to_actor_via([:post, :organization, :users]))
end
end
postgres do
table "comments"
repo(AshPostgres.TestRepo)
references do
reference(:post, on_delete: :delete, on_update: :update, name: "special_name_fkey")
end
end
actions do
defaults([:read, :update, :destroy])
create :create do
primary?(true)
argument(:rating, :map)
change(manage_relationship(:rating, :ratings, on_missing: :ignore, on_match: :create))
end
end
attributes do
uuid_primary_key(:id)
attribute(:title, :string)
attribute(:likes, :integer)
attribute(:arbitrary_timestamp, :utc_datetime_usec)
create_timestamp(:created_at, writable?: true)
end
aggregates do
first(:post_category, :post, :category)
count(:co_popular_comments, [:post, :popular_comments])
count(:count_of_comments_containing_title, [:post, :comments_containing_title])
list(:posts_for_comments_containing_title, [:post, :comments_containing_title, :post], :title)
end
relationships do
belongs_to(:post, AshPostgres.Test.Post)
belongs_to(:author, AshPostgres.Test.Author)
has_many(:ratings, AshPostgres.Test.Rating,
destination_attribute: :resource_id,
relationship_context: %{data_layer: %{table: "comment_ratings"}}
)
has_many(:popular_ratings, AshPostgres.Test.Rating,
destination_attribute: :resource_id,
relationship_context: %{data_layer: %{table: "comment_ratings"}},
filter: expr(score > 5)
)
end
end