From 8793c5309f0f655ae27e760f7e8660a3a7ec82e2 Mon Sep 17 00:00:00 2001 From: aubrey Date: Mon, 29 Apr 2024 14:31:43 -0700 Subject: [PATCH] metadata: add tests for decoding failure when asset_id and denom don't match and simple roundtrip encoding test --- crates/core/asset/src/asset/denom_metadata.rs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crates/core/asset/src/asset/denom_metadata.rs b/crates/core/asset/src/asset/denom_metadata.rs index 582a476a98..994e96f622 100644 --- a/crates/core/asset/src/asset/denom_metadata.rs +++ b/crates/core/asset/src/asset/denom_metadata.rs @@ -545,6 +545,8 @@ impl Display for Unit { #[cfg(test)] mod tests { + use std::sync::Arc; + #[test] fn can_parse_metadata_from_chain_registry() { const SOME_COSMOS_JSON: &str = r#" @@ -590,4 +592,31 @@ mod tests { //let json2 = serde_json::to_string_pretty(&_metadata).unwrap(); //println!("{}", json2); } + + #[test] + fn encoding_round_trip_succeeds() { + let metadata = super::Metadata::try_from("upenumbra").unwrap(); + + let proto = super::pb::Metadata::from(metadata.clone()); + + let metadata_2 = super::Metadata::try_from(proto).unwrap(); + + assert_eq!(metadata, metadata_2); + } + + #[test] + #[should_panic] + fn changing_asset_id_without_changing_denom_fails_decoding() { + let mut metadata = super::Metadata::try_from("upenumbra").unwrap(); + + let inner = Arc::get_mut(&mut metadata.inner).unwrap(); + + inner.id = super::Id::from_raw_denom("uusd"); + + let proto = super::pb::Metadata::from(metadata); + + // This should throw an error, because the asset ID and denom are now inconsistent. + + let _domain_type = super::Metadata::try_from(proto).unwrap(); + } }