first commit

This commit is contained in:
2026-02-26 14:04:18 +07:00
parent 57ac80a666
commit 4b7236493f
92 changed files with 4999 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
using FluentAssertions;
using FluentValidation.TestHelper;
using MyNewProjectName.Application.Features.Sample.Commands.CreateSample;
namespace MyNewProjectName.UnitTest.Application;
public class CreateSampleCommandValidatorTests
{
private readonly CreateSampleCommandValidator _validator;
public CreateSampleCommandValidatorTests()
{
_validator = new CreateSampleCommandValidator();
}
[Fact]
public void Validate_WithValidCommand_ShouldNotHaveErrors()
{
// Arrange
var command = new CreateSampleCommand("Test Name", "Test Description");
// Act
var result = _validator.TestValidate(command);
// Assert
result.ShouldNotHaveAnyValidationErrors();
}
[Fact]
public void Validate_WithEmptyName_ShouldHaveError()
{
// Arrange
var command = new CreateSampleCommand("", "Test Description");
// Act
var result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Name);
}
[Fact]
public void Validate_WithNameExceeding200Characters_ShouldHaveError()
{
// Arrange
var longName = new string('a', 201);
var command = new CreateSampleCommand(longName, "Test Description");
// Act
var result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Name);
}
[Fact]
public void Validate_WithDescriptionExceeding1000Characters_ShouldHaveError()
{
// Arrange
var longDescription = new string('a', 1001);
var command = new CreateSampleCommand("Test Name", longDescription);
// Act
var result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Description);
}
[Fact]
public void Validate_WithNullDescription_ShouldNotHaveError()
{
// Arrange
var command = new CreateSampleCommand("Test Name", null);
// Act
var result = _validator.TestValidate(command);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Description);
}
}

View File

@@ -0,0 +1,59 @@
using FluentAssertions;
using MyNewProjectName.Domain.Common;
namespace MyNewProjectName.UnitTest.Domain;
public class BaseEntityTests
{
private class TestEntity : BaseEntity { }
private class TestDomainEvent : IDomainEvent
{
public DateTime OccurredOn { get; } = DateTime.UtcNow;
}
[Fact]
public void AddDomainEvent_ShouldAddEventToCollection()
{
// Arrange
var entity = new TestEntity();
var domainEvent = new TestDomainEvent();
// Act
entity.AddDomainEvent(domainEvent);
// Assert
entity.DomainEvents.Should().Contain(domainEvent);
entity.DomainEvents.Should().HaveCount(1);
}
[Fact]
public void RemoveDomainEvent_ShouldRemoveEventFromCollection()
{
// Arrange
var entity = new TestEntity();
var domainEvent = new TestDomainEvent();
entity.AddDomainEvent(domainEvent);
// Act
entity.RemoveDomainEvent(domainEvent);
// Assert
entity.DomainEvents.Should().BeEmpty();
}
[Fact]
public void ClearDomainEvents_ShouldRemoveAllEvents()
{
// Arrange
var entity = new TestEntity();
entity.AddDomainEvent(new TestDomainEvent());
entity.AddDomainEvent(new TestDomainEvent());
// Act
entity.ClearDomainEvents();
// Assert
entity.DomainEvents.Should().BeEmpty();
}
}

View File

@@ -0,0 +1,60 @@
using FluentAssertions;
using MyNewProjectName.Domain.ValueObjects;
namespace MyNewProjectName.UnitTest.Domain;
public class ValueObjectTests
{
private class TestValueObject : ValueObject
{
public string Value1 { get; }
public int Value2 { get; }
public TestValueObject(string value1, int value2)
{
Value1 = value1;
Value2 = value2;
}
protected override IEnumerable<object?> GetEqualityComponents()
{
yield return Value1;
yield return Value2;
}
}
[Fact]
public void ValueObjects_WithSameValues_ShouldBeEqual()
{
// Arrange
var vo1 = new TestValueObject("test", 123);
var vo2 = new TestValueObject("test", 123);
// Act & Assert
vo1.Should().Be(vo2);
(vo1 == vo2).Should().BeTrue();
}
[Fact]
public void ValueObjects_WithDifferentValues_ShouldNotBeEqual()
{
// Arrange
var vo1 = new TestValueObject("test", 123);
var vo2 = new TestValueObject("test", 456);
// Act & Assert
vo1.Should().NotBe(vo2);
(vo1 != vo2).Should().BeTrue();
}
[Fact]
public void GetHashCode_ShouldBeSameForEqualObjects()
{
// Arrange
var vo1 = new TestValueObject("test", 123);
var vo2 = new TestValueObject("test", 123);
// Act & Assert
vo1.GetHashCode().Should().Be(vo2.GetHashCode());
}
}

View File

@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="FluentAssertions" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyNewProjectName.Domain\MyNewProjectName.Domain.csproj" />
<ProjectReference Include="..\MyNewProjectName.Application\MyNewProjectName.Application.csproj" />
<ProjectReference Include="..\MyNewProjectName.Infrastructure\MyNewProjectName.Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>