Unit tests for browse

This commit is contained in:
Wojciech Kozlowski 2024-09-06 22:36:06 +02:00
parent c6a6d21199
commit 669d4c9da9
2 changed files with 211 additions and 209 deletions

View File

@ -89,16 +89,11 @@ impl IAppInteractBrowse for AppMachine<AppBrowse> {
#[cfg(test)]
mod tests {
use mockall::{predicate, Sequence};
use musichoard::collection::{album::AlbumMeta, artist::ArtistMeta, musicbrainz::Mbid};
use crate::tui::{
app::{
machine::tests::{inner, inner_with_mb, music_hoard},
AppAlbumMatches, AppArtistMatches, AppMatchesInfo, Category, IAppAccess, IAppInteract,
IAppInteractMatches, MatchOption,
machine::tests::{inner, music_hoard},
Category, IAppInteract,
},
lib::interface::musicbrainz::{self, Match, MockIMusicBrainz},
testmod::COLLECTION,
};
@ -174,208 +169,12 @@ mod tests {
app.unwrap_search();
}
// #[test]
// fn fetch_musicbrainz() {
// let mut mb_api = MockIMusicBrainz::new();
// let arid: Mbid = "11111111-1111-1111-1111-111111111111".try_into().unwrap();
// let album_1 = COLLECTION[1].albums[0].meta.clone();
// let album_4 = COLLECTION[1].albums[3].meta.clone();
// let album_match_1_1 = Match::new(100, album_1.clone());
// let album_match_1_2 = Match::new(50, album_4.clone());
// let album_match_4_1 = Match::new(100, album_4.clone());
// let album_match_4_2 = Match::new(30, album_1.clone());
// let matches_1 = vec![album_match_1_1.clone(), album_match_1_2.clone()];
// let matches_4 = vec![album_match_4_1.clone(), album_match_4_2.clone()];
// let result_1: Result<Vec<Match<AlbumMeta>>, musicbrainz::Error> = Ok(matches_1.clone());
// let result_4: Result<Vec<Match<AlbumMeta>>, musicbrainz::Error> = Ok(matches_4.clone());
// // Other albums have an MBID and so they will be skipped.
// let mut seq = Sequence::new();
// mb_api
// .expect_search_release_group()
// .with(predicate::eq(arid.clone()), predicate::eq(album_1.clone()))
// .times(1)
// .in_sequence(&mut seq)
// .return_once(|_, _| result_1);
// mb_api
// .expect_search_release_group()
// .with(predicate::eq(arid.clone()), predicate::eq(album_4.clone()))
// .times(1)
// .in_sequence(&mut seq)
// .return_once(|_, _| result_4);
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
// // Use the second artist for this test.
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let mut app = browse.fetch_musicbrainz();
// let public = app.get();
// assert!(matches!(public.state, AppState::Matches(_)));
// let public_matches = public.state.unwrap_matches();
// let mut matches_1: Vec<MatchOption<AlbumMeta>> =
// matches_1.into_iter().map(Into::into).collect();
// matches_1.push(MatchOption::CannotHaveMbid);
// let expected = Some(AppMatchesInfo::Album(AppAlbumMatches {
// matching: album_1.clone(),
// list: matches_1.clone(),
// }));
// assert_eq!(public_matches.matches, expected.as_ref());
// let mut app = app.unwrap_matches().select();
// let public = app.get();
// assert!(matches!(public.state, AppState::Matches(_)));
// let public_matches = public.state.unwrap_matches();
// let mut matches_4: Vec<MatchOption<AlbumMeta>> =
// matches_4.into_iter().map(Into::into).collect();
// matches_4.push(MatchOption::CannotHaveMbid);
// let expected = Some(AppMatchesInfo::Album(AppAlbumMatches {
// matching: album_4.clone(),
// list: matches_4.clone(),
// }));
// assert_eq!(public_matches.matches, expected.as_ref());
// let app = app.unwrap_matches().select();
// app.unwrap_browse();
// }
// #[test]
// fn fetch_musicbrainz_no_artist() {
// let browse = AppMachine::browse(inner(music_hoard(vec![])));
// let app = browse.fetch_musicbrainz();
// app.unwrap_error();
// }
// #[test]
// fn fetch_musicbrainz_no_artist_mbid() {
// let mut mb_api = MockIMusicBrainz::new();
// let artist = COLLECTION[3].meta.clone();
// let artist_match_1 = Match::new(100, artist.clone());
// let artist_match_2 = Match::new(50, artist.clone());
// let matches = vec![artist_match_1.clone(), artist_match_2.clone()];
// let result: Result<Vec<Match<ArtistMeta>>, musicbrainz::Error> = Ok(matches.clone());
// mb_api
// .expect_search_artist()
// .with(predicate::eq(artist.clone()))
// .times(1)
// .return_once(|_| result);
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
// // Use the fourth artist for this test as they have no MBID.
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let mut app = browse.fetch_musicbrainz();
// let public = app.get();
// assert!(matches!(public.state, AppState::Matches(_)));
// let public_matches = public.state.unwrap_matches();
// let mut matches: Vec<MatchOption<ArtistMeta>> =
// matches.into_iter().map(Into::into).collect();
// matches.push(MatchOption::CannotHaveMbid);
// let expected = Some(AppMatchesInfo::Artist(AppArtistMatches {
// matching: artist.clone(),
// list: matches.clone(),
// }));
// assert_eq!(public_matches.matches, expected.as_ref());
// let app = app.unwrap_matches().select();
// app.unwrap_browse();
// }
// #[test]
// fn fetch_musicbrainz_artist_api_error() {
// let mut mb_api = MockIMusicBrainz::new();
// let error = Err(musicbrainz::Error::RateLimit);
// mb_api
// .expect_search_artist()
// .times(1)
// .return_once(|_| error);
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
// // Use the fourth artist for this test as they have no MBID.
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let app = browse.fetch_musicbrainz();
// app.unwrap_error();
// }
// #[test]
// fn fetch_musicbrainz_album_api_error() {
// let mut mb_api = MockIMusicBrainz::new();
// let error = Err(musicbrainz::Error::RateLimit);
// mb_api
// .expect_search_release_group()
// .times(1)
// .return_once(|_, _| error);
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
// let app = browse.fetch_musicbrainz();
// app.unwrap_error();
// }
// #[test]
// fn fetch_musicbrainz_artist_receiver_disconnect() {
// let (tx, rx) = mpsc::channel::<FetchResult>();
// drop(rx);
// let mut mb_api = MockIMusicBrainz::new();
// mb_api
// .expect_search_artist()
// .times(1)
// .return_once(|_| Ok(vec![]));
// // We only check it does not panic and that it doesn't call the API more than once.
// AppMachine::fetch_artist(Arc::new(Mutex::new(mb_api)), tx, COLLECTION[3].meta.clone());
// }
// #[test]
// fn fetch_musicbrainz_albums_receiver_disconnect() {
// let (tx, rx) = mpsc::channel::<FetchResult>();
// drop(rx);
// let mut mb_api = MockIMusicBrainz::new();
// mb_api
// .expect_search_release_group()
// .times(1)
// .return_once(|_, _| Ok(vec![]));
// // We only check it does not panic and that it doesn't call the API more than once.
// let mbref = &COLLECTION[1].meta.musicbrainz;
// let albums = &COLLECTION[1].albums;
// AppMachine::fetch_albums(
// Arc::new(Mutex::new(mb_api)),
// tx,
// mbref.as_ref().unwrap().mbid().clone(),
// albums.iter().map(|a| &a.meta).cloned().collect(),
// );
// }
#[test]
fn fetch_musicbrainz() {
let browse = AppMachine::browse(inner(music_hoard(vec![])));
let app = browse.fetch_musicbrainz();
app.unwrap_error();
}
#[test]
fn no_op() {

View File

@ -200,6 +200,209 @@ mod tests {
// use super::*;
// #[test]
// fn fetch_musicbrainz() {
// let mut mb_api = MockIMusicBrainz::new();
// let arid: Mbid = "11111111-1111-1111-1111-111111111111".try_into().unwrap();
// let album_1 = COLLECTION[1].albums[0].meta.clone();
// let album_4 = COLLECTION[1].albums[3].meta.clone();
// let album_match_1_1 = Match::new(100, album_1.clone());
// let album_match_1_2 = Match::new(50, album_4.clone());
// let album_match_4_1 = Match::new(100, album_4.clone());
// let album_match_4_2 = Match::new(30, album_1.clone());
// let matches_1 = vec![album_match_1_1.clone(), album_match_1_2.clone()];
// let matches_4 = vec![album_match_4_1.clone(), album_match_4_2.clone()];
// let result_1: Result<Vec<Match<AlbumMeta>>, musicbrainz::Error> = Ok(matches_1.clone());
// let result_4: Result<Vec<Match<AlbumMeta>>, musicbrainz::Error> = Ok(matches_4.clone());
// // Other albums have an MBID and so they will be skipped.
// let mut seq = Sequence::new();
// mb_api
// .expect_search_release_group()
// .with(predicate::eq(arid.clone()), predicate::eq(album_1.clone()))
// .times(1)
// .in_sequence(&mut seq)
// .return_once(|_, _| result_1);
// mb_api
// .expect_search_release_group()
// .with(predicate::eq(arid.clone()), predicate::eq(album_4.clone()))
// .times(1)
// .in_sequence(&mut seq)
// .return_once(|_, _| result_4);
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
// // Use the second artist for this test.
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let mut app = browse.fetch_musicbrainz();
// let public = app.get();
// assert!(matches!(public.state, AppState::Matches(_)));
// let public_matches = public.state.unwrap_matches();
// let mut matches_1: Vec<MatchOption<AlbumMeta>> =
// matches_1.into_iter().map(Into::into).collect();
// matches_1.push(MatchOption::CannotHaveMbid);
// let expected = Some(AppMatchesInfo::Album(AppAlbumMatches {
// matching: album_1.clone(),
// list: matches_1.clone(),
// }));
// assert_eq!(public_matches.matches, expected.as_ref());
// let mut app = app.unwrap_matches().select();
// let public = app.get();
// assert!(matches!(public.state, AppState::Matches(_)));
// let public_matches = public.state.unwrap_matches();
// let mut matches_4: Vec<MatchOption<AlbumMeta>> =
// matches_4.into_iter().map(Into::into).collect();
// matches_4.push(MatchOption::CannotHaveMbid);
// let expected = Some(AppMatchesInfo::Album(AppAlbumMatches {
// matching: album_4.clone(),
// list: matches_4.clone(),
// }));
// assert_eq!(public_matches.matches, expected.as_ref());
// let app = app.unwrap_matches().select();
// app.unwrap_browse();
// }
// #[test]
// fn fetch_musicbrainz_no_artist() {
// let browse = AppMachine::browse(inner(music_hoard(vec![])));
// let app = browse.fetch_musicbrainz();
// app.unwrap_error();
// }
// #[test]
// fn fetch_musicbrainz_no_artist_mbid() {
// let mut mb_api = MockIMusicBrainz::new();
// let artist = COLLECTION[3].meta.clone();
// let artist_match_1 = Match::new(100, artist.clone());
// let artist_match_2 = Match::new(50, artist.clone());
// let matches = vec![artist_match_1.clone(), artist_match_2.clone()];
// let result: Result<Vec<Match<ArtistMeta>>, musicbrainz::Error> = Ok(matches.clone());
// mb_api
// .expect_search_artist()
// .with(predicate::eq(artist.clone()))
// .times(1)
// .return_once(|_| result);
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
// // Use the fourth artist for this test as they have no MBID.
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let mut app = browse.fetch_musicbrainz();
// let public = app.get();
// assert!(matches!(public.state, AppState::Matches(_)));
// let public_matches = public.state.unwrap_matches();
// let mut matches: Vec<MatchOption<ArtistMeta>> =
// matches.into_iter().map(Into::into).collect();
// matches.push(MatchOption::CannotHaveMbid);
// let expected = Some(AppMatchesInfo::Artist(AppArtistMatches {
// matching: artist.clone(),
// list: matches.clone(),
// }));
// assert_eq!(public_matches.matches, expected.as_ref());
// let app = app.unwrap_matches().select();
// app.unwrap_browse();
// }
// #[test]
// fn fetch_musicbrainz_artist_api_error() {
// let mut mb_api = MockIMusicBrainz::new();
// let error = Err(musicbrainz::Error::RateLimit);
// mb_api
// .expect_search_artist()
// .times(1)
// .return_once(|_| error);
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
// // Use the fourth artist for this test as they have no MBID.
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let app = browse.fetch_musicbrainz();
// app.unwrap_error();
// }
// #[test]
// fn fetch_musicbrainz_album_api_error() {
// let mut mb_api = MockIMusicBrainz::new();
// let error = Err(musicbrainz::Error::RateLimit);
// mb_api
// .expect_search_release_group()
// .times(1)
// .return_once(|_, _| error);
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
// let app = browse.fetch_musicbrainz();
// app.unwrap_error();
// }
// #[test]
// fn fetch_musicbrainz_artist_receiver_disconnect() {
// let (tx, rx) = mpsc::channel::<FetchResult>();
// drop(rx);
// let mut mb_api = MockIMusicBrainz::new();
// mb_api
// .expect_search_artist()
// .times(1)
// .return_once(|_| Ok(vec![]));
// // We only check it does not panic and that it doesn't call the API more than once.
// AppMachine::fetch_artist(Arc::new(Mutex::new(mb_api)), tx, COLLECTION[3].meta.clone());
// }
// #[test]
// fn fetch_musicbrainz_albums_receiver_disconnect() {
// let (tx, rx) = mpsc::channel::<FetchResult>();
// drop(rx);
// let mut mb_api = MockIMusicBrainz::new();
// mb_api
// .expect_search_release_group()
// .times(1)
// .return_once(|_, _| Ok(vec![]));
// // We only check it does not panic and that it doesn't call the API more than once.
// let mbref = &COLLECTION[1].meta.musicbrainz;
// let albums = &COLLECTION[1].albums;
// AppMachine::fetch_albums(
// Arc::new(Mutex::new(mb_api)),
// tx,
// mbref.as_ref().unwrap().mbid().clone(),
// albums.iter().map(|a| &a.meta).cloned().collect(),
// );
// }
// impl<T> Match<T> {
// pub fn new(score: u8, item: T) -> Self {
// Match {