diff --git a/src/db.rs b/src/db.rs index 155299f..79ed59e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1410,28 +1410,6 @@ mod tests { conn } - fn setup_camera(conn: &Connection, uuid: Uuid, short_name: &str) -> i32 { - let uuid_bytes = &uuid.as_bytes()[..]; - conn.execute_named(r#" - insert into camera (uuid, short_name, description, host, username, password, - main_rtsp_path, sub_rtsp_path, retain_bytes, next_recording_id) - values (:uuid, :short_name, :description, :host, :username, :password, - :main_rtsp_path, :sub_rtsp_path, :retain_bytes, :next_recording_id) - "#, &[ - (":uuid", &uuid_bytes), - (":short_name", &short_name), - (":description", &""), - (":host", &"test-camera"), - (":username", &"foo"), - (":password", &"bar"), - (":main_rtsp_path", &"/main"), - (":sub_rtsp_path", &"/sub"), - (":retain_bytes", &42i64), - (":next_recording_id", &0i64), - ]).unwrap(); - conn.last_insert_rowid() as i32 - } - fn assert_no_recordings(db: &Database, uuid: Uuid) { let mut rows = 0; let mut camera_id = -1; @@ -1633,8 +1611,27 @@ mod tests { fn test_full_lifecycle() { testutil::init(); let conn = setup_conn(); - let camera_uuid = Uuid::new_v4(); - let camera_id = setup_camera(&conn, camera_uuid, "testcam"); + let db = Database::new(conn).unwrap(); + let camera_id = { db.lock() }.add_camera(CameraChange { + short_name: "testcam".to_owned(), + description: "".to_owned(), + host: "test-camera".to_owned(), + username: "foo".to_owned(), + password: "bar".to_owned(), + main_rtsp_path: "/main".to_owned(), + sub_rtsp_path: "/sub".to_owned(), + }).unwrap(); + { + let mut l = db.lock(); + let mut tx = l.tx().unwrap(); + tx.update_retention(camera_id, 42).unwrap(); + tx.commit().unwrap(); + } + let camera_uuid = { db.lock().cameras_by_id().get(&camera_id).unwrap().uuid }; + assert_no_recordings(&db, camera_uuid); + + // Closing and reopening the database should present the same contents. + let conn = db.close(); let db = Database::new(conn).unwrap(); assert_no_recordings(&db, camera_uuid); diff --git a/src/testutil.rs b/src/testutil.rs index 6f76de9..ec2eae7 100644 --- a/src/testutil.rs +++ b/src/testutil.rs @@ -43,11 +43,6 @@ use uuid::Uuid; static INIT: sync::Once = sync::ONCE_INIT; -lazy_static! { - pub static ref TEST_CAMERA_UUID: Uuid = - Uuid::parse_str("ce2d9bc2-0cd3-4204-9324-7b5ccb07183c").unwrap(); -} - /// id of the camera created by `TestDb::new` below. pub const TEST_CAMERA_ID: i32 = 1; @@ -73,6 +68,7 @@ pub struct TestDb { pub syncer_channel: dir::SyncerChannel, pub syncer_join: thread::JoinHandle<()>, pub tmpdir: tempdir::TempDir, + pub test_camera_uuid: Uuid, } impl TestDb { @@ -83,35 +79,34 @@ impl TestDb { let conn = rusqlite::Connection::open_in_memory().unwrap(); let schema = include_str!("schema.sql"); conn.execute_batch(schema).unwrap(); - let uuid_bytes = &TEST_CAMERA_UUID.as_bytes()[..]; - conn.execute_named(r#" - insert into camera (uuid, short_name, description, host, username, password, - main_rtsp_path, sub_rtsp_path, retain_bytes, next_recording_id) - values (:uuid, :short_name, :description, :host, :username, :password, - :main_rtsp_path, :sub_rtsp_path, :retain_bytes, :next_recording_id) - "#, &[ - (":uuid", &uuid_bytes), - (":short_name", &"test camera"), - (":description", &""), - (":host", &"test-camera"), - (":username", &"foo"), - (":password", &"bar"), - (":main_rtsp_path", &"/main"), - (":sub_rtsp_path", &"/sub"), - (":retain_bytes", &1048576i64), - (":next_recording_id", &1i64), - ]).unwrap(); - assert_eq!(TEST_CAMERA_ID as i64, conn.last_insert_rowid()); let db = sync::Arc::new(db::Database::new(conn).unwrap()); + let test_camera_uuid; + { + let mut l = db.lock(); + assert_eq!(TEST_CAMERA_ID, l.add_camera(db::CameraChange { + short_name: "test camera".to_owned(), + description: "".to_owned(), + host: "test-camera".to_owned(), + username: "foo".to_owned(), + password: "bar".to_owned(), + main_rtsp_path: "/main".to_owned(), + sub_rtsp_path: "/sub".to_owned(), + }).unwrap()); + test_camera_uuid = l.cameras_by_id().get(&TEST_CAMERA_ID).unwrap().uuid; + let mut tx = l.tx().unwrap(); + tx.update_retention(TEST_CAMERA_ID, 1048576).unwrap(); + tx.commit().unwrap(); + } let path = tmpdir.path().to_str().unwrap().to_owned(); let dir = dir::SampleFileDir::new(&path, db.clone()).unwrap(); let (syncer_channel, syncer_join) = dir::start_syncer(dir.clone()).unwrap(); - TestDb{ - db: db, - dir: dir, - syncer_channel: syncer_channel, - syncer_join: syncer_join, - tmpdir: tmpdir, + TestDb { + db, + dir, + syncer_channel, + syncer_join, + tmpdir, + test_camera_uuid, } } diff --git a/src/web.rs b/src/web.rs index c506257..d8ce81d 100644 --- a/src/web.rs +++ b/src/web.rs @@ -503,14 +503,17 @@ mod bench { use hyper; use self::test::Bencher; use testutil::{self, TestDb}; + use uuid::Uuid; struct Server { base_url: String, + test_camera_uuid: Uuid, } impl Server { fn new() -> Server { let db = TestDb::new(); + let test_camera_uuid = db.test_camera_uuid; testutil::add_dummy_recordings_to_db(&db.db, 1440); let (tx, rx) = ::std::sync::mpsc::channel(); ::std::thread::spawn(move || { @@ -524,7 +527,10 @@ mod bench { server.run().unwrap(); }); let addr = rx.recv().unwrap(); - Server{base_url: format!("http://{}:{}", addr.ip(), addr.port())} + Server { + base_url: format!("http://{}:{}", addr.ip(), addr.port()), + test_camera_uuid, + } } } @@ -537,7 +543,7 @@ mod bench { testutil::init(); let server = &*SERVER; let url = reqwest::Url::parse(&format!("{}/api/cameras/{}/recordings", server.base_url, - *testutil::TEST_CAMERA_UUID)).unwrap(); + server.test_camera_uuid)).unwrap(); let mut buf = Vec::new(); let client = reqwest::Client::new(); let mut f = || {