📄 OrderServiceTests.cs
using FluentAssertions;
using Xunit;
using Walley.Checkout.Api.Models;
using Walley.Checkout.Api.Services;

namespace Walley.Checkout.Api.Tests;

public class OrderServiceTests
{
    private readonly OrderService _sut;

    public OrderServiceTests()
    {
        _sut = new OrderService();
    }

    [Fact]
    public async Task GetAllOrdersAsync_ShouldReturnAllOrders()
    {
        // Act
        var orders = await _sut.GetAllOrdersAsync();

        // Assert
        orders.Should().HaveCount(3);
    }

    [Fact]
    public async Task GetOrderByIdAsync_ShouldReturnTheSoughtOrder()
    {
        // Act
        var order = await _sut.GetOrderByIdAsync("ORD-001");

        // Assert
        order.Should().NotBeNull();
        order!.Id.Should().Be("ORD-001");
    }

    [Fact]
    public async Task GetOrderByIdAsync_ShouldReturnNull_WhenTheSoughtOrderIsNotFound()
    {
        // Act
        var order = await _sut.GetOrderByIdAsync("ORD-004");

        // Assert
        order.Should().BeNull();
    }

    [Fact]
    public async Task CreateOrderAsync_ShouldSetCalculatedValuesOnTheOrder()
    {
        // Arrange
        var order = new Order
        {
            CustomerName = "A.N. Other",
            CustomerEmail = "a.n.other@example.com",
            Currency = "SEK",
            Lines = new List<OrderLine>
            {
                new() { ProductName = "Product A", Quantity = 2, UnitPrice = 3m },
                new() { ProductName = "Product B", Quantity = 1, UnitPrice = 6m },
            }
        };

        // Act
        order = await _sut.CreateOrderAsync(order);

        // Assert
        order.Id.Should().NotBeEmpty();
        order.CreatedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(1));
        order.Status.Should().Be(OrderStatus.Pending);
        order.TotalAmount.Should().Be(12m);
    }

    [Fact]
    public async Task CreateOrderAsync_ShouldPersistTheOrder()
    {
        // Arrange
        var order = new Order
        {
            CustomerName = "A.N. Other",
            CustomerEmail = "a.n.other@example.com",
            Currency = "SEK",
            Lines = new List<OrderLine>
            {
                new() { ProductName = "Product A", Quantity = 2, UnitPrice = 3m },
                new() { ProductName = "Product B", Quantity = 1, UnitPrice = 6m },
            }
        };

        // Act
        order = await _sut.CreateOrderAsync(order);

        // Assert
        var persistedOrder = await _sut.GetOrderByIdAsync(order.Id);
        persistedOrder.Should().NotBeNull();
    }
}