diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/warden-core/src/configuration/conv.rs | 147 | ||||
-rw-r--r-- | lib/warden-core/src/google/parser/dt.rs | 91 | ||||
-rw-r--r-- | lib/warden-core/src/lib.rs | 1 |
3 files changed, 239 insertions, 0 deletions
diff --git a/lib/warden-core/src/configuration/conv.rs b/lib/warden-core/src/configuration/conv.rs index cbb1e88..7f982b4 100644 --- a/lib/warden-core/src/configuration/conv.rs +++ b/lib/warden-core/src/configuration/conv.rs @@ -157,3 +157,150 @@ impl TryFrom<String> for Operator { Operator::from_str_name(&value).ok_or_else(|| format!("unsupported operator: {}", value)) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::google::protobuf::{Value, value::Kind}; + use serde::{Deserialize, Serialize}; + use serde_json::{self, json}; + + #[derive(Deserialize, Serialize)] + struct OpWrap { + #[serde(with = "super::operator_serde")] + op: i32, + } + + fn normalize_json_numbers(val: serde_json::Value) -> serde_json::Value { + match val { + serde_json::Value::Array(arr) => { + serde_json::Value::Array(arr.into_iter().map(normalize_json_numbers).collect()) + } + serde_json::Value::Object(map) => serde_json::Value::Object( + map.into_iter() + .map(|(k, v)| (k, normalize_json_numbers(v))) + .collect(), + ), + serde_json::Value::Number(n) => { + if let Some(f) = n.as_f64() { + serde_json::Value::Number( + serde_json::Number::from_f64(f) + .unwrap_or_else(|| serde_json::Number::from(0)), + ) + } else { + serde_json::Value::Number(n) + } + } + other => other, + } + } + + #[test] + fn test_json_to_protobuf_value_null() { + let json = serde_json::Value::Null; + let protobuf_value = Value::try_from(json).unwrap(); + assert!(matches!(protobuf_value.kind.unwrap(), Kind::NullValue(_))); + } + + #[test] + fn test_json_to_protobuf_value_bool() { + let json = serde_json::Value::Bool(true); + let protobuf_value = Value::try_from(json).unwrap(); + assert_eq!(protobuf_value.kind.unwrap(), Kind::BoolValue(true)); + } + + #[test] + fn test_json_to_protobuf_value_number() { + let json = serde_json::Value::Number(serde_json::Number::from(42)); + let protobuf_value = Value::try_from(json).unwrap(); + assert_eq!(protobuf_value.kind.unwrap(), Kind::NumberValue(42.0)); + } + + #[test] + fn test_json_to_protobuf_value_string() { + let json = serde_json::Value::String("hello".to_string()); + let protobuf_value = Value::try_from(json).unwrap(); + assert_eq!( + protobuf_value.kind.unwrap(), + Kind::StringValue("hello".to_string()) + ); + } + + #[test] + fn test_json_to_protobuf_value_array() { + let json = json!([1, 2, "three"]); + let protobuf_value = Value::try_from(json).unwrap(); + if let Kind::ListValue(list) = protobuf_value.kind.unwrap() { + assert_eq!(list.values.len(), 3); + } else { + panic!("Expected ListValue"); + } + } + + #[test] + fn test_json_to_protobuf_value_object() { + let json = json!({ + "a": 1, + "b": true, + "c": "hello" + }); + let protobuf_value = Value::try_from(json).unwrap(); + if let Kind::StructValue(s) = protobuf_value.kind.unwrap() { + assert_eq!( + s.fields["a"].kind.as_ref().unwrap(), + &Kind::NumberValue(1.0) + ); + assert_eq!(s.fields["b"].kind.as_ref().unwrap(), &Kind::BoolValue(true)); + assert_eq!( + s.fields["c"].kind.as_ref().unwrap(), + &Kind::StringValue("hello".to_string()) + ); + } else { + panic!("Expected StructValue"); + } + } + + #[test] + fn test_protobuf_to_json_roundtrip() { + let original = json!({ + "x": 1, + "y": [true, null, "str"], + "z": { + "nested": 3.14 + } + }); + + let protobuf_value = Value::try_from(original.clone()).unwrap(); + let json_value: serde_json::Value = protobuf_value.into(); + + assert_eq!( + normalize_json_numbers(original), + normalize_json_numbers(json_value) + ); + } + + #[test] + fn test_operator_serialization() { + let wrap = OpWrap { + op: Operator::Add as i32, // Replace with actual enum variant + }; + + let s = serde_json::to_string(&wrap).unwrap(); + assert!(s.contains("ADD")); // Assuming .as_str_name() gives "ADD" + } + + #[test] + fn test_operator_deserialization() { + let json_data = json!({ "op": "ADD" }).to_string(); + let wrap: OpWrap = serde_json::from_str(&json_data).unwrap(); + + assert_eq!(wrap.op, Operator::Add as i32); // Replace with actual enum variant + } + + #[test] + fn test_operator_invalid_deserialization() { + let json_data = json!({ "op": "UNKNOWN_OP" }).to_string(); + let result: Result<OpWrap, _> = serde_json::from_str(&json_data); + assert!(result.is_err()); + } +} diff --git a/lib/warden-core/src/google/parser/dt.rs b/lib/warden-core/src/google/parser/dt.rs index 0e57833..7e61c1f 100644 --- a/lib/warden-core/src/google/parser/dt.rs +++ b/lib/warden-core/src/google/parser/dt.rs @@ -103,6 +103,97 @@ mod date { ) } } + + #[cfg(test)] + mod tests { + use super::*; + use time::{Month, OffsetDateTime}; + + use time::Date as TimeDate; + + #[test] + fn converts_dates() { + let d = TimeDate::from_calendar_date(2023, Month::August, 17).unwrap(); + let date: Date = d.into(); + + let time_date = TimeDate::try_from(date); + + assert!(time_date.is_ok()); + } + + #[test] + fn converts_regular_date_no_time() { + let d = TimeDate::from_calendar_date(2023, Month::August, 17).unwrap(); + let date: Date = d.into(); + + assert_eq!(date.year, 2023); + assert_eq!(date.month, 8); + assert_eq!(date.day, 17); + } + + #[test] + fn converts_leap_year_date() { + let d = TimeDate::from_calendar_date(2020, Month::February, 29).unwrap(); + let date: Date = d.into(); + + assert_eq!(date.year, 2020); + assert_eq!(date.month, 2); + assert_eq!(date.day, 29); + } + + #[test] + fn converts_minimum_date() { + let d = TimeDate::MIN; // Year -9999-01-01 + let date: Date = d.into(); + + assert_eq!(date.year, -9999); + assert_eq!(date.month, 1); + assert_eq!(date.day, 1); + } + + #[test] + fn converts_maximum_date() { + let d = TimeDate::MAX; // Year 9999-12-31 + let date: Date = d.into(); + + assert_eq!(date.year, 9999); + assert_eq!(date.month, 12); + assert_eq!(date.day, 31); + } + + #[test] + fn converts_regular_date() { + let dt = OffsetDateTime::from_unix_timestamp(1_600_000_000).unwrap(); // 2020-09-13 UTC + let date: Date = dt.into(); + + assert_eq!(date.year, 2020); + assert_eq!(date.month, 9); + assert_eq!(date.day, 13); + } + + #[test] + fn converts_leap_year_feb_29() { + let dt = OffsetDateTime::new_utc( + time::Date::from_calendar_date(2020, Month::February, 29).unwrap(), + time::Time::from_hms(0, 0, 0).unwrap(), + ); + let date: Date = dt.into(); + + assert_eq!(date.year, 2020); + assert_eq!(date.month, 2); + assert_eq!(date.day, 29); + } + + #[test] + fn converts_first_day_of_epoch() { + let dt = OffsetDateTime::UNIX_EPOCH; // 1970-01-01 + let date: Date = dt.into(); + + assert_eq!(date.year, 1970); + assert_eq!(date.month, 1); + assert_eq!(date.day, 1); + } + } } #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] diff --git a/lib/warden-core/src/lib.rs b/lib/warden-core/src/lib.rs index c97bef3..80fb34d 100644 --- a/lib/warden-core/src/lib.rs +++ b/lib/warden-core/src/lib.rs @@ -22,6 +22,7 @@ pub mod iso20022; /// Message in transit #[allow(missing_docs)] +#[allow(clippy::large_enum_variant)] #[cfg(feature = "message")] pub mod message; |