cart and checkout
This commit is contained in:
@@ -146,3 +146,104 @@ func TestApplyResultsRecordsEffects(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuyXGetYEffect(t *testing.T) {
|
||||
rule := PromotionRule{
|
||||
ID: "buy2get1",
|
||||
Name: "Buy 2 Get 1 Free",
|
||||
Status: StatusActive,
|
||||
Actions: []Action{
|
||||
{
|
||||
ID: "buy2get1-action",
|
||||
Type: ActionBuyXGetY,
|
||||
Config: map[string]interface{}{
|
||||
"buy": 2.0,
|
||||
"get": 1.0,
|
||||
"discount": 100.0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
svc := NewPromotionService(nil)
|
||||
|
||||
// Cart with 3 items of price 1000 each. Buy 2 get 1 free -> 1 is free -> 1000 discount.
|
||||
g := cart.NewCartGrain(1, timeZero())
|
||||
g.Items = []*cart.CartItem{
|
||||
{Id: 1, Sku: "SKU1", Quantity: 3, Price: *cart.NewPriceFromIncVat(1000, 25)},
|
||||
}
|
||||
g.UpdateTotals()
|
||||
|
||||
results, _ := svc.EvaluateAll([]PromotionRule{rule}, NewContextFromCart(g, WithNow(timeZero())))
|
||||
svc.ApplyResults(g, results, NewContextFromCart(g, WithNow(timeZero())))
|
||||
|
||||
if got := g.TotalDiscount.IncVat; got != 1000 {
|
||||
t.Errorf("discount = %d, want 1000", got)
|
||||
}
|
||||
if got := g.TotalPrice.IncVat; got != 2000 {
|
||||
t.Errorf("total price = %d, want 2000", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBundleDiscountEffect(t *testing.T) {
|
||||
rule := PromotionRule{
|
||||
ID: "bundle-deal",
|
||||
Name: "Bundle Deal",
|
||||
Status: StatusActive,
|
||||
Actions: []Action{
|
||||
{
|
||||
ID: "bundle-action",
|
||||
Type: ActionBundleDiscount,
|
||||
BundleConfig: &BundleConfig{
|
||||
Containers: []BundleContainer{
|
||||
{
|
||||
ID: "shoes",
|
||||
Name: "Shoes",
|
||||
Quantity: 1,
|
||||
SelectionType: "any",
|
||||
QualifyingRules: BundleQualifyingRules{
|
||||
Type: "category",
|
||||
Value: "shoes",
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: "socks",
|
||||
Name: "Socks",
|
||||
Quantity: 2,
|
||||
SelectionType: "any",
|
||||
QualifyingRules: BundleQualifyingRules{
|
||||
Type: "category",
|
||||
Value: "socks",
|
||||
},
|
||||
},
|
||||
},
|
||||
Pricing: BundlePricing{
|
||||
Type: "fixed_price",
|
||||
Value: 40.0, // 40 kr -> 4000 ore
|
||||
},
|
||||
RequireAllContainers: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
svc := NewPromotionService(nil)
|
||||
|
||||
// Cart with 1 shoe (5000) and 2 socks (1000 each). Total original = 7000. Bundle fixed price is 4000. Discount = 3000.
|
||||
g := cart.NewCartGrain(1, timeZero())
|
||||
g.Items = []*cart.CartItem{
|
||||
{Id: 1, Sku: "shoe-1", Quantity: 1, Price: *cart.NewPriceFromIncVat(5000, 25), Meta: &cart.ItemMeta{Category: "shoes"}},
|
||||
{Id: 2, Sku: "socks-1", Quantity: 2, Price: *cart.NewPriceFromIncVat(1000, 25), Meta: &cart.ItemMeta{Category: "socks"}},
|
||||
}
|
||||
g.UpdateTotals()
|
||||
|
||||
results, _ := svc.EvaluateAll([]PromotionRule{rule}, NewContextFromCart(g, WithNow(timeZero())))
|
||||
svc.ApplyResults(g, results, NewContextFromCart(g, WithNow(timeZero())))
|
||||
|
||||
if got := g.TotalDiscount.IncVat; got != 3000 {
|
||||
t.Errorf("discount = %d, want 3000", got)
|
||||
}
|
||||
if got := g.TotalPrice.IncVat; got != 4000 {
|
||||
t.Errorf("total price = %d, want 4000", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user