Skip to content

Commit

Permalink
Add some serde serialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
locka99 committed Jun 3, 2018
1 parent aae7b6b commit daa0ebf
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ byteorder = "0.5"
chrono = "0.4"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
rand = "0.3"
regex = "0.2"
lazy_static = "1.0"
Expand Down
6 changes: 6 additions & 0 deletions types/src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ impl<'de> Deserialize<'de> for DateTime {
}
}

impl ToString for DateTime {
fn to_string(&self) -> String {
self.date_time.to_string()
}
}

/// DateTime encoded as 64-bit signed int
impl BinaryEncoder<DateTime> for DateTime {
fn byte_len(&self) -> usize {
Expand Down
6 changes: 6 additions & 0 deletions types/src/guid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ impl<'de> Deserialize<'de> for Guid {
}
}

impl ToString for Guid {
fn to_string(&self) -> String {
self.uuid.to_string()
}
}

impl fmt::Debug for Guid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.uuid.hyphenated())
Expand Down
2 changes: 2 additions & 0 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ extern crate rand;
extern crate uuid;
extern crate url as url_external;
extern crate base64;
#[cfg(test)]
extern crate serde_json;

#[macro_export]
macro_rules! supported_message_as {
Expand Down
1 change: 1 addition & 0 deletions types/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod encoding;
mod date_time;
mod parse;
mod serialize;

use std::fmt::Debug;
use std::cmp::PartialEq;
Expand Down
47 changes: 47 additions & 0 deletions types/src/tests/serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use serde_json;

use data_value::DataValue;
use variant::Variant;
use guid::Guid;
use date_time::DateTime;
use status_codes::StatusCode;

#[test]
fn serialize_variant() {
let v = Variant::from("Hello");
let vs = serde_json::to_string(&v).unwrap();
println!("v = {}", vs);
assert_eq!(vs, r#"{"String":{"value":"Hello"}}"#);

let guid = Guid::new();
let guid_str = guid.to_string();
let v = Variant::from(guid);
let vs = serde_json::to_string(&v).unwrap();
println!("v = {}", vs);
assert_eq!(vs, format!("{{\"Guid\":\"{}\"}}", guid_str));

let dt = DateTime::now();
let dt_str = dt.to_string();
let v = Variant::from(dt);
let vs = serde_json::to_string(&v).unwrap();
println!("v = {}", vs);
assert_eq!(vs, format!("{{\"DateTime\":\"{}\"}}", dt_str));
}

#[test]
fn serialize_data_value() {
let source_timestamp = DateTime::now();
let server_timestamp = DateTime::now();
let dv = DataValue {
value: Some(Variant::from(100u16)),
status: Some(StatusCode::BadAggregateListMismatch),
source_timestamp: Some(source_timestamp.clone()),
source_picoseconds: Some(123),
server_timestamp: Some(server_timestamp.clone()),
server_picoseconds: Some(456),
};
let dvs = serde_json::to_string(&dv).unwrap();
println!("dv = {}", dvs);

assert_eq!(dvs, format!("{{\"value\":{{\"UInt16\":100}},\"status\":\"BadAggregateListMismatch\",\"source_timestamp\":\"{}\",\"source_picoseconds\":123,\"server_timestamp\":\"{}\",\"server_picoseconds\":456}}", source_timestamp.to_string(), server_timestamp.to_string()));
}

0 comments on commit daa0ebf

Please sign in to comment.