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
60 lines
2.4 KiB
C#
60 lines
2.4 KiB
C#
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);
|
|
}
|
|
}
|