Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types (de)serialization tests #326

Merged
merged 1 commit into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion types/src/v2/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<'a> Id<'a> {

#[cfg(test)]
mod test {
use super::{Cow, Id, JsonValue, RpcParams};
use super::{Cow, Id, JsonRpcParams, JsonValue, RpcParams, SubscriptionId, TwoPointZero};

#[test]
fn id_deserialization() {
Expand Down Expand Up @@ -228,6 +228,23 @@ mod test {
assert_eq!(serialized, r#"[null,0,2,3,"3","test"]"#);
}

#[test]
fn params_serialize() {
let test_vector = &[
("null", JsonRpcParams::NoParams),
("[42,23]", JsonRpcParams::Array(serde_json::from_str("[42,23]").unwrap())),
(
r#"{"a":42,"b":null,"c":"aa"}"#,
JsonRpcParams::Map(serde_json::from_str(r#"{"a":42,"b":null,"c":"aa"}"#).unwrap()),
),
];

for (initial_ser, params) in test_vector {
let serialized = serde_json::to_string(params).unwrap();
assert_eq!(&serialized, initial_ser);
}
}

#[test]
fn params_parse() {
let none = RpcParams::new(None);
Expand All @@ -248,4 +265,25 @@ mod test {
let obj: Result<JsonValue, _> = object_params.parse();
assert!(obj.is_ok());
}

#[test]
fn two_point_zero_serde_works() {
let initial_ser = r#""2.0""#;
// The fact that it was deserialized is enough.
let two_point_zero: TwoPointZero = serde_json::from_str(initial_ser).unwrap();
let serialized = serde_json::to_string(&two_point_zero).unwrap();
assert_eq!(serialized, initial_ser);
}

#[test]
fn subscription_id_serde_works() {
let test_vector = &[("42", SubscriptionId::Num(42)), (r#""one""#, SubscriptionId::Str("one".into()))];

for (initial_ser, expected) in test_vector {
let id: SubscriptionId = serde_json::from_str(initial_ser).unwrap();
assert_eq!(&id, expected);
let serialized = serde_json::to_string(&id).unwrap();
assert_eq!(&serialized, initial_ser);
}
}
}
75 changes: 56 additions & 19 deletions types/src/v2/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,35 @@ impl<'a> JsonRpcNotificationSer<'a> {
#[cfg(test)]
mod test {
use super::{
Id, JsonRpcCallSer, JsonRpcInvalidRequest, JsonRpcNotification, JsonRpcNotificationSer, JsonRpcRequest,
TwoPointZero,
Id, JsonRpcCallSer, JsonRpcInvalidRequest, JsonRpcNotification, JsonRpcNotificationSer, JsonRpcParams,
JsonRpcRequest, TwoPointZero,
};
use serde_json::{value::RawValue, Value};

fn assert_request<'a>(request: JsonRpcRequest<'a>, id: Id<'a>, method: &str, params: Option<&str>) {
assert_eq!(request.jsonrpc, TwoPointZero);
assert_eq!(request.id, id);
assert_eq!(request.method, method);
assert_eq!(request.params.map(RawValue::get), params);
Copy link
Member

@niklasad1 niklasad1 May 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, we should implement PartialEq for the types manually that has serde_json::RawValue entrie(s) in another PR I guess.

}

/// Checks that we can deserialize the object with or without non-mandatory fields.
#[test]
fn deserialize_valid_call_works() {
let ser = r#"{"jsonrpc":"2.0","method":"say_hello","params":[1,"bar"],"id":1}"#;
let dsr: JsonRpcRequest = serde_json::from_str(ser).unwrap();
assert_eq!(dsr.method, "say_hello");
assert_eq!(dsr.jsonrpc, TwoPointZero);
fn deserialize_request() {
let method = "subtract";
let params = "[42, 23]";

let test_vector = vec![
// With all fields set.
(r#"{"jsonrpc":"2.0", "method":"subtract", "params":[42, 23], "id":1}"#, Id::Number(1), Some(params)),
// Without params field
(r#"{"jsonrpc":"2.0", "method":"subtract", "id":null}"#, Id::Null, None),
];

for (ser, id, params) in test_vector.into_iter() {
let request = serde_json::from_str(ser).unwrap();
assert_request(request, id, method, params);
}
}

#[test]
Expand All @@ -102,14 +121,6 @@ mod test {
assert_eq!(dsr.jsonrpc, TwoPointZero);
}

#[test]
fn deserialize_valid_call_without_params_works() {
let ser = r#"{"jsonrpc":"2.0","method":"say_hello", "id":1}"#;
let dsr: JsonRpcRequest = serde_json::from_str(ser).unwrap();
assert_eq!(dsr.method, "say_hello");
assert_eq!(dsr.jsonrpc, TwoPointZero);
}

// TODO(niklasad1): merge the types `JsonRpcParams` and `RpcParams` and remove `RawValue`.
#[test]
#[ignore]
Expand All @@ -131,12 +142,38 @@ mod test {
assert_eq!(deserialized, JsonRpcInvalidRequest { id: Id::Number(120) });
}

/// Checks that we can serialize the object with or without non-mandatory fields.
#[test]
fn serialize_call() {
let exp = r#"{"jsonrpc":"2.0","method":"say_hello","id":"bar","params":[]}"#;
let req = JsonRpcCallSer::new(Id::Str("bar".into()), "say_hello", vec![].into());
let ser = serde_json::to_string(&req).unwrap();
assert_eq!(exp, ser);
let method = "subtract";
let id = Id::Number(1); // It's enough to check one variant, since the type itself also has tests.
let params: JsonRpcParams = vec![Value::Number(42.into()), Value::Number(23.into())].into(); // Same as above.
let test_vector = &[
// With all fields set.
(
r#"{"jsonrpc":"2.0","method":"subtract","id":1,"params":[42,23]}"#,
Some(id.clone()),
Some(params.clone()),
),
// Without ID field.
(r#"{"jsonrpc":"2.0","method":"subtract","id":null,"params":[42,23]}"#, None, Some(params)),
// Without params field
(r#"{"jsonrpc":"2.0","method":"subtract","id":1,"params":null}"#, Some(id), None),
// Without params and ID.
(r#"{"jsonrpc":"2.0","method":"subtract","id":null,"params":null}"#, None, None),
];

for (ser, id, params) in test_vector.iter().cloned() {
let request = serde_json::to_string(&JsonRpcCallSer {
jsonrpc: TwoPointZero,
method,
id: id.unwrap_or(Id::Null),
params: params.unwrap_or(JsonRpcParams::NoParams),
})
.unwrap();

assert_eq!(&request, ser);
}
}

#[test]
Expand Down