use add_camera in tests, not direct db inserts
This is a wash in terms of lines of code now, but it makes it a bit easier to maintain as I make changes to the schema (such as separating out streams from cameras), and it helps ensure the tests reflect reality.
This commit is contained in:
parent
c43fb80639
commit
0d69f4f49b
45
src/db.rs
45
src/db.rs
|
@ -1410,28 +1410,6 @@ mod tests {
|
||||||
conn
|
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) {
|
fn assert_no_recordings(db: &Database, uuid: Uuid) {
|
||||||
let mut rows = 0;
|
let mut rows = 0;
|
||||||
let mut camera_id = -1;
|
let mut camera_id = -1;
|
||||||
|
@ -1633,8 +1611,27 @@ mod tests {
|
||||||
fn test_full_lifecycle() {
|
fn test_full_lifecycle() {
|
||||||
testutil::init();
|
testutil::init();
|
||||||
let conn = setup_conn();
|
let conn = setup_conn();
|
||||||
let camera_uuid = Uuid::new_v4();
|
let db = Database::new(conn).unwrap();
|
||||||
let camera_id = setup_camera(&conn, camera_uuid, "testcam");
|
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();
|
let db = Database::new(conn).unwrap();
|
||||||
assert_no_recordings(&db, camera_uuid);
|
assert_no_recordings(&db, camera_uuid);
|
||||||
|
|
||||||
|
|
|
@ -43,11 +43,6 @@ use uuid::Uuid;
|
||||||
|
|
||||||
static INIT: sync::Once = sync::ONCE_INIT;
|
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.
|
/// id of the camera created by `TestDb::new` below.
|
||||||
pub const TEST_CAMERA_ID: i32 = 1;
|
pub const TEST_CAMERA_ID: i32 = 1;
|
||||||
|
|
||||||
|
@ -73,6 +68,7 @@ pub struct TestDb {
|
||||||
pub syncer_channel: dir::SyncerChannel,
|
pub syncer_channel: dir::SyncerChannel,
|
||||||
pub syncer_join: thread::JoinHandle<()>,
|
pub syncer_join: thread::JoinHandle<()>,
|
||||||
pub tmpdir: tempdir::TempDir,
|
pub tmpdir: tempdir::TempDir,
|
||||||
|
pub test_camera_uuid: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestDb {
|
impl TestDb {
|
||||||
|
@ -83,35 +79,34 @@ impl TestDb {
|
||||||
let conn = rusqlite::Connection::open_in_memory().unwrap();
|
let conn = rusqlite::Connection::open_in_memory().unwrap();
|
||||||
let schema = include_str!("schema.sql");
|
let schema = include_str!("schema.sql");
|
||||||
conn.execute_batch(schema).unwrap();
|
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 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 path = tmpdir.path().to_str().unwrap().to_owned();
|
||||||
let dir = dir::SampleFileDir::new(&path, db.clone()).unwrap();
|
let dir = dir::SampleFileDir::new(&path, db.clone()).unwrap();
|
||||||
let (syncer_channel, syncer_join) = dir::start_syncer(dir.clone()).unwrap();
|
let (syncer_channel, syncer_join) = dir::start_syncer(dir.clone()).unwrap();
|
||||||
TestDb{
|
TestDb {
|
||||||
db: db,
|
db,
|
||||||
dir: dir,
|
dir,
|
||||||
syncer_channel: syncer_channel,
|
syncer_channel,
|
||||||
syncer_join: syncer_join,
|
syncer_join,
|
||||||
tmpdir: tmpdir,
|
tmpdir,
|
||||||
|
test_camera_uuid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
src/web.rs
10
src/web.rs
|
@ -503,14 +503,17 @@ mod bench {
|
||||||
use hyper;
|
use hyper;
|
||||||
use self::test::Bencher;
|
use self::test::Bencher;
|
||||||
use testutil::{self, TestDb};
|
use testutil::{self, TestDb};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
struct Server {
|
struct Server {
|
||||||
base_url: String,
|
base_url: String,
|
||||||
|
test_camera_uuid: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Server {
|
impl Server {
|
||||||
fn new() -> Server {
|
fn new() -> Server {
|
||||||
let db = TestDb::new();
|
let db = TestDb::new();
|
||||||
|
let test_camera_uuid = db.test_camera_uuid;
|
||||||
testutil::add_dummy_recordings_to_db(&db.db, 1440);
|
testutil::add_dummy_recordings_to_db(&db.db, 1440);
|
||||||
let (tx, rx) = ::std::sync::mpsc::channel();
|
let (tx, rx) = ::std::sync::mpsc::channel();
|
||||||
::std::thread::spawn(move || {
|
::std::thread::spawn(move || {
|
||||||
|
@ -524,7 +527,10 @@ mod bench {
|
||||||
server.run().unwrap();
|
server.run().unwrap();
|
||||||
});
|
});
|
||||||
let addr = rx.recv().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();
|
testutil::init();
|
||||||
let server = &*SERVER;
|
let server = &*SERVER;
|
||||||
let url = reqwest::Url::parse(&format!("{}/api/cameras/{}/recordings", server.base_url,
|
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 mut buf = Vec::new();
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let mut f = || {
|
let mut f = || {
|
||||||
|
|
Loading…
Reference in New Issue