mirror of
https://github.com/fergalmoran/EFCore.NamingConventions.git
synced 2025-12-22 09:38:21 +00:00
97
EFCore.NamingConventions.Test/UpperSnakeCaseNamingTests.cs
Normal file
97
EFCore.NamingConventions.Test/UpperSnakeCaseNamingTests.cs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace EFCore.NamingConventions.Test
|
||||||
|
{
|
||||||
|
public class UpperSnakeCaseNamingTests : RewriterTestBase
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Table_name_is_rewritten()
|
||||||
|
{
|
||||||
|
using var context = CreateContext();
|
||||||
|
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||||
|
Assert.Equal("SIMPLE_BLOG", entityType.GetTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Table_name_is_rewritten_in_turkish()
|
||||||
|
{
|
||||||
|
using var context = CreateContext(CultureInfo.CreateSpecificCulture("tr-TR"));
|
||||||
|
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||||
|
Assert.Equal("SİMPLE_BLOG", entityType.GetTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Table_name_is_rewritten_in_invariant()
|
||||||
|
{
|
||||||
|
using var context = CreateContext(CultureInfo.InvariantCulture);
|
||||||
|
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||||
|
Assert.Equal("SIMPLE_BLOG", entityType.GetTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Column_name_is_rewritten()
|
||||||
|
{
|
||||||
|
using var context = CreateContext();
|
||||||
|
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||||
|
Assert.Equal("ID", entityType.FindProperty("Id").GetColumnName());
|
||||||
|
Assert.Equal("FULL_NAME", entityType.FindProperty("FullName").GetColumnName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Owned_entity_is_rewritten()
|
||||||
|
{
|
||||||
|
using var context = CreateContext();
|
||||||
|
var entityType = context.Model.FindEntityType(typeof(OwnedStatistics1));
|
||||||
|
Assert.Equal("SIMPLE_BLOG", entityType.GetTableName());
|
||||||
|
var property = entityType.GetProperty(nameof(OwnedStatistics1.SomeStatistic));
|
||||||
|
Assert.Equal("OWNED_STATISTICS1_SOME_STATISTIC", property.GetColumnName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Owned_entity_split_is_rewritten()
|
||||||
|
{
|
||||||
|
using var context = CreateContext();
|
||||||
|
var entityType = context.Model.FindEntityType(typeof(OwnedStatistics2));
|
||||||
|
Assert.Equal("OwnedStatisticsSplit", entityType.GetTableName());
|
||||||
|
var property = entityType.GetProperty(nameof(OwnedStatistics2.SomeStatistic));
|
||||||
|
Assert.Equal("SOME_STATISTIC", property.GetColumnName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Primary_key_name_is_rewritten()
|
||||||
|
{
|
||||||
|
using var context = CreateContext();
|
||||||
|
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||||
|
Assert.Equal("PK_SIMPLE_BLOG", entityType.GetKeys().Single(k => k.IsPrimaryKey()).GetName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Alternative_key_name_is_rewritten()
|
||||||
|
{
|
||||||
|
using var context = CreateContext();
|
||||||
|
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||||
|
Assert.Equal("AK_SIMPLE_BLOG_SOME_ALTERNATIVE_KEY", entityType.GetKeys().Single(k => !k.IsPrimaryKey()).GetName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Foreign_key_name_is_rewritten()
|
||||||
|
{
|
||||||
|
using var context = CreateContext();
|
||||||
|
var entityType = context.Model.FindEntityType(typeof(Post));
|
||||||
|
Assert.Equal("FK_POST_SIMPLE_BLOG_BLOG_ID", entityType.GetForeignKeys().Single().GetConstraintName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Index_name_is_rewritten()
|
||||||
|
{
|
||||||
|
using var context = CreateContext();
|
||||||
|
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||||
|
Assert.Equal("IX_SIMPLE_BLOG_FULL_NAME", entityType.GetIndexes().Single().GetName());
|
||||||
|
}
|
||||||
|
|
||||||
|
TestContext CreateContext(CultureInfo culture = null) => new TestContext(builder => builder.UseUpperSnakeCaseNamingConvention(culture));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ namespace EFCore.NamingConventions.Internal
|
|||||||
None,
|
None,
|
||||||
SnakeCase,
|
SnakeCase,
|
||||||
LowerCase,
|
LowerCase,
|
||||||
UpperCase
|
UpperCase,
|
||||||
|
UpperSnakeCase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ namespace EFCore.NamingConventions.Internal
|
|||||||
NamingConvention.SnakeCase => new SnakeCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
|
NamingConvention.SnakeCase => new SnakeCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
|
||||||
NamingConvention.LowerCase => new LowerCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
|
NamingConvention.LowerCase => new LowerCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
|
||||||
NamingConvention.UpperCase => new UpperCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
|
NamingConvention.UpperCase => new UpperCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
|
||||||
|
NamingConvention.UpperSnakeCase => new UpperSnakeCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
|
||||||
_ => throw new NotImplementedException("Unhandled enum value: " + namingStyle)
|
_ => throw new NotImplementedException("Unhandled enum value: " + namingStyle)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -60,6 +60,14 @@ namespace EFCore.NamingConventions.Internal
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual NamingConventionsOptionsExtension WithUpperSnakeCaseNamingConvention(CultureInfo culture = null)
|
||||||
|
{
|
||||||
|
var clone = Clone();
|
||||||
|
clone._namingConvention = NamingConvention.UpperSnakeCase;
|
||||||
|
clone._culture = culture;
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
public void Validate(IDbContextOptions options) {}
|
public void Validate(IDbContextOptions options) {}
|
||||||
|
|
||||||
public void ApplyServices(IServiceCollection services)
|
public void ApplyServices(IServiceCollection services)
|
||||||
@@ -89,6 +97,7 @@ namespace EFCore.NamingConventions.Internal
|
|||||||
NamingConvention.SnakeCase => "using snake-case naming ",
|
NamingConvention.SnakeCase => "using snake-case naming ",
|
||||||
NamingConvention.LowerCase => "using lower case naming",
|
NamingConvention.LowerCase => "using lower case naming",
|
||||||
NamingConvention.UpperCase => "using upper case naming",
|
NamingConvention.UpperCase => "using upper case naming",
|
||||||
|
NamingConvention.UpperSnakeCase => "using upper snake-case namming",
|
||||||
_ => throw new NotImplementedException("Unhandled enum value: " +
|
_ => throw new NotImplementedException("Unhandled enum value: " +
|
||||||
Extension._namingConvention)
|
Extension._namingConvention)
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace EFCore.NamingConventions.Internal
|
||||||
|
{
|
||||||
|
class UpperSnakeCaseNameRewriter : SnakeCaseNameRewriter
|
||||||
|
{
|
||||||
|
private readonly CultureInfo _culture;
|
||||||
|
|
||||||
|
public UpperSnakeCaseNameRewriter(CultureInfo culture) : base(culture) => _culture = culture;
|
||||||
|
|
||||||
|
protected override string RewriteName(string name) => base.RewriteName(name).ToUpper(_culture);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -55,5 +55,21 @@ namespace Microsoft.EntityFrameworkCore
|
|||||||
public static DbContextOptionsBuilder<TContext> UseUpperCaseNamingConvention<TContext>([NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder, CultureInfo culture = null)
|
public static DbContextOptionsBuilder<TContext> UseUpperCaseNamingConvention<TContext>([NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder, CultureInfo culture = null)
|
||||||
where TContext : DbContext
|
where TContext : DbContext
|
||||||
=> (DbContextOptionsBuilder<TContext>)UseUpperCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder,culture);
|
=> (DbContextOptionsBuilder<TContext>)UseUpperCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder,culture);
|
||||||
|
|
||||||
|
public static DbContextOptionsBuilder UseUpperSnakeCaseNamingConvention([NotNull] this DbContextOptionsBuilder optionsBuilder, CultureInfo culture = null)
|
||||||
|
{
|
||||||
|
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
||||||
|
|
||||||
|
var extension = (optionsBuilder.Options.FindExtension<NamingConventionsOptionsExtension>() ?? new NamingConventionsOptionsExtension())
|
||||||
|
.WithUpperSnakeCaseNamingConvention(culture);
|
||||||
|
|
||||||
|
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
|
||||||
|
|
||||||
|
return optionsBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DbContextOptionsBuilder<TContext> UseUpperSnakeCaseNamingConvention<TContext>([NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder, CultureInfo culture = null)
|
||||||
|
where TContext : DbContext
|
||||||
|
=> (DbContextOptionsBuilder<TContext>)UseUpperSnakeCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user