From 1abdad71139ee6c8ca28256759b533ea5f906fd0 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Thu, 1 Oct 2020 18:07:31 -0600 Subject: [PATCH] Add test for database updating program --- sprinklers_database/src/program.rs | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/sprinklers_database/src/program.rs b/sprinklers_database/src/program.rs index bd9cb12..a81213f 100644 --- a/sprinklers_database/src/program.rs +++ b/sprinklers_database/src/program.rs @@ -157,3 +157,55 @@ VALUES (?1, ?2, ?3, ?4);"; save.commit()?; 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"); + } +}