discount.rs (2106B)
1 mod common; 2 3 use radroots_core::{ 4 RadrootsCoreDiscount, RadrootsCoreDiscountScope, RadrootsCoreDiscountThreshold, 5 RadrootsCoreDiscountValue, RadrootsCorePercent, RadrootsCoreUnit, 6 }; 7 8 #[test] 9 fn is_non_negative_validates_discount_shapes() { 10 let money_pos = common::money("1", "USD"); 11 let money_neg = common::money("-1", "USD"); 12 let qty_pos = common::qty("1", RadrootsCoreUnit::Each); 13 let qty_neg = common::qty("-1", RadrootsCoreUnit::Each); 14 let pct_pos = RadrootsCorePercent::new(common::dec("10")); 15 let pct_neg = RadrootsCorePercent::new(common::dec("-5")); 16 17 let d = RadrootsCoreDiscount { 18 scope: RadrootsCoreDiscountScope::Bin, 19 threshold: RadrootsCoreDiscountThreshold::BinCount { 20 bin_id: "bin-1".to_string(), 21 min: 2, 22 }, 23 value: RadrootsCoreDiscountValue::MoneyPerBin(money_pos.clone()), 24 }; 25 assert!(d.is_non_negative()); 26 27 let d = RadrootsCoreDiscount { 28 scope: RadrootsCoreDiscountScope::Bin, 29 threshold: RadrootsCoreDiscountThreshold::BinCount { 30 bin_id: "bin-1".to_string(), 31 min: 2, 32 }, 33 value: RadrootsCoreDiscountValue::MoneyPerBin(money_neg), 34 }; 35 assert!(!d.is_non_negative()); 36 37 let d = RadrootsCoreDiscount { 38 scope: RadrootsCoreDiscountScope::OrderTotal, 39 threshold: RadrootsCoreDiscountThreshold::OrderQuantity { 40 min: qty_pos.clone(), 41 }, 42 value: RadrootsCoreDiscountValue::Percent(pct_pos.clone()), 43 }; 44 assert!(d.is_non_negative()); 45 46 let d = RadrootsCoreDiscount { 47 scope: RadrootsCoreDiscountScope::OrderTotal, 48 threshold: RadrootsCoreDiscountThreshold::OrderQuantity { min: qty_neg }, 49 value: RadrootsCoreDiscountValue::Percent(pct_pos), 50 }; 51 assert!(!d.is_non_negative()); 52 53 let d = RadrootsCoreDiscount { 54 scope: RadrootsCoreDiscountScope::OrderTotal, 55 threshold: RadrootsCoreDiscountThreshold::OrderQuantity { min: qty_pos }, 56 value: RadrootsCoreDiscountValue::Percent(pct_neg), 57 }; 58 assert!(!d.is_non_negative()); 59 }