Skip to content

Commit 7cdf273

Browse files
committed
Apply global convention for all integration tests: change DeleteBehavior.ClientSetNull to DeleteBehavior.SetNull
This improves speed and reduces the number of errors logged by the PostgreSQL server when running tests, because we no longer need the "truncate table cascade" fallback in DbContextExtensions.
1 parent fd16904 commit 7cdf273

File tree

17 files changed

+43
-14
lines changed

17 files changed

+43
-14
lines changed

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/OperationsDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3131
builder.Entity<MusicTrack>()
3232
.HasMany(musicTrack => musicTrack.OccursIn)
3333
.WithMany(playlist => playlist.Tracks);
34+
35+
base.OnModelCreating(builder);
3436
}
3537
}

test/JsonApiDotNetCoreTests/IntegrationTests/CompositeKeys/CompositeDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3939
builder.Entity<Car>()
4040
.HasMany(car => car.PreviousDealerships)
4141
.WithMany(dealership => dealership.SoldCars);
42+
43+
base.OnModelCreating(builder);
4244
}
4345
}

test/JsonApiDotNetCoreTests/IntegrationTests/EagerLoading/EagerLoadingDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3434
.HasOne(building => building.SecondaryDoor)
3535
.WithOne()
3636
.HasForeignKey<Building>("SecondaryDoorId");
37+
38+
base.OnModelCreating(builder);
3739
}
3840
}

test/JsonApiDotNetCoreTests/IntegrationTests/InputValidation/ModelState/ModelStateDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3737
builder.Entity<SystemDirectory>()
3838
.HasOne(systemDirectory => systemDirectory.AlsoSelf)
3939
.WithOne();
40+
41+
base.OnModelCreating(builder);
4042
}
4143
}

test/JsonApiDotNetCoreTests/IntegrationTests/Links/LinksDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ protected override void OnModelCreating(ModelBuilder builder)
2424
.HasOne(photo => photo.Location)
2525
.WithOne(location => location.Photo)
2626
.HasForeignKey<Photo>("LocationId");
27+
28+
base.OnModelCreating(builder);
2729
}
2830
}

test/JsonApiDotNetCoreTests/IntegrationTests/MultiTenancy/MultiTenancyDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3131

3232
builder.Entity<WebProduct>()
3333
.HasQueryFilter(webProduct => webProduct.Shop.TenantId == _tenantProvider.TenantId);
34+
35+
base.OnModelCreating(builder);
3436
}
3537
}

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/Filtering/FilterDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ protected override void OnModelCreating(ModelBuilder builder)
2121
builder.Entity<FilterableResource>()
2222
.Property(resource => resource.SomeDateTimeInLocalZone)
2323
.HasColumnType("timestamp without time zone");
24+
25+
base.OnModelCreating(builder);
2426
}
2527
}

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/QueryStringDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3737
.HasOne(man => man.Wife)
3838
.WithOne(woman => woman.Husband)
3939
.HasForeignKey<Man>();
40+
41+
base.OnModelCreating(builder);
4042
}
4143
}

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/ReadWriteDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,7 @@ protected override void OnModelCreating(ModelBuilder builder)
4949
left => left
5050
.HasOne(joinEntity => joinEntity.ToItem)
5151
.WithMany());
52+
53+
base.OnModelCreating(builder);
5254
}
5355
}

test/JsonApiDotNetCoreTests/IntegrationTests/RequiredRelationships/DefaultBehaviorDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3333
.HasOne(order => order.Shipment)
3434
.WithOne(shipment => shipment.Order)
3535
.HasForeignKey<Shipment>("OrderId");
36+
37+
base.OnModelCreating(builder);
3638
}
3739
}

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceDefinitions/Serialization/SerializationDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ protected override void OnModelCreating(ModelBuilder builder)
2222
builder.Entity<Scholarship>()
2323
.HasMany(scholarship => scholarship.Participants)
2424
.WithOne(student => student.Scholarship!);
25+
26+
base.OnModelCreating(builder);
2527
}
2628
}

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/ResourceInheritanceReadTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,7 +2429,7 @@ public async Task Can_sort_on_derived_attribute_from_resource_definition_using_e
24292429

