dynip-cloudflare/src/tests/config_serialization.rs

76 lines
1.6 KiB
Rust
Raw Normal View History

2024-07-25 18:16:12 +02:00
use std::time::Duration;
use toml;
use crate::Config;
const TOML_STR_ONE: &str = r#"
zone_id = ""
api_key = ""
domains = [""]
max_duration = "1d 2h 30m 45s 500000000ns"
"#;
#[test]
fn test_deserialize() {
let config: Config = toml::from_str(TOML_STR_ONE).unwrap();
assert_eq!(config.max_duration, Some(Duration::new(95445, 500000000)));
}
#[test]
fn test_serialize() {
let config = Config {
zone_id: "".into(),
api_key: "".into(),
domains: vec!["".into()],
max_errors_in_row: None,
max_duration: Some(Duration::new(95445, 500000000)),
};
let toml_str = toml::to_string(&config).unwrap();
assert_eq!(TOML_STR_ONE.trim(), toml_str.trim());
}
#[test]
fn test_deserialize_none() {
let toml_str = r#"
zone_id = ""
api_key = ""
domains = [""]
max_errors_in_row = 5
"#;
let config: Config = toml::from_str(toml_str).unwrap();
assert_eq!(config.max_duration, None);
}
#[test]
fn test_serialize_none() {
let toml_to_be = r#"
zone_id = ""
api_key = ""
domains = [""]
"#;
let config = Config {
zone_id: "".into(),
api_key: "".into(),
domains: vec!["".into()],
max_errors_in_row: None,
max_duration: None,
};
let toml_str = toml::to_string(&config).unwrap();
assert_eq!(toml_to_be.trim(), toml_str.trim());
}
fn main() {
let toml_str = r#"
zone_id = ""
api_key = ""
domains = [""]
max_errors_in_row = 5
max_duration = "1d 2h 30m 45s 500000000ns"
"#;
let config: Config = toml::from_str(toml_str).unwrap();
println!("{:?}", config);
let toml_out = toml::to_string(&config).unwrap();
println!("{}", toml_out);
}