Started writing more tests in the handlers
This commit is contained in:
+2
-1
@@ -27,4 +27,5 @@ tui = "0.19.0"
|
||||
urlencoding = "2.1.2"
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.3.0"
|
||||
pretty_assertions = "1.3.0"
|
||||
rstest = "0.16.0"
|
||||
+6
-3
@@ -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(),
|
||||
|
||||
+7
-2
@@ -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();
|
||||
|
||||
+1
-2
@@ -55,8 +55,7 @@ impl From<KeyEvent> for Key {
|
||||
..
|
||||
} => Key::Home,
|
||||
KeyEvent {
|
||||
code: KeyCode::End,
|
||||
..
|
||||
code: KeyCode::End, ..
|
||||
} => Key::End,
|
||||
KeyEvent {
|
||||
code: KeyCode::Delete,
|
||||
|
||||
+103
-1
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -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()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user