Add unit test
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 2m5s
Cargo CI / Lint (pull_request) Successful in 1m9s

This commit is contained in:
Wojciech Kozlowski 2025-01-05 14:24:23 +01:00
parent 6919f0214a
commit 6c89131316

View File

@ -682,15 +682,13 @@ mod tests {
reduced[0].albums.retain(|a| a.meta.id.mb_ref != mb_ref); reduced[0].albums.retain(|a| a.meta.id.mb_ref != mb_ref);
// We need a custom music_hoard to override the get_filtered expectations. // We need a custom music_hoard to override the get_filtered expectations.
let mut music_hoard = MockIMusicHoard::new(); let mut mh = MockIMusicHoard::new();
let mut seq = Sequence::new(); let mut seq = Sequence::new();
music_hoard mh.expect_get_filtered()
.expect_get_filtered()
.times(2) .times(2)
.in_sequence(&mut seq) .in_sequence(&mut seq)
.return_const(collection.clone()); .return_const(collection.clone());
music_hoard mh.expect_get_filtered()
.expect_get_filtered()
.times(1) .times(1)
.in_sequence(&mut seq) .in_sequence(&mut seq)
.return_const(reduced.clone()); .return_const(reduced.clone());
@ -701,31 +699,27 @@ mod tests {
let artist = matches.artist.clone(); let artist = matches.artist.clone();
let mut seq = Sequence::new(); let mut seq = Sequence::new();
music_hoard mh.expect_get_collection()
.expect_get_collection()
.times(1) .times(1)
.in_sequence(&mut seq) .in_sequence(&mut seq)
.return_const(collection.clone()); .return_const(collection.clone());
music_hoard mh.expect_remove_album()
.expect_remove_album()
.times(1) .times(1)
.with(eq(artist), eq(removed.meta.id.clone())) .with(eq(artist), eq(removed.meta.id.clone()))
.in_sequence(&mut seq) .in_sequence(&mut seq)
.return_once(|_, _| Ok(())); .return_once(|_, _| Ok(()));
music_hoard mh.expect_merge_album_info()
.expect_merge_album_info()
.times(1) .times(1)
.in_sequence(&mut seq) .in_sequence(&mut seq)
.return_once(|_, _, _| Ok(())); .return_once(|_, _, _| Ok(()));
music_hoard mh.expect_set_album_mb_ref()
.expect_set_album_mb_ref()
.times(1) .times(1)
.in_sequence(&mut seq) .in_sequence(&mut seq)
.return_once(|_, _, _| Ok(())); .return_once(|_, _, _| Ok(()));
} }
} }
let mut matches = AppMachine::match_state(inner(music_hoard), app_matches); let mut matches = AppMachine::match_state(inner(mh), app_matches);
// Make sure the last album is selected. // Make sure the last album is selected.
let selection = &mut matches.inner.selection; let selection = &mut matches.inner.selection;
@ -747,6 +741,58 @@ mod tests {
assert_eq!(album.meta, selected.meta); assert_eq!(album.meta, selected.meta);
} }
#[test]
fn set_album_info_with_clashing_cannot_have_mbid() {
let matches_info = album_match();
// The collection below should trigger MH to remove the album with the same MBID but not
// matching album_id.
// (1) Same artist as matches_info.
let mut artist = Artist::new(ArtistId::new("Artist"));
// (2) An album with the same album_id as the selected one.
artist.albums.push(Album::new(AlbumId::new("Album")));
// (3) An album with a different album_id than the selected one.
// (4) This album has an MBID that is identical to that of the selected match.
let removed =
Album::new(AlbumId::new("Album: Not the Same").with_mb_ref(AlbumMbRef::CannotHaveMbid));
artist.albums.push(removed.clone());
// (5) An album after the one that will be removed. Selection must remain on it.
let selected = Album::new(AlbumId::new("Album: Z"));
artist.albums.push(selected.clone());
let (_tx, rx) = mpsc::channel();
let app_matches = MatchState::new(matches_info.clone(), FetchState::search(rx));
let collection = vec![artist];
let mut mh = music_hoard(collection.clone());
match matches_info {
EntityMatches::Artist(_) => panic!(),
EntityMatches::Album(_) => {
let mut seq = Sequence::new();
mh.expect_get_collection()
.times(1)
.in_sequence(&mut seq)
.return_const(collection.clone());
mh.expect_merge_album_info()
.times(1)
.in_sequence(&mut seq)
.return_once(|_, _, _| Ok(()));
mh.expect_set_album_mb_ref()
.times(1)
.in_sequence(&mut seq)
.return_once(|_, _, _| Ok(()));
}
}
let matches = AppMachine::match_state(inner(mh), app_matches);
matches.select().unwrap_fetch();
}
#[test] #[test]
fn set_info_error() { fn set_info_error() {
let matches_info = artist_match(); let matches_info = artist_match();