Skip to content

Commit

Permalink
Repair value type tests in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Jan 17, 2025
1 parent e5f6c91 commit 0876fa3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 109 deletions.
45 changes: 15 additions & 30 deletions nautilus_core/model/src/types/money.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,41 +414,26 @@ mod tests {
let _result = usd + btc; // This should panic since currencies are different
}

#[rstest]
#[cfg(feature = "high-precision")]
fn test_money_min_max_values() {
let min_money = Money::new(MONEY_MIN, Currency::USD());
let max_money = Money::new(MONEY_MAX, Currency::USD());
assert_eq!(
min_money.raw,
f64_to_fixed_i128(MONEY_MIN, Currency::USD().precision)
);
assert_eq!(
max_money.raw,
f64_to_fixed_i128(MONEY_MAX, Currency::USD().precision)
);
#[rstest] // Test does not panic rather than exact value
fn test_with_maximum_value() {
let money = Money::new_checked(MONEY_MAX, Currency::USD());
assert!(money.is_ok());
}

#[rstest]
#[cfg(not(feature = "high-precision"))]
fn test_money_min_max_values() {
let min_money = Money::new(MONEY_MIN, Currency::USD());
let max_money = Money::new(MONEY_MAX, Currency::USD());
assert_eq!(
min_money.raw,
f64_to_fixed_i64(MONEY_MIN, Currency::USD().precision)
);
assert_eq!(
max_money.raw,
f64_to_fixed_i64(MONEY_MAX, Currency::USD().precision)
);
#[rstest] // Test does not panic rather than exact value
fn test_with_minimum_value() {
let money = Money::new_checked(MONEY_MIN, Currency::USD());
assert!(money.is_ok());
}

#[rstest]
fn test_money_addition_f64() {
let money = Money::new(1000.0, Currency::USD());
let result = money + 500.0;
assert_eq!(result, 1500.0);
fn test_add() {
let a = 1000.0;
let b = 500.0;
let money1 = Money::new(a, Currency::USD());
let money2 = Money::new(b, Currency::USD());
let money3 = money1 + money2;
assert_eq!(money3.raw, Money::new(a + b, Currency::USD()).raw);
}

#[rstest]
Expand Down
105 changes: 47 additions & 58 deletions nautilus_core/model/src/types/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ impl<'de> Deserialize<'de> for Price {
////////////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////////////
#[cfg(not(feature = "high-precision"))]
#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand All @@ -485,44 +484,45 @@ mod tests {
#[should_panic(expected = "Condition failed: `precision` exceeded maximum `FIXED_PRECISION`")]
fn test_invalid_precision_new() {
// Precision out of range for fixed
let _ = Price::new(1.0, 10);
let _ = Price::new(1.0, FIXED_PRECISION + 1);
}

#[rstest]
#[should_panic(expected = "Condition failed: `precision` exceeded maximum `FIXED_PRECISION`")]
fn test_invalid_precision_from_raw() {
// Precision out of range for fixed
let _ = Price::from_raw(1, 10);
let _ = Price::from_raw(1, FIXED_PRECISION + 1);
}

#[rstest]
#[should_panic(expected = "Condition failed: `precision` exceeded maximum `FIXED_PRECISION`")]
fn test_invalid_precision_max() {
// Precision out of range for fixed
let _ = Price::max(10);
let _ = Price::max(FIXED_PRECISION + 1);
}

#[rstest]
#[should_panic(expected = "Condition failed: `precision` exceeded maximum `FIXED_PRECISION`")]
fn test_invalid_precision_min() {
// Precision out of range for fixed
let _ = Price::min(10);
let _ = Price::min(FIXED_PRECISION + 1);
}

#[rstest]
#[should_panic(expected = "Condition failed: `precision` exceeded maximum `FIXED_PRECISION`")]
fn test_invalid_precision_zero() {
// Precision out of range for fixed
let _ = Price::zero(10);
let _ = Price::zero(FIXED_PRECISION + 1);
}

#[rstest]
fn test_new() {
let price = Price::new(0.00812, 8);
let value = 0.00812;
let precision = 8;
let price = Price::new(value, precision);
assert_eq!(price, price);
assert_eq!(price.raw, 8_120_000);
assert_eq!(price.precision, 8);
assert_eq!(price.as_f64(), 0.00812);
assert_eq!(price.raw, Price::new(value, precision).raw);
assert_eq!(price.precision, precision);
assert_eq!(price.to_string(), "0.00812000");
assert!(!price.is_zero());
assert_eq!(price.as_decimal(), dec!(0.00812000));
Expand All @@ -534,47 +534,29 @@ mod tests {
));
}

#[rstest]
#[rstest] // Test does not panic rather than exact value
fn test_with_maximum_value() {
let price = Price::new(PRICE_MAX, 9);
assert_eq!(price.raw, 9_223_372_036_000_000_000);
assert_eq!(price.as_decimal(), dec!(9223372036));
assert_eq!(price.to_string(), "9223372036.000000000");
let price = Price::new_checked(PRICE_MAX, FIXED_PRECISION);
assert!(price.is_ok());
}

#[rstest] // Test does not panic rather than exact value
fn test_with_minimum_value() {
let price = Price::new_checked(PRICE_MIN, FIXED_PRECISION);
assert!(price.is_ok());
}

#[rstest]
fn test_max() {
let price = Price::max(FIXED_PRECISION);
assert_eq!(price.raw, Price::new(PRICE_MAX, FIXED_PRECISION).raw);
}

#[rstest]
fn test_with_minimum_positive_value() {
let price = Price::new(0.000_000_001, 9);
assert_eq!(price.raw, 1);
assert_eq!(price.as_decimal(), dec!(0.000000001));
assert_eq!(price.to_string(), "0.000000001");
}

// #[rstest]
// fn test_with_minimum_value() {
// let price = Price::new(PRICE_MIN, 9);
// assert_eq!(price.raw, -9_223_372_036_000_000_000);
// assert_eq!(price.as_decimal(), dec!(-9223372036));
// assert_eq!(price.to_string(), "-9223372036.000000000");
// assert_eq!(price.to_formatted_string(), "-9_223_372_036.000000000");
// }
//
// #[rstest]
// fn test_max() {
// let price = Price::max(9);
// assert_eq!(price.raw, 9_223_372_036_000_000_000);
// assert_eq!(price.as_decimal(), dec!(9223372036));
// assert_eq!(price.to_string(), "9223372036.000000000");
// assert_eq!(price.to_formatted_string(), "9_223_372_036.000000000");
// }
//
// #[rstest]
// fn test_min() {
// let price = Price::min(9);
// assert_eq!(price.raw, -9_223_372_036_000_000_000);
// assert_eq!(price.as_decimal(), dec!(-9223372036));
// assert_eq!(price.to_string(), "-9223372036.000000000");
// }
fn test_min() {
let price = Price::min(FIXED_PRECISION);
assert_eq!(price.raw, Price::new(PRICE_MIN, FIXED_PRECISION).raw);
}

#[rstest]
fn test_undefined() {
Expand Down Expand Up @@ -605,15 +587,13 @@ mod tests {
#[rstest]
fn test_precision() {
let price = Price::new(1.001, 2);
assert_eq!(price.raw, 1_000_000_000);
assert_eq!(price.to_string(), "1.00");
}

#[rstest]
fn test_new_from_str() {
let price: Price = "0.00812000".into();
assert_eq!(price, price);
assert_eq!(price.raw, 8_120_000);
assert_eq!(price.precision, 8);
assert_eq!(price.as_f64(), 0.00812);
assert_eq!(price.to_string(), "0.00812000");
Expand Down Expand Up @@ -651,25 +631,34 @@ mod tests {

#[rstest]
fn test_add() {
let price1 = Price::new(1.000, 3);
let price2 = Price::new(1.011, 3);
let a = 1.0;
let b = 1.011;
let precision = 3;
let price1 = Price::new(a, precision);
let price2 = Price::new(b, precision);
let price3 = price1 + price2;
assert_eq!(price3.raw, Price::from("2.011").raw);
assert_eq!(price3.raw, Price::new(a + b, precision).raw);
}

#[rstest]
fn test_sub() {
let price1 = Price::new(1.011, 3);
let price2 = Price::new(1.000, 3);
let a = 1.011;
let b = 1.0;
let precision = 3;
let price1 = Price::new(a, precision);
let price2 = Price::new(b, precision);
let price3 = price1 - price2;
assert_eq!(price3.raw, Price::from("0.011").raw);
assert_eq!(price3.raw, Price::new(a - b, precision).raw);
}

#[rstest]
fn test_add_assign() {
let mut price = Price::new(1.000, 3);
price += Price::new(1.011, 3);
assert_eq!(price.raw, Price::from("2.011").raw);
let a = 1.0;
let b = 1.011;
let precision = 3;
let mut price = Price::new(a, precision);
price += Price::new(b, precision);
assert_eq!(price.raw, Price::new(a + b, precision).raw);
}

#[rstest]
Expand Down
70 changes: 49 additions & 21 deletions nautilus_core/model/src/types/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ pub fn check_quantity_positive(value: Quantity) -> anyhow::Result<()> {
////////////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////////////
#[cfg(not(feature = "high-precision"))]
#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand All @@ -502,34 +501,35 @@ mod tests {
#[should_panic(expected = "Condition failed: `precision` exceeded maximum `FIXED_PRECISION`")]
fn test_invalid_precision_new() {
// Precision out of range for fixed
let _ = Quantity::new(1.0, 10);
let _ = Quantity::new(1.0, FIXED_PRECISION + 1);
}

#[rstest]
#[should_panic(expected = "Condition failed: `precision` exceeded maximum `FIXED_PRECISION`")]
fn test_invalid_precision_from_raw() {
// Precision out of range for fixed
let _ = Quantity::from_raw(1, 10);
let _ = Quantity::from_raw(1, FIXED_PRECISION + 1);
}

#[rstest]
#[should_panic(expected = "Condition failed: `precision` exceeded maximum `FIXED_PRECISION`")]
fn test_invalid_precision_zero() {
// Precision out of range for fixed
let _ = Quantity::zero(10);
let _ = Quantity::zero(FIXED_PRECISION + 1);
}

#[rstest]
fn test_new() {
let value = 0.00812;
let qty = Quantity::new(value, 8);
assert_eq!(qty, qty);
assert_eq!(qty.raw, Quantity::from(&format!("{value}")).raw);
assert_eq!(qty.precision, 8);
assert_eq!(qty.as_f64(), 0.00812);
assert_eq!(qty.as_decimal(), dec!(0.00812000));
assert_eq!(qty.to_string(), "0.00812000");
assert!(!qty.is_zero());
assert!(qty.is_positive());
assert_eq!(qty.as_decimal(), dec!(0.00812000));
assert!(approx_eq!(f64, qty.as_f64(), 0.00812, epsilon = 0.000_001));
}

Expand All @@ -549,11 +549,30 @@ mod tests {
assert!(!qty.is_positive());
}

#[rstest]
#[test]
fn test_from_i32() {
let value = 100_000i32;
let qty = Quantity::from(value);
assert_eq!(qty, qty);
assert_eq!(qty.raw, Quantity::from(&format!("{value}")).raw);
assert_eq!(qty.precision, 0);
}

#[test]
fn test_from_i64() {
let qty = Quantity::from(100_000);
let value = 100_000i64;
let qty = Quantity::from(value);
assert_eq!(qty, qty);
assert_eq!(qty.raw, Quantity::from("100000").raw);
assert_eq!(qty.raw, Quantity::from(&format!("{value}")).raw);
assert_eq!(qty.precision, 0);
}

#[test]
fn test_from_u64() {
let value = 100_000u64;
let qty = Quantity::from(value);
assert_eq!(qty, qty);
assert_eq!(qty.raw, Quantity::from(&format!("{value}")).raw);
assert_eq!(qty.precision, 0);
}

Expand Down Expand Up @@ -627,42 +646,51 @@ mod tests {

#[rstest]
fn test_add() {
let a = 1.0;
let b = 2.0;
let quantity1 = Quantity::new(1.0, 0);
let quantity2 = Quantity::new(2.0, 0);
let quantity3 = quantity1 + quantity2;
assert_eq!(quantity3.raw, 3_000_000_000);
assert_eq!(quantity3.raw, Quantity::new(a + b, 0).raw);
}

#[rstest]
fn test_sub() {
let quantity1 = Quantity::new(3.0, 0);
let quantity2 = Quantity::new(2.0, 0);
let a = 3.0;
let b = 2.0;
let quantity1 = Quantity::new(a, 0);
let quantity2 = Quantity::new(b, 0);
let quantity3 = quantity1 - quantity2;
assert_eq!(quantity3.raw, 1_000_000_000);
assert_eq!(quantity3.raw, Quantity::new(a - b, 0).raw);
}

#[rstest]
fn test_add_assign() {
let mut quantity1 = Quantity::new(1.0, 0);
let quantity2 = Quantity::new(2.0, 0);
let a = 1.0;
let b = 2.0;
let mut quantity1 = Quantity::new(a, 0);
let quantity2 = Quantity::new(b, 0);
quantity1 += quantity2;
assert_eq!(quantity1.raw, 3_000_000_000);
assert_eq!(quantity1.raw, Quantity::new(a + b, 0).raw);
}

#[rstest]
fn test_sub_assign() {
let mut quantity1 = Quantity::new(3.0, 0);
let quantity2 = Quantity::new(2.0, 0);
let a = 3.0;
let b = 2.0;
let mut quantity1 = Quantity::new(a, 0);
let quantity2 = Quantity::new(b, 0);
quantity1 -= quantity2;
assert_eq!(quantity1.raw, 1_000_000_000);
assert_eq!(quantity1.raw, Quantity::new(a - b, 0).raw);
}

#[rstest]
fn test_mul() {
let quantity1 = Quantity::new(2.0, 1);
let quantity2 = Quantity::new(2.0, 1);
let value = 2.0;
let quantity1 = Quantity::new(value, 1);
let quantity2 = Quantity::new(value, 1);
let quantity3 = quantity1 * quantity2;
assert_eq!(quantity3.raw, 4_000_000_000);
assert_eq!(quantity3.raw, Quantity::new(value * value, 0).raw);
}

#[rstest]
Expand Down

0 comments on commit 0876fa3

Please sign in to comment.