Improve testing and some cleanup refactoring.

This commit is contained in:
2026-07-29 11:07:12 +02:00
parent 249a166d07
commit 44846eaa13
15 changed files with 253 additions and 96 deletions
@@ -0,0 +1,33 @@
using FinancialApi.Domain.Entities;
using FluentAssertions;
namespace FinancialApi.Application.UnitTests;
public class AccountTests
{
[Theory]
[InlineData(AccountType.Credit)]
public void GivenCreditAccountType_ShouldAllowNegativeBalance(AccountType accountType)
{
var account = new Account(1, 0, accountType);
var updatedAccount = account.ApplyPosting(100, EntryType.Debit);
updatedAccount.Balance.Should()
.BeNegative();
}
[Theory]
[InlineData(AccountType.Debit)]
[InlineData(AccountType.Revenue)]
[InlineData(AccountType.Liability)]
public void GivenDebitAccountType_ShouldNotAllowNegativeBalance(AccountType accountType)
{
var account = new Account(1, 0, accountType);
var act = () => account.ApplyPosting(100, EntryType.Debit);
act.Should()
.Throw<InsufficientFundsException>();
}
}
@@ -16,8 +16,8 @@ public class ApplyFeeHandlerTests
{
// Arrange
var command = new ApplyFeeCommand(1, 100);
var account = new Account(1, 100, "Debit");
var revenueAccount = new Account(99999, 0, "Credit");
var account = new Account(1, 100, AccountType.Debit);
var revenueAccount = new Account(99999, 0, AccountType.Credit);
var pair = new FeeContext(account, revenueAccount, TimeProvider.System.GetUtcNow());
// Act
@@ -36,4 +36,110 @@ public class ApplyFeeHandlerTests
intents.Event.Amount.Should()
.Be(command.Amount);
}
public class LiabilityAccount
{
[Theory]
[InlineData(100.0, 20.0, 80.0)]
[InlineData(199.78, 10.0, 189.78)]
[InlineData(200000, 0.09, 199999.91)]
[InlineData(1000, 1000, 0)]
public void GivenStartingBalanceGreaterThanFee_ApplyFee_ShouldBeExpectedBalance(
decimal startingBalance,
decimal feeAmount,
decimal expectedBalance
)
{
var command = new ApplyFeeCommand(1, feeAmount);
var account = new Account(1, startingBalance, AccountType.Liability);
var revenueAccount = new Account(99999, 0, AccountType.Credit);
var pair = new FeeContext(account, revenueAccount, TimeProvider.System.GetUtcNow());
// Act
var intents = ApplyFeeHandler.Handle(command, pair);
// Assert
using var _ = new AssertionScope();
intents.CustomerWrite.Entity.Balance.Should()
.Be(expectedBalance);
}
[Theory]
[InlineData(100.0, 110.0)]
[InlineData(199.78, 200.0)]
[InlineData(200000, 200000.01)]
[InlineData(1000, 1999999)]
public void GivenStartingBalanceLessThanFee_ApplyFee_ShouldFail(decimal startingBalance, decimal feeAmount)
{
var command = new ApplyFeeCommand(1, feeAmount);
var account = new Account(1, startingBalance, AccountType.Liability);
var revenueAccount = new Account(99999, 0, AccountType.Credit);
var pair = new FeeContext(account, revenueAccount, TimeProvider.System.GetUtcNow());
// Act
var act = () => ApplyFeeHandler.Handle(command, pair);
// Assert
using var _ = new AssertionScope();
act.Should()
.Throw<InsufficientFundsException>();
account.Balance.Should()
.Be(startingBalance);
revenueAccount.Balance.Should()
.Be(0);
}
}
public class CreditAccount
{
[Theory]
[InlineData(100.0, 20.0, 80.0)]
[InlineData(199.78, 10.0, 189.78)]
[InlineData(200000, 0.09, 199999.91)]
[InlineData(1000, 1000, 0)]
public void GivenStartingBalanceGreaterThanFee_ApplyFee_ShouldBeExpectedBalance(
decimal startingBalance,
decimal feeAmount,
decimal expectedBalance
)
{
var command = new ApplyFeeCommand(1, feeAmount);
var account = new Account(1, startingBalance, AccountType.Credit);
var revenueAccount = new Account(99999, 0, AccountType.Credit);
var pair = new FeeContext(account, revenueAccount, TimeProvider.System.GetUtcNow());
// Act
var intents = ApplyFeeHandler.Handle(command, pair);
// Assert
using var _ = new AssertionScope();
intents.CustomerWrite.Entity.Balance.Should()
.Be(expectedBalance);
}
[Theory]
[InlineData(100.0, 110.0, -10.0)]
[InlineData(199.78, 200.0, -0.22)]
[InlineData(200000, 200000.01, -0.01)]
[InlineData(1000, 1999999, -1998999)]
public void GivenStartingBalanceLessThanFee_ApplyFee_ShouldBeExpectedBalance(
decimal startingBalance,
decimal feeAmount,
decimal expectedBalance
)
{
var command = new ApplyFeeCommand(1, feeAmount);
var account = new Account(1, startingBalance, AccountType.Credit);
var revenueAccount = new Account(99999, 0, AccountType.Credit);
var pair = new FeeContext(account, revenueAccount, TimeProvider.System.GetUtcNow());
// Act
var intents = ApplyFeeHandler.Handle(command, pair);
// Assert
using var _ = new AssertionScope();
intents.CustomerWrite.Entity.Balance.Should()
.Be(expectedBalance);
}
}
}
@@ -0,0 +1,37 @@
using FinancialApi.Application.Commands;
using FinancialApi.Application.Handlers;
using FinancialApi.Application.Models;
using FinancialApi.Domain.Entities;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.Extensions.Time.Testing;
namespace FinancialApi.Application.UnitTests;
public class ApplyTransferHandlerTests
{
public class ApplyFeeHandlerTests
{
[Fact]
public void GivenValidAccounts_BasicFee_ShouldValidateEverything()
{
// Arrange
var fakeTimeProvider = new FakeTimeProvider();
var command = new TransferFundsCommand(1, 2, 100);
var sourceAccount = new Account(1, 100, AccountType.Liability);
var destAccount = new Account(1, 100, AccountType.Liability);
var transferContext = new TransferContext(sourceAccount, destAccount, fakeTimeProvider.GetUtcNow());
// Act
var intents = TransferFundsHandler.Handle(command, transferContext);
// Assert
using var _ = new AssertionScope();
intents.SourceWrite.Entity.Balance.Should()
.Be(0);
intents.DestWrite.Entity.Balance.Should()
.Be(200);
}
}
}
@@ -9,9 +9,9 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4"/>
<PackageReference Include="FluentAssertions" Version="8.10.0" />
<PackageReference Include="JasperFx" Version="2.27.0" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="10.6.0" />
<PackageReference Include="FluentAssertions" Version="8.10.0"/>
<PackageReference Include="JasperFx" Version="2.27.0"/>
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="10.6.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1"/>
<PackageReference Include="xunit" Version="2.9.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4"/>
@@ -22,13 +22,13 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FinancialApi.Application\FinancialApi.Application.csproj" />
<ProjectReference Include="..\FinancialApi.Application\FinancialApi.Application.csproj"/>
</ItemGroup>
<ItemGroup>
<Reference Include="Wolverine">
<HintPath>..\..\..\..\..\..\.nuget\packages\wolverinefx\6.18.0\lib\net10.0\Wolverine.dll</HintPath>
</Reference>
<Reference Include="Wolverine">
<HintPath>..\..\..\..\..\..\.nuget\packages\wolverinefx\6.18.0\lib\net10.0\Wolverine.dll</HintPath>
</Reference>
</ItemGroup>
</Project>