From a87df109396cd5d21f4e2d95f4256f3cf0d6aa05 Mon Sep 17 00:00:00 2001 From: Filippo Costa Date: Mon, 15 Apr 2024 13:37:20 +0200 Subject: [PATCH] Add unit test for `HexString` --- crates/common_types/src/hex_string.rs | 29 ++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/common_types/src/hex_string.rs b/crates/common_types/src/hex_string.rs index affc48f..8a67671 100644 --- a/crates/common_types/src/hex_string.rs +++ b/crates/common_types/src/hex_string.rs @@ -20,7 +20,18 @@ use serde::{Deserialize, Serialize}; /// alias it to something more descriptive for its intended use case, possibly /// by enforcing a specific length. #[derive( - Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, AsExpression, FromSqlRow, + Copy, + Clone, + Default, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + AsExpression, + FromSqlRow, + derive_more::From, )] // TODO: The fact that we SQL-encode all kinds of hex strings, even fixed-length // ones, as variable-length byte sequences is a bit of a wart. Not that big of a @@ -37,12 +48,6 @@ impl HexString { } } -impl From for HexString { - fn from(t: T) -> Self { - HexString(t) - } -} - #[async_graphql::Scalar] impl async_graphql::ScalarType for HexString where @@ -136,10 +141,20 @@ impl Arbitrary for HexString { #[cfg(test)] mod tests { + use async_graphql::ScalarType; use quickcheck_macros::quickcheck; use super::*; + #[quickcheck] + fn async_graphql_roundtrip(hex_string: HexString>) -> bool { + let async_graphql_value = hex_string.to_value(); + let hex_string2: HexString> = + async_graphql::ScalarType::parse(async_graphql_value).unwrap(); + + hex_string == hex_string2 + } + #[quickcheck] fn serde_roundtrip(hex_string: HexString>) -> bool { let json = serde_json::to_string(&hex_string).unwrap();