#[cfg(test)]
mod tests {
use pricing_kit::{Currency, CurrencyConverter, MarkupType, PricingDetail, dec};
fn setup_converter() -> CurrencyConverter {
let mut converter = CurrencyConverter::new();
let usd = Currency::new("USD", "US Dollar");
let idr = Currency::new("IDR", "Indonesian Rupiah");
converter.add_exchange_rate(&usd, dec!(1.0));
converter.add_exchange_rate(&idr, dec!(16500.0));
converter
}
fn currencies() -> (Currency, Currency) {
(
Currency::new("USD", "US Dollar"),
Currency::new("IDR", "Indonesian Rupiah"),
)
}
#[test]
fn test_markup_amount_in_idr() {
let (usd, idr) = currencies();
let converter = setup_converter();
let mut pricing = PricingDetail::new(dec!(1000.0), usd.clone(), idr.clone());
pricing.markup = Some(MarkupType::Amount {
value: dec!(49500.0),
currency: idr.clone(),
});
pricing.apply_markup(&converter).unwrap();
assert_eq!(pricing.buy_currency_rate, Some(dec!(1.0)));
assert_eq!(pricing.sell_currency_rate, Some(dec!(16500.0)));
assert!((pricing.converted_buy_price.unwrap() - dec!(1003.0)).abs() < dec!(0.01));
assert!((pricing.sell_price - dec!(16549500.0)).abs() < dec!(0.01));
assert_eq!(pricing.markup_value_in_buy_currency, Some(dec!(3.0)));
}
#[test]
fn test_markup_percentage() {
let (usd, idr) = currencies();
let converter = setup_converter();
let mut pricing = PricingDetail::new(dec!(1000.0), usd.clone(), idr.clone());
pricing.markup = Some(MarkupType::Percentage(dec!(10.0)));
pricing.apply_markup(&converter).unwrap();
assert_eq!(pricing.markup_value_in_buy_currency.unwrap(), dec!(100.0));
assert!((pricing.sell_price - dec!(18150000.0)).abs() < dec!(0.01));
}
#[test]
fn test_markup_commission() {
let (usd, idr) = currencies();
let converter = setup_converter();
let mut pricing = PricingDetail::new(dec!(1000.0), usd.clone(), idr.clone());
pricing.markup = Some(MarkupType::Commission(dec!(10.0)));
pricing.apply_markup(&converter).unwrap();
assert!((pricing.markup_value_in_buy_currency.unwrap() - dec!(111.111111)).abs() < dec!(0.000001));
assert!((pricing.converted_buy_price.unwrap() - dec!(1111.111111)).abs() < dec!(0.000001));
assert!((pricing.sell_price - dec!(18333333.333333)).abs() < dec!(0.01)); }
#[test]
fn test_no_markup() {
let (usd, idr) = currencies();
let converter = setup_converter();
let mut pricing = PricingDetail::new(dec!(1000.0), usd.clone(), idr.clone());
pricing.apply_markup(&converter).unwrap();
assert_eq!(pricing.markup, None);
assert_eq!(pricing.markup_value_in_buy_currency.unwrap(), dec!(0.0));
assert_eq!(pricing.sell_price, dec!(16500000.0));
}
}