diff options
author | alpaylan | 2021-04-14 02:14:09 +0300 |
---|---|---|
committer | alpaylan | 2021-04-14 02:14:09 +0300 |
commit | 2a843e8a6adae3df4214e6393facd6ec64bcd8f7 (patch) | |
tree | e265f95c4566ae03e2e84fa60a6a4cf4e296bbf8 | |
parent | 75cf1be9e4b70fd9300e373ab56469d69e340170 (diff) | |
download | gradecoin-2a843e8a6adae3df4214e6393facd6ec64bcd8f7.tar.gz gradecoin-2a843e8a6adae3df4214e6393facd6ec64bcd8f7.tar.bz2 gradecoin-2a843e8a6adae3df4214e6393facd6ec64bcd8f7.zip |
finished block testing
-rw-r--r-- | src/schema.rs | 2 | ||||
-rw-r--r-- | tests/schema_tests.rs | 34 |
2 files changed, 35 insertions, 1 deletions
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 { | |||
90 | /// https://serde.rs/container-attrs.html might be valuable to normalize the serialize/deserialize | 90 | /// https://serde.rs/container-attrs.html might be valuable to normalize the serialize/deserialize |
91 | /// conventions as these will be hashed | 91 | /// conventions as these will be hashed |
92 | /// | 92 | /// |
93 | #[derive(Serialize, Deserialize, Debug)] | 93 | #[derive(Serialize, Deserialize, Debug, PartialEq)] |
94 | pub struct Block { | 94 | pub struct Block { |
95 | #[serde(skip_serializing_if = "Vec::is_empty")] | 95 | #[serde(skip_serializing_if = "Vec::is_empty")] |
96 | pub transaction_list: Vec<PublicKeySignature>, | 96 | pub transaction_list: Vec<PublicKeySignature>, |
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 { | |||
85 | 85 | ||
86 | #[test] | 86 | #[test] |
87 | fn block_serialize_correctly() { | 87 | fn block_serialize_correctly() { |
88 | let block = Block { | ||
89 | transaction_list: vec!["transaction1".to_owned()], | ||
90 | nonce: 0, | ||
91 | timestamp: NaiveDate::from_ymd(2021, 4, 2).and_hms(4, 2, 42), | ||
92 | hash: "hash".to_owned() | ||
93 | }; | ||
88 | 94 | ||
95 | assert_tokens( | ||
96 | &block, | ||
97 | &[ | ||
98 | Token::Struct{name: "Block", len: 4}, | ||
99 | Token::String("transaction_list"), | ||
100 | Token::String("transaction1"), | ||
101 | Token::String("nonce"), | ||
102 | Token::I32(0), | ||
103 | Token::String("timestamp"), | ||
104 | Token::String("2021-04-02T04:02:42"), | ||
105 | Token::String("hash"), | ||
106 | Token::String("hash"), | ||
107 | Token::StructEnd, | ||
108 | ] | ||
109 | ) | ||
89 | } | 110 | } |
90 | 111 | ||
91 | #[test] | 112 | #[test] |
92 | fn block_deserialize_correctly() { | 113 | fn block_deserialize_correctly() { |
114 | let expected_block = Block { | ||
115 | transaction_list: vec!["transaction1".to_owned()], | ||
116 | nonce: 0, | ||
117 | timestamp: NaiveDate::from_ymd(2021, 4, 2).and_hms(4, 2, 42), | ||
118 | hash: "hash".to_owned() | ||
119 | }; | ||
120 | let data = r#"{"transaction_list":["transaction1"],"nonce":0,"timestamp":"2021-04-02T04:02:42","hash":"hash"}"#; | ||
121 | let block: Block = serde_json::from_str(data).unwrap(); | ||
122 | |||
123 | assert_eq!(block, expected_block); | ||
93 | 124 | ||
94 | } | 125 | } |
95 | 126 | ||
96 | #[test] | 127 | #[test] |
97 | fn block_deserialize_when_vec_emptpy() { | 128 | fn block_deserialize_when_vec_emptpy() { |
129 | let data = r#"{"transaction_list":[],"nonce":0,"timestamp":"2021-04-02T04:02:42","hash":"hash"}"#; | ||
130 | let err: Error = serde_json::from_str::<Block>(data).unwrap_err(); | ||
98 | 131 | ||
132 | assert_eq!(err.is_data(), true); | ||
99 | } | 133 | } |
100 | 134 | ||
101 | #[test] | 135 | #[test] |