mirror of
https://github.com/fergalmoran/EFCore.NamingConventions.git
synced 2025-12-22 09:38:21 +00:00
Refactor tests for better code reuse
This commit is contained in:
@@ -1,41 +1,28 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
namespace EFCore.Naming.Test
|
||||
{
|
||||
public class LowerCaseNamingTest
|
||||
public class LowerCaseNamingTest : RewriterTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void Table_name_is_rewritten()
|
||||
{
|
||||
using var context = new TestContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(BlogTable));
|
||||
Assert.Equal("blogtable", entityType.GetTableName());
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||
Assert.Equal("simpleblog", entityType.GetTableName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Column_name_is_rewritten()
|
||||
{
|
||||
using var context = new TestContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(BlogTable));
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||
Assert.Equal("id", entityType.FindProperty("Id").GetColumnName());
|
||||
Assert.Equal("fullname", entityType.FindProperty("FullName").GetColumnName());
|
||||
}
|
||||
|
||||
public class TestContext : DbContext
|
||||
{
|
||||
public DbSet<BlogTable> BlogTable { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> optionsBuilder
|
||||
.UseInMemoryDatabase("test")
|
||||
.UseLowerCaseNamingConvention();
|
||||
}
|
||||
|
||||
public class BlogTable
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string FullName { get; set; }
|
||||
}
|
||||
TestContext CreateContext() => new TestContext(NamingConventionsExtensions.UseLowerCaseNamingConvention);
|
||||
}
|
||||
}
|
||||
|
||||
27
EFCore.NamingConventions.Test/RewriterTestBase.cs
Normal file
27
EFCore.NamingConventions.Test/RewriterTestBase.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace EFCore.Naming.Test
|
||||
{
|
||||
public abstract class RewriterTestBase
|
||||
{
|
||||
public class TestContext : DbContext
|
||||
{
|
||||
readonly Func<DbContextOptionsBuilder, DbContextOptionsBuilder> _useNamingConvention;
|
||||
|
||||
public TestContext(Func<DbContextOptionsBuilder, DbContextOptionsBuilder> useNamingConvention)
|
||||
=> _useNamingConvention = useNamingConvention;
|
||||
|
||||
public DbSet<SimpleBlog> Blog { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> _useNamingConvention(optionsBuilder.UseInMemoryDatabase("test"));
|
||||
}
|
||||
|
||||
public class SimpleBlog
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string FullName { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,41 +1,28 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
namespace EFCore.Naming.Test
|
||||
{
|
||||
public class SnakeCaseNamingTest
|
||||
public class SnakeCaseNamingTest : RewriterTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void Table_name_is_rewritten()
|
||||
{
|
||||
using var context = new TestContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(Blog));
|
||||
Assert.Equal("blog", entityType.GetTableName());
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||
Assert.Equal("simple_blog", entityType.GetTableName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Column_name_is_rewritten()
|
||||
{
|
||||
using var context = new TestContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(Blog));
|
||||
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());
|
||||
}
|
||||
|
||||
public class TestContext : DbContext
|
||||
{
|
||||
public DbSet<Blog> Blog { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> optionsBuilder
|
||||
.UseInMemoryDatabase("test")
|
||||
.UseSnakeCaseNamingConvention();
|
||||
}
|
||||
|
||||
public class Blog
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string FullName { get; set; }
|
||||
}
|
||||
TestContext CreateContext() => new TestContext(NamingConventionsExtensions.UseSnakeCaseNamingConvention);
|
||||
}
|
||||
}
|
||||
|
||||
28
EFCore.NamingConventions.Test/UpperCaseNamingTest.cs
Normal file
28
EFCore.NamingConventions.Test/UpperCaseNamingTest.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
namespace EFCore.Naming.Test
|
||||
{
|
||||
public class UpperCaseNamingTest : RewriterTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void Table_name_is_rewritten()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entityType = context.Model.FindEntityType(typeof(SimpleBlog));
|
||||
Assert.Equal("SIMPLEBLOG", 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("FULLNAME", entityType.FindProperty("FullName").GetColumnName());
|
||||
}
|
||||
|
||||
TestContext CreateContext() => new TestContext(NamingConventionsExtensions.UseUpperCaseNamingConvention);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user