@@ -184,6 +184,26 @@ defmodule AshPostgres.Test.MultitenancyTest do
184
184
|> Ash . update! ( )
185
185
end
186
186
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
+
187
207
# Verify that a post with no linked posts in org2 can be updated
188
208
org2_post =
189
209
Post
@@ -201,6 +221,21 @@ defmodule AshPostgres.Test.MultitenancyTest do
201
221
|> Ash . update! ( )
202
222
203
223
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! ( )
204
239
end
205
240
206
241
test "loading attribute multitenant resources from context multitenant resources works" do
0 commit comments