Skip to content

Commit 1664331

Browse files
authored
Merge pull request #1249 from json-api-dotnet/package-updates-ef7
Package updates; run tests against EF Core 7
2 parents cd6d5bd + ab8759c commit 1664331

File tree

14 files changed

+120
-41
lines changed

14 files changed

+120
-41
lines changed

.config/dotnet-tools.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]
1010
},
1111
"regitlint": {
12-
"version": "6.2.1",
12+
"version": "6.3.10",
1313
"commands": [
1414
"regitlint"
1515
]
@@ -21,7 +21,7 @@
2121
]
2222
},
2323
"dotnet-reportgenerator-globaltool": {
24-
"version": "5.1.11",
24+
"version": "5.1.15",
2525
"commands": [
2626
"reportgenerator"
2727
]

Directory.Build.props

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<PropertyGroup>
33
<TargetFrameworkName>net6.0</TargetFrameworkName>
44
<AspNetVersion>6.0.*</AspNetVersion>
5-
<EFCoreVersion>6.0.*</EFCoreVersion>
6-
<EFCorePostgresVersion>6.0.*</EFCorePostgresVersion>
7-
<MicrosoftCodeAnalysisVersion>4.3.*</MicrosoftCodeAnalysisVersion>
5+
<EFCoreVersion>7.0.*</EFCoreVersion>
6+
<EFCorePostgresVersion>7.0.*</EFCorePostgresVersion>
7+
<MicrosoftCodeAnalysisVersion>4.4.*</MicrosoftCodeAnalysisVersion>
88
<HumanizerVersion>2.14.1</HumanizerVersion>
99
<JsonApiDotNetCoreVersionPrefix>5.1.2</JsonApiDotNetCoreVersionPrefix>
1010
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)CodingGuidelines.ruleset</CodeAnalysisRuleSet>
@@ -33,8 +33,8 @@
3333

3434
<!-- Test Project Dependencies -->
3535
<PropertyGroup>
36-
<CoverletVersion>3.2.0</CoverletVersion>
37-
<MoqVersion>4.18.2</MoqVersion>
38-
<TestSdkVersion>17.4.0</TestSdkVersion>
36+
<CoverletVersion>3.2.*</CoverletVersion>
37+
<MoqVersion>4.18.*</MoqVersion>
38+
<TestSdkVersion>17.4.*</TestSdkVersion>
3939
</PropertyGroup>
4040
</Project>

benchmarks/Benchmarks.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
13+
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
1414
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion)" PrivateAssets="all">
1515
<!-- This reference solely exists to prevent build warnings for conflicting versions of Microsoft.CodeAnalysis. -->
1616
</PackageReference>

src/Examples/JsonApiDotNetCoreExample/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void ConfigureServices(WebApplicationBuilder builder)
4747

4848
builder.Services.AddDbContext<AppDbContext>(options =>
4949
{
50-
string connectionString = GetConnectionString(builder.Configuration);
50+
string? connectionString = GetConnectionString(builder.Configuration);
5151

5252
options.UseNpgsql(connectionString);
5353
#if DEBUG
@@ -73,10 +73,10 @@ static void ConfigureServices(WebApplicationBuilder builder)
7373
}
7474
}
7575

76-
static string GetConnectionString(IConfiguration configuration)
76+
static string? GetConnectionString(IConfiguration configuration)
7777
{
7878
string postgresPassword = Environment.GetEnvironmentVariable("PGPASSWORD") ?? "postgres";
79-
return configuration["Data:DefaultConnection"].Replace("###", postgresPassword);
79+
return configuration["Data:DefaultConnection"]?.Replace("###", postgresPassword);
8080
}
8181

8282
static void ConfigurePipeline(WebApplication webApplication)

src/Examples/NoEntityFrameworkExample/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// Add services to the container.
99

10-
string connectionString = GetConnectionString(builder.Configuration);
10+
string? connectionString = GetConnectionString(builder.Configuration);
1111
builder.Services.AddNpgsql<AppDbContext>(connectionString);
1212

1313
builder.Services.AddJsonApi(options => options.Namespace = "api/v1", resources: resourceGraphBuilder => resourceGraphBuilder.Add<WorkItem, int>());
@@ -26,10 +26,10 @@
2626

2727
app.Run();
2828

29-
static string GetConnectionString(IConfiguration configuration)
29+
static string? GetConnectionString(IConfiguration configuration)
3030
{
3131
string postgresPassword = Environment.GetEnvironmentVariable("PGPASSWORD") ?? "postgres";
32-
return configuration["Data:DefaultConnection"].Replace("###", postgresPassword);
32+
return configuration["Data:DefaultConnection"]?.Replace("###", postgresPassword);
3333
}
3434

3535
static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/ResourceInheritanceWriteTests.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -2298,7 +2298,7 @@ public async Task Can_add_concrete_base_resources_stored_as_derived_at_ToMany_re
22982298

