#[cfg(test)] mod tests { use gradecoin::schema::*; use serde_json::error::Error; use serde_test::{assert_tokens, Token}; use chrono::{NaiveDate, NaiveTime, NaiveDateTime}; #[test] fn claims_serialize_correctly() { let claims = Claims { tha: "hashed_string".to_owned(), iat: 0, exp: 100, }; assert_tokens( &claims, &[ Token::Struct{name: "Claims", len: 3}, Token::String("tha"), Token::String("hashed_string"), Token::String("iat"), Token::U64(0), Token::String("exp"), Token::U64(100), Token::StructEnd, ] ) } #[test] fn claims_deserialize_correctly() { let data = r#"{"tha":"hashed_string","iat":0,"exp":100}"#; let claims: Claims = serde_json::from_str(data).unwrap(); let expected_claims = Claims { tha: "hashed_string".to_owned(), iat: 0, exp: 100, }; assert_eq!(claims, expected_claims); } #[test] fn transaction_serialize_correctly() { let transaction = Transaction { by: "source".to_owned(), source: "source".to_owned(), target: "target".to_owned(), amount: 0, timestamp: NaiveDate::from_ymd(2021, 4, 2).and_hms(4, 2, 42), }; assert_tokens( &transaction, &[ Token::Struct{name: "Transaction", len: 5}, Token::String("by"), Token::String("source"), Token::String("source"), Token::String("source"), Token::String("target"), Token::String("target"), Token::String("amount"), Token::I32(0), Token::String("timestamp"), Token::String("2021-04-02T04:02:42"), Token::StructEnd, ] ) } #[test] fn transaction_deserialize_correctly() { let data = r#"{"by":"source","source":"source","target":"target","amount":0,"timestamp":"2021-04-02T04:02:42"}"#; let transaction: Transaction = serde_json::from_str(data).unwrap(); let expected_transaction = Transaction { by: "source".to_owned(), source: "source".to_owned(), target: "target".to_owned(), amount: 0, timestamp: NaiveDate::from_ymd(2021, 4, 2).and_hms(4, 2, 42), }; assert_eq!(transaction, expected_transaction); } #[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::Seq {len: Some(1)}, Token::String("transaction1"), Token::SeqEnd, Token::String("nonce"), Token::U32(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] fn naked_block_serialize_correctly() { let naked_block = NakedBlock { transaction_list: vec!["transaction1".to_owned()], nonce: 0, timestamp: NaiveDate::from_ymd(2021, 4, 2).and_hms(4, 2, 42), }; assert_tokens( &naked_block, &[ Token::Struct{name: "NakedBlock", len: 3}, Token::String("transaction_list"), Token::Seq {len: Some(1)}, Token::String("transaction1"), Token::SeqEnd, Token::String("nonce"), Token::U32(0), Token::String("timestamp"), Token::String("2021-04-02T04:02:42"), Token::StructEnd, ] ) } #[test] fn naked_block_deserialize_correctly() { let expected_naked_block = NakedBlock { transaction_list: vec!["transaction1".to_owned()], nonce: 0, timestamp: NaiveDate::from_ymd(2021, 4, 2).and_hms(4, 2, 42), }; let data = r#"{"transaction_list":["transaction1"],"nonce":0,"timestamp":"2021-04-02T04:02:42"}"#; let naked_block: NakedBlock = serde_json::from_str(data).unwrap(); assert_eq!(naked_block, expected_naked_block); } #[test] fn naked_block_deserialize_when_vec_emptpy() { // } #[test] fn user_serialize_correctly() { let user = User { user_id: MetuId::new("e254275".to_owned()).unwrap(), public_key: "public_key".to_owned(), balance: 0 }; assert_tokens( &user, &[ Token::Struct{name: "User", len: 3}, Token::String("user_id"), Token::Struct {name: "MetuId", len: 1}, Token::String("id"), Token::String("e254275"), Token::StructEnd, Token::String("public_key"), Token::String("public_key"), Token::String("balance"), Token::I32(0), Token::StructEnd, ] ) } #[test] fn user_deserialize_correctly() { let expected_user = User { user_id: MetuId::new("e254275".to_owned()).unwrap(), public_key: "public_key".to_owned(), balance: 0 }; let data = r#"{"user_id":{"id":"e254275"},"public_key":"public_key","balance":0}"#; let user: User = serde_json::from_str(data).unwrap(); assert_eq!(user, expected_user); } #[test] fn metu_id_serialize_correctly() { } #[test] fn metu_id_deserialize_correctly() { } #[test] fn auth_request_serialize_correctly() { } #[test] fn auth_request_deserialize_correctly() { } }