Add a SQLite database backend #265

Merged
wojtek merged 20 commits from 248---replace-json-file-as-a-database-with-sqlite into main 2025-01-12 10:24:53 +01:00
3 changed files with 71 additions and 4 deletions
Showing only changes of commit a9782b74cc - Show all commits

View File

@ -100,7 +100,7 @@ pub struct SqlDatabase<SDB> {
backend: SDB,
}
impl<SDB: for<'c> ISqlDatabaseBackend<'c>> SqlDatabase<SDB> {
impl<SDB: for<'conn> ISqlDatabaseBackend<'conn>> SqlDatabase<SDB> {
/// Create a new SQL database with the provided backend, e.g.
/// [`backend::SqlDatabaseSqliteBackend`].
pub fn new(backend: SDB) -> Result<Self, Error> {
@ -126,7 +126,7 @@ impl<SDB: for<'c> ISqlDatabaseBackend<'c>> SqlDatabase<SDB> {
}
}
impl<SDB: for<'c> ISqlDatabaseBackend<'c>> IDatabase for SqlDatabase<SDB> {
impl<SDB: for<'conn> ISqlDatabaseBackend<'conn>> IDatabase for SqlDatabase<SDB> {
fn load(&self) -> Result<Collection, LoadError> {
Ok(vec![])
}

View File

@ -1,3 +1,4 @@
#![cfg(feature = "database-json")]
#[cfg(feature = "database-json")]
pub mod json;
#[cfg(feature = "database-sqlite")]
pub mod sql;

66
tests/database/sql.rs Normal file
View File

@ -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<PathBuf> =
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<Artist> = 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<Artist> = database.load().unwrap();
// // Album data is not saved into database.
// let expected = expected();
// assert_eq!(read_data, expected);
// }