|
1 |
| -/* |
2 | 1 | #[test_only]
|
3 |
| -module voting::voting_tests { |
4 |
| - // uncomment this line to import the module |
5 |
| - // use voting::voting; |
| 2 | +module voting::voting_tests; |
| 3 | +// uncomment this line to import the module |
| 4 | +use voting::voting::{Self, AdminCap, Votes, toggle_voting, ENotInWhitelist, EInvalidProjectId, EVotingInactive}; |
| 5 | +use voting::approval::{create_shortlist, TeamOrca, approve}; |
| 6 | +use sui::test_scenario::{Self as ts, Scenario}; |
| 7 | +use sui::random::{Self,Random}; |
| 8 | +use std::debug; |
6 | 9 |
|
7 |
| - const ENotImplemented: u64 = 0; |
| 10 | +const ORGANIZER: address = @0xAAA; |
| 11 | +const Voter1: address = @0x111; |
| 12 | +const Voter2: address = @0x222; |
| 13 | +const Voter3: address = @0x333; |
| 14 | +const Voter4: address = @0x444; |
| 15 | +const Voter5: address = @0x555; |
8 | 16 |
|
9 |
| - #[test] |
10 |
| - fun test_voting() { |
11 |
| - // pass |
12 |
| - } |
| 17 | +#[test] |
| 18 | +fun initialize(): Scenario { |
| 19 | + let mut scenario = ts::begin(ORGANIZER); |
| 20 | + voting::init_for_test(scenario.ctx()); |
13 | 21 |
|
14 |
| - #[test, expected_failure(abort_code = ::voting::voting_tests::ENotImplemented)] |
15 |
| - fun test_voting_fail() { |
16 |
| - abort ENotImplemented |
17 |
| - } |
| 22 | + ts::next_tx(&mut scenario, @0x0); |
| 23 | + { |
| 24 | + random::create_for_testing(scenario.ctx()); |
| 25 | + }; |
| 26 | + |
| 27 | + ts::next_tx(&mut scenario, @0x0); |
| 28 | + let mut random_state: Random = scenario.take_shared(); |
| 29 | + { |
| 30 | + random_state.update_randomness_state_for_testing( |
| 31 | + 0, |
| 32 | + x"1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1F1FFF", |
| 33 | + scenario.ctx(), |
| 34 | + ); |
| 35 | + ts::return_shared(random_state); |
| 36 | + }; |
| 37 | + ts::next_tx(&mut scenario, ORGANIZER); |
| 38 | + { |
| 39 | + let random_state: Random = scenario.take_shared<Random>(); |
| 40 | + let cap = scenario.take_from_sender<AdminCap>(); |
| 41 | + let og_votes = scenario.take_shared<Votes>(); |
| 42 | + let shortlisted_projects = vector[1, 2, 3, 4, 5]; |
| 43 | + let whitelist_addresses = vector[Voter1, Voter2, Voter3, Voter4, Voter5]; |
| 44 | + // Guaranteed to split the team the same way due to hardcoded randomness |
| 45 | + // TeamPolarBear: @0x222, @0x555, @0x444 |
| 46 | + // TeamOrca: @0x111, @0x333 |
| 47 | + create_shortlist(&cap, &og_votes, shortlisted_projects, whitelist_addresses, &random_state, scenario.ctx()); |
| 48 | + ts::return_shared(og_votes); |
| 49 | + ts::return_shared(random_state); |
| 50 | + ts::return_to_sender(&scenario, cap); |
| 51 | + }; |
| 52 | + scenario |
| 53 | +} |
| 54 | + |
| 55 | +#[test] |
| 56 | +fun happy_path(): Scenario { |
| 57 | + let mut scenario = initialize(); |
| 58 | + |
| 59 | + let effects = ts::next_tx(&mut scenario, ORGANIZER); |
| 60 | + let approval_votes = ts::shared(&effects); |
| 61 | + { |
| 62 | + let mut team_polar_bear_projects = scenario.take_shared_by_id<Votes>(approval_votes[0]); |
| 63 | + let mut team_orca_projects = scenario.take_shared_by_id<Votes>(approval_votes[1]); |
| 64 | + let cap = scenario.take_from_sender<AdminCap>(); |
| 65 | + toggle_voting(&cap, true, &mut team_polar_bear_projects); |
| 66 | + toggle_voting(&cap, true, &mut team_orca_projects); |
| 67 | + ts::return_shared(team_polar_bear_projects); |
| 68 | + ts::return_shared(team_orca_projects); |
| 69 | + ts::return_to_sender(&scenario, cap); |
| 70 | + }; |
| 71 | + |
| 72 | + |
| 73 | + // Team Orca member votes for project 4 and 5 |
| 74 | + ts::next_tx(&mut scenario, Voter1); |
| 75 | + { |
| 76 | + let mut votes = scenario.take_shared_by_id<Votes>(approval_votes[0]); |
| 77 | + let orca = scenario.take_from_sender<TeamOrca>(); |
| 78 | + debug::print(&votes); |
| 79 | + |
| 80 | + approve<TeamOrca>(&orca, vector[4, 5], &mut votes, scenario.ctx()); |
| 81 | + |
| 82 | + ts::return_shared(votes); |
| 83 | + ts::return_to_sender(&scenario, orca); |
| 84 | + }; |
| 85 | + ts::next_tx(&mut scenario, Voter3); |
| 86 | + { |
| 87 | + let mut votes = scenario.take_shared_by_id<Votes>(approval_votes[0]); |
| 88 | + let orca = scenario.take_from_sender<TeamOrca>(); |
| 89 | + debug::print(&votes); |
| 90 | + |
| 91 | + approve<TeamOrca>(&orca, vector[4], &mut votes, scenario.ctx()); |
| 92 | + |
| 93 | + ts::return_shared(votes); |
| 94 | + ts::return_to_sender(&scenario, orca); |
| 95 | + }; |
| 96 | + // Revote #1 |
| 97 | + ts::next_tx(&mut scenario, Voter1); |
| 98 | + { |
| 99 | + let mut votes = scenario.take_shared_by_id<Votes>(approval_votes[0]); |
| 100 | + let orca = scenario.take_from_sender<TeamOrca>(); |
| 101 | + debug::print(&votes); |
| 102 | + |
| 103 | + approve<TeamOrca>(&orca, vector[5, 4], &mut votes, scenario.ctx()); |
| 104 | + |
| 105 | + ts::return_shared(votes); |
| 106 | + ts::return_to_sender(&scenario, orca); |
| 107 | + }; |
| 108 | + |
| 109 | + // Assert first vote |
| 110 | + ts::next_tx(&mut scenario, Voter1); |
| 111 | + { |
| 112 | + let votes = scenario.take_shared_by_id<Votes>(approval_votes[0]); |
| 113 | + debug::print(&votes); |
| 114 | + assert!(votes.project_list(4).project_votes() == 2); |
| 115 | + assert!(votes.project_list(5).project_votes() == 1); |
| 116 | + assert!(votes.total_votes() == 3); |
| 117 | + assert!(votes.ballots(Voter1) == vector[5, 4]); |
| 118 | + assert!(votes.ballots(Voter3) == vector[4]); |
| 119 | + ts::return_shared(votes); |
| 120 | + }; |
| 121 | + |
| 122 | + // Revote #2 |
| 123 | + ts::next_tx(&mut scenario, Voter1); |
| 124 | + { |
| 125 | + let mut votes = scenario.take_shared_by_id<Votes>(approval_votes[0]); |
| 126 | + let orca = scenario.take_from_sender<TeamOrca>(); |
| 127 | + debug::print(&votes); |
| 128 | + |
| 129 | + // Team Orca member votes for project 4 and 5 |
| 130 | + approve<TeamOrca>(&orca, vector[2], &mut votes, scenario.ctx()); |
| 131 | + |
| 132 | + ts::return_shared(votes); |
| 133 | + ts::return_to_sender(&scenario, orca); |
| 134 | + }; |
| 135 | + // Assert revote |
| 136 | + ts::next_tx(&mut scenario, Voter1); |
| 137 | + { |
| 138 | + let votes = scenario.take_shared_by_id<Votes>(approval_votes[0]); |
| 139 | + debug::print(&votes); |
| 140 | + assert!(votes.project_list(2).project_votes() == 1); |
| 141 | + assert!(votes.project_list(4).project_votes() == 1); |
| 142 | + assert!(votes.project_list(5).project_votes() == 0); |
| 143 | + assert!(votes.total_votes() == 2); |
| 144 | + assert!(votes.ballots(Voter1) == vector[2]); |
| 145 | + assert!(votes.ballots(Voter3) == vector[4]); |
| 146 | + ts::return_shared(votes); |
| 147 | + }; |
| 148 | + |
| 149 | + scenario |
| 150 | +} |
| 151 | + |
| 152 | +#[test] |
| 153 | +#[expected_failure(abort_code = EVotingInactive)] |
| 154 | +fun voting_not_active_error(): Scenario { |
| 155 | + let mut scenario = initialize(); |
| 156 | + |
| 157 | + let effects = ts::next_tx(&mut scenario, ORGANIZER); |
| 158 | + let approval_votes = ts::shared(&effects); |
| 159 | + let team_polar_bear_id = approval_votes[0]; |
| 160 | + |
| 161 | + // Team Orca member votes for project 4 and 5 |
| 162 | + ts::next_tx(&mut scenario, Voter1); |
| 163 | + { |
| 164 | + let mut votes = scenario.take_shared_by_id<Votes>(team_polar_bear_id); |
| 165 | + let orca = scenario.take_from_sender<TeamOrca>(); |
| 166 | + debug::print(&votes); |
| 167 | + |
| 168 | + approve<TeamOrca>(&orca, vector[4, 5], &mut votes, scenario.ctx()); |
| 169 | + |
| 170 | + ts::return_shared(votes); |
| 171 | + ts::return_to_sender(&scenario, orca); |
| 172 | + }; |
| 173 | + |
| 174 | + scenario |
| 175 | +} |
| 176 | + |
| 177 | +#[test] |
| 178 | +#[expected_failure(abort_code = ENotInWhitelist)] |
| 179 | +fun not_in_whitelist_error(): Scenario { |
| 180 | + let mut scenario = initialize(); |
| 181 | + |
| 182 | + let effects = ts::next_tx(&mut scenario, ORGANIZER); |
| 183 | + let approval_votes = ts::shared(&effects); |
| 184 | + let team_polar_bear_id = approval_votes[0]; |
| 185 | + let team_orca_id = approval_votes[1]; |
| 186 | + { |
| 187 | + let mut team_polar_bear_projects = scenario.take_shared_by_id<Votes>(team_polar_bear_id); |
| 188 | + let mut team_orca_projects = scenario.take_shared_by_id<Votes>(team_orca_id); |
| 189 | + let cap = scenario.take_from_sender<AdminCap>(); |
| 190 | + toggle_voting(&cap, true, &mut team_polar_bear_projects); |
| 191 | + toggle_voting(&cap, true, &mut team_orca_projects); |
| 192 | + ts::return_shared(team_polar_bear_projects); |
| 193 | + ts::return_shared(team_orca_projects); |
| 194 | + ts::return_to_sender(&scenario, cap); |
| 195 | + }; |
| 196 | + |
| 197 | + // Team Orca member votes for Team Orca projects |
| 198 | + ts::next_tx(&mut scenario, Voter1); |
| 199 | + { |
| 200 | + let mut votes = scenario.take_shared_by_id<Votes>(team_orca_id); |
| 201 | + let orca = scenario.take_from_sender<TeamOrca>(); |
| 202 | + debug::print(&votes); |
| 203 | + |
| 204 | + approve<TeamOrca>(&orca, vector[3], &mut votes, scenario.ctx()); |
| 205 | + |
| 206 | + ts::return_shared(votes); |
| 207 | + ts::return_to_sender(&scenario, orca); |
| 208 | + }; |
| 209 | + |
| 210 | + scenario |
| 211 | +} |
| 212 | + |
| 213 | +#[test] |
| 214 | +#[expected_failure(abort_code = EInvalidProjectId)] |
| 215 | +fun invalid_project_id_error(): Scenario { |
| 216 | + let mut scenario = initialize(); |
| 217 | + |
| 218 | + let effects = ts::next_tx(&mut scenario, ORGANIZER); |
| 219 | + let approval_votes = ts::shared(&effects); |
| 220 | + let team_polar_bear_id = approval_votes[0]; |
| 221 | + let team_orca_id = approval_votes[1]; |
| 222 | + { |
| 223 | + let mut team_polar_bear_projects = scenario.take_shared_by_id<Votes>(team_polar_bear_id); |
| 224 | + let mut team_orca_projects = scenario.take_shared_by_id<Votes>(team_orca_id); |
| 225 | + let cap = scenario.take_from_sender<AdminCap>(); |
| 226 | + toggle_voting(&cap, true, &mut team_polar_bear_projects); |
| 227 | + toggle_voting(&cap, true, &mut team_orca_projects); |
| 228 | + ts::return_shared(team_polar_bear_projects); |
| 229 | + ts::return_shared(team_orca_projects); |
| 230 | + ts::return_to_sender(&scenario, cap); |
| 231 | + }; |
| 232 | + |
| 233 | + // Team Orca member tries to vote for project 1 |
| 234 | + ts::next_tx(&mut scenario, Voter1); |
| 235 | + { |
| 236 | + let mut votes = scenario.take_shared_by_id<Votes>(team_polar_bear_id); |
| 237 | + let orca = scenario.take_from_sender<TeamOrca>(); |
| 238 | + debug::print(&votes); |
| 239 | + |
| 240 | + approve<TeamOrca>(&orca, vector[1], &mut votes, scenario.ctx()); |
| 241 | + |
| 242 | + ts::return_shared(votes); |
| 243 | + ts::return_to_sender(&scenario, orca); |
| 244 | + }; |
| 245 | + |
| 246 | + scenario |
18 | 247 | }
|
19 |
| -*/ |
|
0 commit comments