identity read-back: FromOrderData/FromClientData + DbContext.LoadOrder/LoadClient

Plain field-copy materialization so the persistence round-trip closes.
When OrderLines arrive, FromOrderData becomes the one-line reconstruction
(row description + total become a single line) — the hinge Exercise 2
pivots on.

yak: orderdata-read-back-identity-version-j9ew [order-data-mapping]
This commit is contained in:
2026-09-16 22:17:28 +01:00
parent 37ebb21182
commit 53586d33a4
3 changed files with 122 additions and 0 deletions
@@ -88,4 +88,61 @@ public class AfterPersistenceTests
Assert.Equal(order.Id, found!.Id);
Assert.Null(db.Find<OrderData>(row => row.Description == "no such order"));
}
// ---------------------------------------------------------------------
// Read-back: LoadOrder / LoadClient reassemble domain objects from the
// stored rows (identity round-trip for now — OrderLines come later).
// ---------------------------------------------------------------------
[Fact]
public void Loading_an_order_restores_its_state_and_its_client()
{
var db = new DbContext();
var client = new Client { Name = "Acme" };
var order = new Order { Description = "one", TotalInCents = 150_00 };
client.Orders.Add(order);
db.Save(client);
db.Save(order);
var loaded = db.LoadOrder(order.Id);
Assert.NotNull(loaded);
Assert.Equal(order.Id, loaded!.Id);
Assert.Equal("one", loaded.Description);
Assert.Equal(150_00, loaded.TotalInCents);
// The graph is reassembled: the loaded order knows its client again,
// and the client's collection holds the loaded order.
Assert.NotNull(loaded.Client);
Assert.Equal("Acme", loaded.Client!.Name);
Assert.Same(loaded, Assert.Single(loaded.Client.Orders));
}
[Fact]
public void Loading_a_client_restores_it_with_all_of_its_orders()
{
var db = new DbContext();
var client = new Client { Name = "Acme" };
client.Orders.Add(new Order { Description = "one", TotalInCents = 150_00 });
client.Orders.Add(new Order { Description = "two", TotalInCents = 250_00 });
db.Save(client);
db.Save(client.Orders[0]);
db.Save(client.Orders[1]);
var loaded = db.LoadClient(client.Id);
Assert.NotNull(loaded);
Assert.Equal("Acme", loaded!.Name);
Assert.Equal(2, loaded.Orders.Count);
Assert.All(loaded.Orders, o => Assert.Same(loaded, o.Client));
}
[Fact]
public void Loading_an_unknown_id_returns_null()
{
var db = new DbContext();
Assert.Null(db.LoadOrder(Guid.NewGuid()));
Assert.Null(db.LoadClient(Guid.NewGuid()));
}
}