49 lines
1.7 KiB
Go
49 lines
1.7 KiB
Go
package cart
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
// helper to create a cart grain with items and deliveries
|
|
func newTestCart() *CartGrain {
|
|
return &CartGrain{Items: []*CartItem{}, Deliveries: []*CartDelivery{}, Vouchers: []*Voucher{}, Notifications: []CartNotification{}}
|
|
}
|
|
|
|
func TestCartGrainUpdateTotalsBasic(t *testing.T) {
|
|
c := newTestCart()
|
|
// Item1 price 1250 (ex 1000 vat 250) org price higher -> discount 200 per unit
|
|
item1Price := Price{IncVat: 1250, VatRates: map[float32]int64{25: 250}}
|
|
item1Org := &Price{IncVat: 1500, VatRates: map[float32]int64{25: 300}}
|
|
item2Price := Price{IncVat: 2000, VatRates: map[float32]int64{25: 400}}
|
|
c.Items = []*CartItem{
|
|
{Id: 1, Price: item1Price, OrgPrice: item1Org, Quantity: 2},
|
|
{Id: 2, Price: item2Price, OrgPrice: &item2Price, Quantity: 1},
|
|
}
|
|
deliveryPrice := Price{IncVat: 4900, VatRates: map[float32]int64{25: 980}}
|
|
c.Deliveries = []*CartDelivery{{Id: 1, Price: deliveryPrice, Items: []uint32{1, 2}}}
|
|
|
|
c.UpdateTotals()
|
|
|
|
// Expected totals: sum inc vat of items * qty plus delivery
|
|
// item1 total inc = 1250*2 = 2500
|
|
// item2 total inc = 2000*1 = 2000
|
|
// delivery inc = 4900
|
|
expectedInc := int64(2500 + 2000 + 4900)
|
|
if c.TotalPrice.IncVat != expectedInc {
|
|
t.Fatalf("TotalPrice IncVat expected %d got %d", expectedInc, c.TotalPrice.IncVat)
|
|
}
|
|
|
|
// Discount: current implementation computes (OrgPrice - Price) ignoring quantity -> 1500-1250=250
|
|
if c.TotalDiscount.IncVat != 500 {
|
|
t.Fatalf("TotalDiscount expected 500 got %d", c.TotalDiscount.IncVat)
|
|
}
|
|
}
|
|
|
|
func TestCartGrainUpdateTotalsNoItems(t *testing.T) {
|
|
c := newTestCart()
|
|
c.UpdateTotals()
|
|
if c.TotalPrice.IncVat != 0 || c.TotalDiscount.IncVat != 0 {
|
|
t.Fatalf("expected zero totals got %+v", c)
|
|
}
|
|
}
|