41 lines
1.3 KiB
Go
41 lines
1.3 KiB
Go
package cart
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
cart_messages "git.k6n.net/mats/go-cart-actor/proto/cart"
|
|
)
|
|
|
|
// Item identity is the catalog item id, not the (reference-only) SKU: two adds
|
|
// with the same ItemId but different SKU strings must merge into one line.
|
|
func TestAddItem_MergesByItemIdNotSku(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: 144047, Sku: "A0162590", Quantity: 1, Price: 1000}); err != nil {
|
|
t.Fatalf("first add: %v", err)
|
|
}
|
|
// Same id, different (reference) sku string -> should merge, not create a line.
|
|
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 144047, Sku: "STALE-REF", Quantity: 2, Price: 1000}); err != nil {
|
|
t.Fatalf("second add: %v", err)
|
|
}
|
|
|
|
if len(g.Items) != 1 {
|
|
t.Fatalf("items = %d, want 1 (merged by id)", len(g.Items))
|
|
}
|
|
if g.Items[0].Quantity != 3 {
|
|
t.Errorf("quantity = %d, want 3", g.Items[0].Quantity)
|
|
}
|
|
|
|
// A different id is a distinct line.
|
|
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 170852, Sku: "A0190103", Quantity: 1, Price: 500}); err != nil {
|
|
t.Fatalf("third add: %v", err)
|
|
}
|
|
if len(g.Items) != 2 {
|
|
t.Fatalf("items = %d, want 2", len(g.Items))
|
|
}
|
|
}
|