Skip to content

Package updates; run tests against EF Core 7 #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"regitlint": {
"version": "6.2.1",
"version": "6.3.10",
"commands": [
"regitlint"
]
Expand All @@ -21,7 +21,7 @@
]
},
"dotnet-reportgenerator-globaltool": {
"version": "5.1.11",
"version": "5.1.15",
"commands": [
"reportgenerator"
]
Expand Down
12 changes: 6 additions & 6 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<PropertyGroup>
<TargetFrameworkName>net6.0</TargetFrameworkName>
<AspNetVersion>6.0.*</AspNetVersion>
<EFCoreVersion>6.0.*</EFCoreVersion>
<EFCorePostgresVersion>6.0.*</EFCorePostgresVersion>
<MicrosoftCodeAnalysisVersion>4.3.*</MicrosoftCodeAnalysisVersion>
<EFCoreVersion>7.0.*</EFCoreVersion>
<EFCorePostgresVersion>7.0.*</EFCorePostgresVersion>
<MicrosoftCodeAnalysisVersion>4.4.*</MicrosoftCodeAnalysisVersion>
<HumanizerVersion>2.14.1</HumanizerVersion>
<JsonApiDotNetCoreVersionPrefix>5.1.2</JsonApiDotNetCoreVersionPrefix>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)CodingGuidelines.ruleset</CodeAnalysisRuleSet>
Expand Down Expand Up @@ -33,8 +33,8 @@

<!-- Test Project Dependencies -->
<PropertyGroup>
<CoverletVersion>3.2.0</CoverletVersion>
<MoqVersion>4.18.2</MoqVersion>
<TestSdkVersion>17.4.0</TestSdkVersion>
<CoverletVersion>3.2.*</CoverletVersion>
<MoqVersion>4.18.*</MoqVersion>
<TestSdkVersion>17.4.*</TestSdkVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion)" PrivateAssets="all">
<!-- This reference solely exists to prevent build warnings for conflicting versions of Microsoft.CodeAnalysis. -->
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions src/Examples/JsonApiDotNetCoreExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void ConfigureServices(WebApplicationBuilder builder)

builder.Services.AddDbContext<AppDbContext>(options =>
{
string connectionString = GetConnectionString(builder.Configuration);
string? connectionString = GetConnectionString(builder.Configuration);

options.UseNpgsql(connectionString);
#if DEBUG
Expand All @@ -73,10 +73,10 @@ static void ConfigureServices(WebApplicationBuilder builder)
}
}

static string GetConnectionString(IConfiguration configuration)
static string? GetConnectionString(IConfiguration configuration)
{
string postgresPassword = Environment.GetEnvironmentVariable("PGPASSWORD") ?? "postgres";
return configuration["Data:DefaultConnection"].Replace("###", postgresPassword);
return configuration["Data:DefaultConnection"]?.Replace("###", postgresPassword);
}

static void ConfigurePipeline(WebApplication webApplication)
Expand Down
6 changes: 3 additions & 3 deletions src/Examples/NoEntityFrameworkExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Add services to the container.

string connectionString = GetConnectionString(builder.Configuration);
string? connectionString = GetConnectionString(builder.Configuration);
builder.Services.AddNpgsql<AppDbContext>(connectionString);

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

app.Run();

static string GetConnectionString(IConfiguration configuration)
static string? GetConnectionString(IConfiguration configuration)
{
string postgresPassword = Environment.GetEnvironmentVariable("PGPASSWORD") ?? "postgres";
return configuration["Data:DefaultConnection"].Replace("###", postgresPassword);
return configuration["Data:DefaultConnection"]?.Replace("###", postgresPassword);
}

static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,7 @@ public async Task Can_add_concrete_base_resources_stored_as_derived_at_ToMany_re

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.Set<VehicleManufacturer>().Add(existingManufacturer);
dbContext.VehicleManufacturers.Add(existingManufacturer);
dbContext.Vehicles.Add(existingTandem);
await dbContext.SaveChangesAsync();
});
Expand Down Expand Up @@ -2327,9 +2327,17 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
VehicleManufacturer manufacturerInDatabase = await dbContext.Set<VehicleManufacturer>().Include(manufacturer => manufacturer.Vehicles)
// @formatter:wrap_chained_method_calls chop_always
// @formatter:keep_existing_linebreaks true

VehicleManufacturer manufacturerInDatabase = await dbContext.VehicleManufacturers
.Include(manufacturer => manufacturer.Vehicles
.OrderByDescending(vehicle => vehicle.Id))
.FirstWithIdAsync(existingManufacturer.Id);

// @formatter:keep_existing_linebreaks restore
// @formatter:wrap_chained_method_calls restore

manufacturerInDatabase.Vehicles.ShouldHaveCount(2);
manufacturerInDatabase.Vehicles.ElementAt(0).Should().BeOfType<Car>();
manufacturerInDatabase.Vehicles.ElementAt(0).Id.Should().Be(existingManufacturer.Vehicles.ElementAt(0).Id);
Expand Down Expand Up @@ -2578,7 +2586,7 @@ public async Task Can_remove_concrete_base_resources_stored_as_derived_at_ToMany

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.Set<VehicleManufacturer>().Add(existingManufacturer);
dbContext.VehicleManufacturers.Add(existingManufacturer);
await dbContext.SaveChangesAsync();
});

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

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
VehicleManufacturer manufacturerInDatabase = await dbContext.Set<VehicleManufacturer>().Include(manufacturer => manufacturer.Vehicles)
VehicleManufacturer manufacturerInDatabase = await dbContext.VehicleManufacturers.Include(manufacturer => manufacturer.Vehicles)
.FirstWithIdAsync(existingManufacturer.Id);

manufacturerInDatabase.Vehicles.ShouldHaveCount(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using JetBrains.Annotations;
using JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.Models;
using Microsoft.EntityFrameworkCore;

// @formatter:wrap_chained_method_calls chop_always

namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.TablePerConcreteType;

[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class TablePerConcreteTypeDbContext : ResourceInheritanceDbContext
{
public TablePerConcreteTypeDbContext(DbContextOptions<TablePerConcreteTypeDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Vehicle>()
.UseTpcMappingStrategy();

builder.Entity<Wheel>()
.UseTpcMappingStrategy();

builder.Entity<Engine>()
.UseTpcMappingStrategy();

builder.Entity<GenericProperty>()
.UseTpcMappingStrategy();

base.OnModelCreating(builder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using JetBrains.Annotations;
using TestBuildingBlocks;

namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.TablePerConcreteType;

[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public sealed class TablePerConcreteTypeReadTests : ResourceInheritanceReadTests<TablePerConcreteTypeDbContext>
{
public TablePerConcreteTypeReadTests(IntegrationTestContext<TestableStartup<TablePerConcreteTypeDbContext>, TablePerConcreteTypeDbContext> testContext)
: base(testContext)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using JetBrains.Annotations;
using TestBuildingBlocks;

namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.TablePerConcreteType;

[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public sealed class TablePerConcreteTypeWriteTests : ResourceInheritanceWriteTests<TablePerConcreteTypeDbContext>
{
public TablePerConcreteTypeWriteTests(IntegrationTestContext<TestableStartup<TablePerConcreteTypeDbContext>, TablePerConcreteTypeDbContext> testContext)
: base(testContext)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.Models;
using Microsoft.EntityFrameworkCore;

// @formatter:wrap_chained_method_calls chop_always

namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceInheritance.TablePerType;

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

protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Vehicle>().ToTable("Vehicles");
builder.Entity<Bike>().ToTable("Bikes");
builder.Entity<Tandem>().ToTable("Tandems");
builder.Entity<MotorVehicle>().ToTable("MotorVehicles");
builder.Entity<Car>().ToTable("Cars");
builder.Entity<Truck>().ToTable("Trucks");

builder.Entity<Wheel>().ToTable("Wheels");
builder.Entity<CarbonWheel>().ToTable("CarbonWheels");
builder.Entity<ChromeWheel>().ToTable("ChromeWheels");

builder.Entity<Engine>().ToTable("Engines");
builder.Entity<GasolineEngine>().ToTable("GasolineEngines");
builder.Entity<DieselEngine>().ToTable("DieselEngines");

builder.Entity<GenericProperty>().ToTable("GenericProperties");
builder.Entity<StringProperty>().ToTable("StringProperties");
builder.Entity<NumberProperty>().ToTable("NumberProperties");
builder.Entity<Vehicle>()
.UseTptMappingStrategy();

builder.Entity<Wheel>()
.UseTptMappingStrategy();

builder.Entity<Engine>()
.UseTptMappingStrategy();

builder.Entity<GenericProperty>()
.UseTptMappingStrategy();

base.OnModelCreating(builder);
}
Expand Down
8 changes: 7 additions & 1 deletion test/SourceGeneratorTests/CompilationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ namespace SourceGeneratorTests;

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

private readonly HashSet<SyntaxTree> _syntaxTrees = new();
private readonly HashSet<MetadataReference> _references = new();
Expand Down
14 changes: 12 additions & 2 deletions test/TestBuildingBlocks/DbContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ private static async Task ClearTablesAsync(this DbContext dbContext, params Type
throw new InvalidOperationException($"Table for '{model.Name}' not found.");
}

string tableName = entityType.GetTableName()!;
await dbContext.Database.ExecuteSqlRawAsync($"delete from \"{tableName}\"");
string? tableName = entityType.GetTableName();

if (tableName == null)
{
// There is no table for the specified abstract base type when using TablePerConcreteType inheritance.
IEnumerable<IEntityType> derivedTypes = entityType.GetConcreteDerivedTypesInclusive();
await ClearTablesAsync(dbContext, derivedTypes.Select(derivedType => derivedType.ClrType).ToArray());
}
else
{
await dbContext.Database.ExecuteSqlRawAsync($"delete from \"{tableName}\"");
}
}
}
}
1 change: 1 addition & 0 deletions test/TestBuildingBlocks/FakeLoggerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
}

public IDisposable BeginScope<TState>(TState state)
where TState : notnull
{
return NullScope.Instance;
}
Expand Down
2 changes: 1 addition & 1 deletion test/TestBuildingBlocks/TestBuildingBlocks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="coverlet.collector" Version="$(CoverletVersion)" PrivateAssets="All" />
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="FluentAssertions" Version="6.9.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(AspNetVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="$(EFCoreVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
Expand Down