From 8bcec241a207682a8fe9b03bb4a794dd2c8de235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 27 Oct 2022 11:33:33 +0100 Subject: [PATCH] Moves Percent tests to the crate with the type definition (#1714) --- Cargo.lock | 1 + .../contracts-common/Cargo.toml | 3 ++ .../contracts-common/src/types.rs | 44 ++++++++++++++++++ .../mixnet-contract/src/types.rs | 45 ------------------- 4 files changed, 48 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c7367f201e..d88659c295 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -739,6 +739,7 @@ dependencies = [ "cosmwasm-std", "schemars", "serde", + "serde_json", "thiserror", ] diff --git a/common/cosmwasm-smart-contracts/contracts-common/Cargo.toml b/common/cosmwasm-smart-contracts/contracts-common/Cargo.toml index 1c1ec8ebee..4f98451100 100644 --- a/common/cosmwasm-smart-contracts/contracts-common/Cargo.toml +++ b/common/cosmwasm-smart-contracts/contracts-common/Cargo.toml @@ -11,3 +11,6 @@ cosmwasm-std = "1.0.0" serde = { version = "1.0", features = ["derive"] } schemars = "0.8" thiserror = "1" + +[dev-dependencies] +serde_json = "1.0.0" diff --git a/common/cosmwasm-smart-contracts/contracts-common/src/types.rs b/common/cosmwasm-smart-contracts/contracts-common/src/types.rs index e9b399c02e..a4dda07148 100644 --- a/common/cosmwasm-smart-contracts/contracts-common/src/types.rs +++ b/common/cosmwasm-smart-contracts/contracts-common/src/types.rs @@ -152,3 +152,47 @@ pub struct ContractBuildInformation { /// Provides the rustc version that was used for the build, for example `1.52.0-nightly`. pub rustc_version: String, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn percent_serde() { + let valid_value = Percent::from_percentage_value(80).unwrap(); + let serialized = serde_json::to_string(&valid_value).unwrap(); + + let deserialized: Percent = serde_json::from_str(&serialized).unwrap(); + assert_eq!(valid_value, deserialized); + + let invalid_values = vec!["\"42\"", "\"1.1\"", "\"1.00000001\"", "\"foomp\"", "\"1a\""]; + for invalid_value in invalid_values { + assert!(serde_json::from_str::<'_, Percent>(invalid_value).is_err()) + } + assert_eq!( + serde_json::from_str::<'_, Percent>("\"0.95\"").unwrap(), + Percent::from_percentage_value(95).unwrap() + ) + } + + #[test] + fn percent_to_absolute_integer() { + let p = serde_json::from_str::<'_, Percent>("\"0.0001\"").unwrap(); + assert_eq!(p.round_to_integer(), 0); + + let p = serde_json::from_str::<'_, Percent>("\"0.0099\"").unwrap(); + assert_eq!(p.round_to_integer(), 0); + + let p = serde_json::from_str::<'_, Percent>("\"0.0199\"").unwrap(); + assert_eq!(p.round_to_integer(), 1); + + let p = serde_json::from_str::<'_, Percent>("\"0.45123\"").unwrap(); + assert_eq!(p.round_to_integer(), 45); + + let p = serde_json::from_str::<'_, Percent>("\"0.999999999\"").unwrap(); + assert_eq!(p.round_to_integer(), 99); + + let p = serde_json::from_str::<'_, Percent>("\"1.00\"").unwrap(); + assert_eq!(p.round_to_integer(), 100); + } +} diff --git a/common/cosmwasm-smart-contracts/mixnet-contract/src/types.rs b/common/cosmwasm-smart-contracts/mixnet-contract/src/types.rs index f4af0362db..41914e62fb 100644 --- a/common/cosmwasm-smart-contracts/mixnet-contract/src/types.rs +++ b/common/cosmwasm-smart-contracts/mixnet-contract/src/types.rs @@ -122,48 +122,3 @@ pub struct PagedRewardedSetResponse { pub nodes: Vec<(MixId, RewardedSetNodeStatus)>, pub start_next_after: Option, } - -#[cfg(test)] -mod tests { - use contracts_common::Percent; - - #[test] - fn percent_serde() { - let valid_value = Percent::from_percentage_value(80).unwrap(); - let serialized = serde_json::to_string(&valid_value).unwrap(); - - println!("{}", serialized); - let deserialized: Percent = serde_json::from_str(&serialized).unwrap(); - assert_eq!(valid_value, deserialized); - - let invalid_values = vec!["\"42\"", "\"1.1\"", "\"1.00000001\"", "\"foomp\"", "\"1a\""]; - for invalid_value in invalid_values { - assert!(serde_json::from_str::<'_, Percent>(invalid_value).is_err()) - } - assert_eq!( - serde_json::from_str::<'_, Percent>("\"0.95\"").unwrap(), - Percent::from_percentage_value(95).unwrap() - ) - } - - #[test] - fn percent_to_absolute_integer() { - let p = serde_json::from_str::<'_, Percent>("\"0.0001\"").unwrap(); - assert_eq!(p.round_to_integer(), 0); - - let p = serde_json::from_str::<'_, Percent>("\"0.0099\"").unwrap(); - assert_eq!(p.round_to_integer(), 0); - - let p = serde_json::from_str::<'_, Percent>("\"0.0199\"").unwrap(); - assert_eq!(p.round_to_integer(), 1); - - let p = serde_json::from_str::<'_, Percent>("\"0.45123\"").unwrap(); - assert_eq!(p.round_to_integer(), 45); - - let p = serde_json::from_str::<'_, Percent>("\"0.999999999\"").unwrap(); - assert_eq!(p.round_to_integer(), 99); - - let p = serde_json::from_str::<'_, Percent>("\"1.00\"").unwrap(); - assert_eq!(p.round_to_integer(), 100); - } -}