[test scenarii] Ability to build expected provider state

This commit is contained in:
daladim 2021-04-03 17:43:36 +02:00
parent f760176435
commit 952fa67d99

View file

@ -59,7 +59,7 @@ pub enum ChangeToApply {
pub struct ItemScenario { pub struct ItemScenario {
id: ItemId, id: ItemId,
before_sync: LocatedState, initial_state: LocatedState,
local_changes_to_apply: Vec<ChangeToApply>, local_changes_to_apply: Vec<ChangeToApply>,
remote_changes_to_apply: Vec<ChangeToApply>, remote_changes_to_apply: Vec<ChangeToApply>,
after_sync: LocatedState, after_sync: LocatedState,
@ -86,7 +86,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task A"), name: String::from("Task A"),
completed: false, completed: false,
@ -104,7 +104,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task B"), name: String::from("Task B"),
completed: false, completed: false,
@ -118,7 +118,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task C"), name: String::from("Task C"),
completed: false, completed: false,
@ -132,7 +132,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task D"), name: String::from("Task D"),
completed: false, completed: false,
@ -150,7 +150,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task E"), name: String::from("Task E"),
completed: false, completed: false,
@ -168,7 +168,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task F"), name: String::from("Task F"),
completed: false, completed: false,
@ -187,7 +187,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task G"), name: String::from("Task G"),
completed: false, completed: false,
@ -205,7 +205,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task H"), name: String::from("Task H"),
completed: false, completed: false,
@ -223,7 +223,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task I"), name: String::from("Task I"),
completed: false, completed: false,
@ -242,7 +242,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task J"), name: String::from("Task J"),
completed: false, completed: false,
@ -256,21 +256,25 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task K"), name: String::from("Task K"),
completed: false, completed: false,
}), }),
local_changes_to_apply: vec![ChangeToApply::Remove], local_changes_to_apply: vec![ChangeToApply::Remove],
remote_changes_to_apply: vec![ChangeToApply::SetCompletion(true)], remote_changes_to_apply: vec![ChangeToApply::SetCompletion(true)],
after_sync: LocatedState::None, after_sync: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(),
name: String::from("Task K"),
completed: true,
}),
} }
); );
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task L"), name: String::from("Task L"),
completed: false, completed: false,
@ -284,7 +288,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task M"), name: String::from("Task M"),
completed: true, completed: true,
@ -302,7 +306,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task N"), name: String::from("Task N"),
completed: true, completed: true,
@ -320,7 +324,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(),
before_sync: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: main_cal.clone(), calendar: main_cal.clone(),
name: String::from("Task O"), name: String::from("Task O"),
completed: true, completed: true,
@ -339,7 +343,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: id_p.clone(), id: id_p.clone(),
before_sync: LocatedState::None, initial_state: LocatedState::None,
local_changes_to_apply: vec![ChangeToApply::Create(main_cal.clone(), Item::Task( local_changes_to_apply: vec![ChangeToApply::Create(main_cal.clone(), Item::Task(
Task::new(String::from("Task P, created locally"), id_p, SyncStatus::NotSynced, false ) Task::new(String::from("Task P, created locally"), id_p, SyncStatus::NotSynced, false )
))], ))],
@ -356,7 +360,7 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: id_q.clone(), id: id_q.clone(),
before_sync: LocatedState::None, initial_state: LocatedState::None,
local_changes_to_apply: Vec::new(), local_changes_to_apply: Vec::new(),
remote_changes_to_apply: vec![ChangeToApply::Create(main_cal.clone(), Item::Task( remote_changes_to_apply: vec![ChangeToApply::Create(main_cal.clone(), Item::Task(
Task::new(String::from("Task Q, created on the server"), id_q, SyncStatus::random_synced(), false ) Task::new(String::from("Task Q, created on the server"), id_q, SyncStatus::random_synced(), false )
@ -372,14 +376,26 @@ pub fn basic_scenarii() -> Vec<ItemScenario> {
tasks tasks
} }
/// Build a `Provider` that contains the data defined in the given scenarii /// Build a `Provider` that contains the data (defined in the given scenarii) before sync
pub async fn populate_test_provider(scenarii: &[ItemScenario]) -> Provider<Cache, CachedCalendar, Cache, CachedCalendar> { pub async fn populate_test_provider_before_sync(scenarii: &[ItemScenario]) -> Provider<Cache, CachedCalendar, Cache, CachedCalendar> {
let mut provider = populate_test_provider(scenarii, false).await;
apply_changes_on_provider(&mut provider, scenarii).await;
provider
}
/// Build a `Provider` that contains the data (defined in the given scenarii) after sync
pub async fn populate_test_provider_after_sync(scenarii: &[ItemScenario]) -> Provider<Cache, CachedCalendar, Cache, CachedCalendar> {
populate_test_provider(scenarii, true).await
}
async fn populate_test_provider(scenarii: &[ItemScenario], final_state: bool) -> Provider<Cache, CachedCalendar, Cache, CachedCalendar> {
let mut remote = Cache::new(&PathBuf::from(String::from("test_cache_remote/"))); let mut remote = Cache::new(&PathBuf::from(String::from("test_cache_remote/")));
let mut local = Cache::new(&PathBuf::from(String::from("test_cache_local/"))); let mut local = Cache::new(&PathBuf::from(String::from("test_cache_local/")));
// Create the initial state, as if we synced both sources in a given state // Create the initial state, as if we synced both sources in a given state
for item in scenarii { for item in scenarii {
let (state, sync_status) = match &item.before_sync { let required_state = if final_state { &item.after_sync } else { &item.initial_state };
let (state, sync_status) = match required_state {
LocatedState::None => continue, LocatedState::None => continue,
LocatedState::Local(s) => (s, SyncStatus::NotSynced), LocatedState::Local(s) => (s, SyncStatus::NotSynced),
LocatedState::Remote(s) => (s, SyncStatus::random_synced()), LocatedState::Remote(s) => (s, SyncStatus::random_synced()),
@ -394,7 +410,7 @@ pub async fn populate_test_provider(scenarii: &[ItemScenario]) -> Provider<Cache
state.completed, state.completed,
)); ));
match &item.before_sync { match required_state {
LocatedState::None => panic!("Should not happen, we've continued already"), LocatedState::None => panic!("Should not happen, we've continued already"),
LocatedState::Local(s) => { LocatedState::Local(s) => {
get_or_insert_calendar(&mut local, &s.calendar).await.unwrap().lock().unwrap().add_item(new_item).await.unwrap(); get_or_insert_calendar(&mut local, &s.calendar).await.unwrap().lock().unwrap().add_item(new_item).await.unwrap();
@ -408,12 +424,14 @@ pub async fn populate_test_provider(scenarii: &[ItemScenario]) -> Provider<Cache
}, },
} }
} }
let provider = Provider::new(remote, local); Provider::new(remote, local)
}
/// Apply `local_changes_to_apply` and `remote_changes_to_apply` to a provider that contains data before sync
async fn apply_changes_on_provider(provider: &mut Provider<Cache, CachedCalendar, Cache, CachedCalendar>, scenarii: &[ItemScenario]) {
// Apply changes to each item // Apply changes to each item
for item in scenarii { for item in scenarii {
let initial_calendar_id = match &item.before_sync { let initial_calendar_id = match &item.initial_state {
LocatedState::None => None, LocatedState::None => None,
LocatedState::Local(state) => Some(&state.calendar), LocatedState::Local(state) => Some(&state.calendar),
LocatedState::Remote(state) => Some(&state.calendar), LocatedState::Remote(state) => Some(&state.calendar),
@ -428,8 +446,6 @@ pub async fn populate_test_provider(scenarii: &[ItemScenario]) -> Provider<Cache
apply_change(provider.remote(), initial_calendar_id, &item.id, remote_change, true).await; apply_change(provider.remote(), initial_calendar_id, &item.id, remote_change, true).await;
} }
} }
provider
} }
async fn get_or_insert_calendar<S, C>(source: &mut S, id: &CalendarId) -> Result<Arc<Mutex<C>>, Box<dyn Error>> async fn get_or_insert_calendar<S, C>(source: &mut S, id: &CalendarId) -> Result<Arc<Mutex<C>>, Box<dyn Error>>