Skip to content

Commit

Permalink
Make ModelBuilderTest asserts more robust and extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Nov 2, 2023
1 parent ab4b617 commit 1e116d2
Show file tree
Hide file tree
Showing 32 changed files with 1,625 additions and 582 deletions.
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
<!-- NB: This version affects Visual Studio compatibility. See https://learn.microsoft.com/visualstudio/extensibility/roslyn-version-support -->
<MicrosoftCodeAnalysisVersion>4.5.0</MicrosoftCodeAnalysisVersion>
<MicrosoftCodeAnalysisTestingVersion>1.1.2-beta1.23371.1</MicrosoftCodeAnalysisTestingVersion>
<XUnitVersion>2.6.1</XUnitVersion>
</PropertyGroup>
</Project>
6 changes: 4 additions & 2 deletions src/EFCore/Metadata/Internal/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ public class Model : ConventionAnnotatable, IMutableModel, IConventionModel, IRu
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public Model()
public Model(Guid? modelId = null)
: this(new ConventionSet())
{
ModelId = modelId ?? Guid.NewGuid();
}

/// <summary>
Expand All @@ -75,6 +76,7 @@ public Model(ConventionSet conventions, ModelDependencies? modelDependencies = n
_modelFinalizedConventions = conventions.ModelFinalizedConventions;
Builder = builder;
Configuration = modelConfiguration;
ModelId = Guid.NewGuid();
dispatcher.OnModelInitialized(builder);
}

