From a9782b74ccf3e63802df488b1e84a2c2ae71d4e4 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sat, 11 Jan 2025 14:12:40 +0100 Subject: [PATCH] Add tests --- src/external/database/sql/mod.rs | 4 +- tests/database/mod.rs | 5 ++- tests/database/sql.rs | 66 ++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 tests/database/sql.rs diff --git a/src/external/database/sql/mod.rs b/src/external/database/sql/mod.rs index 8a02eda..4b1f198 100644 --- a/src/external/database/sql/mod.rs +++ b/src/external/database/sql/mod.rs @@ -100,7 +100,7 @@ pub struct SqlDatabase { backend: SDB, } -impl ISqlDatabaseBackend<'c>> SqlDatabase { +impl ISqlDatabaseBackend<'conn>> SqlDatabase { /// Create a new SQL database with the provided backend, e.g. /// [`backend::SqlDatabaseSqliteBackend`]. pub fn new(backend: SDB) -> Result { @@ -126,7 +126,7 @@ impl ISqlDatabaseBackend<'c>> SqlDatabase { } } -impl ISqlDatabaseBackend<'c>> IDatabase for SqlDatabase { +impl ISqlDatabaseBackend<'conn>> IDatabase for SqlDatabase { fn load(&self) -> Result { Ok(vec![]) } diff --git a/tests/database/mod.rs b/tests/database/mod.rs index 4e33b53..c088ad8 100644 --- a/tests/database/mod.rs +++ b/tests/database/mod.rs @@ -1,3 +1,4 @@ -#![cfg(feature = "database-json")] - +#[cfg(feature = "database-json")] pub mod json; +#[cfg(feature = "database-sqlite")] +pub mod sql; diff --git a/tests/database/sql.rs b/tests/database/sql.rs new file mode 100644 index 0000000..ccdedb9 --- /dev/null +++ b/tests/database/sql.rs @@ -0,0 +1,66 @@ +use std::{fs, path::PathBuf}; + +use once_cell::sync::Lazy; + +use musichoard::{ + external::database::sql::{backend::SqlDatabaseSqliteBackend, SqlDatabase}, + interface::database::IDatabase, +}; + +use crate::testlib::COLLECTION; + +pub static DATABASE_TEST_FILE: Lazy = + Lazy::new(|| fs::canonicalize("./tests/files/database/database.db").unwrap()); + +// fn expected() -> Collection { +// let mut expected = COLLECTION.to_owned(); +// for artist in expected.iter_mut() { +// for album in artist.albums.iter_mut() { +// album.tracks.clear(); +// } +// } +// expected +// } + +#[test] +fn save() { + // let file = NamedTempFile::new().unwrap(); + + let backend = SqlDatabaseSqliteBackend::new(&*DATABASE_TEST_FILE).unwrap(); + let mut database = SqlDatabase::new(backend).unwrap(); + + let write_data = COLLECTION.to_owned(); + database.save(&write_data).unwrap(); + + // let expected = fs::read_to_string(&*DATABASE_TEST_FILE).unwrap(); + // let actual = fs::read_to_string(file.path()).unwrap(); + + // assert_eq!(actual, expected); +} + +// #[test] +// fn load() { +// let backend = JsonDatabaseFileBackend::new(&*DATABASE_TEST_FILE); +// let database = JsonDatabase::new(backend); + +// let read_data: Vec = database.load().unwrap(); + +// let expected = expected(); +// assert_eq!(read_data, expected); +// } + +// #[test] +// fn reverse() { +// let file = NamedTempFile::new().unwrap(); + +// let backend = JsonDatabaseFileBackend::new(file.path()); +// let mut database = JsonDatabase::new(backend); + +// let write_data = COLLECTION.to_owned(); +// database.save(&write_data).unwrap(); +// let read_data: Vec = database.load().unwrap(); + +// // Album data is not saved into database. +// let expected = expected(); +// assert_eq!(read_data, expected); +// }