parent item mutations
This commit is contained in:
@@ -38,3 +38,33 @@ func TestAddItem_MergesByItemIdNotSku(t *testing.T) {
|
||||
t.Fatalf("items = %d, want 2", len(g.Items))
|
||||
}
|
||||
}
|
||||
|
||||
// Re-adding the same parent (same ItemId + StoreId, no ParentId on the
|
||||
// re-add itself) merges the quantity, and the proportional cascade resizes
|
||||
// every direct child to match the new parent qty so a "+1 drill" leaves
|
||||
// the bundle internally consistent.
|
||||
func TestAddItem_MergeCascadesToChildren(t *testing.T) {
|
||||
reg := NewCartMultationRegistry(NewCartMutationContext(nil))
|
||||
g := NewCartGrain(1, time.Now())
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 100, Sku: "P", Quantity: 1, Price: 1000}); err != nil {
|
||||
t.Fatalf("add parent: %v", err)
|
||||
}
|
||||
parentLine := g.Items[0].Id
|
||||
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 101, Sku: "C1", Quantity: 1, Price: 100, ParentId: &parentLine}); err != nil {
|
||||
t.Fatalf("add child: %v", err)
|
||||
}
|
||||
|
||||
// Re-add the same parent — same ItemId, no ParentId on the re-add itself.
|
||||
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 100, Sku: "P", Quantity: 1, Price: 1000}); err != nil {
|
||||
t.Fatalf("re-add parent: %v", err)
|
||||
}
|
||||
|
||||
// Parent qty 1 -> 2; child qty 1 -> 2 via cascade.
|
||||
for _, it := range g.Items {
|
||||
if it.Quantity != 2 {
|
||||
t.Errorf("line %s qty = %d, want 2 (merge cascade)", it.Sku, it.Quantity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user