24302430
await _testContext.RunOnDatabaseAsync(async dbContext =>
24312431
{
2432-
await dbContext.ClearTableAsync<Vehicle>();
2432+
await dbContext.ClearTableAsync<Wheel>();
24332433
dbContext.Wheels.AddRange(chromeWheel1, chromeWheel2, chromeWheel3, carbonWheel1, carbonWheel2);
24342434
await dbContext.SaveChangesAsync();
24352435
});
@@ -2487,7 +2487,7 @@ public async Task Can_sort_on_derived_attribute_from_resource_definition_using_l
24872487

24882488
await _testContext.RunOnDatabaseAsync(async dbContext =>
24892489
{
2490-
await dbContext.ClearTableAsync<Vehicle>();
2490+
await dbContext.ClearTableAsync<Wheel>();
24912491
dbContext.Wheels.AddRange(chromeWheel1, chromeWheel2, chromeWheel3, carbonWheel1, carbonWheel2);
24922492
await dbContext.SaveChangesAsync();
24932493
});

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/TablePerType/TablePerTypeDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3232
builder.Entity<GenericProperty>().ToTable("GenericProperties");
3333
builder.Entity<StringProperty>().ToTable("StringProperties");
3434
builder.Entity<NumberProperty>().ToTable("NumberProperties");
35+
36+
base.OnModelCreating(builder);
3537
}
3638
}

test/JsonApiDotNetCoreTests/IntegrationTests/SoftDeletion/SoftDeletionDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ protected override void OnModelCreating(ModelBuilder builder)
2424

2525
builder.Entity<Department>()
2626
.HasQueryFilter(department => department.SoftDeletedAt == null);
27+
28+
base.OnModelCreating(builder);
2729
}
2830
}

test/JsonApiDotNetCoreTests/IntegrationTests/ZeroKeys/ZeroKeyDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ protected override void OnModelCreating(ModelBuilder builder)
2727
builder.Entity<Player>()
2828
.HasOne(player => player.ActiveGame)
2929
.WithMany(game => game.ActivePlayers);
30+
31+
base.OnModelCreating(builder);
3032
}
3133
}

test/TestBuildingBlocks/DbContextExtensions.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.EntityFrameworkCore;
22
using Microsoft.EntityFrameworkCore.Metadata;
3-
using Npgsql;
43

54
namespace TestBuildingBlocks;
65

@@ -36,17 +35,7 @@ private static async Task ClearTablesAsync(this DbContext dbContext, params Type
3635
}
3736

3837
string tableName = entityType.GetTableName()!;
39-
40-
// PERF: We first try to clear the table, which is fast and usually succeeds, unless foreign key constraints are violated.
41-
// In that case, we recursively delete all related data, which is slow.
42-
try
43-
{
44-
await dbContext.Database.ExecuteSqlRawAsync($"delete from \"{tableName}\"");
45-
}
46-
catch (PostgresException)
47-
{
48-
await dbContext.Database.ExecuteSqlRawAsync($"truncate table \"{tableName}\" cascade");
49-
}
38+
await dbContext.Database.ExecuteSqlRawAsync($"delete from \"{tableName}\"");
5039
}
5140
}
5241
}

test/TestBuildingBlocks/TestableDbContext.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Diagnostics;
22
using JsonApiDotNetCore;
33
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Metadata;
45
using Microsoft.Extensions.Logging;
56

67
namespace TestBuildingBlocks;
@@ -17,4 +18,15 @@ protected override void OnConfiguring(DbContextOptionsBuilder builder)
1718
// Writes SQL statements to the Output Window when debugging.
1819
builder.LogTo(message => Debug.WriteLine(message), DbLoggerCategory.Database.Name.AsArray(), LogLevel.Information);
1920
}
21+
22+
protected override void OnModelCreating(ModelBuilder builder)
23+
{
24+
foreach (IMutableForeignKey foreignKey in builder.Model.GetEntityTypes().SelectMany(entityType => entityType.GetForeignKeys()))
25+
{
26+
if (foreignKey.DeleteBehavior == DeleteBehavior.ClientSetNull)
27+
{
28+
foreignKey.DeleteBehavior = DeleteBehavior.SetNull;
29+
}
30+
}
31+
}
2032
}

0 commit comments

Comments
 (0)