From 9f7c2805ca76db1099b9d03374f08abef3057434 Mon Sep 17 00:00:00 2001 From: daladim Date: Mon, 5 Apr 2021 22:21:56 +0200 Subject: [PATCH] [tests] Added transient task scenarii --- tests/scenarii.rs | 76 ++++++++++++++++++++++++++++++++++++++++------- tests/sync.rs | 17 +++++++++++ 2 files changed, 83 insertions(+), 10 deletions(-) diff --git a/tests/scenarii.rs b/tests/scenarii.rs index 972e0b3..3fcb632 100644 --- a/tests/scenarii.rs +++ b/tests/scenarii.rs @@ -505,6 +505,53 @@ pub fn scenarii_first_sync_to_server() -> Vec { } +/// This scenario tests a task added and deleted before a sync happens +pub fn scenarii_transient_task() -> Vec { + let mut tasks = Vec::new(); + + let cal = CalendarId::from("https://some.calend.ar/transient/".parse().unwrap()); + + tasks.push( + ItemScenario { + id: ItemId::random(), + initial_state: LocatedState::Local( ItemState{ + calendar: cal.clone(), + name: String::from("A task, so that the calendar actually exists"), + completed: false, + }), + local_changes_to_apply: Vec::new(), + remote_changes_to_apply: Vec::new(), + after_sync: LocatedState::BothSynced( ItemState{ + calendar: cal.clone(), + name: String::from("A task, so that the calendar actually exists"), + completed: false, + }), + } + ); + + let id_transient = ItemId::random(); + tasks.push( + ItemScenario { + id: id_transient.clone(), + initial_state: LocatedState::None, + local_changes_to_apply: vec![ + ChangeToApply::Create(cal, Item::Task( + Task::new(String::from("A transient task that will be deleted before the sync"), id_transient, SyncStatus::NotSynced, false ) + )), + + ChangeToApply::Rename(String::from("A new name")), + ChangeToApply::SetCompletion(true), + ChangeToApply::Remove, + ], + remote_changes_to_apply: Vec::new(), + after_sync: LocatedState::None, + } + ); + + tasks +} + + /// Build a `Provider` that contains the data (defined in the given scenarii) before sync pub async fn populate_test_provider_before_sync(scenarii: &[ItemScenario]) -> Provider { let mut provider = populate_test_provider(scenarii, false).await; @@ -569,17 +616,19 @@ async fn apply_changes_on_provider(provider: &mut Provider None, - LocatedState::Local(state) => Some(&state.calendar), - LocatedState::Remote(state) => Some(&state.calendar), - LocatedState::BothSynced(state) => Some(&state.calendar), + LocatedState::Local(state) => Some(state.calendar.clone()), + LocatedState::Remote(state) => Some(state.calendar.clone()), + LocatedState::BothSynced(state) => Some(state.calendar.clone()), }; + let mut calendar_id = initial_calendar_id.clone(); for local_change in &item.local_changes_to_apply { - apply_change(provider.local(), initial_calendar_id, &item.id, local_change, false).await; + calendar_id = Some(apply_change(provider.local(), calendar_id, &item.id, local_change, false).await); } + let mut calendar_id = initial_calendar_id; for remote_change in &item.remote_changes_to_apply { - apply_change(provider.remote(), initial_calendar_id, &item.id, remote_change, true).await; + calendar_id = Some(apply_change(provider.remote(), calendar_id, &item.id, remote_change, true).await); } } } @@ -602,15 +651,20 @@ async fn get_or_insert_calendar(source: &mut Cache, id: &CalendarId) } } -/// Apply a single change on a given source -async fn apply_change(source: &S, calendar_id: Option<&CalendarId>, item_id: &ItemId, change: &ChangeToApply, is_remote: bool) +/// Apply a single change on a given source, and returns the calendar ID that was modified +async fn apply_change(source: &S, calendar_id: Option, item_id: &ItemId, change: &ChangeToApply, is_remote: bool) -> CalendarId where S: CalDavSource, C: CompleteCalendar + DavCalendar, // in this test, we're using a calendar that mocks both kinds { match calendar_id { - Some(cal) => apply_changes_on_an_existing_item(source, cal, item_id, change, is_remote).await, - None => create_test_item(source, change).await, + Some(cal) => { + apply_changes_on_an_existing_item(source, &cal, item_id, change, is_remote).await; + cal + }, + None => { + create_test_item(source, change).await + }, } } @@ -653,7 +707,8 @@ where } } -async fn create_test_item(source: &S, change: &ChangeToApply) +/// Create an item, and returns the calendar ID it was inserted in +async fn create_test_item(source: &S, change: &ChangeToApply) -> CalendarId where S: CalDavSource, C: CompleteCalendar + DavCalendar, // in this test, we're using a calendar that mocks both kinds @@ -668,6 +723,7 @@ where ChangeToApply::Create(calendar_id, item) => { let cal = source.get_calendar(calendar_id).await.unwrap(); cal.lock().unwrap().add_item(item.clone()).await.unwrap(); + calendar_id.clone() }, } } diff --git a/tests/sync.rs b/tests/sync.rs index 2e76821..686dc1f 100644 --- a/tests/sync.rs +++ b/tests/sync.rs @@ -17,6 +17,8 @@ impl TestFlavour { pub fn first_sync_to_local() -> Self { Self{} } #[cfg(not(feature = "local_calendar_mocks_remote_calendars"))] pub fn first_sync_to_server() -> Self { Self{} } + #[cfg(not(feature = "local_calendar_mocks_remote_calendars"))] + pub fn transient_task() -> Self { Self{} } #[cfg(feature = "local_calendar_mocks_remote_calendars")] pub fn normal() -> Self { @@ -39,6 +41,13 @@ impl TestFlavour { } } + #[cfg(feature = "local_calendar_mocks_remote_calendars")] + pub fn transient_task() -> Self { + Self { + scenarii: scenarii::scenarii_transient_task(), + } + } + #[cfg(not(feature = "local_calendar_mocks_remote_calendars"))] pub async fn run(&self) { @@ -96,6 +105,14 @@ async fn test_sync_empty_initial_server() { flavour.run().await; } +#[tokio::test] +async fn test_sync_transient_task() { + let _ = env_logger::builder().is_test(true).try_init(); + + let flavour = TestFlavour::transient_task(); + flavour.run().await; +} + #[cfg(feature = "integration_tests")] use my_tasks::{traits::CalDavSource,