Unit test improvements.

This commit is contained in:
2026-07-30 12:35:33 +02:00
parent fe9b68dba0
commit 8145ff1054
22 changed files with 283 additions and 160 deletions
@@ -1,56 +0,0 @@
using FinancialApi.Domain.Entities;
using FluentAssertions;
namespace FinancialApi.Application.UnitTests;
public class AccountTests
{
[Theory]
[InlineData(AccountType.Credit)]
public void CreateCreditAccountType_ShouldAllowNegativeBalance(AccountType accountType)
{
var act = () => new Account(1, -100, accountType);
act()
.Balance.Should()
.BeNegative();
}
[Theory]
[InlineData(AccountType.Debit)]
[InlineData(AccountType.Revenue)]
[InlineData(AccountType.Liability)]
public void CreateDebitAccountType_ShouldNotAllowNegativeBalance(AccountType accountType)
{
var act = () => new Account(1, -100, accountType);
act.Should()
.Throw<InvalidStartingBalanceException>();
}
[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>();
}
}
@@ -2,39 +2,15 @@
using FinancialApi.Application.Handlers;
using FinancialApi.Application.Models;
using FinancialApi.Domain.Entities;
using FinancialApi.Domain.Exceptions;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.Extensions.Time.Testing;
namespace FinancialApi.Application.UnitTests;
public class ApplyFeeHandlerTests
{
[Fact]
public void GivenValidAccounts_BasicFee_ShouldValidateEverything()
{
// Arrange
var command = new ApplyFeeCommand(1, 100);
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
var intents = ApplyFeeHandler.Handle(command, pair);
// Assert
using var _ = new AssertionScope();
intents.CustomerWrite.Entity.Balance.Should()
.Be(account.Balance - command.Amount);
intents.RevenueWrite.Entity.Balance.Should()
.Be(revenueAccount.Balance + command.Amount);
intents.JournalWrite.Entity.Lines.Count.Should()
.BeGreaterThanOrEqualTo(2);
intents.Event.AccountId.Should()
.Be(account.Id);
intents.Event.Amount.Should()
.Be(command.Amount);
}
public class LiabilityAccount
{
[Theory]
@@ -49,17 +25,18 @@ public class ApplyFeeHandlerTests
)
{
// Arrange
var timeProvider = new FakeTimeProvider();
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());
var feeContext = new FeeContext(account, revenueAccount, timeProvider.GetUtcNow());
// Act
var intents = ApplyFeeHandler.Handle(command, pair);
var (customerWrite, revenueWrite, journalWrite, @event) = ApplyFeeHandler.Handle(command, feeContext);
// Assert
using var _ = new AssertionScope();
intents.CustomerWrite.Entity.Balance.Should()
customerWrite.Entity.Balance.Should()
.Be(expectedBalance);
}
@@ -70,13 +47,15 @@ public class ApplyFeeHandlerTests
[InlineData(1000, 1999999)]
public void GivenStartingBalanceLessThanFee_ApplyFee_ShouldFail(decimal startingBalance, decimal feeAmount)
{
// Arrange
var timeProvider = new FakeTimeProvider();
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());
var feeContext = new FeeContext(account, revenueAccount, timeProvider.GetUtcNow());
// Act
var act = () => ApplyFeeHandler.Handle(command, pair);
var act = () => ApplyFeeHandler.Handle(command, feeContext);
// Assert
using var _ = new AssertionScope();
@@ -98,17 +77,19 @@ public class ApplyFeeHandlerTests
decimal expectedBalance
)
{
// Arrange
var timeProvider = new FakeTimeProvider();
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());
var feeContext = new FeeContext(account, revenueAccount, timeProvider.GetUtcNow());
// Act
var intents = ApplyFeeHandler.Handle(command, pair);
var (customerWrite, revenueWrite, journalWrite, @event) = ApplyFeeHandler.Handle(command, feeContext);
// Assert
using var _ = new AssertionScope();
intents.CustomerWrite.Entity.Balance.Should()
customerWrite.Entity.Balance.Should()
.Be(expectedBalance);
}
@@ -123,17 +104,19 @@ public class ApplyFeeHandlerTests
decimal expectedBalance
)
{
// Arrange
var timeProvider = new FakeTimeProvider();
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());
var feeContext = new FeeContext(account, revenueAccount, timeProvider.GetUtcNow());
// Act
var intents = ApplyFeeHandler.Handle(command, pair);
var (customerWrite, revenueWrite, journalWrite, @event) = ApplyFeeHandler.Handle(command, feeContext);
// Assert
using var _ = new AssertionScope();
intents.CustomerWrite.Entity.Balance.Should()
customerWrite.Entity.Balance.Should()
.Be(expectedBalance);
}
}
@@ -0,0 +1,67 @@
using FinancialApi.Application.Commands;
using FinancialApi.Application.Events;
using FinancialApi.Application.Handlers;
using FinancialApi.Application.Models;
using FinancialApi.Domain.Entities;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.Extensions.Time.Testing;
using Wolverine.Persistence;
namespace FinancialApi.Application.UnitTests;
public class ReverseJournalHandlerTests
{
[Fact]
public void GivenValidJournalEntry_Reversal_ShouldResetAccountBalances()
{
// Arrange
var timeProvider = new FakeTimeProvider();
var expectedSourceAccountBalance = 100.0m;
var expectedDestAccountBalance = 100.0m;
var (initialSourceAccountWrite, initialDestAccountWrite, initialJournalWrite, initialEvent) =
CreateInitialTransferTransaction(
expectedSourceAccountBalance,
expectedDestAccountBalance,
10,
timeProvider
);
var command = new ReverseJournalCommand(initialJournalWrite.Entity.Id, "Invalid Transaction");
var context = new JournalReversalContext(
initialJournalWrite.Entity,
[initialSourceAccountWrite.Entity, initialDestAccountWrite.Entity],
timeProvider.GetUtcNow()
);
// Act
var (journalWrite, accountWrites, @event) = ReverseJournalHandler.Handle(command, context);
// Assert
using var _ = new AssertionScope();
accountWrites[0]
.Entity.Balance.Should()
.Be(expectedSourceAccountBalance);
accountWrites[1]
.Entity.Balance.Should()
.Be(expectedDestAccountBalance);
journalWrite.Entity.Lines.Should()
.HaveCount(initialJournalWrite.Entity.Lines.Count);
}
private (IStorageAction<Account> SourceWrite, IStorageAction<Account> DestWrite, IStorageAction<BalancedJournalEntry>
JournalWrite, FundsTransferredEvent Event) CreateInitialTransferTransaction(
decimal sourceStartingBalance,
decimal destStartingBalance,
decimal transferAmount,
TimeProvider timeProvider
)
{
var command = new TransferFundsCommand(1, 2, transferAmount);
var sourceAccount = new Account(1, sourceStartingBalance, AccountType.Liability);
var destAccount = new Account(2, destStartingBalance, AccountType.Liability);
var transferContext = new TransferContext(sourceAccount, destAccount, timeProvider.GetUtcNow());
// Act
return TransferFundsHandler.Handle(command, transferContext);
}
}
@@ -1,37 +1,38 @@
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);
}
}
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 TransferFundsHandlerTests
{
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 (sourceWrite, destWrite, journalWrite, @event) = TransferFundsHandler.Handle(command, transferContext);
// Assert
using var _ = new AssertionScope();
sourceWrite.Entity.Balance.Should()
.Be(0);
destWrite.Entity.Balance.Should()
.Be(200);
journalWrite.Entity.Lines.Should()
.HaveCountGreaterThanOrEqualTo(2);
}
}
}