From 00648063e2f679b84c68b29473f0e29d7c04a2b3 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Thu, 2 Jan 2025 19:41:06 +0100 Subject: [PATCH] Complete coverage --- src/core/musichoard/database.rs | 65 +++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/core/musichoard/database.rs b/src/core/musichoard/database.rs index d227709..52b35b9 100644 --- a/src/core/musichoard/database.rs +++ b/src/core/musichoard/database.rs @@ -746,6 +746,71 @@ mod tests { assert_eq!(music_hoard.collection, collection); } + #[test] + fn set_clear_album_db_id() { + let mut database = MockIDatabase::new(); + + let artist_id = ArtistId::new("an artist"); + let mut album_id = AlbumId::new("an album"); + 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())); + + database + .expect_load() + .times(1) + .return_once(|| Ok(database_result)); + database.expect_save().times(2).returning(|_| Ok(())); + + let mut music_hoard = MusicHoard::database(database).unwrap(); + assert_eq!( + music_hoard.collection[0].albums[0].meta.id.db_id, + AlbumDbId::None + ); + + // Seting db_id on an album not belonging to the artist is an error. + assert!(music_hoard + .set_album_db_id(&artist_id, &album_id_2, AlbumDbId::CannotHaveMbid) + .is_err()); + assert_eq!( + music_hoard.collection[0].albums[0].meta.id.db_id, + AlbumDbId::None + ); + + // Set db_id. + assert!(music_hoard + .set_album_db_id(&artist_id, &album_id, AlbumDbId::CannotHaveMbid) + .is_ok()); + assert_eq!( + music_hoard.collection[0].albums[0].meta.id.db_id, + AlbumDbId::CannotHaveMbid + ); + + // Clearing db_id on an album that does not exist is an error. + assert!(music_hoard + .clear_album_db_id(&artist_id, &album_id_2) + .is_err()); + + // Clearing db_id from album without the db_id set is an error. Effectively the album does + // not exist. + assert!(music_hoard + .clear_album_db_id(&artist_id, &album_id) + .is_err()); + assert_eq!( + music_hoard.collection[0].albums[0].meta.id.db_id, + AlbumDbId::CannotHaveMbid + ); + + // To clear the db_id we need the album_id to have the db_id to identify the correct album. + album_id.set_db_id(AlbumDbId::CannotHaveMbid); + assert!(music_hoard.clear_album_db_id(&artist_id, &album_id).is_ok()); + assert_eq!( + music_hoard.collection[0].albums[0].meta.id.db_id, + AlbumDbId::None + ); + } + #[test] fn set_clear_album_seq() { let mut database = MockIDatabase::new();