Exercise 1 solution: DiscountedTotalInCents on the domain order + integration tests

Domain-only field: OrderDto and OrderData stay without it; the stored row
keeps the original total after saving a discounted order. The discount
percentage is not copied (derivable).

yak: exercise-1-solution-on-branch-exercise-1-solution-4s5q
This commit is contained in:
2026-09-16 22:32:35 +01:00
parent 08b52806de
commit 89a1158c93
2 changed files with 68 additions and 0 deletions
+9
View File
@@ -24,6 +24,15 @@ public class Order
/// </summary> /// </summary>
public int TotalInCents { get; set; } public int TotalInCents { get; set; }
/// <summary>
/// The discounted total in cents, copied onto the order by the caller of
/// the discount flow (see <see cref="DiscountCalculator"/>). Derived
/// data: it lives in the domain only — neither <see cref="OrderDto"/>
/// nor <see cref="OrderData"/> carries it — and the applied percentage
/// is not stored either, being derivable from the two totals.
/// </summary>
public int DiscountedTotalInCents { get; set; }
/// <summary> /// <summary>
/// The client that owns this order. Set automatically by /// The client that owns this order. Set automatically by
/// <see cref="ClientOrders"/> when the order is added to /// <see cref="ClientOrders"/> when the order is added to
@@ -0,0 +1,59 @@
using After;
using static BeforeAfter.Tests.TestData.ClientBuilder;
using static BeforeAfter.Tests.TestData.OrderBuilder;
namespace BeforeAfter.Tests;
/// <summary>
/// EXERCISE — the mapping from a <see cref="DiscountedOrder"/> onto an
/// <see cref="Order"/> is NOT one-on-one. (This is the solution participants
/// arrive at from the stub in EXERCISE.md.)
/// </summary>
public class DiscountedOrderMappingTests
{
[Fact] // EXERCISE STEP 2
public void A_discounted_order_can_be_correlated_onto_its_order()
{
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);
// 10 % for orders above €100:
var discounted = DiscountCalculator.CalculateDiscount(
order, discountBarrierCents: 100_00, discountPercentage: 10);
// Correlate: the discounted result belongs to the order carrying this id.
Assert.Equal(order.Id, discounted.OrderId);
// Copy the discounted total onto the order. The discount percentage is
// deliberately NOT copied — it is derivable from the two totals.
order.DiscountedTotalInCents = discounted.DiscountedTotalInCents;
Assert.Equal(135_00, order.DiscountedTotalInCents);
}
[Fact] // EXERCISE STEP 3
public void Saving_a_discounted_order_keeps_the_stored_total_untouched()
{
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 discounted = DiscountCalculator.CalculateDiscount(
order, discountBarrierCents: 100_00, discountPercentage: 10);
Assert.Equal(order.Id, discounted.OrderId);
order.DiscountedTotalInCents = discounted.DiscountedTotalInCents;
// Save the updated order: the context refreshes the stored row in place.
db.Save(order);
// The persisted record keeps the ORIGINAL total: the database knows
// nothing about discounts — derived data lives in the domain only.
var row = db.Find<OrderData>(candidate => candidate.Id == discounted.OrderId)!;
Assert.Equal(150_00, row.TotalInCents);
}
}