From d2e3fd4cd813aac9277f96975250335e1d98ea44 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 5 Jan 2025 10:26:00 +0100 Subject: [PATCH] Code fix and passing UTs --- src/core/musichoard/base.rs | 43 +++++++++++++-------------------- src/core/musichoard/builder.rs | 4 --- src/core/musichoard/database.rs | 6 ++--- src/core/musichoard/library.rs | 25 +++++++++++++++---- src/core/musichoard/mod.rs | 1 - 5 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/core/musichoard/base.rs b/src/core/musichoard/base.rs index 08a04c7..c405e9b 100644 --- a/src/core/musichoard/base.rs +++ b/src/core/musichoard/base.rs @@ -33,7 +33,7 @@ pub trait IMusicHoardBasePrivate { fn sort_artists(collection: &mut [Artist]); fn sort_albums_and_tracks<'a, C: Iterator>(collection: C); - fn merge_collections(&self) -> Collection; + fn merge_collections>(&self, database: It) -> Collection; fn filter_collection(&self) -> Collection; fn filter_artist(&self, artist: &Artist) -> Option; @@ -69,19 +69,19 @@ impl IMusicHoardBasePrivate for MusicHoard } } - fn merge_collections(&self) -> Collection { - let mut primary = NormalMap::::new(); - let mut secondary = NormalMap::::new(); + fn merge_collections>(&self, database: It) -> Collection { + let mut primary_map = NormalMap::::new(); + let mut secondary_map = NormalMap::::new(); for artist in self.library_cache.iter().cloned() { - primary.insert(string::normalize_string(&artist.meta.id.name), artist); + primary_map.insert(string::normalize_string(&artist.meta.id.name), artist); } - for artist in self.database_cache.iter().cloned() { - secondary.insert(string::normalize_string(&artist.meta.id.name), artist); + for artist in database.into_iter() { + secondary_map.insert(string::normalize_string(&artist.meta.id.name), artist); } - let mut collection = MergeCollections::merge_by_name(primary, secondary); + let mut collection = MergeCollections::merge_by_name(primary_map, secondary_map); collection.sort_unstable(); collection @@ -168,21 +168,19 @@ mod tests { let mut mh = MusicHoard { library_cache: left.clone(), - database_cache: right.clone(), ..Default::default() }; - mh.collection = mh.merge_collections(); + mh.collection = mh.merge_collections(right.clone()); assert_eq!(expected, mh.collection); // The merge is completely non-overlapping so it should be commutative. let mut mh = MusicHoard { library_cache: right.clone(), - database_cache: left.clone(), ..Default::default() }; - mh.collection = mh.merge_collections(); + mh.collection = mh.merge_collections(left.clone()); assert_eq!(expected, mh.collection); } @@ -198,21 +196,19 @@ mod tests { let mut mh = MusicHoard { library_cache: left.clone(), - database_cache: right.clone(), ..Default::default() }; - mh.collection = mh.merge_collections(); + mh.collection = mh.merge_collections(right.clone()); assert_eq!(expected, mh.collection); // The merge does not overwrite any data so it should be commutative. let mut mh = MusicHoard { library_cache: right.clone(), - database_cache: left.clone(), ..Default::default() }; - mh.collection = mh.merge_collections(); + mh.collection = mh.merge_collections(left.clone()); assert_eq!(expected, mh.collection); } @@ -241,21 +237,19 @@ mod tests { let mut mh = MusicHoard { library_cache: left.clone(), - database_cache: right.clone(), ..Default::default() }; - mh.collection = mh.merge_collections(); + mh.collection = mh.merge_collections(right.clone()); assert_eq!(expected, mh.collection); // The merge overwrites the sort data, but no data is erased so it should be commutative. let mut mh = MusicHoard { library_cache: right.clone(), - database_cache: left.clone(), ..Default::default() }; - mh.collection = mh.merge_collections(); + mh.collection = mh.merge_collections(left.clone()); assert_eq!(expected, mh.collection); } @@ -273,11 +267,10 @@ mod tests { let mut mh = MusicHoard { library_cache: left.clone(), - database_cache: right.clone(), ..Default::default() }; - mh.collection = mh.merge_collections(); + mh.collection = mh.merge_collections(right.clone()); } #[test] @@ -294,11 +287,10 @@ mod tests { let mut mh = MusicHoard { library_cache: left.clone(), - database_cache: right.clone(), ..Default::default() }; - mh.collection = mh.merge_collections(); + mh.collection = mh.merge_collections(right.clone()); } #[test] @@ -318,11 +310,10 @@ mod tests { let mut mh = MusicHoard { library_cache: left.clone(), - database_cache: right.clone(), ..Default::default() }; - mh.collection = mh.merge_collections(); + mh.collection = mh.merge_collections(right.clone()); assert_eq!(expected, mh.collection); } diff --git a/src/core/musichoard/builder.rs b/src/core/musichoard/builder.rs index 1901a9f..d030fd7 100644 --- a/src/core/musichoard/builder.rs +++ b/src/core/musichoard/builder.rs @@ -69,7 +69,6 @@ impl MusicHoard { collection: vec![], pre_commit: vec![], database: NoDatabase, - database_cache: vec![], library: NoLibrary, library_cache: vec![], } @@ -92,7 +91,6 @@ impl MusicHoard { collection: vec![], pre_commit: vec![], database: NoDatabase, - database_cache: vec![], library, library_cache: vec![], } @@ -115,7 +113,6 @@ impl MusicHoard { collection: vec![], pre_commit: vec![], database, - database_cache: vec![], library: NoLibrary, library_cache: vec![], }; @@ -140,7 +137,6 @@ impl MusicHoard { collection: vec![], pre_commit: vec![], database, - database_cache: vec![], library, library_cache: vec![], }; diff --git a/src/core/musichoard/database.rs b/src/core/musichoard/database.rs index 990871c..cf6c6f3 100644 --- a/src/core/musichoard/database.rs +++ b/src/core/musichoard/database.rs @@ -116,10 +116,10 @@ pub trait IMusicHoardDatabase { impl IMusicHoardDatabase for MusicHoard { fn reload_database(&mut self) -> Result<(), Error> { - self.database_cache = self.database.load()?; - Self::sort_albums_and_tracks(self.database_cache.iter_mut()); + let mut database_cache = self.database.load()?; + Self::sort_albums_and_tracks(database_cache.iter_mut()); - self.collection = self.merge_collections(); + self.collection = self.merge_collections(database_cache); self.filtered = self.filter_collection(); self.pre_commit = self.collection.clone(); diff --git a/src/core/musichoard/library.rs b/src/core/musichoard/library.rs index 70eabe2..2107cdb 100644 --- a/src/core/musichoard/library.rs +++ b/src/core/musichoard/library.rs @@ -23,25 +23,28 @@ pub trait IMusicHoardLibrary { impl IMusicHoardLibrary for MusicHoard { fn rescan_library(&mut self) -> Result<(), Error> { - self.pre_commit = self.rescan_library_inner()?; + self.pre_commit = self.rescan_library_inner(vec![])?; self.commit() } } impl IMusicHoardLibrary for MusicHoard { fn rescan_library(&mut self) -> Result<(), Error> { - self.pre_commit = self.rescan_library_inner()?; + let mut database_cache = self.database.load()?; + Self::sort_albums_and_tracks(database_cache.iter_mut()); + + self.pre_commit = self.rescan_library_inner(database_cache)?; self.commit() } } impl MusicHoard { - fn rescan_library_inner(&mut self) -> Result { + fn rescan_library_inner(&mut self, database: Collection) -> Result { let items = self.library.list(&Query::new())?; self.library_cache = Self::items_to_artists(items)?; Self::sort_albums_and_tracks(self.library_cache.iter_mut()); - Ok(self.merge_collections()) + Ok(self.merge_collections(database)) } fn items_to_artists(items: Vec) -> Result { @@ -152,11 +155,23 @@ mod tests { .times(1) .return_once(|_| library_result); - database.expect_load().times(1).returning(|| Ok(vec![])); + // The database contents are not relevant in this test. + let mut seq = Sequence::new(); + database + .expect_load() + .times(1) + .in_sequence(&mut seq) + .returning(|| Ok(vec![])); + database + .expect_load() + .times(1) + .in_sequence(&mut seq) + .returning(|| Ok(vec![])); database .expect_save() .with(predicate::eq(&*LIBRARY_COLLECTION)) .times(1) + .in_sequence(&mut seq) .return_once(|_| Ok(())); let mut music_hoard = MusicHoard::new(database, library).unwrap(); diff --git a/src/core/musichoard/mod.rs b/src/core/musichoard/mod.rs index 3b3230d..bfe9c04 100644 --- a/src/core/musichoard/mod.rs +++ b/src/core/musichoard/mod.rs @@ -32,7 +32,6 @@ pub struct MusicHoard { collection: Collection, pre_commit: Collection, database: Database, - database_cache: Collection, library: Library, library_cache: Collection, }