Expand Down Expand Up @@ -459,7 +461,7 @@ public virtual IEnumerable<EntityType> FindEntityTypes(Type type)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Guid ModelId { get; set; } = Guid.NewGuid();
public virtual Guid ModelId { get; set; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ namespace Microsoft.EntityFrameworkCore.ModelBuilding;

public class CosmosModelBuilderGenericTest : ModelBuilderGenericTest
{
public class CosmosGenericNonRelationship : GenericNonRelationship
public class CosmosGenericNonRelationship : GenericNonRelationship, IClassFixture<CosmosModelBuilderFixture>
{
public CosmosGenericNonRelationship(CosmosModelBuilderFixture fixture)
: base(fixture)
{
}

public override void Can_set_composite_key_for_primitive_collection_on_an_entity_with_fields()
=> Assert.Equal(
CosmosStrings.PrimitiveCollectionsNotSupported(nameof(EntityWithFields), "CollectionCompanyId"),
Expand Down Expand Up @@ -433,8 +438,13 @@ protected override TestModelBuilder CreateModelBuilder(Action<ModelConfiguration
=> CreateTestModelBuilder(CosmosTestHelpers.Instance, configure);
}

public class CosmosGenericComplexType : GenericComplexType
public class CosmosGenericComplexType : GenericComplexType, IClassFixture<CosmosModelBuilderFixture>
{
public CosmosGenericComplexType(CosmosModelBuilderFixture fixture)
: base(fixture)
{
}

public override void Access_mode_can_be_overridden_at_entity_and_property_levels()
=> Assert.Equal(
CosmosStrings.PrimitiveCollectionsNotSupported(nameof(CollectionQuarks), "Down"),
Expand Down Expand Up @@ -766,8 +776,13 @@ protected override TestModelBuilder CreateModelBuilder(Action<ModelConfiguration
=> CreateTestModelBuilder(CosmosTestHelpers.Instance, configure);
}

public class CosmosGenericInheritance : GenericInheritance
public class CosmosGenericInheritance : GenericInheritance, IClassFixture<CosmosModelBuilderFixture>
{
public CosmosGenericInheritance(CosmosModelBuilderFixture fixture)
: base(fixture)
{
}

public override void Base_type_can_be_discovered_after_creating_foreign_keys_on_derived()
{
var mb = CreateModelBuilder();
Expand Down Expand Up @@ -795,8 +810,13 @@ protected override TestModelBuilder CreateModelBuilder(Action<ModelConfiguration
=> CreateTestModelBuilder(CosmosTestHelpers.Instance, configure);
}

public class CosmosGenericOneToMany : GenericOneToMany
public class CosmosGenericOneToMany : GenericOneToMany, IClassFixture<CosmosModelBuilderFixture>
{
public CosmosGenericOneToMany(CosmosModelBuilderFixture fixture)
: base(fixture)
{
}

public override void Navigation_to_shared_type_is_not_discovered_by_convention()
{
var modelBuilder = CreateModelBuilder();
Expand All @@ -818,14 +838,24 @@ protected override TestModelBuilder CreateModelBuilder(Action<ModelConfiguration
=> CreateTestModelBuilder(CosmosTestHelpers.Instance, configure);
}

public class CosmosGenericManyToOne : GenericManyToOne
public class CosmosGenericManyToOne : GenericManyToOne, IClassFixture<CosmosModelBuilderFixture>
{
public CosmosGenericManyToOne(CosmosModelBuilderFixture fixture)
: base(fixture)
{
}

protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(CosmosTestHelpers.Instance, configure);
}

public class CosmosGenericOneToOne : GenericOneToOne
public class CosmosGenericOneToOne : GenericOneToOne, IClassFixture<CosmosModelBuilderFixture>
{
public CosmosGenericOneToOne(CosmosModelBuilderFixture fixture)
: base(fixture)
{
}

public override void Navigation_to_shared_type_is_not_discovered_by_convention()
{
var modelBuilder = CreateModelBuilder();
Expand All @@ -847,8 +877,13 @@ protected override TestModelBuilder CreateModelBuilder(Action<ModelConfiguration
=> CreateTestModelBuilder(CosmosTestHelpers.Instance, configure);
}

public class CosmosGenericManyToMany : GenericManyToMany
public class CosmosGenericManyToMany : GenericManyToMany, IClassFixture<CosmosModelBuilderFixture>
{
public CosmosGenericManyToMany(CosmosModelBuilderFixture fixture)
: base(fixture)
{
}

[ConditionalFact]
public virtual void Can_use_shared_type_as_join_entity_with_partition_keys()
{
Expand Down Expand Up @@ -1002,8 +1037,13 @@ protected override TestModelBuilder CreateModelBuilder(Action<ModelConfiguration
=> CreateTestModelBuilder(CosmosTestHelpers.Instance, configure);
}

public class CosmosGenericOwnedTypes : GenericOwnedTypes
public class CosmosGenericOwnedTypes : GenericOwnedTypes, IClassFixture<CosmosModelBuilderFixture>
{
public CosmosGenericOwnedTypes(CosmosModelBuilderFixture fixture)
: base(fixture)
{
}

public override void Deriving_from_owned_type_throws()
// On Cosmos the base type starts as owned
=> Assert.Contains(
Expand Down Expand Up @@ -1048,4 +1088,19 @@ public virtual void Reference_type_is_discovered_as_owned()
protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(CosmosTestHelpers.Instance, configure);
}

public class CosmosModelBuilderFixture : ModelBuilderFixtureBase
{
public override void AssertEqual(
IEnumerable<IReadOnlyProperty> expectedProperties,
IEnumerable<IReadOnlyProperty> actualProperties,
bool assertOrder = false,
bool compareAnnotations = false)
{
expectedProperties = expectedProperties.Where(p => p.Name != "__jObject" && p.Name != "__id");
actualProperties = actualProperties.Where(p => p.Name != "__jObject" && p.Name != "__id");

base.AssertEqual(expectedProperties, actualProperties, assertOrder, compareAnnotations);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,46 @@ namespace Microsoft.EntityFrameworkCore.ModelBuilding;

public class InMemoryModelBuilderGenericTest : ModelBuilderGenericTest
{
public class InMemoryGenericNonRelationship : GenericNonRelationship
public class InMemoryGenericNonRelationship : GenericNonRelationship, IClassFixture<InMemoryModelBuilderFixture>
{
public InMemoryGenericNonRelationship(InMemoryModelBuilderFixture fixture)
: base(fixture)
{
}

protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(InMemoryTestHelpers.Instance, configure);
}

public class InMemoryGenericComplexTypeTestBase : GenericComplexType
public class InMemoryGenericComplexTypeTestBase : GenericComplexType, IClassFixture<InMemoryModelBuilderFixture>
{
public InMemoryGenericComplexTypeTestBase(InMemoryModelBuilderFixture fixture)
: base(fixture)
{
}

protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(InMemoryTestHelpers.Instance, configure);
}

public class InMemoryGenericInheritance : GenericInheritance
public class InMemoryGenericInheritance : GenericInheritance, IClassFixture<InMemoryModelBuilderFixture>
{
public InMemoryGenericInheritance(InMemoryModelBuilderFixture fixture)
: base(fixture)
{
}

protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(InMemoryTestHelpers.Instance, configure);
}

public class InMemoryGenericOneToMany : GenericOneToMany
public class InMemoryGenericOneToMany : GenericOneToMany, IClassFixture<InMemoryModelBuilderFixture>
{
public InMemoryGenericOneToMany(InMemoryModelBuilderFixture fixture)
: base(fixture)
{
}

[ConditionalFact] // Issue #3376
public virtual void Can_use_self_referencing_overlapping_FK_PK()
{
Expand Down Expand Up @@ -145,14 +165,24 @@ protected override TestModelBuilder CreateModelBuilder(Action<ModelConfiguration
=> CreateTestModelBuilder(InMemoryTestHelpers.Instance, configure);
}

public class InMemoryGenericManyToOne : GenericManyToOne
public class InMemoryGenericManyToOne : GenericManyToOne, IClassFixture<InMemoryModelBuilderFixture>
{
public InMemoryGenericManyToOne(InMemoryModelBuilderFixture fixture)
: base(fixture)
{
}

protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(InMemoryTestHelpers.Instance, configure);
}

public class InMemoryGenericOneToOne : GenericOneToOne
public class InMemoryGenericOneToOne : GenericOneToOne, IClassFixture<InMemoryModelBuilderFixture>
{
public InMemoryGenericOneToOne(InMemoryModelBuilderFixture fixture)
: base(fixture)
{
}

[ConditionalFact]
public virtual void Can_use_self_referencing_overlapping_FK_PK()
{
Expand Down Expand Up @@ -236,9 +266,18 @@ protected override TestModelBuilder CreateModelBuilder(Action<ModelConfiguration
=> CreateTestModelBuilder(InMemoryTestHelpers.Instance, configure);
}

public class InMemoryGenericOwnedTypes : GenericOwnedTypes
public class InMemoryGenericOwnedTypes : GenericOwnedTypes, IClassFixture<InMemoryModelBuilderFixture>
{
public InMemoryGenericOwnedTypes(InMemoryModelBuilderFixture fixture)
: base(fixture)
{
}

protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)
=> CreateTestModelBuilder(InMemoryTestHelpers.Instance, configure);
}

public class InMemoryModelBuilderFixture : ModelBuilderFixtureBase
{
}
}
Loading

0 comments on commit 1e116d2

Please sign in to comment.