85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package promotions
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"git.k6n.net/mats/go-cart-actor/pkg/cart"
|
|
)
|
|
|
|
func timeZero() time.Time { return time.Date(2024, 6, 10, 12, 0, 0, 0, time.UTC) }
|
|
|
|
// volymrabattRule mirrors the seeded data/promotions.json rule so the test
|
|
// exercises the same tier configuration shipped to the cart service.
|
|
func volymrabattRule() PromotionRule {
|
|
return PromotionRule{
|
|
ID: "volymrabatt",
|
|
Name: "Volymrabatt",
|
|
Status: StatusActive,
|
|
Priority: 100,
|
|
Conditions: Conditions{
|
|
BaseCondition{ID: "min", Type: CondCartTotal, Operator: OpGreaterOrEqual, Value: cvNum(2000000)},
|
|
},
|
|
Actions: []Action{
|
|
{
|
|
ID: "tiers",
|
|
Type: ActionTieredDiscount,
|
|
Config: map[string]interface{}{
|
|
"tiers": []interface{}{
|
|
map[string]interface{}{"minTotal": 2000000.0, "maxTotal": 4000000.0, "percent": 5.0},
|
|
map[string]interface{}{"minTotal": 4000000.0, "maxTotal": 6000000.0, "percent": 9.0},
|
|
map[string]interface{}{"minTotal": 6000000.0, "maxTotal": 9000000.0, "percent": 14.0},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// cartWithTotal builds a grain whose single line item produces the requested
|
|
// gross (incl. 25% VAT) total in öre, then runs UpdateTotals.
|
|
func cartWithTotal(incVat int64) *cart.CartGrain {
|
|
g := cart.NewCartGrain(1, timeZero())
|
|
g.Items = []*cart.CartItem{
|
|
{Id: 1, Sku: "SKU", Quantity: 1, Price: *cart.NewPriceFromIncVat(incVat, 25)},
|
|
}
|
|
g.UpdateTotals()
|
|
return g
|
|
}
|
|
|
|
func TestVolymrabattTiers(t *testing.T) {
|
|
svc := NewPromotionService(nil)
|
|
rules := []PromotionRule{volymrabattRule()}
|
|
|
|
cases := []struct {
|
|
name string
|
|
total int64
|
|
wantDiscount int64
|
|
wantNet int64
|
|
}{
|
|
{"below threshold (19 999 kr)", 1999900, 0, 1999900},
|
|
{"low tier 5% (20 000 kr)", 2000000, 100000, 1900000},
|
|
{"low tier boundary just under 40 000", 3999900, 199995, 3799905},
|
|
{"mid tier 9% at 40 000 boundary", 4000000, 360000, 3640000},
|
|
{"mid tier 9% (50 000 kr)", 5000000, 450000, 4550000},
|
|
{"high tier 14% at 60 000 boundary", 6000000, 840000, 5160000},
|
|
{"high tier 14% (90 000 kr)", 9000000, 1260000, 7740000},
|
|
{"above top tier (100 000 kr) -> no discount", 10000000, 0, 10000000},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
g := cartWithTotal(c.total)
|
|
_, actions := svc.EvaluateAll(rules, NewContextFromCart(g, WithNow(timeZero())))
|
|
ApplyActions(g, actions)
|
|
|
|
if got := g.TotalDiscount.IncVat; got != c.wantDiscount {
|
|
t.Errorf("discount = %d, want %d", got, c.wantDiscount)
|
|
}
|
|
if got := g.TotalPrice.IncVat; got != c.wantNet {
|
|
t.Errorf("net total = %d, want %d", got, c.wantNet)
|
|
}
|
|
})
|
|
}
|
|
}
|