Skip to content

Commit

Permalink
fix: tests so types can be tested as deser
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax committed Aug 19, 2024
1 parent f7596dd commit 54a5e77
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 62 deletions.
6 changes: 2 additions & 4 deletions src/msgs/assert_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
//! if cosmwasm feature is enabled, also assert that we can deserialize the json back to the msg
#[track_caller]
#[cfg(not(feature = "cosmwasm"))]
pub fn assert_json_ok<M>(msg: M, expected_json: serde_json::Value)
pub fn assert_json_ser<M>(msg: M, expected_json: serde_json::Value)
where
M: serde::Serialize + std::fmt::Debug + PartialEq,
{
Expand All @@ -12,8 +11,7 @@ where
}

#[track_caller]
#[cfg(feature = "cosmwasm")]
pub fn assert_json_ok<M>(msg: M, expected_json: serde_json::Value)
pub fn assert_json_deser<M>(msg: M, expected_json: serde_json::Value)
where
M: serde::Serialize + std::fmt::Debug + PartialEq + serde::de::DeserializeOwned,
{
Expand Down
17 changes: 13 additions & 4 deletions src/msgs/data_requests/execute_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde_json::json;
#[cfg(feature = "cosmwasm")]
use super::Bytes;
use super::{execute::*, ExecuteMsg, PostDataRequestArgs, RevealBody};
use crate::msgs::assert_json_ok;
use crate::msgs::*;

#[test]
fn json_commit_result() {
Expand All @@ -23,7 +23,10 @@ fn json_commit_result() {
proof: "proof".to_string(),
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand Down Expand Up @@ -98,7 +101,10 @@ fn json_post_request() {
payback_address,
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand Down Expand Up @@ -140,5 +146,8 @@ fn json_reveal_result() {
stdout: vec!["some-output".to_string()],
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}
43 changes: 32 additions & 11 deletions src/msgs/data_requests/query_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde_json::json;

use super::{data_requests::DataRequestStatus, query::QueryMsg as DrQueryMsg, QueryMsg};
use crate::msgs::assert_json_ok;
use crate::msgs::*;

#[test]
fn json_get_data_request() {
Expand All @@ -14,7 +14,10 @@ fn json_get_data_request() {
dr_id: "dr_id".to_string(),
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -30,7 +33,10 @@ fn json_get_data_request_commitment() {
public_key: "public_key".to_string(),
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -44,7 +50,10 @@ fn json_get_data_request_commitments() {
dr_id: "dr_id".to_string(),
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -60,21 +69,27 @@ fn json_get_data_request_reveal() {
public_key: "public_key".to_string(),
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
fn json_get_data_request_reveals() {
let expected_json = json!({
"get_data_request_reveals": {
"dr_id": "dr_id",
}
"get_data_request_reveals": {
"dr_id": "dr_id",
}
});
let msg: QueryMsg = DrQueryMsg::GetDataRequestReveals {
dr_id: "dr_id".to_string(),
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -88,7 +103,10 @@ fn json_get_data_result() {
dr_id: "dr_id".to_string(),
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -106,5 +124,8 @@ fn json_get_data_requests_by_status() {
limit: 10,
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}
7 changes: 5 additions & 2 deletions src/msgs/data_requests/sudo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde_json::json;
#[cfg(feature = "cosmwasm")]
use super::Bytes;
use super::{sudo::*, DataResult, SudoMsg};
use crate::msgs::assert_json_ok;
use crate::msgs::*;

#[test]
fn json_post_result() {
Expand Down Expand Up @@ -58,5 +58,8 @@ fn json_post_result() {
exit_code: 0,
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}
35 changes: 22 additions & 13 deletions src/msgs/data_requests/types_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde_json::json;
#[cfg(feature = "cosmwasm")]
use super::Bytes;
use super::{DataRequest, DataResult, HashSelf, PostDataRequestArgs, RevealBody, U128};
use crate::msgs::assert_json_ok;
use crate::msgs::*;

#[test]
fn json_data_request() {
Expand Down Expand Up @@ -44,7 +44,7 @@ fn json_data_request() {
let reveals = HashMap::new();
let height = 1;

let serialized = json!({
let expected_json = json!({
"id": id,
"version": version,
"dr_binary_id": dr_binary_id,
Expand All @@ -63,7 +63,7 @@ fn json_data_request() {
"height": height,
});

let data_request = DataRequest {
let msg = DataRequest {
id,
version: version.parse().unwrap(),
dr_binary_id,
Expand All @@ -82,7 +82,10 @@ fn json_data_request() {
height,
};

assert_json_ok(data_request, serialized);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -106,7 +109,7 @@ fn json_data_result() {
#[cfg(feature = "cosmwasm")]
let seda_payload: Bytes = "seda_payload".as_bytes().into();

let serialized = json!({
let expected_json = json!({
"version": version,
"dr_id": dr_id,
"consensus": consensus,
Expand All @@ -117,7 +120,7 @@ fn json_data_result() {
"payback_address": payback_address,
"seda_payload": seda_payload,
});
let result = DataResult {
let msg = DataResult {
version: version.parse().unwrap(),
dr_id,
consensus,
Expand All @@ -129,7 +132,10 @@ fn json_data_result() {
seda_payload,
};

assert_json_ok(result, serialized);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -142,21 +148,24 @@ fn json_reveal_body() {
#[cfg(feature = "cosmwasm")]
let reveal: Bytes = "reveal".as_bytes().into();

let serialized = json!({
let expected_json = json!({
"salt": salt,
"exit_code": exit_code,
"gas_used": gas_used,
"reveal": reveal,
});

let reveal_body = RevealBody {
let msg = RevealBody {
salt,
exit_code,
gas_used,
reveal,
};

assert_json_ok(reveal_body, serialized);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

// pub struct PostDataRequestArgs {
Expand Down Expand Up @@ -197,7 +206,7 @@ fn json_post_data_request_args() {
#[cfg(feature = "cosmwasm")]
let memo: Bytes = "memo".as_bytes().into();

let serialized = json!({
let expected_json = json!({
"version": version,
"dr_binary_id": dr_binary_id,
"dr_inputs": dr_inputs,
Expand All @@ -210,7 +219,7 @@ fn json_post_data_request_args() {
"memo": memo,
});

let args = PostDataRequestArgs {
let msg = PostDataRequestArgs {
version: version.parse().unwrap(),
dr_binary_id,
dr_inputs,
Expand All @@ -223,5 +232,5 @@ fn json_post_data_request_args() {
memo,
};

assert_json_ok(args, serialized);
assert_json_ser(msg, expected_json);
}
2 changes: 1 addition & 1 deletion src/msgs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod staking;
#[cfg(test)]
mod assert_json;
#[cfg(test)]
pub use assert_json::assert_json_ok;
pub use assert_json::*;

#[cfg_attr(feature = "cosmwasm", cw_serde)]
#[cfg_attr(not(feature = "cosmwasm"), derive(Serialize, Debug, PartialEq))]
Expand Down
22 changes: 17 additions & 5 deletions src/msgs/owner/execute_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde_json::json;

use super::{execute::*, ExecuteMsg};
use crate::msgs::assert_json_ok;
use crate::msgs::*;

#[test]
fn json_accept_ownership() {
Expand All @@ -10,7 +10,10 @@ fn json_accept_ownership() {
"accept_ownership": {}
});
let msg: ExecuteMsg = accept_ownership::Execute {}.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -24,7 +27,10 @@ fn json_add_to_allowlist() {
public_key: "public_key".to_string(),
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -38,7 +44,10 @@ fn json_remove_from_allowlist() {
public_key: "public_key".to_string(),
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -52,5 +61,8 @@ fn json_transfer_ownership() {
new_owner: "new_owner".to_string(),
}
.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}
12 changes: 9 additions & 3 deletions src/msgs/owner/query_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde_json::json;

use super::{query::QueryMsg as OwnerQueryMsg, QueryMsg};
use crate::msgs::assert_json_ok;
use crate::msgs::*;

#[test]
fn json_get_owner() {
Expand All @@ -10,7 +10,10 @@ fn json_get_owner() {
"get_owner": {}
});
let msg: QueryMsg = OwnerQueryMsg::GetOwner {}.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}

#[test]
Expand All @@ -20,5 +23,8 @@ fn json_get_pending_owner() {
"get_pending_owner": {}
});
let msg: QueryMsg = OwnerQueryMsg::GetPendingOwner {}.into();
assert_json_ok(msg, expected_json);
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
assert_json_deser(msg, expected_json);
}
Loading

0 comments on commit 54a5e77

Please sign in to comment.