Skip to content

Commit 5934f94

Browse files
committed
Tests: disable model cache only if the tests are changing the model
1 parent 09b15ba commit 5934f94

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

tests/Thinktecture.EntityFrameworkCore.Relational.Tests/EntityFrameworkCore/Infrastructure/DefaultSchemaModelCustomizerTests/Customize.cs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,70 @@
11
using Microsoft.EntityFrameworkCore.Infrastructure;
22
using Microsoft.Extensions.DependencyInjection;
3+
using Thinktecture.EntityFrameworkCore.Testing;
34
using Thinktecture.TestDatabaseContext;
45

56
namespace Thinktecture.EntityFrameworkCore.Infrastructure.DefaultSchemaModelCustomizerTests;
67

78
public class Customize : IntegrationTestsBase
89
{
10+
private string? _schema;
11+
912
public Customize(ITestOutputHelper testOutputHelper)
1013
: base(testOutputHelper)
1114
{
12-
ConfigureOptionsBuilder = builder =>
13-
{
14-
builder.AddOrUpdateExtension<RelationalDbContextOptionsExtension>(extension =>
15-
{
16-
extension.Register(typeof(ModelCustomizer), typeof(ModelCustomizer), ServiceLifetime.Singleton);
17-
return extension;
18-
});
19-
builder.ReplaceService<IModelCustomizer, DefaultSchemaModelCustomizer<ModelCustomizer>>();
20-
};
15+
}
16+
17+
protected override void ConfigureTestDbContextProvider(SqliteTestDbContextProviderBuilder<DbContextWithSchema> builder)
18+
{
19+
base.ConfigureTestDbContextProvider(builder);
20+
21+
builder.ConfigureOptions(optionsBuilder =>
22+
{
23+
optionsBuilder.AddOrUpdateExtension<RelationalDbContextOptionsExtension>(extension =>
24+
{
25+
extension.Register(typeof(ModelCustomizer), typeof(ModelCustomizer), ServiceLifetime.Singleton);
26+
return extension;
27+
});
28+
optionsBuilder.ReplaceService<IModelCustomizer, DefaultSchemaModelCustomizer<ModelCustomizer>>();
29+
});
30+
31+
if (_schema is not null)
32+
{
33+
builder.UseContextFactory(options => new DbContextWithSchema(options, _schema))
34+
.DisableModelCache();
35+
}
2136
}
2237

2338
[Fact]
2439
public void Should_set_schema_if_entity_schema_is_null()
2540
{
26-
Schema = "BA3C32B0-D7EC-422C-AE3B-3206E7D67735";
41+
_schema = "BA3C32B0-D7EC-422C-AE3B-3206E7D67735";
2742

28-
ActDbContext.Model.FindEntityType(typeof(TestEntity))!.GetSchema().Should().Be(Schema);
43+
ActDbContext.Model.FindEntityType(typeof(TestEntity))!.GetSchema().Should().Be(_schema);
2944
}
3045

3146
[Fact]
3247
public void Should_set_schema_if_view_schema_is_null()
3348
{
34-
Schema = "E2FBA720-E24C-46C9-B326-46C3C91707F5";
49+
_schema = "E2FBA720-E24C-46C9-B326-46C3C91707F5";
3550

36-
ActDbContext.Model.FindEntityType(typeof(TestQuery))!.GetViewSchema().Should().Be(Schema);
51+
ActDbContext.Model.FindEntityType(typeof(TestQuery))!.GetViewSchema().Should().Be(_schema);
3752
}
3853

3954
[Fact]
4055
public void Should_set_schema_if_dbFunction_schema_is_null()
4156
{
42-
Schema = "E2FBA720-E24C-46C9-B326-46C3C91707F5";
57+
_schema = "E2FBA720-E24C-46C9-B326-46C3C91707F5";
4358

4459
ActDbContext.Model.GetDbFunctions()
4560
.Single(f => f.MethodInfo!.Name == nameof(DbContextWithSchema.TestDbFunction))
46-
.Schema.Should().Be(Schema);
61+
.Schema.Should().Be(_schema);
4762
}
4863

4964
[Fact]
5065
public void Should_not_change_schema_if_entity_schema_is_set_already()
5166
{
52-
Schema = "4BA05B95-7FEA-4F32-A1E0-ACA9CB486EB9";
67+
_schema = "4BA05B95-7FEA-4F32-A1E0-ACA9CB486EB9";
5368
ConfigureModel = builder => builder.Entity<TestEntity>().ToTable("Table", "Schema");
5469

5570
ActDbContext.Model.FindEntityType(typeof(TestEntity))!.GetSchema().Should().Be("Schema");
@@ -58,7 +73,7 @@ public void Should_not_change_schema_if_entity_schema_is_set_already()
5873
[Fact]
5974
public void Should_not_reset_schema_if_entity_schema_is_set_already()
6075
{
61-
Schema = null;
76+
_schema = null;
6277
ConfigureModel = builder => builder.Entity<TestEntity>().ToTable("Table", "Schema");
6378

6479
ActDbContext.Model.FindEntityType(typeof(TestEntity))!.GetSchema().Should().Be("Schema");

tests/Thinktecture.EntityFrameworkCore.Relational.Tests/IntegrationTestsBase.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public class IntegrationTestsBase : SqliteDbContextIntegrationTests<DbContextWit
1111

1212
protected Action<DbContextOptionsBuilder<DbContextWithSchema>>? ConfigureOptionsBuilder { get; set; }
1313
protected Action<ModelBuilder>? ConfigureModel { get; set; }
14-
protected string? Schema { get; set; }
1514
protected ILoggerFactory LoggerFactory { get; }
1615

1716
protected IntegrationTestsBase(
@@ -29,9 +28,9 @@ protected override void ConfigureTestDbContextProvider(SqliteTestDbContextProvid
2928
.UseMigrationLogLevel(LogLevel.Warning)
3029
.ConfigureOptions(optionsBuilder => ConfigureOptionsBuilder?.Invoke(optionsBuilder))
3130
.InitializeContext(ctx => ctx.ConfigureModel = ConfigureModel)
32-
.UseContextFactory(options => new DbContextWithSchema(options, Schema));
31+
.UseContextFactory(options => new DbContextWithSchema(options, null));
3332

34-
if (ConfigureModel is not null || Schema is not null)
33+
if (ConfigureModel is not null)
3534
builder.DisableModelCache();
3635
}
3736

tests/Thinktecture.EntityFrameworkCore.SqlServer.Tests/IntegrationTestsBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ protected override void ConfigureTestDbContextProvider(SqlServerTestDbContextPro
6464
optionsBuilder.AddTenantDatabaseSupport<TestTenantDatabaseProviderFactory>();
6565
})
6666
.UseSharedTableSchema(schema)
67-
.InitializeContext(ctx => ctx.ConfigureModel = ConfigureModel)
68-
.DisableModelCache();
67+
.InitializeContext(ctx => ctx.ConfigureModel = ConfigureModel);
6968
}
7069
}

tests/Thinktecture.EntityFrameworkCore.Sqlite.Tests/IntegrationTestsBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ protected override void ConfigureTestDbContextProvider(SqliteTestDbContextProvid
2929
.ConfigureOptions(optionsBuilder => ConfigureOptionsBuilder?.Invoke(optionsBuilder))
3030
.ConfigureSqliteOptions(optionsBuilder => optionsBuilder.AddBulkOperationSupport()
3131
.AddRowNumberSupport())
32-
.InitializeContext(ctx => ctx.ConfigureModel = ConfigureModel)
33-
.DisableModelCache();
32+
.InitializeContext(ctx => ctx.ConfigureModel = ConfigureModel);
33+
34+
if (ConfigureModel is not null)
35+
builder.DisableModelCache();
3436
}
3537
}

0 commit comments

Comments
 (0)