Skip to content

Commit

Permalink
fixes #20 Optional job config settings (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
hollanbm authored May 14, 2024
1 parent 3d24220 commit 7806e3f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 24 deletions.
21 changes: 17 additions & 4 deletions src/config_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,29 @@
lambda s: len(s) > 0,
error="sonarr[].api_key is a required field",
),
"series_scanner": {
"enabled": bool,
Optional(
"series_scanner",
default=dict(
enabled=False,
hourly_job=False,
hours_before_air=4,
),
ignore_extra_keys=True,
): {
Optional("enabled", default=False): bool,
Optional("hourly_job", default=False): bool,
Optional("hours_before_air", default=4): int,
},
"existing_renamer": {
"enabled": bool,
Optional(
"existing_renamer",
default=dict(enabled=False, hourly_job=False),
ignore_extra_keys=True,
): {
Optional("enabled", default=False): bool,
Optional("hourly_job", default=False): bool,
},
}
],
ignore_extra_keys=True,
),
}
12 changes: 12 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ def start(self) -> None:
exit(1)

for sonarr_config in config.sonarr:
if (
not sonarr_config.series_scanner.enabled
and not sonarr_config.existing_renamer.enabled
):
with logger.contextualize(instance=sonarr_config.name):
logger.warning(
"Possible config error? -- No jobs configured for current instance"
)
logger.warning(
"Please see example config for comparison -- https://github.com/hollanbm/sonarr-series-scanner/blob/main/docker/config.yml.example"
)
continue
if sonarr_config.series_scanner.enabled:
self.__schedule_series_scanner(sonarr_config)
if sonarr_config.existing_renamer.enabled:
Expand Down
60 changes: 40 additions & 20 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,73 @@
from existing_renamer import ExistingRenamer
from main import Main
from pyconfigparser import Config, configparser
from schedule import Job
from series_scanner import SeriesScanner

# disable config caching
configparser.hold_an_instance = False


class TestMain:
def get_single_config(self) -> Config:
@pytest.fixture
def config(self) -> Config:
return configparser.get_config(
CONFIG_SCHEMA,
config_dir="tests/fixtures",
file_name="single_sonarr.yml",
)

@pytest.fixture
def all_disabled(self, mocker):
mocker.patch(
"pyconfigparser.configparser.get_config"
).return_value = self.get_single_config()

@pytest.fixture
def series_scanner_enabled(self, mocker):
config = self.get_single_config()
config.sonarr[0].series_scanner.enabled = True
mocker.patch("pyconfigparser.configparser.get_config").return_value = config

@pytest.fixture
def existing_renamer_enabled(self, mocker):
config = self.get_single_config()
config.sonarr[0].existing_renamer.enabled = True
def test_all_disabled(self, config, mocker) -> None:
mocker.patch("pyconfigparser.configparser.get_config").return_value = config

def test_all_disabled(self, all_disabled, mocker):
series_scanner = mocker.patch.object(SeriesScanner, "scan")
existing_renamer = mocker.patch.object(ExistingRenamer, "scan")

Main().start()

assert not series_scanner.called
assert not existing_renamer.called

def test_series_scanner_scan(self, series_scanner_enabled, mocker) -> None:
def test_series_scanner_scan(self, config, mocker) -> None:
config.sonarr[0].series_scanner.enabled = True
mocker.patch("pyconfigparser.configparser.get_config").return_value = config
mocker.patch.object(Job, "do")

series_scanner = mocker.patch.object(SeriesScanner, "scan")

Main().start()
assert series_scanner.called

def test_existing_renamer_scan(self, existing_renamer_enabled, mocker) -> None:
def test_series_scanner_hourly_job(self, config, mocker) -> None:
config.sonarr[0].series_scanner.enabled = True
config.sonarr[0].series_scanner.hourly_job = True
mocker.patch("pyconfigparser.configparser.get_config").return_value = config
job = mocker.patch.object(Job, "do")

series_scanner = mocker.patch.object(SeriesScanner, "scan")

Main().start()
assert series_scanner.called
assert job.called

def test_existing_renamer_scan(self, config, mocker) -> None:
config.sonarr[0].existing_renamer.enabled = True
mocker.patch("pyconfigparser.configparser.get_config").return_value = config
mocker.patch.object(Job, "do")

existing_renamer = mocker.patch.object(ExistingRenamer, "scan")

Main().start()
assert existing_renamer.called

def test_existing_renamer_hourly_job(self, config, mocker) -> None:
config.sonarr[0].existing_renamer.enabled = True
config.sonarr[0].existing_renamer.hourly_job = True
mocker.patch("pyconfigparser.configparser.get_config").return_value = config
job = mocker.patch.object(Job, "do")

existing_renamer = mocker.patch.object(ExistingRenamer, "scan")

Main().start()
assert existing_renamer.called
assert job.called

0 comments on commit 7806e3f

Please sign in to comment.