Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for cascade destroy change #466

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions test/cascade_destroy_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule AshPostgresTest.CascadeDestroyTest do
use AshPostgres.RepoCase, async: true

alias AshPostgres.Test.{Post, Rating}

test "can cascade destroy a has_many with parent filter" do

Check failure on line 6 in test/cascade_destroy_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test can cascade destroy a has_many with parent filter (AshPostgresTest.CascadeDestroyTest)

Check failure on line 6 in test/cascade_destroy_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test can cascade destroy a has_many with parent filter (AshPostgresTest.CascadeDestroyTest)

Check failure on line 6 in test/cascade_destroy_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test can cascade destroy a has_many with parent filter (AshPostgresTest.CascadeDestroyTest)
post =
Post.create!("post", %{score: 1})

Rating
|> Ash.Changeset.for_create(:create, %{score: 2, resource_id: post.id})
|> Ash.Changeset.set_context(%{data_layer: %{table: "post_ratings"}})
|> Ash.create!()

post
|> Ash.Changeset.for_destroy(:cascade_destroy)
|> Ash.destroy!()

assert [] =
Rating
|> Ash.Query.for_read(:read)
|> Ash.Query.set_context(%{data_layer: %{table: "post_ratings"}})
|> Ash.read!()
end
end
11 changes: 11 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ defmodule AshPostgres.Test.Post do
)
end

destroy :cascade_destroy do
change(cascade_destroy(:high_ratings, after_action?: false))
end

update :update do
primary?(true)
require_atomic?(false)
Expand Down Expand Up @@ -521,6 +525,13 @@ defmodule AshPostgres.Test.Post do
relationship_context: %{data_layer: %{table: "post_ratings"}}
)

has_many :high_ratings, AshPostgres.Test.Rating do
public?(true)
destination_attribute(:resource_id)
relationship_context(%{data_layer: %{table: "post_ratings"}})
filter(expr(score > parent(score)))
end

has_many(:post_links, AshPostgres.Test.PostLink,
public?: true,
destination_attribute: :source_post_id,
Expand Down
Loading