@@ -256,7 +256,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
256
256
}
257
257
258
258
[ Fact ]
259
- public async Task Cannot_clear_required_OneToMany_relationship_through_primary_endpoint ( )
259
+ public async Task Clearing_OneToMany_relationship_through_primary_endpoint_triggers_cascading_delete ( )
260
260
{
261
261
// Arrange
262
262
Order existingOrder = _fakers . Orders . Generate ( ) ;
@@ -288,23 +288,25 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
288
288
string route = $ "/customers/{ existingOrder . Customer . StringId } ";
289
289
290
290
// Act
291
- ( HttpResponseMessage httpResponse , Document responseDocument ) = await _testContext . ExecutePatchAsync < Document > ( route , requestBody ) ;
291
+ ( HttpResponseMessage httpResponse , string responseDocument ) = await _testContext . ExecutePatchAsync < string > ( route , requestBody ) ;
292
292
293
293
// Assert
294
- httpResponse . Should ( ) . HaveStatusCode ( HttpStatusCode . BadRequest ) ;
294
+ httpResponse . Should ( ) . HaveStatusCode ( HttpStatusCode . NoContent ) ;
295
295
296
- responseDocument . Errors . Should ( ) . HaveCount ( 1 ) ;
296
+ responseDocument . Should ( ) . BeEmpty ( ) ;
297
297
298
- ErrorObject error = responseDocument . Errors [ 0 ] ;
299
- error . StatusCode . Should ( ) . Be ( HttpStatusCode . BadRequest ) ;
300
- error . Title . Should ( ) . Be ( "Failed to clear a required relationship." ) ;
298
+ await _testContext . RunOnDatabaseAsync ( async dbContext =>
299
+ {
300
+ Order orderInDatabase = await dbContext . Orders . Include ( order => order . Customer ) . FirstWithIdOrDefaultAsync ( existingOrder . Id ) ;
301
+ orderInDatabase . Should ( ) . BeNull ( ) ;
301
302
302
- error . Detail . Should ( ) . Be ( $ "The relationship 'orders' on resource type 'customers' with ID '{ existingOrder . StringId } ' " +
303
- "cannot be cleared because it is a required relationship." ) ;
303
+ Customer customerInDatabase = await dbContext . Customers . Include ( customer => customer . Orders ) . FirstWithIdAsync ( existingOrder . Customer . Id ) ;
304
+ customerInDatabase . Orders . Should ( ) . BeEmpty ( ) ;
305
+ } ) ;
304
306
}
305
307
306
308
[ Fact ]
307
- public async Task Cannot_clear_required_OneToMany_relationship_by_updating_through_relationship_endpoint ( )
309
+ public async Task Clearing_OneToMany_relationship_through_update_relationship_endpoint_triggers_cascading_delete ( )
308
310
{
309
311
// Arrange
310
312
Order existingOrder = _fakers . Orders . Generate ( ) ;
@@ -325,23 +327,25 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
325
327
string route = $ "/customers/{ existingOrder . Customer . StringId } /relationships/orders";
326
328
327
329
// Act
328
- ( HttpResponseMessage httpResponse , Document responseDocument ) = await _testContext . ExecutePatchAsync < Document > ( route , requestBody ) ;
330
+ ( HttpResponseMessage httpResponse , string responseDocument ) = await _testContext . ExecutePatchAsync < string > ( route , requestBody ) ;
329
331
330
332
// Assert
331
- httpResponse . Should ( ) . HaveStatusCode ( HttpStatusCode . BadRequest ) ;
333
+ httpResponse . Should ( ) . HaveStatusCode ( HttpStatusCode . NoContent ) ;
332
334
333
- responseDocument . Errors . Should ( ) . HaveCount ( 1 ) ;
335
+ responseDocument . Should ( ) . BeEmpty ( ) ;
334
336
335
- ErrorObject error = responseDocument . Errors [ 0 ] ;
336
- error . StatusCode . Should ( ) . Be ( HttpStatusCode . BadRequest ) ;
337
- error . Title . Should ( ) . Be ( "Failed to clear a required relationship." ) ;
337
+ await _testContext . RunOnDatabaseAsync ( async dbContext =>
338
+ {
339
+ Order orderInDatabase = await dbContext . Orders . Include ( order => order . Customer ) . FirstWithIdOrDefaultAsync ( existingOrder . Id ) ;
340
+ orderInDatabase . Should ( ) . BeNull ( ) ;
338
341
339
- error . Detail . Should ( ) . Be ( $ "The relationship 'orders' on resource type 'customers' with ID '{ existingOrder . StringId } ' " +
340
- "cannot be cleared because it is a required relationship." ) ;
342
+ Customer customerInDatabase = await dbContext . Customers . Include ( customer => customer . Orders ) . FirstWithIdAsync ( existingOrder . Customer . Id ) ;
343
+ customerInDatabase . Orders . Should ( ) . BeEmpty ( ) ;
344
+ } ) ;
341
345
}
342
346
343
347
[ Fact ]
344
- public async Task Cannot_clear_required_OneToMany_relationship_by_deleting_through_relationship_endpoint ( )
348
+ public async Task Clearing_OneToMany_relationship_through_delete_relationship_endpoint_triggers_cascading_delete ( )
345
349
{
346
350
// Arrange
347
351
Order existingOrder = _fakers . Orders . Generate ( ) ;
@@ -369,19 +373,21 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
369
373
string route = $ "/customers/{ existingOrder . Customer . StringId } /relationships/orders";
370
374
371
375
// Act
372
- ( HttpResponseMessage httpResponse , Document responseDocument ) = await _testContext . ExecuteDeleteAsync < Document > ( route , requestBody ) ;
376
+ ( HttpResponseMessage httpResponse , string responseDocument ) = await _testContext . ExecuteDeleteAsync < string > ( route , requestBody ) ;
373
377
374
378
// Assert
375
- httpResponse . Should ( ) . HaveStatusCode ( HttpStatusCode . BadRequest ) ;
379
+ httpResponse . Should ( ) . HaveStatusCode ( HttpStatusCode . NoContent ) ;
376
380
377
- responseDocument . Errors . Should ( ) . HaveCount ( 1 ) ;
381
+ responseDocument . Should ( ) . BeEmpty ( ) ;
378
382
379
- ErrorObject error = responseDocument . Errors [ 0 ] ;
380
- error . StatusCode . Should ( ) . Be ( HttpStatusCode . BadRequest ) ;
381
- error . Title . Should ( ) . Be ( "Failed to clear a required relationship." ) ;
383
+ await _testContext . RunOnDatabaseAsync ( async dbContext =>
384
+ {
385
+ Order orderInDatabase = await dbContext . Orders . Include ( order => order . Customer ) . FirstWithIdOrDefaultAsync ( existingOrder . Id ) ;
386
+ orderInDatabase . Should ( ) . BeNull ( ) ;
382
387
383
- error . Detail . Should ( ) . Be ( $ "The relationship 'orders' on resource type 'customers' with ID '{ existingOrder . StringId } ' " +
384
- "cannot be cleared because it is a required relationship." ) ;
388
+ Customer customerInDatabase = await dbContext . Customers . Include ( customer => customer . Orders ) . FirstWithIdAsync ( existingOrder . Customer . Id ) ;
389
+ customerInDatabase . Orders . Should ( ) . BeEmpty ( ) ;
390
+ } ) ;
385
391
}
386
392
387
393
[ Fact ]
0 commit comments