Skip to content

Commit 55c5e8c

Browse files
authored
Merge pull request #2408 from CosmWasm/memory-to-mock
Move `MemoryStorage` to `cosmwasm_std::testing::MockStorage`
2 parents 039795d + ea62466 commit 55c5e8c

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ and this project adheres to
5353
`Decimal::new(Uint128::new(value))`. ([#2399])
5454
- cosmwasm-std: Deprecate `Uint256::from_u128` and `Int256::from_i128` in favour
5555
of `::new`. ([#2399])
56+
- cosmwasm-std: Move `MemoryStorage` to `cosmwasm_std::testing::MockStorage`.
57+
([#2237])
5658

5759
## Fixed
5860

@@ -63,6 +65,7 @@ and this project adheres to
6365
[#2155]: https://github.com/CosmWasm/cosmwasm/issues/2155
6466
[#2156]: https://github.com/CosmWasm/cosmwasm/issues/2156
6567
[#2201]: https://github.com/CosmWasm/cosmwasm/issues/2201
68+
[#2237]: https://github.com/CosmWasm/cosmwasm/issues/2237
6669
[#2262]: https://github.com/CosmWasm/cosmwasm/issues/2262
6770
[#2268]: https://github.com/CosmWasm/cosmwasm/issues/2268
6871
[#2269]: https://github.com/CosmWasm/cosmwasm/issues/2269

packages/std/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ mod results;
3939
mod sections;
4040
mod serde;
4141
mod stdack;
42-
mod storage;
4342
mod timestamp;
4443
mod traits;
4544
mod types;
@@ -113,7 +112,6 @@ pub use crate::results::{DistributionMsg, StakingMsg};
113112
pub use crate::results::{GovMsg, VoteOption};
114113
pub use crate::serde::{from_json, to_json_binary, to_json_string, to_json_vec};
115114
pub use crate::stdack::StdAck;
116-
pub use crate::storage::MemoryStorage;
117115
pub use crate::timestamp::Timestamp;
118116
pub use crate::traits::{Api, HashFunction, Querier, QuerierResult, QuerierWrapper, Storage};
119117
pub use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo, MigrateInfo, TransactionInfo};

packages/std/src/testing/mock.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use crate::query::{
3838
#[cfg(feature = "cosmwasm_1_3")]
3939
use crate::query::{DelegatorWithdrawAddressResponse, DistributionQuery};
4040
use crate::results::{ContractResult, Empty, SystemResult};
41-
use crate::storage::MemoryStorage;
4241
use crate::traits::{Api, Querier, QuerierResult};
4342
use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo, TransactionInfo};
4443
use crate::{from_json, to_json_binary, Binary, Uint128};
@@ -54,6 +53,8 @@ use crate::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse};
5453
use crate::{Decimal256, DelegationRewardsResponse, DelegatorValidatorsResponse};
5554
use crate::{RecoverPubkeyError, StdError, StdResult, SystemError, VerificationError};
5655

56+
use super::MockStorage;
57+
5758
pub const MOCK_CONTRACT_ADDR: &str =
5859
"cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs";
5960

@@ -92,10 +93,6 @@ pub fn mock_dependencies_with_balances(
9293
}
9394
}
9495

95-
// Use MemoryStorage implementation (which is valid in non-testcode)
96-
// We can later make simplifications here if needed
97-
pub type MockStorage = MemoryStorage;
98-
9996
/// Default prefix used when creating Bech32 encoded address.
10097
const BECH32_PREFIX: &str = "cosmwasm";
10198

packages/std/src/testing/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
mod assertions;
77
mod message_info;
88
mod mock;
9+
mod storage;
910

1011
pub use assertions::assert_approx_eq_impl;
1112
#[cfg(test)]
@@ -23,11 +24,13 @@ pub use mock::StakingQuerier;
2324
pub use mock::{
2425
mock_dependencies, mock_dependencies_with_balance, mock_dependencies_with_balances, mock_env,
2526
mock_wasmd_attr, BankQuerier, MockApi, MockQuerier, MockQuerierCustomHandlerResult,
26-
MockStorage, MOCK_CONTRACT_ADDR,
27+
MOCK_CONTRACT_ADDR,
2728
};
2829
#[cfg(feature = "stargate")]
2930
pub use mock::{
3031
mock_ibc_channel, mock_ibc_channel_close_confirm, mock_ibc_channel_close_init,
3132
mock_ibc_channel_connect_ack, mock_ibc_channel_connect_confirm, mock_ibc_channel_open_init,
3233
mock_ibc_channel_open_try, mock_ibc_packet_ack, mock_ibc_packet_recv, mock_ibc_packet_timeout,
3334
};
35+
36+
pub use storage::MockStorage;

packages/std/src/storage.rs renamed to packages/std/src/testing/storage.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ use crate::prelude::*;
1111
use crate::traits::Storage;
1212

1313
#[derive(Default)]
14-
pub struct MemoryStorage {
14+
pub struct MockStorage {
1515
data: BTreeMap<Vec<u8>, Vec<u8>>,
1616
}
1717

18-
impl MemoryStorage {
18+
impl MockStorage {
1919
pub fn new() -> Self {
20-
MemoryStorage::default()
20+
MockStorage::default()
2121
}
2222
}
2323

24-
impl Storage for MemoryStorage {
24+
impl Storage for MockStorage {
2525
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
2626
self.data.get(key).cloned()
2727
}
@@ -68,7 +68,7 @@ impl Storage for MemoryStorage {
6868

6969
/// This debug implementation is made for inspecting storages in unit testing.
7070
/// It is made for human readability only and the output can change at any time.
71-
impl fmt::Debug for MemoryStorage {
71+
impl fmt::Debug for MockStorage {
7272
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7373
write!(f, "MemoryStorage ({} entries)", self.data.len())?;
7474
f.write_str(" {\n")?;
@@ -113,7 +113,7 @@ mod tests {
113113

114114
#[test]
115115
fn get_and_set() {
116-
let mut store = MemoryStorage::new();
116+
let mut store = MockStorage::new();
117117
assert_eq!(store.get(b"foo"), None);
118118
store.set(b"foo", b"bar");
119119
assert_eq!(store.get(b"foo"), Some(b"bar".to_vec()));
@@ -125,13 +125,13 @@ mod tests {
125125
expected = "Getting empty values from storage is not well supported at the moment."
126126
)]
127127
fn set_panics_for_empty() {
128-
let mut store = MemoryStorage::new();
128+
let mut store = MockStorage::new();
129129
store.set(b"foo", b"");
130130
}
131131

132132
#[test]
133133
fn delete() {
134-
let mut store = MemoryStorage::new();
134+
let mut store = MockStorage::new();
135135
store.set(b"foo", b"bar");
136136
store.set(b"food", b"bank");
137137
store.remove(b"foo");
@@ -143,7 +143,7 @@ mod tests {
143143
#[test]
144144
#[cfg(feature = "iterator")]
145145
fn iterator() {
146-
let mut store = MemoryStorage::new();
146+
let mut store = MockStorage::new();
147147
store.set(b"foo", b"bar");
148148

149149
// ensure we had previously set "foo" = "bar"
@@ -283,15 +283,15 @@ mod tests {
283283

284284
#[test]
285285
fn memory_storage_implements_debug() {
286-
let store = MemoryStorage::new();
286+
let store = MockStorage::new();
287287
assert_eq!(
288288
format!("{store:?}"),
289289
"MemoryStorage (0 entries) {\n\
290290
}"
291291
);
292292

293293
// With one element
294-
let mut store = MemoryStorage::new();
294+
let mut store = MockStorage::new();
295295
store.set(&[0x00, 0xAB, 0xDD], &[0xFF, 0xD5]);
296296
assert_eq!(
297297
format!("{store:?}"),
@@ -301,7 +301,7 @@ mod tests {
301301
);
302302

303303
// Sorted by key
304-
let mut store = MemoryStorage::new();
304+
let mut store = MockStorage::new();
305305
store.set(&[0x00, 0xAB, 0xDD], &[0xFF, 0xD5]);
306306
store.set(&[0x00, 0xAB, 0xEE], &[0xFF, 0xD5]);
307307
store.set(&[0x00, 0xAB, 0xCC], &[0xFF, 0xD5]);
@@ -315,7 +315,7 @@ mod tests {
315315
);
316316

317317
// Different lengths
318-
let mut store = MemoryStorage::new();
318+
let mut store = MockStorage::new();
319319
store.set(&[0xAA], &[0x11]);
320320
store.set(&[0xAA, 0xBB], &[0x11, 0x22]);
321321
store.set(&[0xAA, 0xBB, 0xCC], &[0x11, 0x22, 0x33]);

0 commit comments

Comments
 (0)