feat: Füge BaseDto-Datensatz mit überschriebenem GetHashCode
This commit is contained in:
parent
5b21f7f208
commit
419974ba12
17
DigitalData.Core.DTO/BaseDto.cs
Normal file
17
DigitalData.Core.DTO/BaseDto.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
namespace DigitalData.Core.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class ADServiceTests
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValidateUser_CorrectCredentials_ReturnsTrue()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
79
DigitalData.Core.Tests/DTO/BaseDtoTest.cs
Normal file
79
DigitalData.Core.Tests/DTO/BaseDtoTest.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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>
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user