test(domain): add Entity<TId> and ValueObject equality tests
This commit is contained in:
1 parent
0f2d556c26
commit
c02ebe460e
2 files changed
+175
No files matched your search
@@ -0,0 +1,81 @@
|
||||
using Heimdall.Domain.Primitives;
|
||||
using Shouldly;
|
||||
|
||||
namespace Heimdall.Domain.Tests.Primitives;
|
||||
|
||||
public class EntityTests
|
||||
{
|
||||
private sealed class TestEntity : Entity<Guid>
|
||||
{
|
||||
public TestEntity(Guid id) : base(id) { }
|
||||
private TestEntity() { } // EF Core
|
||||
}
|
||||
|
||||
private sealed class OtherEntity : Entity<Guid>
|
||||
{
|
||||
public OtherEntity(Guid id) : base(id) { }
|
||||
private OtherEntity() { }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Same_id_entities_should_be_equal()
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
var e1 = new TestEntity(id);
|
||||
var e2 = new TestEntity(id);
|
||||
|
||||
e1.Equals(e2).ShouldBeTrue();
|
||||
(e1 == e2).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Different_id_entities_should_not_be_equal()
|
||||
{
|
||||
var e1 = new TestEntity(Guid.NewGuid());
|
||||
var e2 = new TestEntity(Guid.NewGuid());
|
||||
|
||||
e1.Equals(e2).ShouldBeFalse();
|
||||
(e1 != e2).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Same_id_should_have_same_hash_code()
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
var e1 = new TestEntity(id);
|
||||
var e2 = new TestEntity(id);
|
||||
|
||||
e1.GetHashCode().ShouldBe(e2.GetHashCode());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Different_entity_types_with_same_id_should_be_equal_by_id()
|
||||
{
|
||||
// DDD convention: entity equality is based on ID alone, not type
|
||||
var id = Guid.NewGuid();
|
||||
var testEntity = new TestEntity(id);
|
||||
var otherEntity = new OtherEntity(id);
|
||||
|
||||
testEntity.Equals(otherEntity).ShouldBeTrue();
|
||||
testEntity.GetHashCode().ShouldBe(otherEntity.GetHashCode());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Entity_should_not_be_equal_to_null()
|
||||
{
|
||||
var entity = new TestEntity(Guid.NewGuid());
|
||||
|
||||
entity.Equals(null).ShouldBeFalse();
|
||||
(entity == null!).ShouldBeFalse();
|
||||
(null! == entity).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Two_null_entities_should_be_equal()
|
||||
{
|
||||
TestEntity? e1 = null;
|
||||
TestEntity? e2 = null;
|
||||
|
||||
(e1 == e2).ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using Heimdall.Domain.Primitives;
|
||||
using Shouldly;
|
||||
|
||||
namespace Heimdall.Domain.Tests.Primitives;
|
||||
|
||||
public class ValueObjectTests
|
||||
{
|
||||
private sealed class Address : ValueObject
|
||||
{
|
||||
public string Street { get; }
|
||||
public string City { get; }
|
||||
public string ZipCode { get; }
|
||||
|
||||
public Address(string street, string city, string zipCode)
|
||||
{
|
||||
Street = street;
|
||||
City = city;
|
||||
ZipCode = zipCode;
|
||||
}
|
||||
|
||||
protected override IEnumerable<object> GetEqualityComponents()
|
||||
{
|
||||
yield return Street;
|
||||
yield return City;
|
||||
yield return ZipCode;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FullName : ValueObject
|
||||
{
|
||||
public string FirstName { get; }
|
||||
public string LastName { get; }
|
||||
|
||||
public FullName(string firstName, string lastName)
|
||||
{
|
||||
FirstName = firstName;
|
||||
LastName = lastName;
|
||||
}
|
||||
|
||||
protected override IEnumerable<object> GetEqualityComponents()
|
||||
{
|
||||
yield return FirstName;
|
||||
yield return LastName;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Same_values_should_be_equal()
|
||||
{
|
||||
var a1 = new Address("123 Main St", "Paris", "75001");
|
||||
var a2 = new Address("123 Main St", "Paris", "75001");
|
||||
|
||||
a1.Equals(a2).ShouldBeTrue();
|
||||
(a1 == a2).ShouldBeTrue();
|
||||
a1.GetHashCode().ShouldBe(a2.GetHashCode());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Different_values_should_not_be_equal()
|
||||
{
|
||||
var a1 = new Address("123 Main St", "Paris", "75001");
|
||||
var a2 = new Address("456 Oak Ave", "Lyon", "69001");
|
||||
|
||||
a1.Equals(a2).ShouldBeFalse();
|
||||
(a1 != a2).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Different_types_should_not_be_equal()
|
||||
{
|
||||
var address = new Address("123 Main St", "Paris", "75001");
|
||||
var name = new FullName("John", "Doe");
|
||||
|
||||
address.Equals(name).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Null_should_not_be_equal()
|
||||
{
|
||||
var address = new Address("123 Main St", "Paris", "75001");
|
||||
|
||||
address.Equals(null).ShouldBeFalse();
|
||||
(address == null!).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Partial_different_values_should_not_be_equal()
|
||||
{
|
||||
var a1 = new Address("123 Main St", "Paris", "75001");
|
||||
var a2 = new Address("123 Main St", "Paris", "75002");
|
||||
|
||||
a1.Equals(a2).ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user