Adds a little more testing and coverage

This commit is contained in:
Dandaedre 2024-11-30 18:19:46 +01:00
parent 7bb6b78352
commit 1a055c7e2a
4 changed files with 107 additions and 10 deletions

View File

@ -500,4 +500,20 @@ pub(crate) mod tests {
// Simple roundtrip assertion
assert_eq!(list, ron::from_str(&s).unwrap());
}
#[test]
fn test_parse_single() {
let cx = NaiveDate::from_ymd_opt(2023, 2, 12);
let tv = vec![
("ven", cx, NaiveDate::from_ymd_opt(2023, 2, 17).unwrap()),
("11", cx, NaiveDate::from_ymd_opt(2023, 3, 11).unwrap()),
("22", cx, NaiveDate::from_ymd_opt(2023, 2, 22).unwrap()),
("dec 30", cx, NaiveDate::from_ymd_opt(2023, 12, 30).unwrap()),
];
for (input, context, res) in tv {
println!("{:?} ({:?}) => {:?}", input, Some(context), res);
assert_eq!(parse_single_date(input, context).unwrap(), res);
}
}
}

View File

@ -89,14 +89,19 @@ pub enum IdleError {
/// Represents an error while acquiring a Discord group
///
/// Raised when there is an error attempting to acquire the discord group
#[error("An error occured while tring to acquire the discord group: {0}")]
#[error("An error occured while trying to acquire the discord group: {0}")]
AcquiringDiscordGroup(String),
/// Indicates an error while acquiring Discord guilds
///
/// This may happen when a function was called outside of the context of a server (likely a DM to the bot).
#[error("An error occured while tring to acquire discord guilds")]
#[error("An error occured while trying to acquire discord guilds")]
AcquiringDiscordGuild,
/// Indicates an error occuring while trying to convert from a DiscordContainer into a ParsedVote
///
/// This error will happen when an incorrect string is sent to a command invocation
#[error("An error occured while trying to parse a Discord command")]
DiscordContainerParse,
}
impl From<nom::Err<nom::error::Error<&str>>> for IdleError {

View File

@ -10,13 +10,18 @@
//! can be in, such as planned, ongoing, closed, and cancelled. This helps
//! to track and manage the progression of each voting event according to
//! predefined rules and conditions.
#![allow(dead_code)]
use std::fmt::Display;
use chrono::NaiveDateTime;
use chrono::{Days, Local, NaiveDate, NaiveDateTime, NaiveTime};
use serde::{Deserialize, Serialize};
use crate::{date::NaiveDateList, io::VoteContainer, IdleError};
use crate::{
date::{parse_date_list, parse_single_date, NaiveDateList},
io::{VoteContainer, VoteTypeIdentifier},
IdleError,
};
use super::Ballot;
@ -111,12 +116,50 @@ impl ParsedVote {
}
}
impl TryFrom<VoteContainer> for ParsedVote {
type Error = IdleError;
fn try_from(value: VoteContainer) -> Result<Self, Self::Error> {
let _ = value;
todo!()
impl ParsedVote {
/// Try to parse a vote from a votecontainer
fn try_from_with_context(
value: VoteContainer,
context: Option<NaiveDate>,
vote_type: VoteTypeIdentifier,
) -> Result<Self, IdleError> {
let context = context.unwrap_or(Local::now().date_naive());
let close_date = parse_single_date(
&value.date_end.ok_or(IdleError::DiscordContainerParse)?,
Some(context),
)?
.and_time(NaiveTime::from_hms_opt(20, 00, 00).unwrap());
// TODO: Have a better auto_delete threshold
let auto_delete_date = close_date.checked_add_days(Days::new(12)).unwrap();
// TODO: Use this to have better date selection handling
match vote_type {
VoteTypeIdentifier::WeeklyVote => {
// Start is next week, end is end of week, days is all week
}
VoteTypeIdentifier::ShortVote => {
// Start is ?, end is ?, days must be given
}
VoteTypeIdentifier::LongVote => (),
VoteTypeIdentifier::Uninitialised => (),
}
Ok(Self {
title: value.title,
description: value.description,
// TODO: Have a logic to find a good start date
launch_date: parse_single_date(
&value.start_date.ok_or(IdleError::DiscordContainerParse)?,
Some(context),
)?
.and_time(NaiveTime::from_hms_opt(20, 00, 00).unwrap()),
close_date,
auto_delete_date,
votes: Vec::new(),
// TODO: How can days be empty ? Add the non_days.
list_candidates: parse_date_list(
&value.days.ok_or(IdleError::DiscordContainerParse)?,
Some(context),
)?,
})
}
}
@ -127,3 +170,35 @@ impl Display for ParsedVote {
write!(f, "{:?}", self)
}
}
#[cfg(test)]
mod test {
use super::{EventPending, ParsedVote};
use crate::io::{VoteContainer, VoteTypeIdentifier};
use chrono::NaiveDate;
#[test]
// TODO: Complete this test
fn test_display() {
let pv = ParsedVote::default();
let _ = format!("{}", pv);
let eo = EventPending::default().into_ongoing();
let _ = format!("{}", eo);
}
#[test]
#[ignore]
// TODO: Complete this test
fn test_conversion() {
let cx = NaiveDate::from_ymd_opt(2000, 12, 12);
let v = ParsedVote::try_from_with_context(
VoteContainer::default(),
cx,
VoteTypeIdentifier::WeeklyVote,
);
println!("{:?}", v);
assert!(v.is_ok());
}
}

View File

@ -36,6 +36,7 @@ pub async fn main_scheduler_loop(mut interval: tokio::time::Interval, ev_ctx: Ev
}
/// Close to finding out
#[cfg(not(tarpaulin_include))] // Just a test function
async fn fk_around(ev_ctx: &EventHolder) {
println!(
"In the fk_around function ({}):",