test: improve error handling in assertions for currency data fetching tests

This commit is contained in:
2026-02-20 14:22:56 +00:00
parent 6f3c5ef106
commit 49291002ac
2 changed files with 47 additions and 14 deletions

View File

@@ -321,7 +321,11 @@ mod tests {
#[ignore = "Calls external API - run manually with: cargo test -- --ignored"]
async fn test_fetch_currency_data_real_api() {
let result = fetch_currency_data("usd").await;
assert!(result.is_ok(), "Failed to fetch currency data: {:?}", result.err());
assert!(
result.is_ok(),
"Failed to fetch currency data: {:?}",
result.err()
);
let data = result.expect("Failed to get currency data");
assert!(!data.date.is_empty(), "Date should not be empty");
@@ -341,7 +345,11 @@ mod tests {
#[ignore = "Calls external API - run manually with: cargo test -- --ignored"]
async fn test_fetch_currency_data_eur_base() {
let result = fetch_currency_data("eur").await;
assert!(result.is_ok(), "Failed to fetch EUR currency data: {:?}", result.err());
assert!(
result.is_ok(),
"Failed to fetch EUR currency data: {:?}",
result.err()
);
let data = result.expect("Failed to get currency data");
assert!(data.rates.contains_key("eur"), "EUR rates should exist");
@@ -356,7 +364,11 @@ mod tests {
let adapter = ExchangeApiAdapter::new();
let result = adapter.get_exchange_rate("usd", "eur").await;
assert!(result.is_ok(), "Failed to get exchange rate: {:?}", result.err());
assert!(
result.is_ok(),
"Failed to get exchange rate: {:?}",
result.err()
);
let (rate, timestamp) = result.expect("Failed to get exchange rate");
assert!(rate > Decimal::ZERO, "Rate should be positive");
@@ -375,9 +387,18 @@ mod tests {
assert!(!currencies.is_empty(), "Should have supported currencies");
// Check for common currencies
assert!(currencies.contains(&"eur".to_string()), "Should contain eur");
assert!(currencies.contains(&"gbp".to_string()), "Should contain gbp");
assert!(currencies.contains(&"jpy".to_string()), "Should contain jpy");
assert!(
currencies.contains(&"eur".to_string()),
"Should contain eur"
);
assert!(
currencies.contains(&"gbp".to_string()),
"Should contain gbp"
);
assert!(
currencies.contains(&"jpy".to_string()),
"Should contain jpy"
);
}
#[tokio::test]
@@ -403,8 +424,11 @@ mod tests {
let diff = (product - one).abs();
// Allow for small differences (0.5% tolerance) due to API rate variations
assert!(diff < Decimal::from_str_exact("0.005").unwrap(),
"Roundtrip conversion should be approximately 1, got: {}", product);
assert!(
diff < Decimal::from_str_exact("0.005").expect("Failed to parse tolerance"),
"Roundtrip conversion should be approximately 1, got: {}",
product
);
}
#[tokio::test]
@@ -419,10 +443,19 @@ mod tests {
// Verify all rates can be converted to Decimal
for (currency, rate_f64) in usd_rates {
let rate = Decimal::from_f64_retain(*rate_f64);
assert!(rate.is_some(), "Failed to convert rate for {}: {}", currency, rate_f64);
assert!(
rate.is_some(),
"Failed to convert rate for {}: {}",
currency,
rate_f64
);
let rate = rate.expect("Decimal conversion failed");
assert!(rate > Decimal::ZERO, "Rate for {} should be positive", currency);
assert!(
rate > Decimal::ZERO,
"Rate for {} should be positive",
currency
);
}
}
}

View File

@@ -463,7 +463,7 @@ mod tests {
// Allow for small floating point differences (0.5% tolerance for real-world rates)
assert!(
diff < Decimal::from_str_exact("0.005").unwrap(),
diff < Decimal::from_str_exact("0.005").expect("Failed to parse tolerance"),
"Roundtrip conversion should be approximately 1, got: {}",
product
);