Download task_046/environment/code/promotion_test.go from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 2.97 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_046/environment/code/promotion_test.go
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_046/environment/code/promotion_test.go
-
curl -L -o promotion_test.go https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_046/environment/code/promotion_test.go
2.97 kB
| 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) | |
| } | |