Skip to content

Commit

Permalink
Finalize the model in tests
Browse files Browse the repository at this point in the history
Fixes #16000
  • Loading branch information
AndriySvyryd committed Jun 8, 2019
1 parent 5dc980d commit a73a976
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/EFCore/Metadata/Internal/EntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public static PropertyCounts GetCounts([NotNull] this IEntityType entityType)
/// </summary>
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3407,8 +3407,7 @@ protected void Test(Action<ModelBuilder> 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));
Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.InMemory.Tests/InMemoryDatabaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private static IModel CreateModel()
b.Property(c => c.Name);
});

return modelBuilder.Model;
return modelBuilder.Model.FinalizeModel();
}

private class Customer
Expand Down
14 changes: 6 additions & 8 deletions test/EFCore.InMemory.Tests/InMemoryValueGeneratorSelectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Microsoft.Extensions.DependencyInjection;
using Xunit;

// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable UnusedMember.Local
namespace Microsoft.EntityFrameworkCore
{
public class InMemoryValueGeneratorSelectorTest
Expand Down Expand Up @@ -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<IValueGeneratorSelector>();

Expand All @@ -89,25 +90,23 @@ public void Throws_for_unsupported_combinations()
var selector = InMemoryTestHelpers.Instance.CreateContextServices(model).GetRequiredService<IValueGeneratorSelector>();

Assert.Equal(
CoreStrings.NoValueGenerator("Random", "AnEntity", typeof(Random).Name),
Assert.Throws<NotSupportedException>(() => selector.Select(entityType.FindProperty("Random"), entityType)).Message);
CoreStrings.NoValueGenerator("Float", "AnEntity", "float"),
Assert.Throws<NotSupportedException>(() => 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<Random>();
builder.Entity<AnEntity>().Property(e => e.Custom).HasValueGenerator<CustomValueGenerator>();
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
Expand All @@ -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<int>
Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.Relational.Tests/RelationalEventIdTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type, Func<object>>
{
Expand Down
19 changes: 9 additions & 10 deletions test/EFCore.Relational.Tests/Update/CommandBatchPreparerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,9 +1128,8 @@ public ICommandBatchPreparer CreateCommandBatchPreparer(
IUpdateAdapter updateAdapter = null,
bool sensitiveLogging = false)
{
modificationCommandBatchFactory =
modificationCommandBatchFactory
?? RelationalTestHelpers.Instance.CreateContextServices().GetRequiredService<IModificationCommandBatchFactory>();
modificationCommandBatchFactory ??=
RelationalTestHelpers.Instance.CreateContextServices().GetRequiredService<IModificationCommandBatchFactory>();

var loggingOptions = new LoggingOptions();
if (sensitiveLogging)
Expand Down Expand Up @@ -1168,7 +1167,7 @@ private static IModel CreateSimpleFKModel()
.HasForeignKey<RelatedFakeEntity>(c => c.Id);
});

return modelBuilder.Model;
return modelBuilder.Model.FinalizeModel();
}

private static IModel CreateCyclicFKModel()
Expand Down Expand Up @@ -1196,7 +1195,7 @@ private static IModel CreateCyclicFKModel()
.WithOne()
.HasForeignKey<FakeEntity>(c => c.RelatedId);

return modelBuilder.Model;
return modelBuilder.Model.FinalizeModel();
}

private static IModel CreateCyclicFkWithTailModel()
Expand Down Expand Up @@ -1232,7 +1231,7 @@ private static IModel CreateCyclicFkWithTailModel()
.HasForeignKey<AnotherFakeEntity>(e => e.AnotherId);
});

return modelBuilder.Model;
return modelBuilder.Model.FinalizeModel();
}

private static IModel CreateTwoLevelFKModel()
Expand All @@ -1257,7 +1256,7 @@ private static IModel CreateTwoLevelFKModel()
.HasForeignKey<AnotherFakeEntity>(c => c.AnotherId);
});

return modelBuilder.Model;
return modelBuilder.Model.FinalizeModel();
}

private static IModel CreateSharedTableModel()
Expand All @@ -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<RelatedFakeEntity>(
b =>
{
b.Property(c => c.RelatedId).IsConcurrencyToken();
b.Property(c => c.RelatedId).IsConcurrencyToken().HasColumnName("RelatedId");
b.HasOne<FakeEntity>()
.WithOne()
.HasForeignKey<RelatedFakeEntity>(c => c.Id);
Expand All @@ -1291,7 +1290,7 @@ private static IModel CreateSharedTableModel()

modelBuilder.Entity<AnotherFakeEntity>().ToTable(nameof(FakeEntity));

return modelBuilder.Model;
return modelBuilder.Model.FinalizeModel();
}

private class FakeEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/EFCore.SqlServer.Tests/SqlServerEventIdTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type, Func<object>>
{
Expand Down
1 change: 1 addition & 0 deletions test/EFCore.Sqlite.Tests/SqliteEventIdTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type, Func<object>>
{
Expand Down

0 comments on commit a73a976

Please sign in to comment.