Complete unit tests

This commit is contained in:
Wojciech Kozlowski 2024-09-07 23:35:45 +02:00
parent f5f2cc478f
commit b1e764ffb9

View File

@ -51,6 +51,7 @@ impl AppMachine<AppFetch> {
}
},
Err(recv_err) => match recv_err {
TryRecvError::Empty => AppMachine::fetch(inner, fetch).into(),
TryRecvError::Disconnected => {
if first {
AppMachine::matches(inner, AppMatches::new(None, fetch)).into()
@ -58,7 +59,6 @@ impl AppMachine<AppFetch> {
AppMachine::browse(inner).into()
}
}
TryRecvError::Empty => AppMachine::fetch(inner, fetch).into(),
},
}
}
@ -101,276 +101,98 @@ impl IAppEventFetch for AppMachine<AppFetch> {
#[cfg(test)]
mod tests {
use musichoard::collection::artist::ArtistMeta;
// use std::sync::mpsc;
use crate::tui::{
app::machine::tests::{inner, music_hoard},
lib::interface::musicbrainz::{self, Match},
testmod::COLLECTION,
};
// use musichoard::collection::{
// album::{AlbumDate, AlbumId, AlbumMeta, AlbumPrimaryType, AlbumSecondaryType},
// artist::{ArtistId, ArtistMeta},
// };
use super::*;
// use crate::tui::{
// app::{
// machine::tests::{inner, music_hoard},
// IAppAccess,
// },
// lib::interface::musicbrainz::Match,
// };
#[test]
fn recv_ok_fetch_ok() {
let (tx, rx) = mpsc::channel::<FetchResult>();
// use super::*;
let artist = COLLECTION[3].meta.clone();
let fetch_result = Ok(AppMatchesInfo::artist::<Match<ArtistMeta>>(artist, vec![]));
tx.send(fetch_result).unwrap();
// #[test]
// fn fetch_musicbrainz_artist_api_error() {
// let mut mb_api = MockIMusicBrainz::new();
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
assert!(matches!(app, AppState::Matches(_)));
}
// let error = Err(musicbrainz::Error::RateLimit);
#[test]
fn recv_ok_fetch_err() {
let (tx, rx) = mpsc::channel::<FetchResult>();
// mb_api
// .expect_search_artist()
// .times(1)
// .return_once(|_| error);
let fetch_result = Err(musicbrainz::Error::RateLimit);
tx.send(fetch_result).unwrap();
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
assert!(matches!(app, AppState::Error(_)));
}
// // 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();
#[test]
fn recv_err_empty() {
let (_tx, rx) = mpsc::channel::<FetchResult>();
// let app = browse.fetch_musicbrainz();
// app.unwrap_error();
// }
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
assert!(matches!(app, AppState::Fetch(_)));
}
// #[test]
// fn fetch_musicbrainz_album_api_error() {
// let mut mb_api = MockIMusicBrainz::new();
#[test]
fn recv_err_disconnected_first() {
let (_, rx) = mpsc::channel::<FetchResult>();
// let error = Err(musicbrainz::Error::RateLimit);
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
assert!(matches!(app, AppState::Matches(_)));
}
// mb_api
// .expect_search_release_group()
// .times(1)
// .return_once(|_, _| error);
#[test]
fn recv_err_disconnected_next() {
let (_, rx) = mpsc::channel::<FetchResult>();
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
let fetch = AppFetch::new(rx);
let app = AppMachine::app_fetch_next(inner(music_hoard(COLLECTION.clone())), fetch);
assert!(matches!(app, AppState::Browse(_)));
}
// let app = browse.fetch_musicbrainz();
// app.unwrap_error();
// }
#[test]
fn empty_first_then_ready() {
let (tx, rx) = mpsc::channel::<FetchResult>();
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
assert!(matches!(app, AppState::Fetch(_)));
// impl<T> Match<T> {
// pub fn new(score: u8, item: T) -> Self {
// Match {
// score,
// item,
// disambiguation: None,
// }
// }
// }
let artist = COLLECTION[3].meta.clone();
let fetch_result = Ok(AppMatchesInfo::artist::<Match<ArtistMeta>>(artist, vec![]));
tx.send(fetch_result).unwrap();
// fn artist_matches_info_vec() -> Vec<AppMatchesInfo> {
// let artist_1 = ArtistMeta::new(ArtistId::new("Artist 1"));
let app = app.unwrap_fetch().fetch_result_ready();
assert!(matches!(app, AppState::Matches(_)));
}
// let artist_1_1 = artist_1.clone();
// let artist_match_1_1 = Match::new(100, artist_1_1);
#[test]
fn abort() {
let (_, rx) = mpsc::channel::<FetchResult>();
// let artist_1_2 = artist_1.clone();
// let mut artist_match_1_2 = Match::new(100, artist_1_2);
// artist_match_1_2.disambiguation = Some(String::from("some disambiguation"));
let fetch = AppFetch::new(rx);
let app = AppMachine::fetch(inner(music_hoard(COLLECTION.clone())), fetch);
// let list = vec![artist_match_1_1.clone(), artist_match_1_2.clone()];
// let matches_info_1 = AppMatchesInfo::artist(artist_1.clone(), list);
let app = app.abort();
assert!(matches!(app, AppState::Browse(_)));
}
// let artist_2 = ArtistMeta::new(ArtistId::new("Artist 2"));
#[test]
fn no_op() {
let (_, rx) = mpsc::channel::<FetchResult>();
// let artist_2_1 = artist_1.clone();
// let album_match_2_1 = Match::new(100, artist_2_1);
let fetch = AppFetch::new(rx);
let app = AppMachine::fetch(inner(music_hoard(COLLECTION.clone())), fetch);
// let list = vec![album_match_2_1.clone()];
// let matches_info_2 = AppMatchesInfo::artist(artist_2.clone(), list);
// vec![matches_info_1, matches_info_2]
// }
// fn album_matches_info_vec() -> Vec<AppMatchesInfo> {
// let album_1 = AlbumMeta::new(
// AlbumId::new("Album 1"),
// AlbumDate::new(Some(1990), Some(5), None),
// Some(AlbumPrimaryType::Album),
// vec![AlbumSecondaryType::Live, AlbumSecondaryType::Compilation],
// );
// let album_1_1 = album_1.clone();
// let album_match_1_1 = Match::new(100, album_1_1);
// let mut album_1_2 = album_1.clone();
// album_1_2.id.title.push_str(" extra title part");
// album_1_2.secondary_types.pop();
// let album_match_1_2 = Match::new(100, album_1_2);
// let list = vec![album_match_1_1.clone(), album_match_1_2.clone()];
// let matches_info_1 = AppMatchesInfo::album(album_1.clone(), list);
// let album_2 = AlbumMeta::new(
// AlbumId::new("Album 2"),
// AlbumDate::new(Some(2001), None, None),
// Some(AlbumPrimaryType::Album),
// vec![],
// );
// let album_2_1 = album_1.clone();
// let album_match_2_1 = Match::new(100, album_2_1);
// let list = vec![album_match_2_1.clone()];
// let matches_info_2 = AppMatchesInfo::album(album_2.clone(), list);
// vec![matches_info_1, matches_info_2]
// }
// fn receiver(matches_info_vec: Vec<AppMatchesInfo>) -> FetchReceiver {
// let (tx, rx) = mpsc::channel();
// for matches_info in matches_info_vec.into_iter() {
// tx.send(Ok(matches_info)).unwrap();
// }
// rx
// }
// fn push_cannot_have_mbid(matches_info_vec: &mut [AppMatchesInfo]) {
// for matches_info in matches_info_vec.iter_mut() {
// matches_info.push_cannot_have_mbid();
// }
// }
// #[test]
// fn create_empty() {
// let matches = AppMachine::matches(inner(music_hoard(vec![])), AppMatches::empty(fetch()));
// let widget_state = WidgetState::default();
// assert_eq!(matches.state.current, None);
// assert_eq!(matches.state.state, widget_state);
// let mut app = matches.no_op();
// let public = app.get();
// let public_matches = public.state.unwrap_matches();
// assert_eq!(public_matches.matches, None);
// assert_eq!(public_matches.state, &widget_state);
// }
// #[test]
// fn create_nonempty() {
// let mut matches_info_vec = album_matches_info_vec();
// let matches = AppMachine::app_matches(
// inner(music_hoard(vec![])),
// receiver(matches_info_vec.clone()),
// )
// .unwrap_matches();
// push_cannot_have_mbid(&mut matches_info_vec);
// let mut widget_state = WidgetState::default();
// widget_state.list.select(Some(0));
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state, widget_state);
// let mut app = matches.no_op();
// let public = app.get();
// let public_matches = public.state.unwrap_matches();
// assert_eq!(public_matches.matches, Some(&matches_info_vec[0]));
// assert_eq!(public_matches.state, &widget_state);
// }
// fn matches_flow(mut matches_info_vec: Vec<AppMatchesInfo>) {
// let matches = AppMachine::app_matches(
// inner(music_hoard(vec![])),
// receiver(matches_info_vec.clone()),
// )
// .unwrap_matches();
// push_cannot_have_mbid(&mut matches_info_vec);
// let mut widget_state = WidgetState::default();
// widget_state.list.select(Some(0));
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state, widget_state);
// let matches = matches.prev_match().unwrap_matches();
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state.list.selected(), Some(0));
// let matches = matches.next_match().unwrap_matches();
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state.list.selected(), Some(1));
// // Next is CannotHaveMBID
// let matches = matches.next_match().unwrap_matches();
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state.list.selected(), Some(2));
// let matches = matches.next_match().unwrap_matches();
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state.list.selected(), Some(2));
// let matches = matches.select().unwrap_matches();
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[1]));
// assert_eq!(matches.state.state.list.selected(), Some(0));
// // And it's done
// matches.select().unwrap_browse();
// }
// #[test]
// fn artist_matches_flow() {
// matches_flow(artist_matches_info_vec());
// }
// #[test]
// fn album_matches_flow() {
// matches_flow(album_matches_info_vec());
// }
// #[test]
// fn matches_abort() {
// let mut matches_info_vec = album_matches_info_vec();
// let matches = AppMachine::app_matches(
// inner(music_hoard(vec![])),
// receiver(matches_info_vec.clone()),
// )
// .unwrap_matches();
// push_cannot_have_mbid(&mut matches_info_vec);
// let mut widget_state = WidgetState::default();
// widget_state.list.select(Some(0));
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state, widget_state);
// matches.abort().unwrap_browse();
// }
// #[test]
// fn matches_select_empty() {
// let matches =
// AppMachine::app_matches(inner(music_hoard(vec![])), receiver(vec![])).unwrap_matches();
// assert_eq!(matches.state.current, None);
// matches.select().unwrap_browse();
// }
// #[test]
// fn no_op() {
// let matches =
// AppMachine::app_matches(inner(music_hoard(vec![])), receiver(vec![])).unwrap_matches();
// let app = matches.no_op();
// app.unwrap_matches();
// }
let app = app.no_op();
assert!(matches!(app, AppState::Fetch(_)));
}
}