35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Globalization;
|
|
using System.Reflection;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace DD.Persistence.Test;
|
|
public class TableAttributeShould
|
|
{
|
|
private const string Separator = "_";
|
|
private const string TargetAssembly = "DD.Persistence.Database";
|
|
private const string TargetNamespace = "DD.Persistence.Database.Entity";
|
|
|
|
[Fact]
|
|
public void Test()
|
|
{
|
|
Assembly assembly = Assembly.Load(TargetAssembly);
|
|
var typesInNamespace = assembly.GetTypes()
|
|
.Where(t => t.IsClass && t.Namespace == TargetNamespace)
|
|
.ToList();
|
|
|
|
foreach (var type in typesInNamespace)
|
|
{
|
|
var tableAttribute = type.GetCustomAttribute<TableAttribute>();
|
|
Assert.NotNull(tableAttribute);
|
|
|
|
var partsOfClassName = Regex
|
|
.Split(type.Name, @"(?=[A-Z])")
|
|
.Where(s => s != string.Empty)
|
|
.Select(s => s.ToLower(CultureInfo.InvariantCulture));
|
|
var expectedClassName = string.Join(Separator, partsOfClassName);
|
|
Assert.Equal(expectedClassName, tableAttribute.Name);
|
|
}
|
|
}
|
|
}
|