Files
EFCore.NamingConventions/EFCore.NamingConventions.Test/NameRewritingConventionTest.cs
Shay Rojansky 3f62586c3f Big overhaul for 5.0 (#47)
* Use EF Core 5.0
* Fixes around TPH, owned entity management
* Redid tests

Fixes #45
Closes #46
Fixes #41
2020-12-10 14:52:26 +02:00

288 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Globalization;
using System.Linq;
using EFCore.NamingConventions.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
// ReSharper disable UnusedMember.Global
namespace EFCore.NamingConventions.Test
{
public class NameRewritingConventionTest
{
[Fact]
public void Table_name()
{
var entityType = BuildEntityType("SimpleBlog", _ => {});
Assert.Equal("simple_blog", entityType.GetTableName());
}
[Fact]
public void Column_name()
{
var entityType = BuildEntityType("SimpleBlog", e => e.Property<int>("SimpleBlogId"));
Assert.Equal("simple_blog_id", entityType.FindProperty("SimpleBlogId")
.GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value));
}
[Fact]
public void Column_name_on_view()
{
var entityType = BuildEntityType("SimpleBlog", e =>
{
e.ToTable("SimpleBlogTable");
e.ToView("SimpleBlogView");
e.ToFunction("SimpleBlogFunction");
e.Property<int>("SimpleBlogId");
});
foreach (var type in new[] { StoreObjectType.Table, StoreObjectType.View, StoreObjectType.Function })
{
Assert.Equal("simple_blog_id", entityType.FindProperty("SimpleBlogId")
.GetColumnName(StoreObjectIdentifier.Create(entityType, type)!.Value));
}
}
[Fact]
public void Column_name_turkish_culture()
{
var entityType = BuildEntityType(
"SimpleBlog",
e => e.Property<int>("SimpleBlogId"),
CultureInfo.CreateSpecificCulture("tr-TR"));
Assert.Equal("simple_blog_ıd", entityType.FindProperty("SimpleBlogId")
.GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value));
}
[Fact]
public void Column_name_invariant_culture()
{
var entityType = BuildEntityType(
"SimpleBlog",
e => e.Property<int>("SimpleBlogId"),
CultureInfo.InvariantCulture);
Assert.Equal("simple_blog_id", entityType.FindProperty("SimpleBlogId")
.GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value));
}
[Fact]
public void Primary_key_name()
{
var entityType = BuildEntityType("SimpleBlog", e =>
{
e.Property<int>("SimpleBlogId");
e.HasKey("SimpleBlogId");
});
Assert.Equal("pk_simple_blog", entityType.GetKeys().Single(k => k.IsPrimaryKey()).GetName());
}
[Fact]
public void Alternative_key_name()
{
var entityType = BuildEntityType("SimpleBlog", e =>
{
e.Property<int>("SimpleBlogId");
e.Property<int>("SomeAlternativeKey");
e.HasKey("SimpleBlogId");
e.HasAlternateKey("SomeAlternativeKey");
});
Assert.Equal("ak_simple_blog_some_alternative_key", entityType.GetKeys().Single(k => !k.IsPrimaryKey()).GetName());
}
[Fact]
public void Foreign_key_name()
{
var model = BuildModel(b =>
{
b.Entity("Blog", e =>
{
e.Property<int>("BlogId");
e.HasKey("BlogId");
e.HasMany("Post").WithOne("Blog");
});
b.Entity("Post", e =>
{
e.Property<int>("PostId");
e.Property<int>("BlogId");
e.HasKey("PostId");
});
});
var entityType = model.FindEntityType("Post");
Assert.Equal("fk_post_blog_blog_id", entityType.GetForeignKeys().Single().GetConstraintName());
}
[Fact]
public void Index_name()
{
var entityType = BuildEntityType("SimpleBlog", e =>
{
e.Property<int>("IndexedProperty");
e.HasIndex("IndexedProperty");
});
Assert.Equal("ix_simple_blog_indexed_property", entityType.GetIndexes().Single().GetDatabaseName());
}
#region Owned entities
[Fact]
public void Owned_entity_with_table_splitting()
{
var model = BuildModel(b =>
{
b.Entity("SimpleBlog", e =>
{
e.OwnsOne("OwnedEntity", "Nav", o => o.Property<int>("OwnedProperty"));
});
});
var entityType = model.FindEntityType("OwnedEntity");
Assert.Equal("pk_simple_blog", entityType.FindPrimaryKey().GetName());
Assert.Equal("simple_blog", entityType.GetTableName());
Assert.Equal("owned_property", entityType.FindProperty("OwnedProperty")
.GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value));
}
[Fact]
public void Owned_entity_without_table_splitting()
{
var model = BuildModel(b =>
{
b.Entity("SimpleBlog", e =>
{
e.Property<int>("SimpleBlogId");
e.HasKey("SimpleBlogId");
e.OwnsOne("OwnedEntity", "Nav", o =>
{
o.ToTable("another_table");
o.Property<int>("OwnedProperty");
});
});
});
var entityType = model.FindEntityType("OwnedEntity");
Assert.Equal("pk_another_table", entityType.FindPrimaryKey().GetName());
Assert.Equal("another_table", entityType.GetTableName());
Assert.Equal("owned_property", entityType.FindProperty("OwnedProperty")
.GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value));
}
[Fact]
public void Owned_entity_with_view_without_table_splitting()
{
var model = BuildModel(b =>
{
b.Entity("OwnedEntity", e =>
{
e.ToTable("OwnedEntityTable");
e.ToView("OwnedEntityView");
e.Property<int>("OwnedProperty");
});
b.Entity("SimpleBlog", e => e.OwnsOne("OwnedEntity", "Nav"));
});
var entityType = model.FindEntityType("OwnedEntity");
Assert.Equal("OwnedEntityTable", entityType.GetTableName());
Assert.Equal("OwnedEntityView", entityType.GetViewName());
Assert.Equal("owned_property", entityType.FindProperty("OwnedProperty")
.GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table)!.Value));
Assert.Equal("owned_property", entityType.FindProperty("OwnedProperty")
.GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.View)!.Value));
}
#endregion Owned entities
#region Inheritance
[Fact]
public void TPH()
{
var model = BuildModel(b =>
{
b.Entity("SimpleBlog", e =>
{
e.Property<int>("SimpleBlogId");
e.HasKey("SimpleBlogId");
});
b.Entity("FancyBlog", e =>
{
e.HasBaseType("SimpleBlog");
e.Property<int>("FancyProperty");
});
});
var simpleBlogEntityType = model.FindEntityType("SimpleBlog");
Assert.Equal("simple_blog", simpleBlogEntityType.GetTableName());
Assert.Equal("simple_blog_id", simpleBlogEntityType.FindProperty("SimpleBlogId")
.GetColumnName(StoreObjectIdentifier.Create(simpleBlogEntityType, StoreObjectType.Table)!.Value));
var fancyBlogEntityType = model.FindEntityType("FancyBlog");
Assert.Equal("simple_blog", fancyBlogEntityType.GetTableName());
Assert.Equal("fancy_property", fancyBlogEntityType.FindProperty("FancyProperty")
.GetColumnName(StoreObjectIdentifier.Create(fancyBlogEntityType, StoreObjectType.Table)!.Value));
}
[Fact]
public void TPT()
{
var model = BuildModel(b =>
{
b.Entity("SimpleBlog", e =>
{
e.Property<int>("SimpleBlogId");
e.HasKey("SimpleBlogId");
});
b.Entity("FancyBlog", e =>
{
e.HasBaseType("SimpleBlog");
e.ToTable("fancy_blog");
e.Property<int>("FancyProperty");
});
});
var simpleBlogEntityType = model.FindEntityType("SimpleBlog");
Assert.Equal("simple_blog", simpleBlogEntityType.GetTableName());
Assert.Equal("simple_blog_id", simpleBlogEntityType.FindProperty("SimpleBlogId")
.GetColumnName(StoreObjectIdentifier.Create(simpleBlogEntityType, StoreObjectType.Table)!.Value));
var fancyBlogEntityType = model.FindEntityType("FancyBlog");
Assert.Equal("fancy_blog", fancyBlogEntityType.GetTableName());
Assert.Equal("fancy_property", fancyBlogEntityType.FindProperty("FancyProperty")
.GetColumnName(StoreObjectIdentifier.Create(fancyBlogEntityType, StoreObjectType.Table)!.Value));
}
#endregion Inheritance
#region Support
private IModel BuildModel(Action<ModelBuilder> buildAction, CultureInfo cultureInfo = null)
{
var conventionSet = InMemoryTestHelpers.Instance.CreateConventionSetBuilder().CreateConventionSet();
ConventionSet.Remove(conventionSet.ModelFinalizedConventions, typeof(ValidatingConvention));
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSnakeCaseNamingConvention(cultureInfo);
new NamingConventionSetPlugin(optionsBuilder.Options).ModifyConventions(conventionSet);
var builder = new ModelBuilder(conventionSet);
buildAction(builder);
return builder.FinalizeModel();
}
private IEntityType BuildEntityType(string entityTypeName, Action<EntityTypeBuilder> buildAction, CultureInfo cultureInfo = null)
=> BuildModel(b => buildAction(b.Entity(entityTypeName)), cultureInfo).GetEntityTypes().Single();
#endregion
}
}