70 lines
3.0 KiB
Rust
70 lines
3.0 KiB
Rust
// file: crates/ksp-offchain-transport-lib/unit_tests/observation.rs
|
|
// version: 1
|
|
|
|
#[test]
|
|
fn observation_preserves_pair_exact_price_semantics_timestamps_and_safe_provenance() -> ksp_core_lib::Result<()> {
|
|
let provider_id = match crate::PriceProviderId::new("kraken") {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
let price = match crate::PriceDecimal::parse("204.125") {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
let provenance = match crate::PriceProvenance::new("market=SOL/USD") {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
let started = crate::PriceTimestamp::from_unix_millis(10_000);
|
|
let received = crate::PriceTimestamp::from_unix_millis(10_250);
|
|
let observation = match crate::SolUsdPriceObservation::new(
|
|
provider_id,
|
|
price,
|
|
crate::PriceSemantics::ExchangeLastTrade,
|
|
started,
|
|
received,
|
|
std::option::Option::None,
|
|
provenance,
|
|
) {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
assert_eq!(observation.pair(), crate::PricePair::SolUsd);
|
|
assert_eq!(observation.pair().code(), "SOL/USD");
|
|
assert_eq!(observation.price(), price);
|
|
assert_eq!(observation.semantics(), crate::PriceSemantics::ExchangeLastTrade);
|
|
assert_eq!(observation.request_started_at(), started);
|
|
assert_eq!(observation.received_at(), received);
|
|
assert_eq!(observation.provider_timestamp(), std::option::Option::None);
|
|
assert_eq!(observation.provenance().as_str(), "market=SOL/USD");
|
|
return std::result::Result::Ok(());
|
|
}
|
|
|
|
#[test]
|
|
fn observation_rejects_reversed_ksp_timestamps_and_unsafe_provenance() -> ksp_core_lib::Result<()> {
|
|
assert!(crate::PriceProvenance::new("line\nbreak").is_err());
|
|
let provider_id = match crate::PriceProviderId::new("coingecko") {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
let price = match crate::PriceDecimal::parse("200") {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
let provenance = match crate::PriceProvenance::new("asset=solana") {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
let result = crate::SolUsdPriceObservation::new(
|
|
provider_id,
|
|
price,
|
|
crate::PriceSemantics::AggregatedMarket,
|
|
crate::PriceTimestamp::from_unix_millis(2),
|
|
crate::PriceTimestamp::from_unix_millis(1),
|
|
std::option::Option::None,
|
|
provenance,
|
|
);
|
|
assert!(result.is_err());
|
|
return std::result::Result::Ok(());
|
|
}
|