Adds more persistence tests

This commit is contained in:
Dandaedre 2024-12-03 05:46:59 -06:00
parent e179620113
commit 53b8f1df3e
10 changed files with 135 additions and 69 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@
# will have compiled files and executables
debug/
target/
test/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html

View File

@ -23,7 +23,7 @@ IdleFox is a feature-rich Discord bot designed to streamline the process of find
- `votes.rs`: Manages event and vote data structures, enabling voting functionality within Discord servers.
- `persistence.rs`: Ensures data persistence and integrity between bot runs.
- `server.rs`: Provides server-related functionalities, such as displaying votes and closing ongoing votes.
- `modules.rs`: Aggregates various voting logic functions into a single module for easier management and use within the application. This includes decision-making features beyond simple vote counting.
- `modules.rs`: Aggregates various voting logic functions into a single module for easier management and use within the application. This includes decision-making features beyond simple vote counting.
- `error_handling.rs`: Handles error reporting and logging, ensuring that any issues encountered by the bot are appropriately recorded and reported.
A great emphasis is put on organizing the votes themselves, delaying posting them and editing the answers as time goes.

View File

@ -149,7 +149,7 @@ pub enum NaiveDateObject {
/// over the dates in order or direct access to any date by its index. The structure is designed to handle
/// different sources of date information, allowing for flexible data manipulation based on the provided dates.
#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Clone))]
#[cfg_attr(test, derive(PartialEq, Eq, Clone))]
pub struct NaiveDateList {
/// The inner vector
inner: Vec<NaiveDateObject>,

View File

@ -103,9 +103,12 @@ pub enum IdleError {
/// 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,
/// Indicate a backup coouldn't be done, usually means the file descriptor is busy or permission are incorrect
#[error("An error occured while trying to save file {0}")]
BackupError(std::io::Error),
/// Indicate a backup write couldn't be done, usually means the file descriptor is busy or permission are incorrect
#[error("An error occured while trying to write file {0}")]
BackupWriteError(std::io::Error),
/// Indicate a backup read couldn't be done, usually means the file descriptor is busy or permission are incorrect
#[error("An error occured while trying to read file {0}")]
BackupReadError(std::io::Error),
/// Indicate a value couldn't be serialized
#[error("An error occured while trying to serialize data {0}")]
SerdeError(serde_json::Error),

View File

@ -26,7 +26,6 @@ use crate::modules::handler;
use crate::votes::EventHolder;
use distance::levenshtein;
use poise::serenity_prelude::{self as serenity};
use poise::Modal as _;
use crate::error_handling::IdleError;
@ -92,7 +91,7 @@ pub enum VoteTypeIdentifier {
WeeklyVote,
/// Represents a short-term vote, that can be of any length and can be cast at any time without any form or plan.
ShortVote,
/// Represents a long-term vote, where the list of days voted upon extends beyond a week.
/// Represents a long-term vote, where the list of days voted upon extends beyond a week.
LongVote,
/// Indicates that the vote type has not been set yet, should only be used by default and is not a valid value.
#[default]

View File

@ -8,35 +8,103 @@
use crate::IdleError;
use serde::{de::DeserializeOwned, Serialize};
use std::{fs::File, io::Write, path::Path};
use std::io::{Read, Write};
/// Tries to load the list of pending events from disk or return a default (empty) list
pub(crate) fn load_from_disk<T>(path: &str) -> T
pub(crate) fn load_from_reader<Data, Reader>(rdr: Reader) -> Data
where
T: Default + DeserializeOwned,
Data: Default + DeserializeOwned,
Reader: Read + Sized,
{
match File::open(&path) {
Ok(f) => serde_json::from_reader(f).unwrap_or_default(),
Err(_) => T::default(),
}
serde_json::from_reader(rdr).unwrap_or_default()
}
/// Load a struct into an Arc Mutex, mapping errors into BackupError
///
/// This macro is direct sugarcoating of the load_from_disk macro
#[macro_export]
macro_rules! load_it {
($r_type:ident, $path:expr) => {
Arc::new(std::sync::Mutex::new(load_from_reader::<$r_type, File>(
File::open($path).map_err(IdleError::BackupReadError)?,
)))
};
}
/// Tries to save the list to disk and return an error on failure
pub(crate) fn save_to_disk<T>(data: T, path: &str) -> Result<(), IdleError>
pub(crate) fn save_to_writer<Data, Writer>(data: Data, mut writer: Writer) -> Result<(), IdleError>
where
T: Serialize,
Data: Serialize,
Writer: Write + Sized,
{
let mut f = File::create(path).map_err(IdleError::BackupError)?;
f.write(
serde_json::to_string_pretty(&data)
.map_err(IdleError::SerdeError)?
.as_bytes(),
)
.map_err(IdleError::BackupError)?;
// let mut f = File::create(wrtr).map_err(IdleError::BackupError)?;
writer
.write(
serde_json::to_string_pretty(&data)
.map_err(IdleError::SerdeError)?
.as_bytes(),
)
.map_err(IdleError::BackupWriteError)?;
Ok(())
}
/// Save a struct behind an Arc Mutex into a file, mapping errors into BackupError
///
/// This macro is direct sugarcoating of the save_to_writer function
///
/// # Usage
///
/// save_it!()
#[macro_export]
macro_rules! save_it {
($value:expr, $r_type:ident, $path:expr) => {
save_to_writer::<&$r_type, File>(
&$value.lock().unwrap(),
File::create($path).map_err(IdleError::BackupWriteError)?,
)?;
};
}
#[cfg(test)]
mod tests {
// use super::*;
use super::*;
use crate::votes::{EventPending, ListOngoingEvents, ListPendingEvents};
use std::fs::{create_dir, remove_dir_all, File};
use std::io::BufWriter;
use std::sync::{Arc, Mutex};
#[test]
fn test_serialisation() {
let event_og = ListOngoingEvents::default();
let mut buffer = BufWriter::new(Vec::new());
let serialisation = save_to_writer(&event_og, &mut buffer);
assert!(serialisation.is_ok());
let deserialized_data: ListOngoingEvents = load_from_reader(buffer.buffer());
assert_eq!(event_og, deserialized_data);
}
#[test]
fn test_macro_caller() {
test_macros().unwrap()
}
fn test_macros() -> Result<(), IdleError> {
let event_og = Arc::new(Mutex::new(ListPendingEvents::default()));
{
let mut e = event_og.lock().unwrap();
println!(" Here {:?}", event_og);
for _ in 0..10 {
e.add_pending_event(EventPending::default())?;
}
}
let _ = remove_dir_all("test");
let _ = create_dir("test");
save_it!(&event_og, ListPendingEvents, "test/testwrite.json");
let event_new = load_it!(ListPendingEvents, "test/testwrite.json");
assert_eq!(
event_og.lock().unwrap().to_owned(),
event_new.lock().unwrap().to_owned()
);
Ok(())
}
}

View File

@ -11,7 +11,7 @@ use super::{BallotValueType, EventIdType, UserDataType};
/// A `Ballot` represents a user interaction towards an event. This struct is used to record the details of a vote,
/// including which user casted it, which event it pertains to, and what value or option was voted for.
#[derive(Serialize, Deserialize, Debug)]
#[cfg_attr(test, derive(Clone))]
#[cfg_attr(test, derive(Clone, Eq, PartialEq))]
pub struct Ballot {
/// The event the vote refers to
linked_event: EventIdType,

View File

@ -28,7 +28,7 @@ use super::Ballot;
/// A pending event, waiting to be opened. In this state the even can be safely modified
/// and either manual input or the autostart date will start the vote.
#[derive(Serialize, Deserialize, Default)]
#[cfg_attr(test, derive(Clone))]
#[cfg_attr(test, derive(Clone, Debug, PartialEq, Eq))]
pub struct EventPending {
/// The parsed representation of the vote associated with this event.
/// It holds the details required for the voting process.
@ -44,7 +44,7 @@ impl EventPending {
}
#[derive(Serialize, Deserialize)]
#[cfg_attr(test, derive(Clone))]
#[cfg_attr(test, derive(Debug, PartialEq, Clone))]
// An ongoing event, waiting to be closed thus allowing the ballots to be counted.
pub struct EventOngoing {
/// The parsed representation of the vote associated with this event.
@ -87,7 +87,7 @@ pub struct EventClosed {
/// ongoing and planned voting events, providing a clear and organized way to manage their
/// data throughout their lifecycle.
#[derive(Debug, Serialize, Deserialize, Default)]
#[cfg_attr(test, derive(Clone))]
#[cfg_attr(test, derive(Clone, Eq, PartialEq))]
pub(super) struct ParsedVote {
/// The title of the even
pub(super) title: String,

View File

@ -1,10 +1,7 @@
//! Submodule including all helper functions to handle vote creation and evolution
//!
use chrono::NaiveDate;
use std::{
ops::{Deref, DerefMut},
sync::Arc,
};
use std::{fs::File, sync::Arc};
use super::{
events::{EventOngoing, EventPending},
@ -13,8 +10,9 @@ use super::{
};
use crate::{
ids::EventIdType,
persistence::{load_from_disk, save_to_disk},
IdleError,
load_it,
persistence::{load_from_reader, save_to_writer},
save_it, IdleError,
};
/// Adds a parsed vote to the list of planned events.
@ -215,37 +213,31 @@ pub struct EventHolder {
impl EventHolder {
pub fn try_load_from_disk() -> Result<Self, IdleError> {
Ok(Self {
pending_events: Arc::new(std::sync::Mutex::new(load_from_disk::<ListPendingEvents>(
"data/pending_events.json",
))),
ongoing_events: Arc::new(std::sync::Mutex::new(load_from_disk::<ListOngoingEvents>(
"data/ongoin_events.json",
))),
closed_events: Arc::new(std::sync::Mutex::new(load_from_disk::<ListClosedEvents>(
"data/ongoin_events.json",
))),
old_date: Arc::new(std::sync::Mutex::new(load_from_disk::<NaiveDate>(
"data/old_date.json",
))),
pending_events: load_it!(ListPendingEvents, "data/pending_events.json"),
ongoing_events: load_it!(ListOngoingEvents, "data/ongoin_events.json"),
closed_events: load_it!(ListClosedEvents, "data/closed_events.json"),
old_date: load_it!(NaiveDate, "data/old_date.json"),
..Default::default()
})
}
pub fn try_save_to_disk(holder: EventHolder) -> Result<(), IdleError> {
save_to_disk::<&ListPendingEvents>(
&holder.pending_events.lock().unwrap(),
"data/pending_events.json",
)?;
save_to_disk::<&ListOngoingEvents>(
&holder.ongoing_events.lock().unwrap(),
"data/ongoing_events.json",
)?;
save_to_disk::<&ListPendingEvents>(
&holder.pending_events.lock().unwrap(),
"data/pending_events.json",
)?;
save_to_disk::<&NaiveDate>(&holder.old_date.lock().unwrap(), "data/old_date.json")?;
save_it!(
holder.pending_events,
ListPendingEvents,
"data/pending_events.json"
);
save_it!(
holder.ongoing_events,
ListOngoingEvents,
"data/ongoing_events.json"
);
save_it!(
holder.closed_events,
ListClosedEvents,
"data/closed_events.json"
);
save_it!(&holder.old_date, NaiveDate, "data/old_date.json");
Ok(())
}
}

View File

@ -11,10 +11,8 @@
//! The module aims to provide a robust framework for managing event lifecycles within an
//! application, ensuring separation between pending and ongoing events and supporting
//! actions such as adding, launching, processing, and autoclosing events.
use std::{fs::File, io::Write};
use chrono::NaiveDateTime;
use nom::AsBytes;
use serde::{Deserialize, Serialize};
use crate::{
@ -30,8 +28,9 @@ use super::{
/// The list of pending events, not yet started
///
/// This struct represents the collection of events that have been scheduled but have not yet begun.
/// This struct represents the collection of events that have been scheduled but have not yet begun.
#[derive(Serialize, Deserialize, Default)]
#[cfg_attr(test, derive(Debug, PartialEq, Clone))]
pub struct ListPendingEvents {
/// The list of pending events
pending_events: PendingHashmap,
@ -41,18 +40,15 @@ impl ListPendingEvents {
/// Add a new event to the list of upcoming events.
///
/// This function generates a new HUID for the event before adding it to the list.
pub(super) fn add_pending_event(
&mut self,
event: EventPending,
) -> Result<EventIdType, IdleError> {
pub fn add_pending_event(&mut self, event: EventPending) -> Result<EventIdType, IdleError> {
let id = generate_id();
self.pending_events.insert(id.clone(), event);
Ok(id)
}
/// Process and activate all upcoming events if the auto-launch time has been reached.
/// Process and activate all upcoming events if the auto-launch time has been reached.
///
/// Returns an `IdleError` if there's an issue launching any event or adding it to the ongoing list.
/// Returns an `IdleError` if there's an issue launching any event or adding it to the ongoing list.
pub fn process_upcoming(
&mut self,
current_time: NaiveDateTime,
@ -108,6 +104,7 @@ impl ListPendingEvents {
///
/// This struct represents the collection of events that have been initiated but have not yet concluded.
#[derive(Serialize, Deserialize, Default)]
#[cfg_attr(test, derive(Debug, PartialEq, Clone))]
pub struct ListOngoingEvents {
/// The list of ongoing events
ongoing_events: OngoingHashmap,
@ -368,4 +365,10 @@ mod tests {
// TODO: Check the status at several points
}
#[ignore]
#[test]
fn serialisation_check() {
todo!()
}
}