/
tests.rs
127 lines (117 loc) · 4.86 KB
/
tests.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
//! Tests for the recorder example contract.
use concordium_smart_contract_testing::*;
use concordium_std_derive::*;
const ACC_0: AccountAddress =
account_address!("2xBpaHottqhwFZURMZW4uZduQvpxNDSy46iXMYs9kceNGaPpZX");
const ACC_1: AccountAddress =
account_address!("2xdTv8awN1BjgYEw8W1BVXVtiEwG2b29U8KoZQqJrDuEqddseE");
const ACC_INITIAL_BALANCE: Amount = Amount::from_ccd(1000);
const SIGNER: Signer = Signer::with_one_key();
#[test]
fn tests() {
// Create the test chain.
let mut chain = Chain::new();
// Create two accounts on the chain.
chain.create_account(Account::new(ACC_0, ACC_INITIAL_BALANCE));
chain.create_account(Account::new(ACC_1, ACC_INITIAL_BALANCE));
// Load and deploy the module.
let module = module_load_v1("concordium-out/module.wasm.v1").expect("Module exists");
let deployment = chain.module_deploy_v1(SIGNER, ACC_0, module).expect("Deploy valid module");
// Initialize the contract.
let initialization = chain
.contract_init(SIGNER, ACC_0, Energy::from(10000), InitContractPayload {
amount: Amount::zero(),
mod_ref: deployment.module_reference,
init_name: OwnedContractName::new_unchecked("init_recorder".to_string()),
param: OwnedParameter::empty(),
})
.expect("Init should succeed");
let contract_address = initialization.contract_address;
// Record two addresses.
chain
.contract_update(
SIGNER,
ACC_0,
Address::Account(ACC_0),
Energy::from(5000),
UpdateContractPayload {
amount: Amount::zero(),
address: contract_address,
receive_name: OwnedReceiveName::new_unchecked("recorder.record".to_string()),
message: OwnedParameter::from_serial(&ACC_0)
.expect("Serialize account address."),
},
)
.expect("Recording `ACC_0`");
chain
.contract_update(
SIGNER,
ACC_0,
Address::Account(ACC_0),
Energy::from(5000),
UpdateContractPayload {
amount: Amount::zero(),
address: contract_address,
receive_name: OwnedReceiveName::new_unchecked("recorder.record".to_string()),
message: OwnedParameter::from_serial(&ACC_1)
.expect("Serialize account address."),
},
)
.expect("Recording `ACC_1`");
// Check that both addresses are returned by the 'list' view function.
let view_list_1 = chain
.contract_invoke(
ACC_0,
Address::Account(ACC_0),
Energy::from(5000),
UpdateContractPayload {
amount: Amount::zero(),
address: contract_address,
receive_name: OwnedReceiveName::new_unchecked("recorder.list".to_string()),
message: OwnedParameter::empty(),
},
)
.expect("Viewing list with two elements");
let returned_list_1: Vec<AccountAddress> =
view_list_1.parse_return_value().expect("Decoding return value");
assert_eq!(returned_list_1[..], [ACC_0, ACC_1]);
// Make the transfers to all accounts.
let update_transfer = chain
.contract_update(
SIGNER,
ACC_0,
Address::Account(ACC_0),
Energy::from(5000),
UpdateContractPayload {
amount: Amount::zero(),
address: contract_address,
receive_name: OwnedReceiveName::new_unchecked("recorder.transfer".to_string()),
message: OwnedParameter::empty(),
},
)
.expect("Recording`ACC_1`");
// Check that the contract returns `2` for the number of transfers made.
let transfers_made: u64 = update_transfer.parse_return_value().expect("Decoding return value.");
assert_eq!(transfers_made, 2);
assert_eq!(update_transfer.account_transfers().collect::<Vec<_>>()[..], [
(contract_address, Amount::zero(), ACC_0),
(contract_address, Amount::zero(), ACC_1)
]);
// Check that the 'list' view function now returns an empty list.
let view_list_2 = chain
.contract_invoke(
ACC_0,
Address::Account(ACC_0),
Energy::from(5000),
UpdateContractPayload {
amount: Amount::zero(),
address: contract_address,
receive_name: OwnedReceiveName::new_unchecked("recorder.list".to_string()),
message: OwnedParameter::empty(),
},
)
.expect("Viewing list with two elements");
let returned_list_2: Vec<AccountAddress> =
view_list_2.parse_return_value().expect("Decoding return value");
assert!(returned_list_2.is_empty());
}