Created
March 13, 2023 06:56
-
-
Save ozanmuyes/3699288973f5713eccabe289915e0ad3 to your computer and use it in GitHub Desktop.
Rust Serde Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use chrono::{serde::ts_seconds, DateTime, Utc}; | |
use serde::{Deserialize, Serialize}; // 1.0.153 // 0.4.23 | |
#[derive(Debug, Serialize, Deserialize, PartialEq)] | |
#[serde(tag = "_type")] | |
pub enum Leave { | |
#[serde(rename = "administrative")] | |
Administrative { | |
id: u8, | |
user_id: u8, | |
#[serde(with = "ts_seconds")] | |
starts_at: DateTime<Utc>, | |
#[serde(with = "ts_seconds")] | |
ends_at: DateTime<Utc>, | |
notes: Option<String>, | |
location: String, | |
}, | |
#[serde(rename = "approbation")] | |
Approbation { | |
id: u8, | |
user_id: u8, | |
#[serde(with = "ts_seconds")] | |
starts_at: DateTime<Utc>, | |
#[serde(with = "ts_seconds")] | |
ends_at: DateTime<Utc>, | |
notes: Option<String>, | |
}, | |
#[serde(rename = "annual")] | |
Annual { | |
id: u8, | |
user_id: u8, | |
#[serde(with = "ts_seconds")] | |
starts_at: DateTime<Utc>, | |
#[serde(with = "ts_seconds")] | |
ends_at: DateTime<Utc>, | |
notes: Option<String>, | |
}, | |
#[serde(rename = "attendant")] | |
Attendant { | |
id: u8, | |
user_id: u8, | |
#[serde(with = "ts_seconds")] | |
starts_at: DateTime<Utc>, | |
#[serde(with = "ts_seconds")] | |
ends_at: DateTime<Utc>, | |
notes: Option<String>, | |
customer: String, // TODO This could be customer ID or sub-document | |
}, | |
#[serde(rename = "maternity")] | |
Maternity { | |
id: u8, | |
user_id: u8, | |
#[serde(with = "ts_seconds")] | |
starts_at: DateTime<Utc>, | |
#[serde(with = "ts_seconds")] | |
ends_at: DateTime<Utc>, | |
notes: Option<String>, | |
}, | |
} | |
// See https://docs.rs/serde_json/1.0.94/serde_json/#parsing-json-as-strongly-typed-data-structures | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
use chrono::NaiveDateTime; // 0.4.23 | |
use serde_json::json; // 1.0.94 | |
#[test] | |
fn serializes_administrative_leave() -> serde_json::Result<()> { | |
let leave = Leave::Administrative { | |
id: 11, | |
user_id: 101, | |
starts_at: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(1678564142, 0).unwrap(), Utc), | |
ends_at: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(1678564147, 0).unwrap(), Utc), | |
notes: Some("some note".to_string()), | |
location: "AUS".to_string(), | |
}; | |
let data = serde_json::to_string(&leave)?; | |
assert_eq!( | |
data, | |
// FIXME See https://users.rust-lang.org/t/how-do-i-test-output-of-serde-json-to-string-pretty/22899/2 | |
json!({ | |
"id": 11, | |
"user_id": 101, | |
"starts_at": 1678564142, | |
"ends_at": 1678564147, | |
"notes": "some note", | |
"location": "AUS" | |
}) | |
); | |
Ok(()) | |
} | |
#[test] | |
fn deserializes_administrative_leave() -> serde_json::Result<()> { | |
let data = r#" | |
{ | |
"_type": "administrative", | |
"id": 12, | |
"user_id": 102, | |
"starts_at": 1678564242, | |
"ends_at": 1678564247, | |
"location": "NYC" | |
} | |
"#; | |
let leave: Leave = serde_json::from_str(data)?; | |
assert_eq!( | |
leave, | |
Leave::Administrative { | |
id: 12, | |
user_id: 102, | |
starts_at: DateTime::<Utc>::from_utc( | |
NaiveDateTime::from_timestamp_opt(1678564242, 0).unwrap(), | |
Utc | |
), | |
ends_at: DateTime::<Utc>::from_utc( | |
NaiveDateTime::from_timestamp_opt(1678564247, 0).unwrap(), | |
Utc | |
), | |
notes: None, | |
location: "NYC".to_string(), | |
} | |
); | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment