mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 13:11:10 +00:00
Hardens test assertions and adds missing test coverage.
This commit is contained in:
1 parent
6b86fd5805
commit
1189bbaf36
489 files changed
+39982
-512
No files matched your search
@@ -0,0 +1,87 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Events;
|
||||
using Lumina.Domain.Common.Models.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Common.Models.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="AggregateRoot{TId}"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class AggregateRootTests
|
||||
{
|
||||
[Fact]
|
||||
public void GetDomainEvents_WhenNoEventsWereRaised_ShouldReturnEmptyList()
|
||||
{
|
||||
// Arrange
|
||||
TestAggregateRoot aggregateRoot = new(Guid.NewGuid());
|
||||
|
||||
// Act
|
||||
List<IDomainEvent> domainEvents = aggregateRoot.GetDomainEvents();
|
||||
|
||||
// Assert
|
||||
Assert.Empty(domainEvents);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDomainEvents_WhenEventsWereRaised_ShouldReturnAllEvents()
|
||||
{
|
||||
// Arrange
|
||||
TestAggregateRoot aggregateRoot = new(Guid.NewGuid());
|
||||
aggregateRoot.RaiseTestEvent();
|
||||
aggregateRoot.RaiseTestEvent();
|
||||
|
||||
// Act
|
||||
List<IDomainEvent> domainEvents = aggregateRoot.GetDomainEvents();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, domainEvents.Count);
|
||||
Assert.All(domainEvents, domainEvent => Assert.IsType<TestDomainEvent>(domainEvent));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDomainEvents_WhenCalled_ShouldClearTheStoredEvents()
|
||||
{
|
||||
// Arrange
|
||||
TestAggregateRoot aggregateRoot = new(Guid.NewGuid());
|
||||
aggregateRoot.RaiseTestEvent();
|
||||
|
||||
// Act
|
||||
aggregateRoot.GetDomainEvents();
|
||||
List<IDomainEvent> domainEvents = aggregateRoot.GetDomainEvents();
|
||||
|
||||
// Assert
|
||||
Assert.Empty(domainEvents);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete test implementation of the abstract <see cref="AggregateRoot{TId}"/> class.
|
||||
/// </summary>
|
||||
private sealed class TestAggregateRoot : AggregateRoot<Guid>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TestAggregateRoot"/> class.
|
||||
/// </summary>
|
||||
/// <param name="id">The id of the aggregate root.</param>
|
||||
public TestAggregateRoot(Guid id) : base(id)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises a <see cref="TestDomainEvent"/> on this aggregate root.
|
||||
/// </summary>
|
||||
public void RaiseTestEvent()
|
||||
{
|
||||
_domainEvents.Add(new TestDomainEvent(Guid.NewGuid(), DateTime.UtcNow));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test implementation of the <see cref="IDomainEvent"/> interface.
|
||||
/// </summary>
|
||||
private sealed record TestDomainEvent(Guid Id, DateTime OccurredOnUtc) : IDomainEvent;
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Models.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Common.Models.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="EntityId{TId}"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class EntityIdTests
|
||||
{
|
||||
[Fact]
|
||||
public void Value_WhenConstructed_ShouldBeSet()
|
||||
{
|
||||
// Arrange
|
||||
Guid value = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
TestEntityId entityId = new(value);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(value, entityId.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToString_WhenCalled_ShouldReturnValueAsString()
|
||||
{
|
||||
// Arrange
|
||||
Guid value = Guid.NewGuid();
|
||||
TestEntityId entityId = new(value);
|
||||
|
||||
// Act
|
||||
string? result = entityId.ToString();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(value.ToString(), result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithSameValue_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
Guid value = Guid.NewGuid();
|
||||
TestEntityId firstId = new(value);
|
||||
TestEntityId secondId = new(value);
|
||||
|
||||
// Act
|
||||
bool result = firstId.Equals(secondId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentValue_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
TestEntityId firstId = new(Guid.NewGuid());
|
||||
TestEntityId secondId = new(Guid.NewGuid());
|
||||
|
||||
// Act
|
||||
bool result = firstId.Equals(secondId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithSameValue_ShouldReturnSameHashCode()
|
||||
{
|
||||
// Arrange
|
||||
Guid value = Guid.NewGuid();
|
||||
TestEntityId firstId = new(value);
|
||||
TestEntityId secondId = new(value);
|
||||
|
||||
// Act
|
||||
int firstHashCode = firstId.GetHashCode();
|
||||
int secondHashCode = secondId.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(firstHashCode, secondHashCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete test implementation of the abstract <see cref="EntityId{TId}"/> class.
|
||||
/// </summary>
|
||||
private sealed class TestEntityId : EntityId<Guid>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TestEntityId"/> class.
|
||||
/// </summary>
|
||||
/// <param name="value">The value representing this object.</param>
|
||||
public TestEntityId(Guid value) : base(value)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of items that define equality of the object.
|
||||
/// </summary>
|
||||
/// <returns>A list of items defining the equality.</returns>
|
||||
public override IEnumerable<object> GetEqualityComponents()
|
||||
{
|
||||
yield return Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Models.Core;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Common.Models.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="Entity{TId}"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class EntityTests
|
||||
{
|
||||
[Fact]
|
||||
public void Id_WhenConstructed_ShouldBeSet()
|
||||
{
|
||||
// Arrange
|
||||
Guid id = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
TestEntity entity = new(id);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(id, entity.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithSameId_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
Guid id = Guid.NewGuid();
|
||||
TestEntity firstEntity = new(id);
|
||||
TestEntity secondEntity = new(id);
|
||||
|
||||
// Act
|
||||
bool result = firstEntity.Equals(secondEntity);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentId_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
TestEntity firstEntity = new(Guid.NewGuid());
|
||||
TestEntity secondEntity = new(Guid.NewGuid());
|
||||
|
||||
// Act
|
||||
bool result = firstEntity.Equals(secondEntity);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EqualityOperator_WithSameId_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
Guid id = Guid.NewGuid();
|
||||
TestEntity firstEntity = new(id);
|
||||
TestEntity secondEntity = new(id);
|
||||
|
||||
// Act
|
||||
bool result = firstEntity == secondEntity;
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InequalityOperator_WithDifferentId_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
TestEntity firstEntity = new(Guid.NewGuid());
|
||||
TestEntity secondEntity = new(Guid.NewGuid());
|
||||
|
||||
// Act
|
||||
bool result = firstEntity != secondEntity;
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithSameId_ShouldReturnSameHashCode()
|
||||
{
|
||||
// Arrange
|
||||
Guid id = Guid.NewGuid();
|
||||
TestEntity firstEntity = new(id);
|
||||
TestEntity secondEntity = new(id);
|
||||
|
||||
// Act
|
||||
int firstHashCode = firstEntity.GetHashCode();
|
||||
int secondHashCode = secondEntity.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(firstHashCode, secondHashCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreatedOnUtc_And_UpdatedOnUtc_WhenSet_ShouldBeStored()
|
||||
{
|
||||
// Arrange
|
||||
TestEntity entity = new(Guid.NewGuid());
|
||||
DateTime createdOnUtc = new(2024, 1, 1, 12, 0, 0, DateTimeKind.Utc);
|
||||
DateTime updatedOnUtc = new(2024, 1, 2, 12, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
// Act
|
||||
entity.CreatedOnUtc = createdOnUtc;
|
||||
entity.UpdatedOnUtc = updatedOnUtc;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(createdOnUtc, entity.CreatedOnUtc);
|
||||
Assert.Equal(updatedOnUtc, entity.UpdatedOnUtc);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete test implementation of the abstract <see cref="Entity{TId}"/> class.
|
||||
/// </summary>
|
||||
private sealed class TestEntity : Entity<Guid>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TestEntity"/> class.
|
||||
/// </summary>
|
||||
/// <param name="id">The id of the entity.</param>
|
||||
public TestEntity(Guid id) : base(id)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Models.Core;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Common.Models.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="ValueObject"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ValueObjectTests
|
||||
{
|
||||
[Fact]
|
||||
public void Equals_WithSameComponents_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
TestValueObject firstValueObject = new("value");
|
||||
TestValueObject secondValueObject = new("value");
|
||||
|
||||
// Act
|
||||
bool result = firstValueObject.Equals(secondValueObject);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentComponents_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
TestValueObject firstValueObject = new("first");
|
||||
TestValueObject secondValueObject = new("second");
|
||||
|
||||
// Act
|
||||
bool result = firstValueObject.Equals(secondValueObject);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentType_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
TestValueObject valueObject = new("value");
|
||||
OtherTestValueObject otherValueObject = new("value");
|
||||
|
||||
// Act
|
||||
bool result = valueObject.Equals(otherValueObject);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithNull_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
TestValueObject valueObject = new("value");
|
||||
|
||||
// Act
|
||||
bool result = valueObject.Equals(null);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EqualityOperator_WithSameComponents_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
TestValueObject firstValueObject = new("value");
|
||||
TestValueObject secondValueObject = new("value");
|
||||
|
||||
// Act
|
||||
bool result = firstValueObject == secondValueObject;
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InequalityOperator_WithDifferentComponents_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
TestValueObject firstValueObject = new("first");
|
||||
TestValueObject secondValueObject = new("second");
|
||||
|
||||
// Act
|
||||
bool result = firstValueObject != secondValueObject;
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithSameComponents_ShouldReturnSameHashCode()
|
||||
{
|
||||
// Arrange
|
||||
TestValueObject firstValueObject = new("value");
|
||||
TestValueObject secondValueObject = new("value");
|
||||
|
||||
// Act
|
||||
int firstHashCode = firstValueObject.GetHashCode();
|
||||
int secondHashCode = secondValueObject.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(firstHashCode, secondHashCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete test implementation of the abstract <see cref="ValueObject"/> class.
|
||||
/// </summary>
|
||||
private sealed class TestValueObject : ValueObject
|
||||
{
|
||||
private readonly string _value;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TestValueObject"/> class.
|
||||
/// </summary>
|
||||
/// <param name="value">The value of the value object.</param>
|
||||
public TestValueObject(string value)
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of items that define equality of the object.
|
||||
/// </summary>
|
||||
/// <returns>A list of items defining the equality.</returns>
|
||||
public override IEnumerable<object?> GetEqualityComponents()
|
||||
{
|
||||
yield return _value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete test implementation of the abstract <see cref="ValueObject"/> class, with a different type.
|
||||
/// </summary>
|
||||
private sealed class OtherTestValueObject : ValueObject
|
||||
{
|
||||
private readonly string _value;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="OtherTestValueObject"/> class.
|
||||
/// </summary>
|
||||
/// <param name="value">The value of the value object.</param>
|
||||
public OtherTestValueObject(string value)
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of items that define equality of the object.
|
||||
/// </summary>
|
||||
/// <returns>A list of items defining the equality.</returns>
|
||||
public override IEnumerable<object?> GetEqualityComponents()
|
||||
{
|
||||
yield return _value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user