From fbaee818afbd1a95e7e42480d863db72a459c8a6 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 29 Sep 2024 12:31:04 +0200 Subject: [PATCH] Add unit tests --- src/tui/app/machine/fetch_state.rs | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/tui/app/machine/fetch_state.rs b/src/tui/app/machine/fetch_state.rs index 7c286b9..c8a15b3 100644 --- a/src/tui/app/machine/fetch_state.rs +++ b/src/tui/app/machine/fetch_state.rs @@ -358,6 +358,81 @@ mod tests { .return_once(|_, _| Ok(())); } + #[test] + fn fetch_single_album() { + let mut mb_job_sender = MockIMbJobSender::new(); + + let artist_id = COLLECTION[1].meta.id.clone(); + let artist_mbid: Mbid = "11111111-1111-1111-1111-111111111111".try_into().unwrap(); + + let album_meta = COLLECTION[1].albums[0].meta.clone(); + + search_release_group_expectation( + &mut mb_job_sender, + &artist_id, + &artist_mbid, + &[album_meta], + ); + + let music_hoard = music_hoard(COLLECTION.to_owned()); + let inner = AppInner::new(music_hoard, mb_job_sender); + + // Use second artist and have album selected to match the expectation. + let browse = AppMachine::browse_state(inner); + let browse = browse.increment_selection(Delta::Line).unwrap_browse(); + let app = browse.increment_category(); + + let app = app.unwrap_browse().fetch_musicbrainz(); + assert!(matches!(app, AppState::Fetch(_))); + } + + #[test] + fn fetch_single_album_nothing_to_fetch() { + let music_hoard = music_hoard(COLLECTION.to_owned()); + let inner = inner(music_hoard); + + // Use second artist, have second album selected (has MBID) to match the expectation. + let browse = AppMachine::browse_state(inner); + let browse = browse.increment_selection(Delta::Line).unwrap_browse(); + let browse = browse.increment_category().unwrap_browse(); + let app = browse.increment_selection(Delta::Line); + + let app = app.unwrap_browse().fetch_musicbrainz(); + assert!(matches!(app, AppState::Browse(_))); + } + + #[test] + fn fetch_single_album_no_artist_mbid() { + let music_hoard = music_hoard(COLLECTION.to_owned()); + let inner = inner(music_hoard); + + // Use third artist and have album selected to match the expectation. + let browse = AppMachine::browse_state(inner); + let browse = browse.increment_selection(Delta::Line).unwrap_browse(); + let browse = browse.increment_selection(Delta::Line).unwrap_browse(); + let app = browse.increment_category(); + + let app = app.unwrap_browse().fetch_musicbrainz(); + assert!(matches!(app, AppState::Error(_))); + } + + #[test] + fn fetch_single_album_no_album() { + let mut collection = COLLECTION.to_owned(); + collection[1].albums.clear(); + + let music_hoard = music_hoard(collection); + let inner = inner(music_hoard); + + // Use second artist and have album selected to match the expectation. + let browse = AppMachine::browse_state(inner); + let browse = browse.increment_selection(Delta::Line).unwrap_browse(); + let app = browse.increment_category(); + + let app = app.unwrap_browse().fetch_musicbrainz(); + assert!(matches!(app, AppState::Error(_))); + } + #[test] fn fetch_albums() { let mut mb_job_sender = MockIMbJobSender::new();