Skip to content

Commit 597efe6

Browse files
committed
Started test environement specific documentation
1 parent 15ae44f commit 597efe6

File tree

8 files changed

+124
-3
lines changed

8 files changed

+124
-3
lines changed

cw-orch/examples/mock_test.rs

+58-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
use cosmwasm_std::Addr;
1+
use std::str::FromStr;
2+
3+
use cosmwasm_std::{coins, Addr, BlockInfo, Decimal, Timestamp};
24

35
use counter_contract::{
46
contract::CounterContract,
57
msg::{ExecuteMsg, GetCountResponse, InstantiateMsg, QueryMsg},
68
};
7-
use cw_orch::prelude::{CwOrchExecute, CwOrchInstantiate, CwOrchQuery, CwOrchUpload, Mock};
9+
use cw_orch::prelude::{CallAs, CwOrchExecute, CwOrchInstantiate, CwOrchQuery, CwOrchUpload, Mock};
810

11+
/// This example shows how to create and use the cw-multi-test mock environment
912
pub fn main() {
13+
// ANCHOR: mock_creation
1014
let sender = Addr::unchecked("juno16g2rahf5846rxzp3fwlswy08fz8ccuwk03k57y");
1115

1216
let mock = Mock::new(&sender);
17+
// ANCHOR_END: mock_creation
1318

19+
// ANCHOR: mock_usage
1420
let contract_counter = CounterContract::new("mock:contract_counter", mock);
1521

1622
let upload_res = contract_counter.upload();
1723
upload_res.unwrap();
24+
// ANCHOR_END: mock_usage
1825

1926
let init_res = contract_counter.instantiate(&InstantiateMsg { count: 0 }, Some(&sender), None);
2027
init_res.unwrap();
@@ -25,3 +32,52 @@ pub fn main() {
2532
let query_res = contract_counter.query::<GetCountResponse>(&QueryMsg::GetCount {});
2633
assert_eq!(query_res.unwrap().count, 1);
2734
}
35+
36+
// This is used for documentation only
37+
// This is actually only used to avoid having the `mut` keyword inside the mock_usage anchor (only necessary for set_sender)
38+
pub fn customize() {
39+
let sender = Addr::unchecked("juno16g2rahf5846rxzp3fwlswy08fz8ccuwk03k57y");
40+
41+
let mock = Mock::new(&sender);
42+
43+
let mut contract_counter = CounterContract::new("mock:contract_counter", mock.clone());
44+
45+
// ANCHOR: mock_customization
46+
let new_sender = Addr::unchecked("entirely-new-sender");
47+
mock.set_balance(&new_sender, coins(100_000, "ujunox"))
48+
.unwrap();
49+
50+
// Reuploads as the new sender
51+
contract_counter.call_as(&new_sender).upload().unwrap();
52+
53+
// Here the contract_counter sender is again `sender`
54+
55+
// Sets the new_sender as the definite sender
56+
contract_counter.set_sender(&new_sender);
57+
58+
// From now on the contract_counter sender is `new_sender`
59+
// ANCHOR_END: mock_customization
60+
61+
// ANCHOR: deep_mock_customization
62+
mock.app
63+
.borrow_mut()
64+
.init_modules(|router, api, storage| {
65+
router.staking.add_validator(
66+
api,
67+
storage,
68+
&BlockInfo {
69+
height: 16736,
70+
time: Timestamp::from_seconds(13345762376),
71+
chain_id: "juno-1".to_string(),
72+
},
73+
cosmwasm_std::Validator {
74+
address: "new-validator-address".to_string(),
75+
commission: Decimal::from_str("0.5").unwrap(), // Greedy validator
76+
max_commission: Decimal::from_str("1").unwrap(), // Dangerous validator
77+
max_change_rate: Decimal::from_str("1").unwrap(), // Very dangerous validator
78+
},
79+
)
80+
})
81+
.unwrap();
82+
// ANCHOR_END: deep_mock_customization
83+
}

docs/src/SUMMARY.md

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
- [Scripting](./single_contract/scripting.md)
1313
- [Integration Tests](./single_contract/integration-tests.md)
1414

15+
- [Integrations](./integrations/index.md)
16+
- [Daemon (Live Chains)](./integrations/daemon.md)
17+
- [Cw Multi Test](./integrations/cw-multi-test.md)
18+
- [Osmosis Test Tube](./integrations/osmosis-test-tube.md)
19+
- [Starship](./integrations/starship.md)
20+
1521

1622
- [Workspace](./workspace/index.md)
1723
- [Structure]()
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# CW Multi Test
2+
3+
Cw Multi Test is a rust-based test framework that allows developers to test for multi-contract interactions without having to dispatch messages, storage and other variables themselves. With cw-orchestrator, most of the `cw-multi-test` logic is abstracted away, but you can still [learn more about the framework here](https://github.com/CosmWasm/cw-multi-test).
4+
5+
## Quick Start
6+
7+
The cw-multi-test integration comes at no extra cost for the developer. Creating a test environement in cw-orchestrator that leverages cw-multi-test goes along the lines of :
8+
9+
```rust,ignore
10+
use cw_orch::prelude::*;
11+
use cosmwasm_std::Addr;
12+
{{#include ../../../cw-orch/examples/mock_test.rs:mock_creation}}
13+
```
14+
15+
> **_NOTE:_** When using cw-multi-test, the addresses are not validated like on a live chain. Therefore, you can use any string format for designating addresses. For instance,`Addr::unchecked("my-first-sender")` is a valid cw-multi-test address.
16+
17+
> **_NOTE:_** When using cw-multi-test, no gas fees are charges to the sender address.
18+
19+
## Interacting with contracts
20+
21+
You can then use the resulting `Mock` variable to interact with your [contracts](../single_contract/index.md):
22+
23+
```rust,ignore
24+
{{#include ../../../cw-orch/examples/mock_test.rs:mock_usage}}
25+
```
26+
27+
When executing contracts in a cw-multi-test environment, the messages and sub-messages sent along the Response of an endpoint, will be executed as well.
28+
This environment mocks the actual on-chain execution exactly
29+
30+
> If you are using the customizable Interface Macro, you will need to have implemented the `wrapper` function for interacting the the `Mock` environment. This function wil allow you to "connect" your contract endpoints to your `Contract` struct [See the dedicated page for more details](../single_contract/interfaces.md#customizable-interface-macro).
31+
32+
33+
> **_NOTE:_** Keep in mind that cw-multi-test is based solely in rust and that a lot of actual blockchain modules are not mocked in the environment. The main cosmos modules are there (Bank, Staking), but some very useful ones (tokenfactory, ibc) as well as Stargate messages are not supported by the environment.
34+
35+
## Cloning
36+
37+
When cloning a cw_multi_test environment, you are not cloning the entire environment, but instead you are creating a new `Mock` typed variable with the same underlying `cw_multi_test::App` object reference. This is useful for objects that require to pass the chain as an object rather than by reference.
38+
The underlying `cw_multi_test::App` object is however not clonable.
39+
40+
## Additional tools
41+
42+
The `Mock` test environment allows you to change application variables (such as the balance of an account) using wrappers around the underlying `cw_multi_test::App` object. Here are some examples of those wrappers in context :
43+
44+
45+
```rust,ignore
46+
{{#include ../../../cw-orch/examples/mock_test.rs:mock_customization}}
47+
```
48+
49+
## Additional customization
50+
51+
As we don't provide wrappers around each and every functionality that cw-multi-test provides, you can also customize the underyling `cw_multi_test::App`object to your specific needs. In the following example, we create a new validator in the test environment :
52+
53+
```rust,ignore
54+
{{#include ../../../cw-orch/examples/mock_test.rs:deep_mock_customization}}
55+
````

docs/src/integrations/daemon.md

Whitespace-only changes.

docs/src/integrations/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Integrations

docs/src/integrations/osmosis-test-tube.md

Whitespace-only changes.

docs/src/integrations/starship.md

Whitespace-only changes.

docs/src/intro.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ cw-orchestrator is an advanced testing and deployment tool for CosmWasm smart-co
1111

1212
## Features
1313

14-
- **Testing**: cw-orchestrator provides a testing framework that makes it easy to write tests for CosmWasm contracts. It does this by providing a testing environment that mimics the behavior of a CosmWasm blockchain. This allows you to write tests that interact with your contract in the same way that it would be interacted with on a real blockchain. These kinds of tests allow developers to more easily test contract-to-contract interactions without having to deal with the overhead of running a local node. The testing framework also provides a number of utilities that make it easy to write tests for CosmWasm contracts. These utilities include the ability to easily set and query balances, set block height/time and more. Additionally by creating a wrapper interface around a project's deployment developers can share their testing infrastructure with other developers, allowing them to easily test how their contracts interact with the project.
14+
- **Testing**: cw-orchestrator provides a testing framework that makes it easy to write tests for CosmWasm contracts. It does this by providing a testing environment that mimics the behavior of a CosmWasm blockchain. This allows you to write tests that interact with your contract in the same way that it would be interacted with on a real blockchain. These kinds of tests allow developers to more easily test contract-to-contract interactions without having to deal with the overhead of running a local node. The testing framework also provides a number of utilities that make it easy to write tests for CosmWasm contracts. These utilities include the ability to easily set and query balances, set block height/time and more. Additionally by creating a wrapper interface around a project's deployment developers can share their testing infrastructure with other developers, allowing them to easily test how their contracts interact with the project. The testing framework supported by cw-orchestrator include :
15+
- **[Cw-Multi-Test](./integrations/cw-multi-test.md)**
16+
- **[Starship](./integrations/starship.md)**
17+
- **[Osmosis-test-tube](./integrations/osmosis-test-tube.md)**
1518

1619
- **Deployment**: cw-orchestrator also provides the ability to deploy to real networks. It does this by providing an easy to use interface to a blockchain node that can be used to submit transactions, query state and inspect transaction results.
1720

0 commit comments

Comments
 (0)