Skip to content

Commit

Permalink
Add unit test for HexString
Browse files Browse the repository at this point in the history
  • Loading branch information
neysofu committed Apr 15, 2024
1 parent 5650cc5 commit a87df10
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions crates/common_types/src/hex_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,12 +48,6 @@ impl<T: ToOwned> HexString<T> {
}
}

impl<T> From<T> for HexString<T> {
fn from(t: T) -> Self {
HexString(t)
}
}

#[async_graphql::Scalar]
impl<T> async_graphql::ScalarType for HexString<T>
where
Expand Down Expand Up @@ -136,10 +141,20 @@ impl<T: Arbitrary> Arbitrary for HexString<T> {

#[cfg(test)]
mod tests {
use async_graphql::ScalarType;
use quickcheck_macros::quickcheck;

use super::*;

#[quickcheck]
fn async_graphql_roundtrip(hex_string: HexString<Vec<u8>>) -> bool {
let async_graphql_value = hex_string.to_value();
let hex_string2: HexString<Vec<u8>> =
async_graphql::ScalarType::parse(async_graphql_value).unwrap();

hex_string == hex_string2
}

#[quickcheck]
fn serde_roundtrip(hex_string: HexString<Vec<u8>>) -> bool {
let json = serde_json::to_string(&hex_string).unwrap();
Expand Down

0 comments on commit a87df10

Please sign in to comment.