|
|
@ -157,3 +157,55 @@ VALUES (?1, ?2, ?3, ?4);"; |
|
|
|
save.commit()?; |
|
|
|
save.commit()?; |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
|
|
|
|
mod test { |
|
|
|
|
|
|
|
use super::*; |
|
|
|
|
|
|
|
use crate::create_migrations; |
|
|
|
|
|
|
|
use rusqlite::Connection; |
|
|
|
|
|
|
|
use sprinklers_core::schedule::Duration; |
|
|
|
|
|
|
|
use std::sync::Arc; |
|
|
|
|
|
|
|
use tracing::{debug, trace}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
|
|
|
fn test_update_program() { |
|
|
|
|
|
|
|
let mut db_conn = Connection::open_in_memory().unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let migs = create_migrations(); |
|
|
|
|
|
|
|
migs.apply(&mut db_conn).unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut programs = query_programs(&db_conn).unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// HACK: ideally ordmap would have iter_mut()
|
|
|
|
|
|
|
|
let program_ids: Vec<_> = programs.keys().cloned().collect(); |
|
|
|
|
|
|
|
let mut trans = db_conn.transaction().unwrap(); |
|
|
|
|
|
|
|
for prog_id in program_ids { |
|
|
|
|
|
|
|
let prog = &mut programs[&prog_id]; |
|
|
|
|
|
|
|
let prog = Arc::make_mut(prog); |
|
|
|
|
|
|
|
debug!(program = debug(&prog), "read program"); |
|
|
|
|
|
|
|
for _ in 0..1000 { |
|
|
|
|
|
|
|
let mut schedule = prog.schedule.clone(); |
|
|
|
|
|
|
|
if let Some(time) = schedule.times.get_mut(0) { |
|
|
|
|
|
|
|
*time += Duration::seconds(5); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
let mut sequence = prog.sequence.clone(); |
|
|
|
|
|
|
|
for item in &mut sequence { |
|
|
|
|
|
|
|
item.duration += std::time::Duration::from_secs(1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
let prog_update = ProgramUpdateData { |
|
|
|
|
|
|
|
schedule: Some(schedule), |
|
|
|
|
|
|
|
sequence: Some(sequence), |
|
|
|
|
|
|
|
..ProgramUpdateData::default() |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
trace!("about to update"); |
|
|
|
|
|
|
|
update_program(&mut trans, prog.id, &prog_update).unwrap(); |
|
|
|
|
|
|
|
trace!("updated, now querying"); |
|
|
|
|
|
|
|
*prog = query_program_by_id(&*trans, prog.id).unwrap(); |
|
|
|
|
|
|
|
trace!("updated program: {:?}", &prog); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
debug!("updated program: {:?}", &prog); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
trans.commit().unwrap(); |
|
|
|
|
|
|
|
debug!("committed final transaction"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|