diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 6164b44..7d0f290 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -12,6 +12,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" rusqlite = { version = "0.38.0", features = ["bundled", "chrono"] } rusqlite_migration = { version = "2.4.0", features = ["from-directory"] } +rusqlite_pool = "0.2.0" include_dir = "0.7.4" chrono = {version = "0.4.43", features = ["serde"]} pushover = "0.4.0" diff --git a/backend/src/db/notifications.rs b/backend/src/db/notifications.rs new file mode 100644 index 0000000..e69de29 diff --git a/backend/tests/common/mod.rs b/backend/tests/common/mod.rs new file mode 100644 index 0000000..7179699 --- /dev/null +++ b/backend/tests/common/mod.rs @@ -0,0 +1,29 @@ +use include_dir::{Dir, include_dir}; +use log::{debug, error}; +use rusqlite::Connection; +use rusqlite_migration::Migrations; +use std::result::Result; +use std::sync::LazyLock; +use tokio::sync::Mutex; + +use crate::settings; + +static TEST_MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/tests/database_migrations"); + +// Define migrations. These are applied atomically. +static TEST_MIGRATIONS: LazyLock> = + LazyLock::new(|| Migrations::from_directory(&TEST_MIGRATIONS_DIR).unwrap()); + +static INITIALISED: Mutex = Mutex::const_new(false); + +pub async fn setup_database() { + let mut initialised = INITIALISED.lock().await; + if *initialised { + return; + } + + let conn = db::init_db().unwrap(); + TEST_MIGRATIONS.to_latest(&mut conn).unwrap(); + + *initialised = true; +} diff --git a/backend/tests/database_migrations/01-notifications/up.sql b/backend/tests/database_migrations/01-notifications/up.sql new file mode 100644 index 0000000..75feec7 --- /dev/null +++ b/backend/tests/database_migrations/01-notifications/up.sql @@ -0,0 +1 @@ +INSERT INTO NOTIFICATIONS (id, created_on, notification_type, title, body, is_new) VALUES (1, '2026-01-03 14:13:12', 'NewDeviceFound', 'Title new device found', 'Body new device found', 1);