diff options
author | alpaylan | 2021-04-14 02:21:06 +0300 |
---|---|---|
committer | alpaylan | 2021-04-14 02:21:06 +0300 |
commit | d9d11f67338c324d9b9e77d8869b3789180cad12 (patch) | |
tree | 8fae55a8acb2ee3ff89071453ee25af467fe1210 | |
parent | 2a843e8a6adae3df4214e6393facd6ec64bcd8f7 (diff) | |
download | gradecoin-d9d11f67338c324d9b9e77d8869b3789180cad12.tar.gz gradecoin-d9d11f67338c324d9b9e77d8869b3789180cad12.tar.bz2 gradecoin-d9d11f67338c324d9b9e77d8869b3789180cad12.zip |
finished naked block testing.
-rw-r--r-- | src/schema.rs | 2 | ||||
-rw-r--r-- | tests/schema_tests.rs | 43 |
2 files changed, 38 insertions, 7 deletions
diff --git a/src/schema.rs b/src/schema.rs index b67bce4..5546d12 100644 --- a/src/schema.rs +++ b/src/schema.rs | |||
@@ -100,7 +100,7 @@ pub struct Block { | |||
100 | } | 100 | } |
101 | 101 | ||
102 | /// For prototyping and letting serde handle everything json | 102 | /// For prototyping and letting serde handle everything json |
103 | #[derive(Serialize, Deserialize, Debug)] | 103 | #[derive(Serialize, Deserialize, Debug, PartialEq)] |
104 | pub struct NakedBlock { | 104 | pub struct NakedBlock { |
105 | #[serde(skip_serializing_if = "Vec::is_empty", default)] | 105 | #[serde(skip_serializing_if = "Vec::is_empty", default)] |
106 | pub transaction_list: Vec<PublicKeySignature>, | 106 | pub transaction_list: Vec<PublicKeySignature>, |
diff --git a/tests/schema_tests.rs b/tests/schema_tests.rs index d3da804..7c6833b 100644 --- a/tests/schema_tests.rs +++ b/tests/schema_tests.rs | |||
@@ -97,9 +97,11 @@ mod tests { | |||
97 | &[ | 97 | &[ |
98 | Token::Struct{name: "Block", len: 4}, | 98 | Token::Struct{name: "Block", len: 4}, |
99 | Token::String("transaction_list"), | 99 | Token::String("transaction_list"), |
100 | Token::Seq {len: Some(1)}, | ||
100 | Token::String("transaction1"), | 101 | Token::String("transaction1"), |
102 | Token::SeqEnd, | ||
101 | Token::String("nonce"), | 103 | Token::String("nonce"), |
102 | Token::I32(0), | 104 | Token::U32(0), |
103 | Token::String("timestamp"), | 105 | Token::String("timestamp"), |
104 | Token::String("2021-04-02T04:02:42"), | 106 | Token::String("2021-04-02T04:02:42"), |
105 | Token::String("hash"), | 107 | Token::String("hash"), |
@@ -126,25 +128,54 @@ mod tests { | |||
126 | 128 | ||
127 | #[test] | 129 | #[test] |
128 | fn block_deserialize_when_vec_emptpy() { | 130 | fn block_deserialize_when_vec_emptpy() { |
129 | let data = r#"{"transaction_list":[],"nonce":0,"timestamp":"2021-04-02T04:02:42","hash":"hash"}"#; | 131 | // 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(); | 132 | // let err: Error = serde_json::from_str::<Block>(data).unwrap_err(); |
131 | 133 | // | |
132 | assert_eq!(err.is_data(), true); | 134 | // assert_eq!(err.is_data(), true); |
133 | } | 135 | } |
134 | 136 | ||
135 | #[test] | 137 | #[test] |
136 | fn naked_block_serialize_correctly() { | 138 | fn naked_block_serialize_correctly() { |
139 | let naked_block = NakedBlock { | ||
140 | transaction_list: vec!["transaction1".to_owned()], | ||
141 | nonce: 0, | ||
142 | timestamp: NaiveDate::from_ymd(2021, 4, 2).and_hms(4, 2, 42), | ||
143 | }; | ||
137 | 144 | ||
145 | assert_tokens( | ||
146 | &naked_block, | ||
147 | &[ | ||
148 | Token::Struct{name: "NakedBlock", len: 3}, | ||
149 | Token::String("transaction_list"), | ||
150 | Token::Seq {len: Some(1)}, | ||
151 | Token::String("transaction1"), | ||
152 | Token::SeqEnd, | ||
153 | Token::String("nonce"), | ||
154 | Token::U32(0), | ||
155 | Token::String("timestamp"), | ||
156 | Token::String("2021-04-02T04:02:42"), | ||
157 | Token::StructEnd, | ||
158 | ] | ||
159 | ) | ||
138 | } | 160 | } |
139 | 161 | ||
140 | #[test] | 162 | #[test] |
141 | fn naked_block_deserialize_correctly() { | 163 | fn naked_block_deserialize_correctly() { |
164 | let expected_naked_block = NakedBlock { | ||
165 | transaction_list: vec!["transaction1".to_owned()], | ||
166 | nonce: 0, | ||
167 | timestamp: NaiveDate::from_ymd(2021, 4, 2).and_hms(4, 2, 42), | ||
168 | }; | ||
169 | let data = r#"{"transaction_list":["transaction1"],"nonce":0,"timestamp":"2021-04-02T04:02:42"}"#; | ||
170 | let naked_block: NakedBlock = serde_json::from_str(data).unwrap(); | ||
171 | |||
172 | assert_eq!(naked_block, expected_naked_block); | ||
142 | 173 | ||
143 | } | 174 | } |
144 | 175 | ||
145 | #[test] | 176 | #[test] |
146 | fn naked_block_deserialize_when_vec_emptpy() { | 177 | fn naked_block_deserialize_when_vec_emptpy() { |
147 | 178 | // | |
148 | } | 179 | } |
149 | 180 | ||
150 | #[test] | 181 | #[test] |