From 20703ca921bd641a19acbf187b74207858fc8583 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 15 Feb 2023 23:09:40 -0500 Subject: [PATCH] Start adding tests for the database and file listing --- music-player/server/src/audio.rs | 17 ++++---- music-player/server/src/core.rs | 9 ++++- music-player/server/src/database.rs | 62 ++++++++++++++++++++--------- music-player/server/src/lib.rs | 4 +- 4 files changed, 63 insertions(+), 29 deletions(-) diff --git a/music-player/server/src/audio.rs b/music-player/server/src/audio.rs index 8071628..46ec41f 100644 --- a/music-player/server/src/audio.rs +++ b/music-player/server/src/audio.rs @@ -27,10 +27,10 @@ pub enum Message { #[derive(Clone, Debug)] pub enum Event { - Paused(Track, Duration), - Playing(Track, Duration), + Paused(TrackId, Duration), + Playing(TrackId, Duration), Stopped, - Position(Track, Duration), + Position(TrackId, Duration), } #[derive(Debug)] @@ -113,14 +113,16 @@ impl AsRef for TrackId { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Serialize)] pub struct TrackInfo { + pub id: TrackId, pub track_number: Option, pub name: Option, pub album: Option, pub artist: Option, } +/* #[derive(Clone, Debug, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct Track { @@ -130,6 +132,7 @@ pub struct Track { pub album: Option, pub artist: Option, } +*/ /* impl From<&mpris::Metadata> for Track { @@ -154,13 +157,13 @@ impl From for Track { #[derive(Clone, Debug, Serialize)] #[serde(rename_all = "camelCase")] pub enum State { - Playing(Track), - Paused(Track), + Playing(TrackInfo), + Paused(TrackInfo), Stopped, } pub struct CurrentlyPlaying { - track: Track, + track: TrackInfo, position: Duration, } diff --git a/music-player/server/src/core.rs b/music-player/server/src/core.rs index d85cdc7..107247e 100644 --- a/music-player/server/src/core.rs +++ b/music-player/server/src/core.rs @@ -178,4 +178,11 @@ impl Core { } #[cfg(test)] -mod test {} +mod test { + use super::*; + + #[test] + fn it_lists_tracks() { + let index = MemoryIndex::new(); + } +} diff --git a/music-player/server/src/database.rs b/music-player/server/src/database.rs index 7ee198c..125231e 100644 --- a/music-player/server/src/database.rs +++ b/music-player/server/src/database.rs @@ -1,5 +1,5 @@ use crate::{ - audio::{Track, TrackId, TrackInfo}, + audio::{TrackId, TrackInfo}, FatalError, }; use flow::{error, ok, Flow}; @@ -11,7 +11,7 @@ use std::{ }; use thiserror::Error; -#[derive(Debug, Error)] +#[derive(Debug, Error, PartialEq)] pub enum DatabaseError { #[error("database is unreadable")] DatabaseUnreadable, @@ -20,13 +20,13 @@ pub enum DatabaseError { } pub trait MusicIndex: Sync + Send { - fn add_track(&mut self, track: &TrackInfo) -> Flow; - fn remove_track(&mut self, id: &TrackId) -> Flow<(), FatalError, DatabaseError>; - fn get_track_info(&self, id: &TrackId) -> Flow, FatalError, DatabaseError>; + fn add_track(&self, track: TrackInfo) -> Flow<(), FatalError, DatabaseError>; + fn remove_track(&self, id: &TrackId) -> Flow<(), FatalError, DatabaseError>; + fn get_track_info(&self, id: &TrackId) -> Flow, FatalError, DatabaseError>; } pub struct MemoryIndex { - tracks: RwLock>, + tracks: RwLock>, } impl MemoryIndex { @@ -38,21 +38,13 @@ impl MemoryIndex { } impl MusicIndex for MemoryIndex { - fn add_track(&mut self, info: &TrackInfo) -> Flow { - let id = TrackId::default(); - let track = Track { - id: id.clone(), - track_number: info.track_number, - name: info.name.clone(), - album: info.album.clone(), - artist: info.artist.clone(), - }; + fn add_track(&self, info: TrackInfo) -> Flow<(), FatalError, DatabaseError> { let mut tracks = self.tracks.write().unwrap(); - tracks.insert(id, track.clone()); - ok(track) + tracks.insert(info.id.clone(), info); + ok(()) } - fn remove_track(&mut self, id: &TrackId) -> Flow<(), FatalError, DatabaseError> { + fn remove_track(&self, id: &TrackId) -> Flow<(), FatalError, DatabaseError> { let mut tracks = self.tracks.write().unwrap(); tracks.remove(&id); ok(()) @@ -61,7 +53,7 @@ impl MusicIndex for MemoryIndex { fn get_track_info<'a>( &'a self, id: &TrackId, - ) -> Flow, FatalError, DatabaseError> { + ) -> Flow, FatalError, DatabaseError> { let track = { let tracks = self.tracks.read().unwrap(); tracks.get(&id).cloned() @@ -104,3 +96,35 @@ impl Database { pool.push(conn); } } + +#[cfg(test)] +mod test { + use super::*; + + fn with_memory_index(f: F) + where + F: Fn(&dyn MusicIndex), + { + let index = MemoryIndex::new(); + f(&index) + } + + #[test] + fn it_saves_and_loads_data() { + with_memory_index(|index| { + let info = TrackInfo { + id: TrackId::from("track_1".to_owned()), + track_number: None, + name: None, + album: None, + artist: None, + }; + index.add_track(info.clone()); + + assert_eq!( + Flow::Ok(Some(info)), + index.get_track_info(&TrackId::from("track_1".to_owned())) + ); + }); + } +} diff --git a/music-player/server/src/lib.rs b/music-player/server/src/lib.rs index 53ec710..4d885c6 100644 --- a/music-player/server/src/lib.rs +++ b/music-player/server/src/lib.rs @@ -4,7 +4,7 @@ pub mod database; use database::DatabaseError; use thiserror::Error; -#[derive(Debug, Error)] +#[derive(Debug, Error, PartialEq)] pub enum Error { #[error("Database error: {0}")] DatabaseError(DatabaseError), @@ -16,7 +16,7 @@ impl From for Error { } } -#[derive(Debug, Error)] +#[derive(Debug, Error, PartialEq)] pub enum FatalError { #[error("Unexpected error")] UnexpectedError,