-
Notifications
You must be signed in to change notification settings - Fork 1
/
robot.rs
149 lines (125 loc) · 4.61 KB
/
robot.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use cosmwasm_std::{Coin, Uint128};
use test_tube::{Bank, Module, Runner, SigningAccount, Wasm};
use crate::helpers::{bank_balance_query, bank_send};
/// Implements a collection of common interactions with a `Runner`, that are all applicable to any
/// cosmos chain.
pub trait TestRobot<'a, R: Runner<'a> + 'a> {
fn runner(&self) -> &'a R;
/// Returns an instance of the [`test_tube::Wasm`] struct for the runner.
fn wasm(&self) -> Wasm<'a, R> {
Wasm::new(self.runner())
}
/// Returns an instance of the [`test_tube::Bank`] struct for the runner.
fn bank(&self) -> Bank<'a, R> {
Bank::new(self.runner())
}
/// Returns the bank balance of `denom` for the given account. Panics on error.
fn query_native_token_balance(
&self,
account: impl Into<String>,
denom: impl Into<String>,
) -> Uint128 {
bank_balance_query(self.runner(), account.into(), denom.into()).unwrap()
}
/// Asserts that the bank balance of `denom` for the given account is equal to `expected`.
/// Panics on error. Returns `self` to allow for chaining.
fn assert_native_token_balance_eq(
&self,
account: impl Into<String>,
denom: impl Into<String>,
expected: impl Into<Uint128>,
) -> &Self {
let actual = self.query_native_token_balance(account, denom);
assert_eq!(actual, expected.into());
self
}
/// Asserts that the bank balance of `denom` for the given account is greater than `expected`.
/// Panics on error. Returns `self` to allow for chaining.
fn assert_native_token_balance_gt(
&self,
account: impl Into<String>,
denom: impl Into<String>,
expected: impl Into<Uint128>,
) -> &Self {
let actual = self.query_native_token_balance(account, denom);
assert!(actual > expected.into());
self
}
/// Asserts that the bank balance of `denom` for the given account is less than `expected`.
/// Panics on error. Returns `self` to allow for chaining.
fn assert_native_token_balance_lt(
&self,
account: impl Into<String>,
denom: impl Into<String>,
expected: impl Into<Uint128>,
) -> &Self {
let actual = self.query_native_token_balance(account, denom);
assert!(actual < expected.into());
self
}
/// Sends `amount` of `denom` from `from` to `to`. Panics on error. Returns `self` to allow for
/// chaining.
fn send_native_tokens(
&self,
from: &SigningAccount,
to: impl Into<String>,
amount: impl Into<Uint128>,
denom: impl Into<String>,
) -> &Self {
let coin = Coin {
amount: amount.into(),
denom: denom.into(),
};
bank_send(self.runner(), from, &to.into(), vec![coin]).unwrap();
self
}
}
#[cfg(feature = "osmosis-test-tube")]
#[cfg(test)]
mod tests {
use osmosis_test_tube::{Account, OsmosisTestApp};
use super::*;
struct OsmosisTestAppRobot<'a>(&'a OsmosisTestApp);
impl<'a> TestRobot<'a, OsmosisTestApp> for OsmosisTestAppRobot<'a> {
fn runner(&self) -> &'a OsmosisTestApp {
self.0
}
}
#[test]
fn test_query_native_token_balance() {
let app = OsmosisTestApp::new();
let robot = OsmosisTestAppRobot(&app);
let account = app
.init_account(&[Coin::new(100_000_000_000_000_000u128, "uatom")])
.unwrap();
let balance = robot.query_native_token_balance(account.address(), "uatom");
assert_eq!(balance, Uint128::from(100_000_000_000_000_000u128));
let balance = robot.query_native_token_balance(account.address(), "uosmo");
assert_eq!(balance, Uint128::zero());
}
#[test]
fn test_send_native_tokens() {
let app = OsmosisTestApp::new();
let robot = OsmosisTestAppRobot(&app);
let account1 = app
.init_account(&[
Coin::new(100_000_000_000_000_000u128, "uatom"),
Coin::new(100_000_000_000_000_000u128, "uosmo"), //uosmo needed to pay gas
])
.unwrap();
let account2 = app.init_account(&[]).unwrap();
robot
.send_native_tokens(
&account1,
account2.address(),
1_000_000_000_000_000u128,
"uatom",
)
.assert_native_token_balance_eq(account2.address(), "uatom", 1_000_000_000_000_000u128)
.assert_native_token_balance_eq(
account1.address(),
"uatom",
99_000_000_000_000_000u128,
);
}
}