feat: Füge BaseDto-Datensatz mit überschriebenem GetHashCode

This commit is contained in:
Developer 02 2024-06-06 15:17:22 +02:00
parent 5b21f7f208
commit 419974ba12
5 changed files with 101 additions and 19 deletions

View File

@ -0,0 +1,17 @@
namespace DigitalData.Core.DTO
{
/// <summary>
/// Represents a base Data Transfer Object (DTO) with an identifier.
/// </summary>
/// <typeparam name="TId">The type of the identifier.</typeparam>
/// <param name="Id">The identifier of the DTO.</param>
public record BaseDto<TId>(TId Id) where TId : notnull
{
/// <summary>
/// Returns the hash code for this instance, based on the identifier.
/// This override ensures that the hash code is derived consistently from the identifier.
/// </summary>
/// <returns>A hash code for the current object, derived from the identifier.</returns>
public override int GetHashCode() => Id.GetHashCode();
}
}

View File

@ -1,18 +0,0 @@
namespace DigitalData.Core.Tests
{
[TestFixture]
public class ADServiceTests
{
[SetUp]
public void Setup()
{
}
[Test]
public void ValidateUser_CorrectCredentials_ReturnsTrue()
{
}
}
}

View File

@ -0,0 +1,79 @@
using DigitalData.Core.DTO;
namespace DigitalData.Core.Tests.DTO
{
public class BaseDtoTest
{
public record SampleDto(int Id) : BaseDto<int>(Id);
[Test]
public void EntitiesWithSameIdShouldBeEqual()
{
var dto1 = new SampleDto(1);
var dto2 = new SampleDto(1);
Assert.That(dto2, Is.EqualTo(dto1));
}
[Test]
public void EntitiesWithDifferentIdsShouldNotBeEqual()
{
var dto1 = new SampleDto(1);
var dto2 = new SampleDto(2);
Assert.That(dto2, Is.Not.EqualTo(dto1));
}
[Test]
public void GetHashCodeShouldReturnSameValueForEntitiesWithSameId()
{
var dto1 = new SampleDto(1);
var dto2 = new SampleDto(1);
Assert.That(dto2.GetHashCode(), Is.EqualTo(dto1.GetHashCode()));
}
[Test]
public void EqualsShouldReturnFalseForNull()
{
var dto = new SampleDto(1);
Assert.IsFalse(dto.Equals(null));
}
[Test]
public void EqualityOperatorShouldReturnTrueForSameReference()
{
var dto = new SampleDto(1);
Assert.That(dto, Is.EqualTo(dto));
}
[Test]
public void EqualityOperatorShouldReturnFalseForDifferentEntitiesWithDifferentIds()
{
var dto1 = new SampleDto(1);
var dto2 = new SampleDto(2);
Assert.IsFalse(dto1 == dto2);
}
[Test]
public void InequalityOperatorShouldReturnTrueForDifferentEntitiesWithDifferentIds()
{
var dto1 = new SampleDto(1);
var dto2 = new SampleDto(2);
Assert.IsTrue(dto1 != dto2);
}
[Test]
public void InequalityOperatorShouldReturnFalseForEntitiesWithSameId()
{
var dto1 = new SampleDto(1);
var dto2 = new SampleDto(1);
Assert.IsFalse(dto1 != dto2);
}
}
}

View File

@ -18,4 +18,8 @@
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DigitalData.Core.DTO\DigitalData.Core.DTO.csproj" />
</ItemGroup>
</Project>

View File

@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.API", "Dig
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Tests", "DigitalData.Core.Tests\DigitalData.Core.Tests.csproj", "{B54DEF90-C30C-44EA-9875-76F1B330CBB7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.DTO", "DigitalData.Core.DTO\DigitalData.Core.DTO.csproj", "{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.DTO", "DigitalData.Core.DTO\DigitalData.Core.DTO.csproj", "{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution