ash_postgres/test/support/resources/comment.ex

83 lines
2.3 KiB
Elixir
Raw Normal View History

defmodule AshPostgres.Test.Comment do
@moduledoc false
use Ash.Resource,
domain: AshPostgres.Test.Domain,
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 19:05:52 +13:00
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"
2022-08-19 06:56:36 +12:00
repo(AshPostgres.TestRepo)
references do
reference(:post, on_delete: :delete, on_update: :update, name: "special_name_fkey")
end
end
actions do
default_accept(:*)
2022-04-20 03:08:28 +12:00
defaults([:read, :update, :destroy])
2021-02-01 10:39:59 +13:00
create :create do
primary?(true)
argument(:rating, :map)
2021-02-01 10:39:59 +13:00
2021-02-23 17:53:18 +13:00
change(manage_relationship(:rating, :ratings, on_missing: :ignore, on_match: :create))
2021-02-01 10:39:59 +13:00
end
end
attributes do
2021-01-13 14:22:28 +13:00
uuid_primary_key(:id)
attribute(:title, :string, public?: true)
attribute(:likes, :integer, public?: true)
attribute(:arbitrary_timestamp, :utc_datetime_usec, public?: true)
create_timestamp(:created_at, writable?: true, public?: 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) do
public?(true)
end
belongs_to(:author, AshPostgres.Test.Author) do
public?(true)
end
has_many(:ratings, AshPostgres.Test.Rating,
public?: true,
2022-08-19 06:56:36 +12:00
destination_attribute: :resource_id,
2021-05-14 17:20:10 +12:00
relationship_context: %{data_layer: %{table: "comment_ratings"}}
)
has_many(:popular_ratings, AshPostgres.Test.Rating,
public?: true,
2022-08-19 06:56:36 +12:00
destination_attribute: :resource_id,
relationship_context: %{data_layer: %{table: "comment_ratings"}},
filter: expr(score > 5)
)
has_many(:ratings_with_same_score_as_post, AshPostgres.Test.Rating,
public?: true,
destination_attribute: :resource_id,
relationship_context: %{data_layer: %{table: "comment_ratings"}},
filter: expr(parent(post.score) == score)
)
end
end