Files
scadalink-design/tests/ScadaLink.Commons.Tests/Entities/EntityConventionTests.cs
Joseph Doherty 22e1eba58a Phase 0 WP-0.2–0.9: Implement Commons (types, entities, interfaces, messages, protocol, tests)
- WP-0.2: Namespace/folder skeleton (26 directories)
- WP-0.3: Shared data types (6 enums, RetryPolicy, Result<T>)
- WP-0.4: 24 domain entity POCOs across 10 domain areas
- WP-0.5: 7 repository interfaces with full CRUD signatures
- WP-0.6: IAuditService cross-cutting interface
- WP-0.7: 26 message contract records across 8 concern areas
- WP-0.8: IDataConnection protocol abstraction with batch ops
- WP-0.9: 8 architectural constraint enforcement tests
All 40 tests pass, zero warnings.
2026-03-16 18:48:24 -04:00

90 lines
3.2 KiB
C#

using System.Collections;
using System.Reflection;
namespace ScadaLink.Commons.Tests.Entities;
public class EntityConventionTests
{
private static readonly Assembly CommonsAssembly = typeof(ScadaLink.Commons.Types.RetryPolicy).Assembly;
private static IEnumerable<Type> GetEntityTypes() =>
CommonsAssembly.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && t.Namespace != null
&& t.Namespace.Contains(".Entities."));
[Fact]
public void AllEntities_ShouldHaveNoEfAttributes()
{
var efAttributeNames = new HashSet<string>
{
"KeyAttribute", "ForeignKeyAttribute", "TableAttribute",
"ColumnAttribute", "RequiredAttribute", "MaxLengthAttribute",
"StringLengthAttribute", "DatabaseGeneratedAttribute",
"NotMappedAttribute", "IndexAttribute", "InversePropertyAttribute"
};
foreach (var entityType in GetEntityTypes())
{
// Check class-level attributes
var classAttrs = entityType.GetCustomAttributes(true);
foreach (var attr in classAttrs)
{
Assert.DoesNotContain(attr.GetType().Name, efAttributeNames);
}
// Check property-level attributes
foreach (var prop in entityType.GetProperties())
{
var propAttrs = prop.GetCustomAttributes(true);
foreach (var attr in propAttrs)
{
Assert.False(efAttributeNames.Contains(attr.GetType().Name),
$"Entity {entityType.Name}.{prop.Name} has EF attribute {attr.GetType().Name}");
}
}
}
}
[Fact]
public void AllTimestampProperties_ShouldBeDateTimeOffset()
{
var timestampNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"Timestamp", "DeployedAt", "CompletedAt", "GeneratedAt",
"ReportTimestamp", "SnapshotTimestamp"
};
foreach (var entityType in GetEntityTypes())
{
foreach (var prop in entityType.GetProperties())
{
if (timestampNames.Contains(prop.Name))
{
var underlyingType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
Assert.True(underlyingType == typeof(DateTimeOffset),
$"{entityType.Name}.{prop.Name} should be DateTimeOffset but is {prop.PropertyType.Name}");
}
}
}
}
[Fact]
public void NavigationProperties_ShouldBeICollection()
{
foreach (var entityType in GetEntityTypes())
{
foreach (var prop in entityType.GetProperties())
{
if (prop.PropertyType.IsGenericType &&
typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) &&
prop.PropertyType != typeof(string))
{
Assert.True(
prop.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>),
$"{entityType.Name}.{prop.Name} should be ICollection<T> but is {prop.PropertyType.Name}");
}
}
}
}
}