Ability to mock behaviours for tests

This commit is contained in:
daladim 2021-04-13 23:32:07 +02:00
parent 081fc2cbc8
commit b2704bd3d2
7 changed files with 340 additions and 80 deletions

View file

@ -4,8 +4,6 @@ use std::path::PathBuf;
use std::path::Path;
use std::error::Error;
use std::collections::HashMap;
use std::collections::HashSet;
use std::hash::Hash;
use std::sync::{Arc, Mutex};
use std::ffi::OsStr;
@ -19,6 +17,9 @@ use crate::calendar::cached_calendar::CachedCalendar;
use crate::calendar::CalendarId;
use crate::calendar::SupportedComponents;
#[cfg(feature = "local_calendar_mocks_remote_calendars")]
use crate::mock_behaviour::MockBehaviour;
const MAIN_FILE: &str = "data.json";
/// A CalDAV source that stores its item in a local folder
@ -27,8 +28,9 @@ pub struct Cache {
backing_folder: PathBuf,
data: CachedData,
/// In tests, we may add forced errors to this object
#[cfg(feature = "local_calendar_mocks_remote_calendars")]
is_mocking_remote_source: bool,
mock_behaviour: Option<Arc<Mutex<MockBehaviour>>>,
}
#[derive(Default, Debug, Serialize, Deserialize)]
@ -40,8 +42,8 @@ struct CachedData {
impl Cache {
/// Activate the "mocking remote source" features (i.e. tell its children calendars that they are mocked remote calendars)
#[cfg(feature = "local_calendar_mocks_remote_calendars")]
pub fn set_is_mocking_remote_source(&mut self) {
self.is_mocking_remote_source = true;
pub fn set_mock_behaviour(&mut self, mock_behaviour: Option<Arc<Mutex<MockBehaviour>>>) {
self.mock_behaviour = mock_behaviour;
}
@ -91,7 +93,7 @@ impl Cache {
data,
#[cfg(feature = "local_calendar_mocks_remote_calendars")]
is_mocking_remote_source: false,
mock_behaviour: None,
})
}
@ -107,7 +109,7 @@ impl Cache {
data: CachedData::default(),
#[cfg(feature = "local_calendar_mocks_remote_calendars")]
is_mocking_remote_source: false,
mock_behaviour: None,
}
}
@ -169,6 +171,9 @@ impl Cache {
#[async_trait]
impl CalDavSource<CachedCalendar> for Cache {
async fn get_calendars(&self) -> Result<HashMap<CalendarId, Arc<Mutex<CachedCalendar>>>, Box<dyn Error>> {
#[cfg(feature = "local_calendar_mocks_remote_calendars")]
self.mock_behaviour.as_ref().map_or(Ok(()), |b| b.lock().unwrap().can_get_calendars())?;
Ok(self.data.calendars.iter()
.map(|(id, cal)| (id.clone(), cal.clone()))
.collect()
@ -181,13 +186,16 @@ impl CalDavSource<CachedCalendar> for Cache {
async fn create_calendar(&mut self, id: CalendarId, name: String, supported_components: SupportedComponents) -> Result<Arc<Mutex<CachedCalendar>>, Box<dyn Error>> {
log::debug!("Inserting local calendar {}", id);
#[cfg(feature = "local_calendar_mocks_remote_calendars")]
self.mock_behaviour.as_ref().map_or(Ok(()), |b| b.lock().unwrap().can_create_calendar())?;
let new_calendar = CachedCalendar::new(name, id.clone(), supported_components);
let arc = Arc::new(Mutex::new(new_calendar));
#[cfg(feature = "local_calendar_mocks_remote_calendars")]
if self.is_mocking_remote_source {
arc.lock().unwrap().set_is_mocking_remote_calendar();
}
if let Some(behaviour) = &self.mock_behaviour {
arc.lock().unwrap().set_mock_behaviour(Some(Arc::clone(behaviour)));
};
match self.data.calendars.insert(id, arc.clone()) {
Some(_) => Err("Attempt to insert calendar failed: there is alredy such a calendar.".into()),