71 lines
2.5 KiB
Go
71 lines
2.5 KiB
Go
package cart
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.k6n.net/mats/go-cart-actor/pkg/actor"
|
|
cart_messages "git.k6n.net/mats/go-cart-actor/proto/cart"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// Removing a parent line cascades to its child sub-articles, while unrelated
|
|
// lines are left untouched.
|
|
func TestRemoveItem_CascadesToChildren(t *testing.T) {
|
|
reg := NewCartMultationRegistry(NewCartMutationContext(nil))
|
|
g := NewCartGrain(1, time.Now())
|
|
ctx := context.Background()
|
|
|
|
// Parent (line 1) + two children (lines 2,3) + an unrelated item (line 4).
|
|
mustApply(t, reg, g, &cart_messages.AddItem{ItemId: 100, Sku: "P", Quantity: 1, Price: 1000})
|
|
parentLine := g.Items[0].Id
|
|
mustApply(t, reg, g, &cart_messages.AddItem{ItemId: 101, Sku: "C1", Quantity: 1, Price: 100, ParentId: &parentLine})
|
|
mustApply(t, reg, g, &cart_messages.AddItem{ItemId: 102, Sku: "C2", Quantity: 1, Price: 200, ParentId: &parentLine})
|
|
mustApply(t, reg, g, &cart_messages.AddItem{ItemId: 200, Sku: "OTHER", Quantity: 1, Price: 300})
|
|
|
|
if len(g.Items) != 4 {
|
|
t.Fatalf("setup: items = %d, want 4", len(g.Items))
|
|
}
|
|
|
|
if _, err := reg.Apply(ctx, g, &cart_messages.RemoveItem{Id: parentLine}); err != nil {
|
|
t.Fatalf("remove parent: %v", err)
|
|
}
|
|
|
|
if len(g.Items) != 1 {
|
|
t.Fatalf("after remove: items = %d, want 1 (only the unrelated line)", len(g.Items))
|
|
}
|
|
if g.Items[0].Sku != "OTHER" {
|
|
t.Errorf("remaining item sku = %q, want OTHER", g.Items[0].Sku)
|
|
}
|
|
}
|
|
|
|
// Removing a child leaves the parent and siblings intact.
|
|
func TestRemoveItem_ChildDoesNotRemoveParent(t *testing.T) {
|
|
reg := NewCartMultationRegistry(NewCartMutationContext(nil))
|
|
g := NewCartGrain(1, time.Now())
|
|
ctx := context.Background()
|
|
|
|
mustApply(t, reg, g, &cart_messages.AddItem{ItemId: 100, Sku: "P", Quantity: 1, Price: 1000})
|
|
parentLine := g.Items[0].Id
|
|
mustApply(t, reg, g, &cart_messages.AddItem{ItemId: 101, Sku: "C1", Quantity: 1, Price: 100, ParentId: &parentLine})
|
|
childLine := g.Items[1].Id
|
|
|
|
if _, err := reg.Apply(ctx, g, &cart_messages.RemoveItem{Id: childLine}); err != nil {
|
|
t.Fatalf("remove child: %v", err)
|
|
}
|
|
if len(g.Items) != 1 {
|
|
t.Fatalf("items = %d, want 1 (parent remains)", len(g.Items))
|
|
}
|
|
if g.Items[0].Id != parentLine {
|
|
t.Errorf("remaining line = %d, want parent %d", g.Items[0].Id, parentLine)
|
|
}
|
|
}
|
|
|
|
func mustApply(t *testing.T, reg actor.MutationRegistry, g *CartGrain, m proto.Message) {
|
|
t.Helper()
|
|
if _, err := reg.Apply(context.Background(), g, m); err != nil {
|
|
t.Fatalf("apply %T: %v", m, err)
|
|
}
|
|
}
|