From a73a976ed31df257c334b382fe96ca27337c0714 Mon Sep 17 00:00:00 2001 From: AndriySvyryd Date: Fri, 7 Jun 2019 17:39:59 -0700 Subject: [PATCH] Finalize the model in tests Fixes #16000 --- .../Metadata/Internal/EntityTypeExtensions.cs | 2 +- .../Migrations/ModelSnapshotSqlServerTest.cs | 3 +-- .../InMemoryDatabaseTest.cs | 2 +- .../InMemoryValueGeneratorSelectorTest.cs | 14 ++++++-------- .../RelationalEventIdTest.cs | 2 +- .../Update/CommandBatchPreparerTest.cs | 19 +++++++++---------- .../Update/ModificationCommandTest.cs | 2 +- .../ReaderModificationCommandBatchTest.cs | 2 +- .../SqlServerEventIdTest.cs | 1 + test/EFCore.Sqlite.Tests/SqliteEventIdTest.cs | 1 + 10 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/EFCore/Metadata/Internal/EntityTypeExtensions.cs b/src/EFCore/Metadata/Internal/EntityTypeExtensions.cs index 86f8dd2d1b9..deb6506308a 100644 --- a/src/EFCore/Metadata/Internal/EntityTypeExtensions.cs +++ b/src/EFCore/Metadata/Internal/EntityTypeExtensions.cs @@ -301,7 +301,7 @@ public static PropertyCounts GetCounts([NotNull] this IEntityType entityType) /// public static PropertyCounts CalculateCounts([NotNull] this EntityType entityType) { - //Debug.Assert(entityType.Model.ConventionDispatcher == null, "Should not be called on a mutable model"); + Debug.Assert(entityType.Model.ConventionDispatcher == null, "Should not be called on a mutable model"); var index = 0; var navigationIndex = 0; diff --git a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs index 98a108cffdc..5eb826a767e 100644 --- a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs +++ b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs @@ -3407,8 +3407,7 @@ protected void Test(Action buildModel, string expectedCode, Action Activator.CreateInstance(factoryType), new object[] { builder }); - var value = builder.Model; - Assert.NotNull(value); + var value = builder.FinalizeModel(); var reporter = new TestOperationReporter(); assert(new SnapshotModelProcessor(reporter).Process(value)); diff --git a/test/EFCore.InMemory.Tests/InMemoryDatabaseTest.cs b/test/EFCore.InMemory.Tests/InMemoryDatabaseTest.cs index 7a2e4cb4437..921bf1ea1b0 100644 --- a/test/EFCore.InMemory.Tests/InMemoryDatabaseTest.cs +++ b/test/EFCore.InMemory.Tests/InMemoryDatabaseTest.cs @@ -186,7 +186,7 @@ private static IModel CreateModel() b.Property(c => c.Name); }); - return modelBuilder.Model; + return modelBuilder.Model.FinalizeModel(); } private class Customer diff --git a/test/EFCore.InMemory.Tests/InMemoryValueGeneratorSelectorTest.cs b/test/EFCore.InMemory.Tests/InMemoryValueGeneratorSelectorTest.cs index 6e2e29583b4..6f699783d21 100644 --- a/test/EFCore.InMemory.Tests/InMemoryValueGeneratorSelectorTest.cs +++ b/test/EFCore.InMemory.Tests/InMemoryValueGeneratorSelectorTest.cs @@ -12,6 +12,8 @@ using Microsoft.Extensions.DependencyInjection; using Xunit; +// ReSharper disable ClassNeverInstantiated.Local +// ReSharper disable UnusedMember.Local namespace Microsoft.EntityFrameworkCore { public class InMemoryValueGeneratorSelectorTest @@ -73,7 +75,6 @@ public void Can_create_factories_for_all_integer_types() private static object CreateAndUseFactory(IProperty property) { var model = BuildModel(); - var entityType = model.FindEntityType(typeof(AnEntity)); var selector = InMemoryTestHelpers.Instance.CreateContextServices(model).GetRequiredService(); @@ -89,25 +90,23 @@ public void Throws_for_unsupported_combinations() var selector = InMemoryTestHelpers.Instance.CreateContextServices(model).GetRequiredService(); Assert.Equal( - CoreStrings.NoValueGenerator("Random", "AnEntity", typeof(Random).Name), - Assert.Throws(() => selector.Select(entityType.FindProperty("Random"), entityType)).Message); + CoreStrings.NoValueGenerator("Float", "AnEntity", "float"), + Assert.Throws(() => selector.Select(entityType.FindProperty("Float"), entityType)).Message); } - private static IMutableModel BuildModel(bool generateValues = true) + private static IModel BuildModel(bool generateValues = true) { var builder = InMemoryTestHelpers.Instance.CreateConventionBuilder(); - builder.Ignore(); builder.Entity().Property(e => e.Custom).HasValueGenerator(); var model = builder.Model; var entityType = model.FindEntityType(typeof(AnEntity)); - entityType.AddProperty("Random", typeof(Random)); foreach (var property in entityType.GetProperties()) { property.ValueGenerated = generateValues ? ValueGenerated.OnAdd : ValueGenerated.Never; } - return model; + return model.FinalizeModel(); } private class AnEntity @@ -133,7 +132,6 @@ private class AnEntity public Guid Guid { get; set; } public byte[] Binary { get; set; } public float Float { get; set; } - public Random Random { get; set; } } private class CustomValueGenerator : ValueGenerator diff --git a/test/EFCore.Relational.Tests/RelationalEventIdTest.cs b/test/EFCore.Relational.Tests/RelationalEventIdTest.cs index b77a73e9ffa..df3ba01684e 100644 --- a/test/EFCore.Relational.Tests/RelationalEventIdTest.cs +++ b/test/EFCore.Relational.Tests/RelationalEventIdTest.cs @@ -38,7 +38,7 @@ public void Every_eventId_has_a_logger_method_and_logs_when_level_enabled() var entityType = new EntityType(typeof(object), model, ConfigurationSource.Convention); var property = new Property( "A", typeof(int), null, null, entityType, ConfigurationSource.Convention, ConfigurationSource.Convention); - var contextServices = RelationalTestHelpers.Instance.CreateContextServices(model); + var contextServices = RelationalTestHelpers.Instance.CreateContextServices(model.FinalizeModel()); var fakeFactories = new Dictionary> { diff --git a/test/EFCore.Relational.Tests/Update/CommandBatchPreparerTest.cs b/test/EFCore.Relational.Tests/Update/CommandBatchPreparerTest.cs index 9b770fa1af8..4be6997234c 100644 --- a/test/EFCore.Relational.Tests/Update/CommandBatchPreparerTest.cs +++ b/test/EFCore.Relational.Tests/Update/CommandBatchPreparerTest.cs @@ -1128,9 +1128,8 @@ public ICommandBatchPreparer CreateCommandBatchPreparer( IUpdateAdapter updateAdapter = null, bool sensitiveLogging = false) { - modificationCommandBatchFactory = - modificationCommandBatchFactory - ?? RelationalTestHelpers.Instance.CreateContextServices().GetRequiredService(); + modificationCommandBatchFactory ??= + RelationalTestHelpers.Instance.CreateContextServices().GetRequiredService(); var loggingOptions = new LoggingOptions(); if (sensitiveLogging) @@ -1168,7 +1167,7 @@ private static IModel CreateSimpleFKModel() .HasForeignKey(c => c.Id); }); - return modelBuilder.Model; + return modelBuilder.Model.FinalizeModel(); } private static IModel CreateCyclicFKModel() @@ -1196,7 +1195,7 @@ private static IModel CreateCyclicFKModel() .WithOne() .HasForeignKey(c => c.RelatedId); - return modelBuilder.Model; + return modelBuilder.Model.FinalizeModel(); } private static IModel CreateCyclicFkWithTailModel() @@ -1232,7 +1231,7 @@ private static IModel CreateCyclicFkWithTailModel() .HasForeignKey(e => e.AnotherId); }); - return modelBuilder.Model; + return modelBuilder.Model.FinalizeModel(); } private static IModel CreateTwoLevelFKModel() @@ -1257,7 +1256,7 @@ private static IModel CreateTwoLevelFKModel() .HasForeignKey(c => c.AnotherId); }); - return modelBuilder.Model; + return modelBuilder.Model.FinalizeModel(); } private static IModel CreateSharedTableModel() @@ -1268,13 +1267,13 @@ private static IModel CreateSharedTableModel() b => { b.Ignore(c => c.UniqueValue); - b.Property(c => c.RelatedId).IsConcurrencyToken(); + b.Property(c => c.RelatedId).IsConcurrencyToken().HasColumnName("RelatedId"); }); modelBuilder.Entity( b => { - b.Property(c => c.RelatedId).IsConcurrencyToken(); + b.Property(c => c.RelatedId).IsConcurrencyToken().HasColumnName("RelatedId"); b.HasOne() .WithOne() .HasForeignKey(c => c.Id); @@ -1291,7 +1290,7 @@ private static IModel CreateSharedTableModel() modelBuilder.Entity().ToTable(nameof(FakeEntity)); - return modelBuilder.Model; + return modelBuilder.Model.FinalizeModel(); } private class FakeEntity diff --git a/test/EFCore.Relational.Tests/Update/ModificationCommandTest.cs b/test/EFCore.Relational.Tests/Update/ModificationCommandTest.cs index 90009db49af..9f8b41be484 100644 --- a/test/EFCore.Relational.Tests/Update/ModificationCommandTest.cs +++ b/test/EFCore.Relational.Tests/Update/ModificationCommandTest.cs @@ -460,7 +460,7 @@ private static IModel BuildModel(bool generateKeyValues, bool computeNonKeyValue nonKey2.SetColumnName("Col3"); nonKey2.ValueGenerated = computeNonKeyValue ? ValueGenerated.OnUpdate : ValueGenerated.Never; - return model; + return model.FinalizeModel(); } private static InternalEntityEntry CreateEntry( diff --git a/test/EFCore.Relational.Tests/Update/ReaderModificationCommandBatchTest.cs b/test/EFCore.Relational.Tests/Update/ReaderModificationCommandBatchTest.cs index 5180969f280..7e278491073 100644 --- a/test/EFCore.Relational.Tests/Update/ReaderModificationCommandBatchTest.cs +++ b/test/EFCore.Relational.Tests/Update/ReaderModificationCommandBatchTest.cs @@ -520,7 +520,7 @@ private static IModel BuildModel(bool generateKeyValues, bool computeNonKeyValue GenerateMapping(key); GenerateMapping(nonKey); - return model; + return model.FinalizeModel(); } private static void GenerateMapping(IMutableProperty property) diff --git a/test/EFCore.SqlServer.Tests/SqlServerEventIdTest.cs b/test/EFCore.SqlServer.Tests/SqlServerEventIdTest.cs index 89f90fd2dc5..ade5d56090a 100644 --- a/test/EFCore.SqlServer.Tests/SqlServerEventIdTest.cs +++ b/test/EFCore.SqlServer.Tests/SqlServerEventIdTest.cs @@ -23,6 +23,7 @@ public void Every_eventId_has_a_logger_method_and_logs_when_level_enabled() var entityType = new EntityType(typeof(object), new Model(new ConventionSet()), ConfigurationSource.Convention); var property = new Property( "A", typeof(int), null, null, entityType, ConfigurationSource.Convention, ConfigurationSource.Convention); + entityType.Model.FinalizeModel(); var fakeFactories = new Dictionary> { diff --git a/test/EFCore.Sqlite.Tests/SqliteEventIdTest.cs b/test/EFCore.Sqlite.Tests/SqliteEventIdTest.cs index a5c2ed41384..c91d002a475 100644 --- a/test/EFCore.Sqlite.Tests/SqliteEventIdTest.cs +++ b/test/EFCore.Sqlite.Tests/SqliteEventIdTest.cs @@ -21,6 +21,7 @@ public class SqliteEventIdTest : EventIdTestBase public void Every_eventId_has_a_logger_method_and_logs_when_level_enabled() { var entityType = new EntityType(typeof(object), new Model(new ConventionSet()), ConfigurationSource.Convention); + entityType.Model.FinalizeModel(); var fakeFactories = new Dictionary> {