22992299
await _testContext.RunOnDatabaseAsync(async dbContext =>
23002300
{
2301-
dbContext.Set<VehicleManufacturer>().Add(existingManufacturer);
2301+
dbContext.VehicleManufacturers.Add(existingManufacturer);
23022302
dbContext.Vehicles.Add(existingTandem);
23032303
await dbContext.SaveChangesAsync();
23042304
});
@@ -2327,9 +2327,17 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
23272327

23282328
await _testContext.RunOnDatabaseAsync(async dbContext =>
23292329
{
2330-
VehicleManufacturer manufacturerInDatabase = await dbContext.Set<VehicleManufacturer>().Include(manufacturer => manufacturer.Vehicles)
2330+
// @formatter:wrap_chained_method_calls chop_always
2331+
// @formatter:keep_existing_linebreaks true
2332+
2333+
VehicleManufacturer manufacturerInDatabase = await dbContext.VehicleManufacturers
2334+
.Include(manufacturer => manufacturer.Vehicles
2335+
.OrderByDescending(vehicle => vehicle.Id))
23312336
.FirstWithIdAsync(existingManufacturer.Id);
23322337

2338+
// @formatter:keep_existing_linebreaks restore
2339+
// @formatter:wrap_chained_method_calls restore
2340+
23332341
manufacturerInDatabase.Vehicles.ShouldHaveCount(2);
23342342
manufacturerInDatabase.Vehicles.ElementAt(0).Should().BeOfType<Car>();
23352343
manufacturerInDatabase.Vehicles.ElementAt(0).Id.Should().Be(existingManufacturer.Vehicles.ElementAt(0).Id);
@@ -2578,7 +2586,7 @@ public async Task Can_remove_concrete_base_resources_stored_as_derived_at_ToMany
25782586

25792587
await _testContext.RunOnDatabaseAsync(async dbContext =>
25802588
{
2581-
dbContext.Set<VehicleManufacturer>().Add(existingManufacturer);
2589+
dbContext.VehicleManufacturers.Add(existingManufacturer);
25822590
await dbContext.SaveChangesAsync();
25832591
});
25842592

@@ -2606,7 +2614,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
26062614

26072615
await _testContext.RunOnDatabaseAsync(async dbContext =>
26082616
{
2609-
VehicleManufacturer manufacturerInDatabase = await dbContext.Set<VehicleManufacturer>().Include(manufacturer => manufacturer.Vehicles)
2617+
VehicleManufacturer manufacturerInDatabase = await dbContext.VehicleManufacturers.Include(manufacturer => manufacturer.Vehicles)
26102618
.FirstWithIdAsync(existingManufacturer.Id);
26112619

26122620
manufacturerInDatabase.Vehicles.ShouldHaveCount(1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using JetBrains.Annotations;
2+
using JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.Models;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
// @formatter:wrap_chained_method_calls chop_always
6+
7+
namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.TablePerConcreteType;
8+
9+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
10+
public sealed class TablePerConcreteTypeDbContext : ResourceInheritanceDbContext
11+
{
12+
public TablePerConcreteTypeDbContext(DbContextOptions<TablePerConcreteTypeDbContext> options)
13+
: base(options)
14+
{
15+
}
16+
17+
protected override void OnModelCreating(ModelBuilder builder)
18+
{
19+
builder.Entity<Vehicle>()
20+
.UseTpcMappingStrategy();
21+
22+
builder.Entity<Wheel>()
23+
.UseTpcMappingStrategy();
24+
25+
builder.Entity<Engine>()
26+
.UseTpcMappingStrategy();
27+
28+
builder.Entity<GenericProperty>()
29+
.UseTpcMappingStrategy();
30+
31+
base.OnModelCreating(builder);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using JetBrains.Annotations;
2+
using TestBuildingBlocks;
3+
4+
namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.TablePerConcreteType;
5+
6+
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
7+
public sealed class TablePerConcreteTypeReadTests : ResourceInheritanceReadTests<TablePerConcreteTypeDbContext>
8+
{
9+
public TablePerConcreteTypeReadTests(IntegrationTestContext<TestableStartup<TablePerConcreteTypeDbContext>, TablePerConcreteTypeDbContext> testContext)
10+
: base(testContext)
11+
{
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using JetBrains.Annotations;
2+
using TestBuildingBlocks;
3+
4+
namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.TablePerConcreteType;
5+
6+
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
7+
public sealed class TablePerConcreteTypeWriteTests : ResourceInheritanceWriteTests<TablePerConcreteTypeDbContext>
8+
{
9+
public TablePerConcreteTypeWriteTests(IntegrationTestContext<TestableStartup<TablePerConcreteTypeDbContext>, TablePerConcreteTypeDbContext> testContext)
10+
: base(testContext)
11+
{
12+
}
13+
}

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

+13-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.Models;
33
using Microsoft.EntityFrameworkCore;
44

5+
// @formatter:wrap_chained_method_calls chop_always
6+
57
namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.TablePerType;
68

79
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
@@ -14,24 +16,17 @@ public TablePerTypeDbContext(DbContextOptions<TablePerTypeDbContext> options)
1416

1517
protected override void OnModelCreating(ModelBuilder builder)
1618
{
17-
builder.Entity<Vehicle>().ToTable("Vehicles");
18-
builder.Entity<Bike>().ToTable("Bikes");
19-
builder.Entity<Tandem>().ToTable("Tandems");
20-
builder.Entity<MotorVehicle>().ToTable("MotorVehicles");
21-
builder.Entity<Car>().ToTable("Cars");
22-
builder.Entity<Truck>().ToTable("Trucks");
23-
24-
builder.Entity<Wheel>().ToTable("Wheels");
25-
builder.Entity<CarbonWheel>().ToTable("CarbonWheels");
26-
builder.Entity<ChromeWheel>().ToTable("ChromeWheels");
27-
28-
builder.Entity<Engine>().ToTable("Engines");
29-
builder.Entity<GasolineEngine>().ToTable("GasolineEngines");
30-
builder.Entity<DieselEngine>().ToTable("DieselEngines");
31-
32-
builder.Entity<GenericProperty>().ToTable("GenericProperties");
33-
builder.Entity<StringProperty>().ToTable("StringProperties");
34-
builder.Entity<NumberProperty>().ToTable("NumberProperties");
19+
builder.Entity<Vehicle>()
20+
.UseTptMappingStrategy();
21+
22+
builder.Entity<Wheel>()
23+
.UseTptMappingStrategy();
24+
25+
builder.Entity<Engine>()
26+
.UseTptMappingStrategy();
27+
28+
builder.Entity<GenericProperty>()
29+
.UseTptMappingStrategy();
3530

3631
base.OnModelCreating(builder);
3732
}

test/SourceGeneratorTests/CompilationBuilder.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ namespace SourceGeneratorTests;
99

1010
internal sealed class CompilationBuilder
1111
{
12-
private static readonly CSharpCompilationOptions DefaultOptions = new(OutputKind.DynamicallyLinkedLibrary);
12+
private static readonly CSharpCompilationOptions DefaultOptions =
13+
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithSpecificDiagnosticOptions(new Dictionary<string, ReportDiagnostic>
14+
{
15+
// Suppress warning for version conflict on Microsoft.Extensions.Logging.Abstractions:
16+
// JsonApiDotNetCore indirectly depends on v6 (via Entity Framework Core 6), whereas Entity Framework Core 7 depends on v7.
17+
["CS1701"] = ReportDiagnostic.Suppress
18+
});
1319

1420
private readonly HashSet<SyntaxTree> _syntaxTrees = new();
1521
private readonly HashSet<MetadataReference> _references = new();

test/TestBuildingBlocks/DbContextExtensions.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@ private static async Task ClearTablesAsync(this DbContext dbContext, params Type
3434
throw new InvalidOperationException($"Table for '{model.Name}' not found.");
3535
}
3636

37-
string tableName = entityType.GetTableName()!;
38-
await dbContext.Database.ExecuteSqlRawAsync($"delete from \"{tableName}\"");
37+
string? tableName = entityType.GetTableName();
38+
39+
if (tableName == null)
40+
{
41+
// There is no table for the specified abstract base type when using TablePerConcreteType inheritance.
42+
IEnumerable<IEntityType> derivedTypes = entityType.GetConcreteDerivedTypesInclusive();
43+
await ClearTablesAsync(dbContext, derivedTypes.Select(derivedType => derivedType.ClrType).ToArray());
44+
}
45+
else
46+
{
47+
await dbContext.Database.ExecuteSqlRawAsync($"delete from \"{tableName}\"");
48+
}
3949
}
4050
}
4151
}

test/TestBuildingBlocks/FakeLoggerFactory.cs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
5959
}
6060

6161
public IDisposable BeginScope<TState>(TState state)
62+
where TState : notnull
6263
{
6364
return NullScope.Instance;
6465
}

test/TestBuildingBlocks/TestBuildingBlocks.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<ItemGroup>
1111
<PackageReference Include="Bogus" Version="34.0.2" />
1212
<PackageReference Include="coverlet.collector" Version="$(CoverletVersion)" PrivateAssets="All" />
13-
<PackageReference Include="FluentAssertions" Version="6.8.0" />
13+
<PackageReference Include="FluentAssertions" Version="6.9.0" />
1414
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(AspNetVersion)" />
1515
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="$(EFCoreVersion)" />
1616
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />

0 commit comments

Comments
 (0)