From c15bda5885a7b549acbb5059ca83f419618e4b03 Mon Sep 17 00:00:00 2001 From: Dark-Alex-17 Date: Tue, 8 Aug 2023 10:50:05 -0600 Subject: [PATCH] Started writing more tests in the handlers --- Cargo.toml | 3 +- src/app/mod.rs | 9 +- src/app/radarr.rs | 9 +- src/event/key.rs | 3 +- src/handlers/mod.rs | 104 +++++++++- src/handlers/radarr_handlers/mod.rs | 308 ++++++++++++++++++++++++++++ src/utils.rs | 2 +- 7 files changed, 428 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6fc4662..c9d93e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,5 @@ tui = "0.19.0" urlencoding = "2.1.2" [dev-dependencies] -pretty_assertions = "1.3.0" \ No newline at end of file +pretty_assertions = "1.3.0" +rstest = "0.16.0" \ No newline at end of file diff --git a/src/app/mod.rs b/src/app/mod.rs index ab2c648..d9df804 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -173,12 +173,15 @@ impl Default for RadarrConfig { #[cfg(test)] mod tests { + use anyhow::anyhow; use pretty_assertions::assert_eq; use tokio::sync::mpsc; + use crate::app::radarr::{ActiveRadarrBlock, RadarrData}; + use crate::app::{App, Data, DEFAULT_ROUTE}; + use crate::models::HorizontallyScrollableText; use crate::network::radarr_network::RadarrEvent; - - use super::*; + use crate::network::NetworkEvent; #[test] fn test_navigation_stack_methods() { @@ -232,7 +235,7 @@ mod tests { fn test_reset() { let mut app = App { tick_count: 2, - error: String::from("Test error").into(), + error: "Test error".to_owned().into(), data: Data { radarr_data: RadarrData { version: "test".to_owned(), diff --git a/src/app/radarr.rs b/src/app/radarr.rs index 4afa58f..28e509f 100644 --- a/src/app/radarr.rs +++ b/src/app/radarr.rs @@ -555,13 +555,18 @@ mod active_radarr_block_tests { #[cfg(test)] mod tests { + use std::time::Duration; + use pretty_assertions::assert_eq; use tokio::sync::mpsc; + use crate::app::radarr::ActiveRadarrBlock; + use crate::app::App; + use crate::models::radarr_models::{Collection, CollectionMovie, Credit, Release}; + use crate::models::StatefulTable; + use crate::network::radarr_network::RadarrEvent; use crate::network::NetworkEvent; - use super::*; - #[tokio::test] async fn test_dispatch_by_collections_block() { let (mut app, mut sync_network_rx) = construct_app_unit(); diff --git a/src/event/key.rs b/src/event/key.rs index 78ae188..86a8f74 100644 --- a/src/event/key.rs +++ b/src/event/key.rs @@ -55,8 +55,7 @@ impl From for Key { .. } => Key::Home, KeyEvent { - code: KeyCode::End, - .. + code: KeyCode::End, .. } => Key::End, KeyEvent { code: KeyCode::Delete, diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 7c677a6..a324643 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -57,7 +57,9 @@ fn handle_clear_errors(app: &mut App) { fn handle_prompt_toggle(app: &mut App, key: &Key) { match key { _ if *key == DEFAULT_KEYBINDINGS.left.key || *key == DEFAULT_KEYBINDINGS.right.key => { - app.data.radarr_data.prompt_confirm = !app.data.radarr_data.prompt_confirm; + if let Route::Radarr(_) = app.get_current_route().clone() { + app.data.radarr_data.prompt_confirm = !app.data.radarr_data.prompt_confirm; + } } _ => (), } @@ -77,3 +79,103 @@ macro_rules! handle_text_box_keys { } }; } + +#[cfg(test)] +mod tests { + use crate::app::App; + use crate::event::Key; + use crate::handlers::{handle_clear_errors, handle_prompt_toggle}; + + #[macro_export] + macro_rules! simple_stateful_iterable_vec { + ($name:ident) => { + vec![ + $name { + title: "Test 1".to_owned(), + ..$name::default() + }, + $name { + title: "Test 2".to_owned(), + ..$name::default() + }, + ] + }; + } + + #[macro_export] + macro_rules! extended_stateful_iterable_vec { + ($name:ident) => { + vec![ + $name { + title: "Test 1".to_owned(), + ..$name::default() + }, + $name { + title: "Test 2".to_owned(), + ..$name::default() + }, + $name { + title: "Test 3".to_owned(), + ..$name::default() + }, + ] + }; + } + + #[test] + fn test_handle_clear_errors() { + let mut app = App::default(); + app.error = "test error".to_owned().into(); + + handle_clear_errors(&mut app); + + assert!(app.error.text.is_empty()); + } + + #[test] + fn test_handle_prompt_toggle_left() { + let mut app = App::default(); + let key = Key::Left; + + assert!(!app.data.radarr_data.prompt_confirm); + + handle_prompt_toggle(&mut app, &key); + + assert!(app.data.radarr_data.prompt_confirm); + + handle_prompt_toggle(&mut app, &key); + + assert!(!app.data.radarr_data.prompt_confirm); + } + + #[test] + fn test_handle_prompt_toggle_right() { + let mut app = App::default(); + let key = Key::Right; + + assert!(!app.data.radarr_data.prompt_confirm); + + handle_prompt_toggle(&mut app, &key); + + assert!(app.data.radarr_data.prompt_confirm); + + handle_prompt_toggle(&mut app, &key); + + assert!(!app.data.radarr_data.prompt_confirm); + } + + #[test] + fn test_handle_prompt_toggle_left_and_right_are_inverses() { + let mut app = App::default(); + + assert!(!app.data.radarr_data.prompt_confirm); + + handle_prompt_toggle(&mut app, &Key::Left); + + assert!(app.data.radarr_data.prompt_confirm); + + handle_prompt_toggle(&mut app, &Key::Right); + + assert!(!app.data.radarr_data.prompt_confirm); + } +} diff --git a/src/handlers/radarr_handlers/mod.rs b/src/handlers/radarr_handlers/mod.rs index ef78741..0b23797 100644 --- a/src/handlers/radarr_handlers/mod.rs +++ b/src/handlers/radarr_handlers/mod.rs @@ -493,3 +493,311 @@ impl RadarrHandler<'_> { filter_matches } } + +#[cfg(test)] +mod tests { + mod test_handle_scroll_up_and_down { + use pretty_assertions::assert_eq; + use rstest::rstest; + + use crate::app::radarr::ActiveRadarrBlock; + use crate::app::App; + use crate::event::Key; + use crate::handlers::radarr_handlers::RadarrHandler; + use crate::handlers::KeyEventHandler; + use crate::models::radarr_models::{Collection, DownloadRecord, Movie}; + use crate::simple_stateful_iterable_vec; + + #[rstest] + fn test_collections_scroll(#[values(Key::Up, Key::Down)] key: Key) { + let mut app = App::default(); + app + .data + .radarr_data + .collections + .set_items(simple_stateful_iterable_vec!(Collection)); + + RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Collections).handle(); + + assert_eq!( + app.data.radarr_data.collections.current_selection().title, + "Test 2".to_owned() + ); + + RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Collections).handle(); + + assert_eq!( + app.data.radarr_data.collections.current_selection().title, + "Test 1".to_owned() + ); + } + + #[rstest] + fn test_filtered_collections_scroll(#[values(Key::Up, Key::Down)] key: Key) { + let mut app = App::default(); + app + .data + .radarr_data + .filtered_collections + .set_items(simple_stateful_iterable_vec!(Collection)); + + RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Collections).handle(); + + assert_eq!( + app + .data + .radarr_data + .filtered_collections + .current_selection() + .title, + "Test 2".to_owned() + ); + + RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Collections).handle(); + + assert_eq!( + app + .data + .radarr_data + .filtered_collections + .current_selection() + .title, + "Test 1".to_owned() + ); + } + + #[rstest] + fn test_movies_scroll(#[values(Key::Up, Key::Down)] key: Key) { + let mut app = App::default(); + app + .data + .radarr_data + .movies + .set_items(simple_stateful_iterable_vec!(Movie)); + + RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Movies).handle(); + + assert_eq!( + app.data.radarr_data.movies.current_selection().title, + "Test 2".to_owned() + ); + + RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Movies).handle(); + + assert_eq!( + app.data.radarr_data.movies.current_selection().title, + "Test 1".to_owned() + ); + } + + #[rstest] + fn test_filtered_movies_scroll(#[values(Key::Up, Key::Down)] key: Key) { + let mut app = App::default(); + app + .data + .radarr_data + .filtered_movies + .set_items(simple_stateful_iterable_vec!(Movie)); + + RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Movies).handle(); + + assert_eq!( + app + .data + .radarr_data + .filtered_movies + .current_selection() + .title, + "Test 2".to_owned() + ); + + RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Movies).handle(); + + assert_eq!( + app + .data + .radarr_data + .filtered_movies + .current_selection() + .title, + "Test 1".to_owned() + ); + } + + #[rstest] + fn test_downloads_scroll(#[values(Key::Up, Key::Down)] key: Key) { + let mut app = App::default(); + app + .data + .radarr_data + .downloads + .set_items(simple_stateful_iterable_vec!(DownloadRecord)); + + RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Downloads).handle(); + + assert_eq!( + app.data.radarr_data.downloads.current_selection().title, + "Test 2".to_owned() + ); + + RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Downloads).handle(); + + assert_eq!( + app.data.radarr_data.downloads.current_selection().title, + "Test 1".to_owned() + ); + } + } + + mod test_handle_home_end { + use pretty_assertions::assert_eq; + + use crate::app::radarr::ActiveRadarrBlock; + use crate::app::App; + use crate::event::Key; + use crate::extended_stateful_iterable_vec; + use crate::handlers::radarr_handlers::RadarrHandler; + use crate::handlers::KeyEventHandler; + use crate::models::radarr_models::{Collection, DownloadRecord, Movie}; + + #[test] + fn test_collections_home_end() { + let mut app = App::default(); + app + .data + .radarr_data + .collections + .set_items(extended_stateful_iterable_vec!(Collection)); + + RadarrHandler::with(&Key::End, &mut app, &ActiveRadarrBlock::Collections).handle(); + + assert_eq!( + app.data.radarr_data.collections.current_selection().title, + "Test 3".to_owned() + ); + + RadarrHandler::with(&Key::Home, &mut app, &ActiveRadarrBlock::Collections).handle(); + + assert_eq!( + app.data.radarr_data.collections.current_selection().title, + "Test 1".to_owned() + ); + } + + #[test] + fn test_filtered_collections_home_end() { + let mut app = App::default(); + app + .data + .radarr_data + .filtered_collections + .set_items(extended_stateful_iterable_vec!(Collection)); + + RadarrHandler::with(&Key::End, &mut app, &ActiveRadarrBlock::Collections).handle(); + + assert_eq!( + app + .data + .radarr_data + .filtered_collections + .current_selection() + .title, + "Test 3".to_owned() + ); + + RadarrHandler::with(&Key::Home, &mut app, &ActiveRadarrBlock::Collections).handle(); + + assert_eq!( + app + .data + .radarr_data + .filtered_collections + .current_selection() + .title, + "Test 1".to_owned() + ); + } + + #[test] + fn test_movies_home_end() { + let mut app = App::default(); + app + .data + .radarr_data + .movies + .set_items(extended_stateful_iterable_vec!(Movie)); + + RadarrHandler::with(&Key::End, &mut app, &ActiveRadarrBlock::Movies).handle(); + + assert_eq!( + app.data.radarr_data.movies.current_selection().title, + "Test 3".to_owned() + ); + + RadarrHandler::with(&Key::Home, &mut app, &ActiveRadarrBlock::Movies).handle(); + + assert_eq!( + app.data.radarr_data.movies.current_selection().title, + "Test 1".to_owned() + ); + } + + #[test] + fn test_filtered_movies_home_end() { + let mut app = App::default(); + app + .data + .radarr_data + .filtered_movies + .set_items(extended_stateful_iterable_vec!(Movie)); + + RadarrHandler::with(&Key::End, &mut app, &ActiveRadarrBlock::Movies).handle(); + + assert_eq!( + app + .data + .radarr_data + .filtered_movies + .current_selection() + .title, + "Test 3".to_owned() + ); + + RadarrHandler::with(&Key::Home, &mut app, &ActiveRadarrBlock::Movies).handle(); + + assert_eq!( + app + .data + .radarr_data + .filtered_movies + .current_selection() + .title, + "Test 1".to_owned() + ); + } + + #[test] + fn test_downloads_home_end() { + let mut app = App::default(); + app + .data + .radarr_data + .downloads + .set_items(extended_stateful_iterable_vec!(DownloadRecord)); + + RadarrHandler::with(&Key::End, &mut app, &ActiveRadarrBlock::Downloads).handle(); + + assert_eq!( + app.data.radarr_data.downloads.current_selection().title, + "Test 3".to_owned() + ); + + RadarrHandler::with(&Key::Home, &mut app, &ActiveRadarrBlock::Downloads).handle(); + + assert_eq!( + app.data.radarr_data.downloads.current_selection().title, + "Test 1".to_owned() + ); + } + } +} diff --git a/src/utils.rs b/src/utils.rs index 4f62d3a..2cac571 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -65,7 +65,7 @@ mod tests { fn test_strop_non_alphanumeric_characters() { assert_eq!( strip_non_alphanumeric_characters("Te$t S7r!ng::'~-_}"), - String::from("tet s7rng") + "tet s7rng".to_owned() ) } }