Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.Commons.Tests/Entities/EntityConventionTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

93 lines
3.6 KiB
C#

using System.Collections;
using System.Reflection;
namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Entities;
public class EntityConventionTests
{
private static readonly Assembly CommonsAssembly = typeof(ZB.MOM.WW.ScadaBridge.Commons.Types.RetryPolicy).Assembly;
private static IEnumerable<Type> GetEntityTypes() =>
CommonsAssembly.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && t.Namespace != null
&& t.Namespace.Contains(".Entities."));
/// <summary>Verifies that no entity class carries Entity Framework attributes.</summary>
[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}");
}
}
}
}
/// <summary>Verifies that every timestamp property on an entity is a DateTimeOffset.</summary>
[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}");
}
}
}
}
/// <summary>Verifies that collection navigation properties on entities are declared as ICollection.</summary>
[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}");
}
}
}
}
}