From 427212be89b5672157c68b3a015da807c4719089 Mon Sep 17 00:00:00 2001 From: daladim Date: Sat, 3 Apr 2021 19:38:18 +0200 Subject: [PATCH] [tests] Testing a first sync from a server --- tests/scenarii.rs | 66 +++++++++++++++++++++++++++++++++- tests/sync.rs | 91 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 135 insertions(+), 22 deletions(-) diff --git a/tests/scenarii.rs b/tests/scenarii.rs index 38d1e40..71bde54 100644 --- a/tests/scenarii.rs +++ b/tests/scenarii.rs @@ -78,7 +78,7 @@ pub struct ItemScenario { /// * X': name has been modified since the last sync /// * X'/X'': name conflict /// * X✓: task has been marked as completed -pub fn basic_scenarii() -> Vec { +pub fn scenarii_basic() -> Vec { let mut tasks = Vec::new(); let main_cal = CalendarId::from("https://some.calend.ar/main/".parse().unwrap()); @@ -376,6 +376,70 @@ pub fn basic_scenarii() -> Vec { tasks } +/// This scenario basically checks a first sync to an empty local cache +pub fn scenarii_first_sync_to_local() -> Vec { + let mut tasks = Vec::new(); + + let cal1 = CalendarId::from("https://some.calend.ar/first/".parse().unwrap()); + let cal2 = CalendarId::from("https://some.calend.ar/second/".parse().unwrap()); + + tasks.push( + ItemScenario { + id: ItemId::random(), + initial_state: LocatedState::Remote( ItemState{ + calendar: cal1.clone(), + name: String::from("Task A1"), + completed: false, + }), + local_changes_to_apply: Vec::new(), + remote_changes_to_apply: Vec::new(), + after_sync: LocatedState::BothSynced( ItemState{ + calendar: cal1.clone(), + name: String::from("Task A1"), + completed: false, + }), + } + ); + + tasks.push( + ItemScenario { + id: ItemId::random(), + initial_state: LocatedState::Remote( ItemState{ + calendar: cal2.clone(), + name: String::from("Task A2"), + completed: false, + }), + local_changes_to_apply: Vec::new(), + remote_changes_to_apply: Vec::new(), + after_sync: LocatedState::BothSynced( ItemState{ + calendar: cal2.clone(), + name: String::from("Task A2"), + completed: false, + }), + } + ); + + tasks.push( + ItemScenario { + id: ItemId::random(), + initial_state: LocatedState::Remote( ItemState{ + calendar: cal1.clone(), + name: String::from("Task B1"), + completed: false, + }), + local_changes_to_apply: Vec::new(), + remote_changes_to_apply: Vec::new(), + after_sync: LocatedState::BothSynced( ItemState{ + calendar: cal1.clone(), + name: String::from("Task B1"), + completed: false, + }), + } + ); + + 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; diff --git a/tests/sync.rs b/tests/sync.rs index 8706a96..f9137a8 100644 --- a/tests/sync.rs +++ b/tests/sync.rs @@ -1,4 +1,3 @@ -#![cfg(feature = "integration_tests")] mod scenarii; use my_tasks::traits::CalDavSource; @@ -7,36 +6,86 @@ use my_tasks::cache::Cache; use my_tasks::calendar::cached_calendar::CachedCalendar; -#[tokio::test] -/// This test simulates a regular synchronisation between a local cache and a server. + +/// A test that simulates a regular synchronisation between a local cache and a server. /// Note that this uses a second cache to "mock" a server. +struct TestFlavour { + #[cfg(feature = "local_calendar_mocks_remote_calendars")] + scenarii: Vec, +} + +impl TestFlavour { + #[cfg(not(feature = "local_calendar_mocks_remote_calendars"))] + pub fn normal() -> Self { Self{} } + #[cfg(not(feature = "local_calendar_mocks_remote_calendars"))] + pub fn first_sync_to_local() -> Self { Self{} } + + #[cfg(feature = "local_calendar_mocks_remote_calendars")] + pub fn normal() -> Self { + Self { + scenarii: scenarii::scenarii_basic(), + } + } + + #[cfg(feature = "local_calendar_mocks_remote_calendars")] + pub fn first_sync_to_local() -> Self { + Self { + scenarii: scenarii::scenarii_first_sync_to_local(), + } + } + + + #[cfg(not(feature = "local_calendar_mocks_remote_calendars"))] + pub async fn run(&self) { + println!("WARNING: This test required the \"integration_tests\" Cargo feature"); + } + + #[cfg(feature = "local_calendar_mocks_remote_calendars")] + pub async fn run(&self) { + let mut provider = scenarii::populate_test_provider_before_sync(&self.scenarii).await; + + print_provider(&provider, "before sync").await; + + println!("\nsyncing...\n"); + provider.sync().await.unwrap(); + + print_provider(&provider, "after sync").await; + + // Check the contents of both sources are the same after sync + assert!(provider.remote().has_same_contents_than(provider.local()).await.unwrap()); + + // But also explicitely check that every item is expected + let expected_provider = scenarii::populate_test_provider_after_sync(&self.scenarii).await; + println!("\n"); + print_provider(&expected_provider, "expected after sync").await; + + assert!(provider.local() .has_same_contents_than(expected_provider.local() ).await.unwrap()); + assert!(provider.remote().has_same_contents_than(expected_provider.remote()).await.unwrap()); + } +} + + + + +#[tokio::test] async fn test_regular_sync() { let _ = env_logger::builder().is_test(true).try_init(); - let scenarii = scenarii::basic_scenarii(); - let mut provider = scenarii::populate_test_provider_before_sync(&scenarii).await; + let flavour = TestFlavour::normal(); + flavour.run().await; +} - print_provider(&provider, "before sync").await; +#[tokio::test] +async fn test_sync_empty_initial_local() { + let _ = env_logger::builder().is_test(true).try_init(); - println!("\nsyncing...\n"); - provider.sync().await.unwrap(); - - print_provider(&provider, "after sync").await; - - // Check the contents of both sources are the same after sync - assert!(provider.remote().has_same_contents_than(provider.local()).await.unwrap()); - - // But also explicitely check that every item is expected - let expected_provider = scenarii::populate_test_provider_after_sync(&scenarii).await; - println!("\n"); - print_provider(&expected_provider, "expected after sync").await; - - assert!(provider.local() .has_same_contents_than(expected_provider.local() ).await.unwrap()); - assert!(provider.remote().has_same_contents_than(expected_provider.remote()).await.unwrap()); + let flavour = TestFlavour::first_sync_to_local(); + flavour.run().await; } /// Print the contents of the provider. This is usually used for debugging #[allow(dead_code)] +#[cfg(feature = "integration_tests")] async fn print_provider(provider: &Provider, title: &str) { let cals_server = provider.remote().get_calendars().await.unwrap(); println!("----Server, {}-------", title);