Refactor tests for better code reuse

This commit is contained in:
Shay Rojansky
2020-01-16 21:22:20 +01:00
parent 5adfa687a4
commit c04026a104
4 changed files with 72 additions and 43 deletions

View File

@@ -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);
}
}

View 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; }
}
}
}

View File

@@ -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);
}
}

View 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);
}
}