Skip to content

Commit bfa17e6

Browse files
committed
feat: add destroy actions with linked posts validation for multitenancy test
1 parent bca05b7 commit bfa17e6

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

test/multitenancy_test.exs

+35
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,26 @@ defmodule AshPostgres.Test.MultitenancyTest do
184184
|> Ash.update!()
185185
end
186186

187+
# Test destroy with atomic validation
188+
assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no linked posts/, fn ->
189+
post
190+
|> Ash.Changeset.new()
191+
|> Ash.Changeset.put_context(:aggregate, :exists)
192+
|> Ash.Changeset.for_destroy(:destroy_if_no_linked_posts, %{})
193+
|> Ash.Changeset.set_tenant("org_" <> org1.id)
194+
|> Ash.destroy!()
195+
end
196+
197+
# Test destroy with non-atomic validation
198+
assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no linked posts/, fn ->
199+
post
200+
|> Ash.Changeset.new()
201+
|> Ash.Changeset.put_context(:aggregate, :exists)
202+
|> Ash.Changeset.for_destroy(:destroy_if_no_linked_posts_non_atomic, %{})
203+
|> Ash.Changeset.set_tenant("org_" <> org1.id)
204+
|> Ash.destroy!()
205+
end
206+
187207
# Verify that a post with no linked posts in org2 can be updated
188208
org2_post =
189209
Post
@@ -201,6 +221,21 @@ defmodule AshPostgres.Test.MultitenancyTest do
201221
|> Ash.update!()
202222

203223
assert updated_post.name == "updated"
224+
225+
# Test that a post with no linked posts in org2 can be destroyed
226+
org2_post_for_destroy =
227+
Post
228+
|> Ash.Changeset.for_create(:create, %{name: "destroyable"})
229+
|> Ash.Changeset.set_tenant("org_" <> org2.id)
230+
|> Ash.create!()
231+
232+
# This should succeed since the post has no linked posts in org2
233+
org2_post_for_destroy
234+
|> Ash.Changeset.new()
235+
|> Ash.Changeset.put_context(:aggregate, :exists)
236+
|> Ash.Changeset.for_destroy(:destroy_if_no_linked_posts, %{})
237+
|> Ash.Changeset.set_tenant("org_" <> org2.id)
238+
|> Ash.destroy!()
204239
end
205240

206241
test "loading attribute multitenant resources from context multitenant resources works" do

test/support/multitenancy/resources/post.ex

+14
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ defmodule AshPostgres.MultitenancyTest.Post do
5959
message "Can only update if Post has no linked posts"
6060
end
6161
end
62+
63+
destroy :destroy_if_no_linked_posts do
64+
validate HasNoLinkedPosts do
65+
message "Can only delete if Post has no linked posts"
66+
end
67+
end
68+
69+
destroy :destroy_if_no_linked_posts_non_atomic do
70+
require_atomic?(false)
71+
72+
validate HasNoLinkedPosts do
73+
message "Can only delete if Post has no linked posts"
74+
end
75+
end
6276
end
6377

6478
postgres do

0 commit comments

Comments
 (0)