Skip to content

Commit 0af8a48

Browse files
authored
test: add test for cascade destroy change (#466)
1 parent dbadef8 commit 0af8a48

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

test/cascade_destroy_test.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule AshPostgresTest.CascadeDestroyTest do
2+
use AshPostgres.RepoCase, async: true
3+
4+
alias AshPostgres.Test.{Post, Rating}
5+
6+
test "can cascade destroy a has_many with parent filter" do
7+
post =
8+
Post.create!("post", %{score: 1})
9+
10+
Rating
11+
|> Ash.Changeset.for_create(:create, %{score: 2, resource_id: post.id})
12+
|> Ash.Changeset.set_context(%{data_layer: %{table: "post_ratings"}})
13+
|> Ash.create!()
14+
15+
post
16+
|> Ash.Changeset.for_destroy(:cascade_destroy)
17+
|> Ash.destroy!()
18+
19+
assert [] =
20+
Rating
21+
|> Ash.Query.for_read(:read)
22+
|> Ash.Query.set_context(%{data_layer: %{table: "post_ratings"}})
23+
|> Ash.read!()
24+
end
25+
end

test/support/resources/post.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ defmodule AshPostgres.Test.Post do
230230
)
231231
end
232232

233+
destroy :cascade_destroy do
234+
change(cascade_destroy(:high_ratings, after_action?: false))
235+
end
236+
233237
update :update do
234238
primary?(true)
235239
require_atomic?(false)
@@ -521,6 +525,13 @@ defmodule AshPostgres.Test.Post do
521525
relationship_context: %{data_layer: %{table: "post_ratings"}}
522526
)
523527

528+
has_many :high_ratings, AshPostgres.Test.Rating do
529+
public?(true)
530+
destination_attribute(:resource_id)
531+
relationship_context(%{data_layer: %{table: "post_ratings"}})
532+
filter(expr(score > parent(score)))
533+
end
534+
524535
has_many(:post_links, AshPostgres.Test.PostLink,
525536
public?: true,
526537
destination_attribute: :source_post_id,

0 commit comments

Comments
 (0)