File size: 2,973 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package promotion

import "testing"

func closeEnough(t *testing.T, got, want float64) {
    t.Helper()
    if got < want-0.001 || got > want+0.001 {
        t.Fatalf("got %.3f want %.3f", got, want)
    }
}

func TestFullReductionDiscounts(t *testing.T) {
    cases := []struct {
        amount float64
        want   float64
    }{
        {150, 0},
        {200, 20},
        {499, 20},
        {500, 50},
        {999, 50},
        {1000, 100},
    }
    s := FullReductionStrategy{}
    for _, tc := range cases {
        closeEnough(t, s.Discount(Order{OriginalAmount: tc.amount}), tc.want)
    }
}

func TestUserLevelDiscountsFromOriginalPrice(t *testing.T) {
    s := UserLevelDiscountStrategy{}
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, UserLevel: "bronze"}), 0)
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, UserLevel: "silver"}), 50)
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, UserLevel: "gold"}), 100)
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, UserLevel: "platinum"}), 150)
}

func TestCategoryDiscountsFromOriginalPrice(t *testing.T) {
    s := CategoryDiscountStrategy{}
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, Category: "electronics"}), 80)
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, Category: "clothing"}), 120)
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, Category: "books"}), 200)
}

func TestPromotionCalculatorStacksDiscounts(t *testing.T) {
    calc := NewPromotionCalculator(FullReductionStrategy{}, UserLevelDiscountStrategy{}, CategoryDiscountStrategy{})
    order := Order{OriginalAmount: 1000, UserLevel: "gold", Category: "books"}
    // Full reduction 100 + gold discount 100 + books discount 200, all from original price.
    closeEnough(t, calc.FinalAmount(order), 600)
}

func TestUnknownUserLevelGivesZeroDiscount(t *testing.T) {
    // Map lookup for an unknown level returns the zero rate, so discount is 0.
    s := UserLevelDiscountStrategy{}
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, UserLevel: "diamond"}), 0)
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, UserLevel: ""}), 0)
}

func TestUnknownCategoryGivesZeroDiscount(t *testing.T) {
    // Map lookup for an unknown category returns the zero rate, so discount is 0.
    s := CategoryDiscountStrategy{}
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, Category: "groceries"}), 0)
    closeEnough(t, s.Discount(Order{OriginalAmount: 1000, Category: ""}), 0)
}

func TestPromotionCalculatorStacksNonRoundPrice(t *testing.T) {
    calc := NewPromotionCalculator(FullReductionStrategy{}, UserLevelDiscountStrategy{}, CategoryDiscountStrategy{})
    order := Order{OriginalAmount: 500, UserLevel: "silver", Category: "electronics"}
    // Full reduction 50 (>=500) + silver 500*0.05=25 + electronics 500*0.08=40 = 115 off.
    closeEnough(t, calc.FinalAmount(order), 385)
}