Skip to content

Commit 5208815

Browse files
authored
Merge pull request #1573 from CosmWasm/serialie-code-info
Test serialisation of WasmQuery::ContractInfo/CodeInfo, Contract/Code…
2 parents 099384a + b4261e2 commit 5208815

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

packages/std/src/errors/system_error.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,48 @@ impl std::fmt::Display for SystemError {
6565
}
6666
}
6767
}
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
use crate::{from_slice, to_vec};
73+
74+
#[test]
75+
fn system_error_no_such_contract_serialization() {
76+
let err = SystemError::NoSuchContract {
77+
addr: "gibtsnicht".to_string(),
78+
};
79+
80+
// ser
81+
let json = to_vec(&err).unwrap();
82+
assert_eq!(
83+
String::from_utf8_lossy(&json),
84+
r#"{"no_such_contract":{"addr":"gibtsnicht"}}"#,
85+
);
86+
87+
// de
88+
let err: SystemError = from_slice(br#"{"no_such_contract":{"addr":"nada"}}"#).unwrap();
89+
assert_eq!(
90+
err,
91+
SystemError::NoSuchContract {
92+
addr: "nada".to_string()
93+
}
94+
);
95+
}
96+
97+
#[test]
98+
fn system_error_no_such_code_serialization() {
99+
let err = SystemError::NoSuchCode { code_id: 13 };
100+
101+
// ser
102+
let json = to_vec(&err).unwrap();
103+
assert_eq!(
104+
String::from_utf8_lossy(&json),
105+
r#"{"no_such_code":{"code_id":13}}"#,
106+
);
107+
108+
// de
109+
let err: SystemError = from_slice(br#"{"no_such_code":{"code_id":987}}"#).unwrap();
110+
assert_eq!(err, SystemError::NoSuchCode { code_id: 987 },);
111+
}
112+
}

packages/std/src/query/wasm.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,68 @@ pub struct CodeInfoResponse {
8787

8888
#[cfg(feature = "cosmwasm_1_2")]
8989
impl QueryResponseType for CodeInfoResponse {}
90+
91+
#[cfg(test)]
92+
mod tests {
93+
use super::*;
94+
use crate::to_binary;
95+
96+
#[test]
97+
fn wasm_query_contract_info_serialization() {
98+
let query = WasmQuery::ContractInfo {
99+
contract_addr: "aabbccdd456".into(),
100+
};
101+
let json = to_binary(&query).unwrap();
102+
assert_eq!(
103+
String::from_utf8_lossy(&json),
104+
r#"{"contract_info":{"contract_addr":"aabbccdd456"}}"#,
105+
);
106+
}
107+
108+
#[test]
109+
#[cfg(feature = "cosmwasm_1_2")]
110+
fn wasm_query_code_info_serialization() {
111+
let query = WasmQuery::CodeInfo { code_id: 70 };
112+
let json = to_binary(&query).unwrap();
113+
assert_eq!(
114+
String::from_utf8_lossy(&json),
115+
r#"{"code_info":{"code_id":70}}"#,
116+
);
117+
}
118+
119+
#[test]
120+
fn contract_info_response_serialization() {
121+
let response = ContractInfoResponse {
122+
code_id: 67,
123+
creator: "jane".to_string(),
124+
admin: Some("king".to_string()),
125+
pinned: true,
126+
ibc_port: Some("wasm.123".to_string()),
127+
};
128+
let json = to_binary(&response).unwrap();
129+
assert_eq!(
130+
String::from_utf8_lossy(&json),
131+
r#"{"code_id":67,"creator":"jane","admin":"king","pinned":true,"ibc_port":"wasm.123"}"#,
132+
);
133+
}
134+
135+
#[test]
136+
#[cfg(feature = "cosmwasm_1_2")]
137+
fn code_info_response_serialization() {
138+
use crate::HexBinary;
139+
140+
let response = CodeInfoResponse {
141+
code_id: 67,
142+
creator: "jane".to_string(),
143+
checksum: HexBinary::from_hex(
144+
"f7bb7b18fb01bbf425cf4ed2cd4b7fb26a019a7fc75a4dc87e8a0b768c501f00",
145+
)
146+
.unwrap(),
147+
};
148+
let json = to_binary(&response).unwrap();
149+
assert_eq!(
150+
String::from_utf8_lossy(&json),
151+
r#"{"code_id":67,"creator":"jane","checksum":"f7bb7b18fb01bbf425cf4ed2cd4b7fb26a019a7fc75a4dc87e8a0b768c501f00"}"#,
152+
);
153+
}
154+
}

0 commit comments

Comments
 (0)