Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Oct 1, 2024
1 parent 14ba78d commit eccc8d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/json_rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use {
derive_more::{Display, From, Into},
serde::{Deserialize, Serialize},
serde_aux::prelude::deserialize_string_from_number,
std::sync::Arc,
};

Expand All @@ -20,7 +21,7 @@ pub static JSON_RPC_VERSION: once_cell::sync::Lazy<Arc<str>> =
/// Represents the message ID type.
#[derive(Debug, Hash, Clone, PartialEq, Eq, Serialize, Deserialize, From, Into, Display)]
#[serde(transparent)]
pub struct MessageId(Arc<str>);
pub struct MessageId(#[serde(deserialize_with = "deserialize_string_from_number")] String);

/// Enum representing a JSON RPC Payload.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
24 changes: 9 additions & 15 deletions src/json_rpc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,25 @@ fn test_request_serialized() {
"eth_chainId".into()
)))
.unwrap(),
"{\"id\":\"1\",\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\"}"
"{\"id\":\"1\",\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\",\"params\":null}"
);
}

#[test]
fn test_request_deserialized() {
assert_eq!(
&serde_json::from_str::<JsonRpcPayload>(
"{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\"}"
&serde_json::from_str::<JsonRpcRequest>(
"{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\",\"params\":null}"
)
.unwrap(),
&JsonRpcPayload::Request(JsonRpcRequest::new(
MessageId("1".into()),
"eth_chainId".into(),
)),
&JsonRpcRequest::new(MessageId("1".into()), "eth_chainId".into(),),
);
assert_eq!(
&serde_json::from_str::<JsonRpcPayload>(
"{\"id\":\"abc\",\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\"}"
&serde_json::from_str::<JsonRpcRequest>(
"{\"id\":\"abc\",\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\",\"params\":null}"
)
.unwrap(),
&JsonRpcPayload::Request(JsonRpcRequest::new(
MessageId("abc".into()),
"eth_chainId".into(),
)),
&JsonRpcRequest::new(MessageId("abc".into()), "eth_chainId".into(),),
);
}

Expand Down Expand Up @@ -60,7 +54,7 @@ fn test_response_result() {
#[test]
fn test_response_error() {
let payload: JsonRpcPayload = JsonRpcPayload::Response(JsonRpcResponse::Error(JsonRpcError {
id: MessageId(1.to_string().into()),
id: MessageId(1.to_string()),
jsonrpc: JSON_RPC_VERSION.clone(),
error: ErrorResponse {
code: 32,
Expand All @@ -73,7 +67,7 @@ fn test_response_error() {

assert_eq!(
&serialized,
"{\"id\":1,\"jsonrpc\":\"2.0\",\"error\":{\"code\":32,\"message\":\"some message\",\"data\":null}}"
"{\"id\":\"1\",\"jsonrpc\":\"2.0\",\"error\":{\"code\":32,\"message\":\"some message\",\"data\":null}}"
);

let deserialized: JsonRpcPayload = serde_json::from_str(&serialized).unwrap();
Expand Down

0 comments on commit eccc8d3

Please sign in to comment.