|
1 | 1 | //! This modules defines some test utilities. |
2 | 2 |
|
3 | | -use std::{collections::BTreeMap, fs, path::Path, time::Duration}; |
| 3 | +use std::{collections::BTreeMap, fs, path::Path, sync::Arc, time::Duration}; |
4 | 4 |
|
5 | 5 | use time::{OffsetDateTime, format_description::well_known::Rfc3339}; |
6 | 6 | use uuid::Uuid; |
7 | 7 |
|
8 | 8 | use crate::{ |
9 | 9 | cfg_repo::{AllowedVoters, Announcements, CfgProfile, DiscussionsAnnouncements}, |
10 | 10 | github::*, |
11 | | - results::{UserVote, Vote, VoteOption, VoteResults}, |
| 11 | + results::{UserVote, Vote, VoteOption, VoteResults, calculate}, |
12 | 12 | }; |
13 | 13 |
|
14 | 14 | pub(crate) const BRANCH: &str = "main"; |
@@ -178,3 +178,32 @@ pub(crate) fn setup_test_vote_results() -> VoteResults { |
178 | 178 | pending_voters: vec![], |
179 | 179 | } |
180 | 180 | } |
| 181 | + |
| 182 | +pub(crate) fn setup_test_vote_with_calculated_results( |
| 183 | + created_at: &str, |
| 184 | + allowed_voters: Vec<UserName>, |
| 185 | + reactions: Vec<Reaction>, |
| 186 | +) -> Vote { |
| 187 | + // Setup GitHub mock |
| 188 | + let mut gh = MockGH::new(); |
| 189 | + gh.expect_get_comment_reactions().return_once(move |_, _, _, _| { |
| 190 | + Box::pin(async move { Ok::<Vec<Reaction>, anyhow::Error>(reactions) }) |
| 191 | + }); |
| 192 | + gh.expect_get_allowed_voters().return_once(move |_, _, _, _, _| { |
| 193 | + Box::pin(async move { Ok::<Vec<UserName>, anyhow::Error>(allowed_voters) }) |
| 194 | + }); |
| 195 | + |
| 196 | + // Setup vote |
| 197 | + let mut vote = setup_test_vote(); |
| 198 | + let created_at_ts = OffsetDateTime::parse(created_at, &Rfc3339).expect("valid created_at timestamp"); |
| 199 | + vote.created_at = created_at_ts; |
| 200 | + |
| 201 | + // Calculate results and attach to vote |
| 202 | + let runtime = tokio::runtime::Runtime::new().expect("runtime creation should succeed"); |
| 203 | + let results = runtime |
| 204 | + .block_on(calculate(Arc::new(gh), OWNER, REPO, &vote)) |
| 205 | + .expect("vote calculation should succeed"); |
| 206 | + vote.results = Some(results); |
| 207 | + |
| 208 | + vote |
| 209 | +} |
0 commit comments