From 967c4b41759b662aefd3b845afdd69ccdaa99e43 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 29 Sep 2024 08:34:49 +0200 Subject: [PATCH] Fix match_state tests --- src/core/collection/artist.rs | 6 +-- src/core/musichoard/database.rs | 2 +- src/tui/app/machine/match_state.rs | 68 ++++++++++++++++++++---------- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/core/collection/artist.rs b/src/core/collection/artist.rs index eb69b81..730ef87 100644 --- a/src/core/collection/artist.rs +++ b/src/core/collection/artist.rs @@ -100,14 +100,14 @@ impl ArtistMeta { impl Default for ArtistInfo { fn default() -> Self { - Self::new() + Self::new(MbRefOption::None) } } impl ArtistInfo { - pub fn new() -> Self { + pub fn new(musicbrainz: MbRefOption) -> Self { ArtistInfo { - musicbrainz: MbRefOption::None, + musicbrainz, properties: HashMap::new(), } } diff --git a/src/core/musichoard/database.rs b/src/core/musichoard/database.rs index f4f2f32..5a37c28 100644 --- a/src/core/musichoard/database.rs +++ b/src/core/musichoard/database.rs @@ -461,7 +461,7 @@ mod tests { let mut expected: MbRefOption = MbRefOption::None; assert_eq!(music_hoard.collection[0].meta.info.musicbrainz, expected); - let mut info = ArtistInfo::new(); + let mut info = ArtistInfo::default(); info.musicbrainz = MbRefOption::Some(MbArtistRef::from_uuid_str(MBID).unwrap()); // Setting a URL on an artist not in the collection is an error. diff --git a/src/tui/app/machine/match_state.rs b/src/tui/app/machine/match_state.rs index 810e51a..3eaf9f3 100644 --- a/src/tui/app/machine/match_state.rs +++ b/src/tui/app/machine/match_state.rs @@ -308,7 +308,7 @@ impl IAppInteractMatch for AppMachine { mod tests { use std::{collections::VecDeque, sync::mpsc}; - use mockall::predicate; + use mockall::predicate::{self, eq}; use musichoard::collection::{ album::{AlbumDate, AlbumId, AlbumInfo, AlbumMeta, AlbumPrimaryType, AlbumSecondaryType}, artist::{ArtistId, ArtistMeta}, @@ -346,8 +346,12 @@ mod tests { } } + fn artist_meta() -> ArtistMeta { + ArtistMeta::new(ArtistId::new("Artist")) + } + fn artist_match() -> MatchStateInfo { - let artist = ArtistMeta::new(ArtistId::new("Artist")); + let artist = artist_meta(); let artist_1 = artist.clone(); let artist_match_1 = Match::new(100, artist_1); @@ -361,14 +365,13 @@ mod tests { } fn artist_lookup() -> MatchStateInfo { - let artist = ArtistMeta::new(ArtistId::new("Artist")); + let artist = artist_meta(); let lookup = Lookup::new(artist.clone()); MatchStateInfo::artist_lookup(artist, lookup) } - fn album_match() -> MatchStateInfo { - let artist_id = ArtistId::new("Artist"); - let album = AlbumMeta::new( + fn album_meta() -> AlbumMeta { + AlbumMeta::new( AlbumId::new("Album"), AlbumDate::new(Some(1990), Some(5), None), AlbumInfo::new( @@ -376,7 +379,12 @@ mod tests { Some(AlbumPrimaryType::Album), vec![AlbumSecondaryType::Live, AlbumSecondaryType::Compilation], ), - ); + ) + } + + fn album_match() -> MatchStateInfo { + let artist_id = ArtistId::new("Artist"); + let album = album_meta(); let album_1 = album.clone(); let album_match_1 = Match::new(100, album_1); @@ -392,15 +400,7 @@ mod tests { fn album_lookup() -> MatchStateInfo { let artist_id = ArtistId::new("Artist"); - let album = AlbumMeta::new( - AlbumId::new("Album"), - AlbumDate::new(Some(1990), Some(5), None), - AlbumInfo::new( - MbRefOption::None, - Some(AlbumPrimaryType::Album), - vec![AlbumSecondaryType::Live, AlbumSecondaryType::Compilation], - ), - ); + let album = album_meta(); let lookup = Lookup::new(album.clone()); MatchStateInfo::album_lookup(artist_id, album, lookup) } @@ -457,11 +457,34 @@ mod tests { fn match_state_flow(mut matches_info: MatchStateInfo, len: usize) { // tx must exist for rx to return Empty rather than Disconnected. - #[allow(unused_variables)] - let (tx, rx) = mpsc::channel(); + let (_tx, rx) = mpsc::channel(); let app_matches = MatchState::new(Some(matches_info.clone()), FetchState::new(rx)); - let matches = AppMachine::match_state(inner(music_hoard(vec![])), app_matches); + let mut music_hoard = music_hoard(vec![]); + let artist_id = ArtistId::new("Artist"); + match matches_info { + MatchStateInfo::Album(_) => { + let album_id = AlbumId::new("Album"); + let mut info = album_meta().info; + info.musicbrainz = MbRefOption::CannotHaveMbid; + music_hoard + .expect_set_album_info() + .with(eq(artist_id.clone()), eq(album_id.clone()), eq(info)) + .times(1) + .return_once(|_, _, _| Ok(())); + } + MatchStateInfo::Artist(_) => { + let mut info = artist_meta().info; + info.musicbrainz = MbRefOption::CannotHaveMbid; + music_hoard + .expect_set_artist_info() + .with(eq(artist_id.clone()), eq(info)) + .times(1) + .return_once(|_, _| Ok(())); + } + } + + let matches = AppMachine::match_state(inner(music_hoard), app_matches); matches_info.push_cannot_have_mbid(); matches_info.push_manual_input_mbid(); @@ -547,10 +570,11 @@ mod tests { #[test] fn select_empty() { - // Note that what really matters in this test is actually that the transmit channel has - // disconnected and so the receive within FetchState concludes there are no more matches. + // This test will become obsolete with #203 so it just needs to work well enough for + // coverage. We expect the error state, because after selecting, fetch will be invoked, but + // with an empty collection, an error will be raised. let matches = AppMachine::match_state(inner(music_hoard(vec![])), match_state(None)); - matches.select().unwrap_browse(); + matches.select().unwrap_error(); } #[test]