feat(cli): Support for adding a series to Sonarr

This commit is contained in:
2024-11-24 14:29:13 -07:00
parent 5ba3f2b1ba
commit 8125bd5ae0
12 changed files with 523 additions and 51 deletions
+372 -1
View File
@@ -23,8 +23,11 @@ mod tests {
}
mod cli {
use crate::models::sonarr_models::{SeriesMonitor, SeriesType};
use super::*;
use pretty_assertions::assert_eq;
use rstest::rstest;
#[test]
fn test_add_root_folder_requires_arguments() {
@@ -60,6 +63,318 @@ mod tests {
}
}
#[test]
fn test_add_series_requires_arguments() {
let result = Cli::command().try_get_matches_from(["managarr", "sonarr", "add", "series"]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_add_series_requires_tvdb_id() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_add_series_requires_root_folder_path() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--tvdb-id",
"1",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_add_series_requires_quality_profile_id() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--tvdb-id",
"1",
"--root-folder-path",
"test",
"--language-profile-id",
"1",
]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_add_series_requires_language_profile_id() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--tvdb-id",
"1",
"--root-folder-path",
"test",
"--quality-profile-id",
"1",
]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[rstest]
fn test_add_series_assert_argument_flags_require_args(
#[values("--series-type", "--tag", "--monitor")] flag: &str,
) {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--tvdb-id",
"1",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
flag,
]);
assert!(result.is_err());
assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn test_add_series_all_arguments_satisfied() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--tvdb-id",
"1",
]);
assert!(result.is_ok());
}
#[test]
fn test_add_series_series_type_validation() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--tvdb-id",
"1",
"--series-type",
"test",
]);
assert!(result.is_err());
assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn test_add_series_monitor_validation() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--tvdb-id",
"1",
"--monitor",
"test",
]);
assert!(result.is_err());
assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn test_add_series_defaults() {
let expected_args = SonarrAddCommand::Series {
tvdb_id: 1,
root_folder_path: "/test".to_owned(),
quality_profile_id: 1,
language_profile_id: 1,
series_type: SeriesType::default(),
disable_monitoring: false,
disable_season_folders: false,
tag: vec![],
monitor: SeriesMonitor::default(),
no_search_for_series: false,
};
let result = Cli::try_parse_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--tvdb-id",
"1",
]);
assert!(result.is_ok());
if let Some(Command::Sonarr(SonarrCommand::Add(add_command))) = result.unwrap().command {
assert_eq!(add_command, expected_args);
}
}
#[test]
fn test_add_series_tags_is_repeatable() {
let expected_args = SonarrAddCommand::Series {
tvdb_id: 1,
root_folder_path: "/test".to_owned(),
quality_profile_id: 1,
language_profile_id: 1,
series_type: SeriesType::default(),
disable_monitoring: false,
disable_season_folders: false,
tag: vec![1, 2],
monitor: SeriesMonitor::default(),
no_search_for_series: false,
};
let result = Cli::try_parse_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--tvdb-id",
"1",
"--tag",
"1",
"--tag",
"2",
]);
assert!(result.is_ok());
if let Some(Command::Sonarr(SonarrCommand::Add(add_command))) = result.unwrap().command {
assert_eq!(add_command, expected_args);
}
}
#[test]
fn test_add_series_all_args_defined() {
let expected_args = SonarrAddCommand::Series {
tvdb_id: 1,
root_folder_path: "/test".to_owned(),
quality_profile_id: 1,
language_profile_id: 1,
series_type: SeriesType::Anime,
disable_monitoring: true,
disable_season_folders: true,
tag: vec![1, 2],
monitor: SeriesMonitor::Future,
no_search_for_series: true,
};
let result = Cli::try_parse_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--series-type",
"anime",
"--disable-monitoring",
"--disable-season-folders",
"--tvdb-id",
"1",
"--tag",
"1",
"--tag",
"2",
"--monitor",
"future",
"--no-search-for-series",
]);
assert!(result.is_ok());
if let Some(Command::Sonarr(SonarrCommand::Add(add_command))) = result.unwrap().command {
assert_eq!(add_command, expected_args);
}
}
#[test]
fn test_add_tag_requires_arguments() {
let result = Cli::command().try_get_matches_from(["managarr", "sonarr", "add", "tag"]);
@@ -93,7 +408,12 @@ mod tests {
use crate::{
app::App,
cli::{sonarr::add_command_handler::SonarrAddCommandHandler, CliCommandHandler},
models::{sonarr_models::SonarrSerdeable, Serdeable},
models::{
sonarr_models::{
AddSeriesBody, AddSeriesOptions, SeriesMonitor, SeriesType, SonarrSerdeable,
},
Serdeable,
},
network::{sonarr_network::SonarrEvent, MockNetworkTrait, NetworkEvent},
};
@@ -131,6 +451,57 @@ mod tests {
assert!(result.is_ok());
}
#[tokio::test]
async fn test_handle_add_series_command() {
let expected_add_series_body = AddSeriesBody {
tvdb_id: 1,
title: String::new(),
root_folder_path: "/test".to_owned(),
quality_profile_id: 1,
language_profile_id: 1,
series_type: "anime".to_owned(),
monitored: false,
tags: vec![1, 2],
season_folder: false,
add_options: AddSeriesOptions {
monitor: "future".to_owned(),
search_for_cutoff_unmet_episodes: false,
search_for_missing_episodes: false,
},
};
let mut mock_network = MockNetworkTrait::new();
mock_network
.expect_handle_network_event()
.with(eq::<NetworkEvent>(
SonarrEvent::AddSeries(Some(expected_add_series_body)).into(),
))
.times(1)
.returning(|_| {
Ok(Serdeable::Sonarr(SonarrSerdeable::Value(
json!({"testResponse": "response"}),
)))
});
let app_arc = Arc::new(Mutex::new(App::default()));
let add_series_command = SonarrAddCommand::Series {
tvdb_id: 1,
root_folder_path: "/test".to_owned(),
quality_profile_id: 1,
language_profile_id: 1,
series_type: SeriesType::Anime,
disable_monitoring: true,
disable_season_folders: true,
tag: vec![1, 2],
monitor: SeriesMonitor::Future,
no_search_for_series: true,
};
let result = SonarrAddCommandHandler::with(&app_arc, add_series_command, &mut mock_network)
.handle()
.await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_handle_add_tag_command() {
let expected_tag_name = "test".to_owned();