From 9fa4cf26b057937b4c2936aac1241fc857946d6c Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Mon, 30 Dec 2024 22:00:31 +0100 Subject: [PATCH] Unit test core --- src/core/collection/album.rs | 51 +++++++++++++-------- src/core/musichoard/database.rs | 72 ++++++++++++++++++++++++------ src/core/musichoard/library.rs | 2 +- src/tui/app/machine/match_state.rs | 12 +++-- src/tui/ui/mod.rs | 14 +++--- 5 files changed, 103 insertions(+), 48 deletions(-) diff --git a/src/core/collection/album.rs b/src/core/collection/album.rs index fdccb1f..762a8e0 100644 --- a/src/core/collection/album.rs +++ b/src/core/collection/album.rs @@ -144,17 +144,26 @@ impl AlbumStatus { } impl Album { - pub fn new, Date: Into>( - id: Id, - date: Date, + pub fn new>(id: Id) -> Self { + Album { + meta: AlbumMeta::new(id), + tracks: vec![], + } + } + + pub fn with_date>(mut self, date: Date) -> Self { + self.meta.date = date.into(); + self + } + + pub fn with_type, Date: Into>( + mut self, primary_type: Option, secondary_types: Vec, ) -> Self { - let info = AlbumInfo::new(MbRefOption::None, primary_type, secondary_types); - Album { - meta: AlbumMeta::new(id, date, info), - tracks: vec![], - } + self.meta.info.primary_type = primary_type; + self.meta.info.secondary_types = secondary_types; + self } pub fn get_status(&self) -> AlbumStatus { @@ -183,19 +192,25 @@ impl Merge for Album { } impl AlbumMeta { - pub fn new, Date: Into>( - id: Id, - date: Date, - info: AlbumInfo, - ) -> Self { + pub fn new>(id: Id) -> Self { AlbumMeta { id: id.into(), - date: date.into(), + date: AlbumDate::default(), seq: AlbumSeq::default(), - info, + info: AlbumInfo::default(), } } + pub fn with_date>(mut self, date: Date) -> Self { + self.date = date.into(); + self + } + + pub fn with_info(mut self, info: AlbumInfo) -> Self { + self.info = info; + self + } + pub fn get_sort_key(&self) -> (&AlbumDate, &AlbumSeq, &AlbumId) { (&self.date, &self.seq, &self.id) } @@ -313,13 +328,13 @@ mod tests { let album_id_1 = AlbumId { title: String::from("album z"), }; - let mut album_1 = Album::new(album_id_1, date.clone(), None, vec![]); + let mut album_1 = Album::new(album_id_1).with_date(date.clone()); album_1.meta.set_seq(AlbumSeq(1)); let album_id_2 = AlbumId { title: String::from("album a"), }; - let mut album_2 = Album::new(album_id_2, date.clone(), None, vec![]); + let mut album_2 = Album::new(album_id_2).with_date(date.clone()); album_2.meta.set_seq(AlbumSeq(2)); assert_ne!(album_1, album_2); @@ -329,7 +344,7 @@ mod tests { #[test] fn set_clear_seq() { - let mut album = Album::new("An album", AlbumDate::default(), None, vec![]); + let mut album = Album::new("An album"); assert_eq!(album.meta.seq, AlbumSeq(0)); diff --git a/src/core/musichoard/database.rs b/src/core/musichoard/database.rs index 9e95ab1..21837d1 100644 --- a/src/core/musichoard/database.rs +++ b/src/core/musichoard/database.rs @@ -392,7 +392,7 @@ mod tests { musicbrainz::{MbArtistRef, MbRefOption}, }, core::{ - collection::{album::AlbumDate, artist::ArtistId}, + collection::artist::ArtistId, interface::database::{self, MockIDatabase}, musichoard::{base::IMusicHoardBase, NoLibrary}, testmod::FULL_COLLECTION, @@ -652,6 +652,62 @@ mod tests { assert!(music_hoard.collection[0].meta.info.properties.is_empty()); } + #[test] + fn album_new_delete() { + let album_id = AlbumId::new("an album"); + let album_meta = AlbumMeta::new(album_id.clone()); + let album_id_2 = AlbumId::new("another album"); + let album_meta_2 = AlbumMeta::new(album_id_2); + + let collection = FULL_COLLECTION.to_owned(); + let artist_id = collection[0].meta.id.clone(); + let mut with_album = collection.clone(); + with_album[0].albums.push(Album::new(album_id)); + with_album[0].albums.sort_unstable(); + + let mut database = MockIDatabase::new(); + let mut seq = Sequence::new(); + database + .expect_load() + .times(1) + .times(1) + .in_sequence(&mut seq) + .returning(|| Ok(FULL_COLLECTION.to_owned())); + database + .expect_save() + .times(1) + .in_sequence(&mut seq) + .with(predicate::eq(with_album.clone())) + .returning(|_| Ok(())); + database + .expect_save() + .times(1) + .in_sequence(&mut seq) + .with(predicate::eq(collection.clone())) + .returning(|_| Ok(())); + + let mut music_hoard = MusicHoard::database(database).unwrap(); + assert_eq!(music_hoard.collection, collection); + + assert!(music_hoard + .add_album(&artist_id, album_meta.clone()) + .is_ok()); + assert_eq!(music_hoard.collection, with_album); + + assert!(music_hoard + .add_album(&artist_id, album_meta.clone()) + .is_ok()); + assert_eq!(music_hoard.collection, with_album); + + assert!(music_hoard + .remove_album(&artist_id, &album_meta_2.id) + .is_ok()); + assert_eq!(music_hoard.collection, with_album); + + assert!(music_hoard.remove_album(&artist_id, &album_meta.id).is_ok()); + assert_eq!(music_hoard.collection, collection); + } + #[test] fn set_clear_album_seq() { let mut database = MockIDatabase::new(); @@ -661,12 +717,7 @@ mod tests { let album_id_2 = AlbumId::new("another album"); let mut database_result = vec![Artist::new(artist_id.clone())]; - database_result[0].albums.push(Album::new( - album_id.clone(), - AlbumDate::default(), - None, - vec![], - )); + database_result[0].albums.push(Album::new(album_id.clone())); database .expect_load() @@ -706,12 +757,7 @@ mod tests { let album_id_2 = AlbumId::new("another album"); let mut database_result = vec![Artist::new(artist_id.clone())]; - database_result[0].albums.push(Album::new( - album_id.clone(), - AlbumDate::default(), - None, - vec![], - )); + database_result[0].albums.push(Album::new(album_id.clone())); database .expect_load() diff --git a/src/core/musichoard/library.rs b/src/core/musichoard/library.rs index 36b5f1e..5272b6d 100644 --- a/src/core/musichoard/library.rs +++ b/src/core/musichoard/library.rs @@ -109,7 +109,7 @@ impl MusicHoard { { Some(album) => album.tracks.push(track), None => { - let mut album = Album::new(album_id, album_date, None, vec![]); + let mut album = Album::new(album_id).with_date(album_date); album.tracks.push(track); artist.albums.push(album); } diff --git a/src/tui/app/machine/match_state.rs b/src/tui/app/machine/match_state.rs index 8a28fd4..f339f60 100644 --- a/src/tui/app/machine/match_state.rs +++ b/src/tui/app/machine/match_state.rs @@ -319,15 +319,13 @@ mod tests { } fn album_meta(id: AlbumId) -> AlbumMeta { - AlbumMeta::new( - id, - AlbumDate::new(Some(1990), Some(5), None), - AlbumInfo::new( + AlbumMeta::new(id) + .with_date(AlbumDate::new(Some(1990), Some(5), None)) + .with_info(AlbumInfo::new( MbRefOption::Some(mbid().into()), Some(AlbumPrimaryType::Album), vec![AlbumSecondaryType::Live, AlbumSecondaryType::Compilation], - ), - ) + )) } fn album_match() -> EntityMatches { @@ -625,7 +623,7 @@ mod tests { fn select_manual_input_album() { let mut mb_job_sender = MockIMbJobSender::new(); let artist_id = ArtistId::new("Artist"); - let album = AlbumMeta::new("Album", 1990, AlbumInfo::default()); + let album = AlbumMeta::new("Album").with_date(1990); let requests = VecDeque::from([MbParams::lookup_release_group( artist_id.clone(), album.id.clone(), diff --git a/src/tui/ui/mod.rs b/src/tui/ui/mod.rs index a633a0f..b192874 100644 --- a/src/tui/ui/mod.rs +++ b/src/tui/ui/mod.rs @@ -289,9 +289,7 @@ mod tests { #[test] fn empty_album() { let mut artists: Vec = vec![Artist::new(ArtistId::new("An artist"))]; - artists[0] - .albums - .push(Album::new("An album", AlbumDate::default(), None, vec![])); + artists[0].albums.push(Album::new("An album")); let mut selection = Selection::new(&artists); draw_test_suite(&artists, &mut selection); @@ -361,15 +359,13 @@ mod tests { } fn album_meta(id: AlbumId) -> AlbumMeta { - AlbumMeta::new( - id, - AlbumDate::new(Some(1990), Some(5), None), - AlbumInfo::new( + AlbumMeta::new(id) + .with_date(AlbumDate::new(Some(1990), Some(5), None)) + .with_info(AlbumInfo::new( MbRefOption::None, Some(AlbumPrimaryType::Album), vec![AlbumSecondaryType::Live, AlbumSecondaryType::Compilation], - ), - ) + )) } fn album_matches() -> EntityMatches {