Exercise 2 solution: OrderLines in the domain, edges unchanged
OrderLine + computed TotalInCents; both inbound mappers reconstruct a one-line order from the flat DTO/row (derived data is re-derived, not written); builder realises WithTotal as a single line. Outbound mappers, DTO, OrderData, DbContext and WebApp untouched — the small-steps payoff. yak: exercise-2-solution-on-branch-exercise-2-solution-hw7e
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using After;
|
||||
using static BeforeAfter.Tests.TestData.ClientBuilder;
|
||||
using static BeforeAfter.Tests.TestData.OrderBuilder;
|
||||
|
||||
namespace BeforeAfter.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// EXERCISE 2 — the domain gained OrderLines; persistence still stores one
|
||||
/// flat row per order. (This is the solution participants arrive at from the
|
||||
/// stub in EXERCISE2.md.)
|
||||
/// </summary>
|
||||
public class OrderLinesEvolutionTests
|
||||
{
|
||||
[Fact] // EXERCISE STEP 3a
|
||||
public void A_reloaded_order_can_be_discounted()
|
||||
{
|
||||
var db = new DbContext();
|
||||
var order = ValidOrder().WithDescription("big order").WithTotal(150_00).Build();
|
||||
var client = ValidClient().WithName("Acme").WithOrder(order).Build();
|
||||
db.Save(client);
|
||||
db.Save(order);
|
||||
|
||||
var loaded = db.LoadOrder(order.Id)!;
|
||||
|
||||
// The discount works off the computed total of the RELOADED order:
|
||||
var discounted = DiscountCalculator.CalculateDiscount(
|
||||
loaded, discountBarrierCents: 100_00, discountPercentage: 10);
|
||||
|
||||
Assert.Equal(135_00, discounted.DiscountedTotalInCents);
|
||||
Assert.Equal(10, discounted.DiscountPercentage);
|
||||
}
|
||||
|
||||
[Fact] // EXERCISE STEP 3b — the accepted loss
|
||||
public void A_multi_line_order_flattens_to_a_single_line_when_stored()
|
||||
{
|
||||
var db = new DbContext();
|
||||
var order = ValidOrder().WithDescription("mixed order").Build();
|
||||
order.Lines.Add(new OrderLine("first item", 100_00));
|
||||
order.Lines.Add(new OrderLine("second item", 50_00));
|
||||
|
||||
db.Save(order);
|
||||
|
||||
var loaded = db.LoadOrder(order.Id)!;
|
||||
|
||||
// Storage flattened the order: one line, the flat total, and the
|
||||
// order-level description. Documented, not hidden.
|
||||
var line = Assert.Single(loaded.Lines);
|
||||
Assert.Equal(150_00, line.AmountInCents);
|
||||
Assert.Equal("mixed order", line.Description);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user