[tests] Testing a first sync from a server
This commit is contained in:
parent
88af366edf
commit
427212be89
2 changed files with 135 additions and 22 deletions
|
@ -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<ItemScenario> {
|
||||
pub fn scenarii_basic() -> Vec<ItemScenario> {
|
||||
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<ItemScenario> {
|
|||
tasks
|
||||
}
|
||||
|
||||
/// This scenario basically checks a first sync to an empty local cache
|
||||
pub fn scenarii_first_sync_to_local() -> Vec<ItemScenario> {
|
||||
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<Cache, CachedCalendar, Cache, CachedCalendar> {
|
||||
let mut provider = populate_test_provider(scenarii, false).await;
|
||||
|
|
|
@ -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<scenarii::ItemScenario>,
|
||||
}
|
||||
|
||||
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<Cache, CachedCalendar, Cache, CachedCalendar>, title: &str) {
|
||||
let cals_server = provider.remote().get_calendars().await.unwrap();
|
||||
println!("----Server, {}-------", title);
|
||||
|
|
Loading…
Add table
Reference in a new issue