From aa0ad3f0810b015953f31f19155343295b0ca0d3 Mon Sep 17 00:00:00 2001 From: alpaylan Date: Wed, 14 Apr 2021 02:14:09 +0300 Subject: finished block testing --- src/schema.rs | 2 +- tests/schema_tests.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/schema.rs b/src/schema.rs index 4d2b442..b67bce4 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -90,7 +90,7 @@ pub struct Transaction { /// https://serde.rs/container-attrs.html might be valuable to normalize the serialize/deserialize /// conventions as these will be hashed /// -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct Block { #[serde(skip_serializing_if = "Vec::is_empty")] pub transaction_list: Vec, diff --git a/tests/schema_tests.rs b/tests/schema_tests.rs index fc06796..d3da804 100644 --- a/tests/schema_tests.rs +++ b/tests/schema_tests.rs @@ -85,17 +85,51 @@ mod tests { #[test] fn block_serialize_correctly() { + let block = Block { + transaction_list: vec!["transaction1".to_owned()], + nonce: 0, + timestamp: NaiveDate::from_ymd(2021, 4, 2).and_hms(4, 2, 42), + hash: "hash".to_owned() + }; + assert_tokens( + &block, + &[ + Token::Struct{name: "Block", len: 4}, + Token::String("transaction_list"), + Token::String("transaction1"), + Token::String("nonce"), + Token::I32(0), + Token::String("timestamp"), + Token::String("2021-04-02T04:02:42"), + Token::String("hash"), + Token::String("hash"), + Token::StructEnd, + ] + ) } #[test] fn block_deserialize_correctly() { + let expected_block = Block { + transaction_list: vec!["transaction1".to_owned()], + nonce: 0, + timestamp: NaiveDate::from_ymd(2021, 4, 2).and_hms(4, 2, 42), + hash: "hash".to_owned() + }; + let data = r#"{"transaction_list":["transaction1"],"nonce":0,"timestamp":"2021-04-02T04:02:42","hash":"hash"}"#; + let block: Block = serde_json::from_str(data).unwrap(); + + assert_eq!(block, expected_block); } #[test] fn block_deserialize_when_vec_emptpy() { + let data = r#"{"transaction_list":[],"nonce":0,"timestamp":"2021-04-02T04:02:42","hash":"hash"}"#; + let err: Error = serde_json::from_str::(data).unwrap_err(); + assert_eq!(err.is_data(), true); } #[test] -- cgit v1.2.3-70-g09d2