kitchen-freezer/tests/sync.rs

128 lines
3.7 KiB
Rust
Raw Normal View History

mod scenarii;
2021-03-22 23:42:41 +01:00
2021-02-25 00:46:32 +01:00
/// 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<scenarii::ItemScenario>,
}
2021-04-05 23:04:52 +02:00
#[cfg(not(feature = "local_calendar_mocks_remote_calendars"))]
impl TestFlavour {
pub fn normal() -> Self { Self{} }
pub fn first_sync_to_local() -> Self { Self{} }
2021-04-04 00:35:59 +02:00
pub fn first_sync_to_server() -> Self { Self{} }
2021-04-05 22:21:56 +02:00
pub fn transient_task() -> Self { Self{} }
2021-04-05 23:04:52 +02:00
pub async fn run(&self) {
println!("WARNING: This test required the \"integration_tests\" Cargo feature");
}
}
#[cfg(feature = "local_calendar_mocks_remote_calendars")]
impl TestFlavour {
pub fn normal() -> Self {
Self {
scenarii: scenarii::scenarii_basic(),
}
}
pub fn first_sync_to_local() -> Self {
Self {
scenarii: scenarii::scenarii_first_sync_to_local(),
}
}
2021-04-04 00:35:59 +02:00
pub fn first_sync_to_server() -> Self {
Self {
scenarii: scenarii::scenarii_first_sync_to_server(),
}
}
2021-04-05 22:21:56 +02:00
pub fn transient_task() -> Self {
Self {
scenarii: scenarii::scenarii_transient_task(),
}
}
pub async fn run(&self) {
let mut provider = scenarii::populate_test_provider_before_sync(&self.scenarii).await;
print_provider(&provider, "before sync").await;
2021-04-03 18:04:56 +02:00
println!("\nsyncing...\n");
provider.sync().await.unwrap();
print_provider(&provider, "after sync").await;
// Check the contents of both sources are the same after sync
2021-04-04 01:02:37 +02:00
assert!(provider.remote().has_same_observable_content_as(provider.local()).await.unwrap());
2021-02-25 00:46:32 +01:00
// 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;
2021-03-31 08:40:21 +02:00
2021-04-04 01:02:37 +02:00
assert!(provider.local() .has_same_observable_content_as(expected_provider.local() ).await.unwrap());
assert!(provider.remote().has_same_observable_content_as(expected_provider.remote()).await.unwrap());
}
}
#[tokio::test]
async fn test_regular_sync() {
let _ = env_logger::builder().is_test(true).try_init();
let flavour = TestFlavour::normal();
flavour.run().await;
}
2021-02-25 00:46:32 +01:00
#[tokio::test]
async fn test_sync_empty_initial_local() {
let _ = env_logger::builder().is_test(true).try_init();
2021-03-18 23:59:06 +01:00
let flavour = TestFlavour::first_sync_to_local();
flavour.run().await;
2021-04-03 18:04:56 +02:00
}
2021-04-04 00:35:59 +02:00
#[tokio::test]
async fn test_sync_empty_initial_server() {
let _ = env_logger::builder().is_test(true).try_init();
let flavour = TestFlavour::first_sync_to_server();
flavour.run().await;
}
2021-04-05 22:21:56 +02:00
#[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;
}
2021-04-04 00:35:59 +02:00
#[cfg(feature = "integration_tests")]
use my_tasks::{traits::CalDavSource,
Provider,
cache::Cache,
calendar::cached_calendar::CachedCalendar,
};
2021-04-03 18:04:56 +02:00
/// Print the contents of the provider. This is usually used for debugging
#[allow(dead_code)]
#[cfg(feature = "integration_tests")]
2021-04-03 18:04:56 +02:00
async fn print_provider(provider: &Provider<Cache, CachedCalendar, Cache, CachedCalendar>, title: &str) {
2021-03-22 22:39:50 +01:00
let cals_server = provider.remote().get_calendars().await.unwrap();
2021-04-03 18:04:56 +02:00
println!("----Server, {}-------", title);
2021-03-21 19:05:22 +01:00
my_tasks::utils::print_calendar_list(&cals_server).await;
2021-02-27 11:58:40 +01:00
let cals_local = provider.local().get_calendars().await.unwrap();
2021-04-03 18:04:56 +02:00
println!("-----Local, {}-------", title);
2021-03-21 19:05:22 +01:00
my_tasks::utils::print_calendar_list(&cals_local).await;
2021-02-25 00:46:32 +01:00
}