Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more unit tests #15

Merged
merged 6 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
"dockerfile": "Dockerfile"
},
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"ghcr.io/devcontainers-contrib/features/zsh-plugins:0": {
"plugins": "git zsh-syntax-highlighting zsh-autosuggestions poetry poetry-env gpg-agent",
"omzPlugins": "https://github.com/zsh-users/zsh-syntax-highlighting https://github.com/zsh-users/zsh-autosuggestions"
},
"ghcr.io/stuartleeks/dev-container-features/shell-history:0": {},
"ghcr.io/devcontainers/features/github-cli:1": {}
},
"containerEnv": {
"LOGURU_LEVEL": "DEBUG",
"GH_TOKEN": "${localEnv:GH_TOKEN}"
},
"mounts": [
"source=${localWorkspaceFolder}/.zsh_history,target=/home/poetry/.zsh_history,type=bind,consistency=cached",
"source=${localWorkspaceFolder}/config.yml,target=/config/config.yml,type=bind,consistency=cached"
],
"postCreateCommand": "zsh .devcontainer/postCreateCommand.sh",
Expand Down Expand Up @@ -63,7 +64,7 @@
"unittestEnabled": false,
"pytestEnabled": true,
"pytestArgs": [
"test"
"tests"
]
}
},
Expand Down
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2

updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
day: sunday

- package-ecosystem: pip
directory: /
schedule:
interval: weekly
day: sunday
11 changes: 9 additions & 2 deletions .github/workflows/build-test-lint.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
name: Python package

on: [push]
on:
push:
branches:
- main
pull_request:
branches:
- feat/**
- main

jobs:
build:
Expand Down Expand Up @@ -45,7 +52,7 @@ jobs:
#----------------------------------------------
- name: Test with pytest
run: |
pytest --cov=src --cov-report=xml
pytest --cov-branch --cov=src --cov-report=xml tests

#----------------------------------------------
# lint and formatting
Expand Down
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@

.zsh_history
config.yml

# Ignore dynaconf secret files
.secrets.*

# Created by https://www.toptal.com/developers/gitignore/api/macos,linux,python,pycharm+all,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,linux,python,pycharm+all,visualstudiocode

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ There is a [devcontainer](https://containers.dev/) provided; it is optional but

####

You will need to create `config.yml` in the root of the repo, to mount your config within the devcontainer

```shell
$ poetry install

Expand Down
52 changes: 51 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ ruff = "^0.4.4"
[tool.poetry.group.test.dependencies]
pytest = "^8.2.0"
pytest-cov = "^5.0.0"
pytest-mock = "^3.14.0"
mock = "^5.1.0"
pytest-loguru = "^0.4.0"

[build-system]
requires = ["poetry-core"]
Expand All @@ -32,13 +35,13 @@ build-backend = "poetry.core.masonry.api"
[tool.pytest.ini_options]
pythonpath = [
"./src",
"./src/config",
"./src/models"
]
testpaths = [
"./test",
"./test/models"
"./tests",
"./tests/models"
]
addopts = [
"--import-mode=importlib",
]
mock_use_standalone_module = "True"
File renamed without changes.
152 changes: 75 additions & 77 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,89 @@
from time import sleep

import schedule
from config.schema import CONFIG_SCHEMA
from config_schema import CONFIG_SCHEMA
from existing_renamer import ExistingRenamer
from loguru import logger
from pycliarr.api import CliServerError
from pyconfigparser import ConfigError, ConfigFileNotFoundError, configparser
from series_scanner import SeriesScanner


def series_scanner_job(sonarr_config):
try:
SeriesScanner(
name=sonarr_config.name,
url=sonarr_config.url,
api_key=sonarr_config.api_key,
hours_before_air=sonarr_config.series_scanner.hours_before_air,
).scan()
except CliServerError as exc:
logger.error(exc)


def schedule_series_scanner(sonarr_config):
series_scanner_job(sonarr_config)

if sonarr_config.series_scanner.hourly_job:
# Add a random delay of +-5 minutes between jobs
schedule.every(55).to(65).minutes.do(
series_scanner_job, sonarr_config=sonarr_config
class Main:
def __init__(self):
logger_format = (
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
"<level>{level}</level> | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | "
"{extra[instance]} | "
"{extra[series]} | "
"<level>{message}</level>"
)


def existing_renamer_job(sonarr_config):
try:
ExistingRenamer(
name=sonarr_config.name,
url=sonarr_config.url,
api_key=sonarr_config.api_key,
).scan()
except CliServerError as exc:
logger.error(exc)


def schedule_existing_renamer(sonarr_config):
existing_renamer_job(sonarr_config)

if sonarr_config.existing_renamer.hourly_job:
# Add a random delay of +-5 minutes between jobs
schedule.every(55).to(65).minutes.do(
existing_renamer_job, sonarr_config=sonarr_config
)


def loguru_config():
logger_format = (
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
"<level>{level}</level> | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | "
"{extra[instance]} | "
"{extra[series]} | "
"<level>{message}</level>"
)
logger.configure(extra={"instance": "", "series": ""}) # Default values
logger.remove()
logger.add(stdout, format=logger_format)
logger.configure(extra={"instance": "", "series": ""}) # Default values
logger.remove()
logger.add(stdout, format=logger_format)

def __series_scanner_job(self, sonarr_config):
try:
SeriesScanner(
name=sonarr_config.name,
url=sonarr_config.url,
api_key=sonarr_config.api_key,
hours_before_air=sonarr_config.series_scanner.hours_before_air,
).scan()
except CliServerError as exc:
logger.error(exc)

Check warning on line 37 in src/main.py

View check run for this annotation

Codecov / codecov/patch

src/main.py#L36-L37

Added lines #L36 - L37 were not covered by tests

def __schedule_series_scanner(self, sonarr_config):
self.__series_scanner_job(sonarr_config)

if sonarr_config.series_scanner.hourly_job:
# Add a random delay of +-5 minutes between jobs
schedule.every(55).to(65).minutes.do(

Check warning on line 44 in src/main.py

View check run for this annotation

Codecov / codecov/patch

src/main.py#L44

Added line #L44 was not covered by tests
self.__series_scanner_job, sonarr_config=sonarr_config
)

def __existing_renamer_job(self, sonarr_config):
try:
ExistingRenamer(
name=sonarr_config.name,
url=sonarr_config.url,
api_key=sonarr_config.api_key,
).scan()
except CliServerError as exc:
logger.error(exc)

Check warning on line 56 in src/main.py

View check run for this annotation

Codecov / codecov/patch

src/main.py#L55-L56

Added lines #L55 - L56 were not covered by tests

def __schedule_existing_renamer(self, sonarr_config):
self.__existing_renamer_job(sonarr_config)

if sonarr_config.existing_renamer.hourly_job:
# Add a random delay of +-5 minutes between jobs
schedule.every(55).to(65).minutes.do(

Check warning on line 63 in src/main.py

View check run for this annotation

Codecov / codecov/patch

src/main.py#L63

Added line #L63 was not covered by tests
self.__existing_renamer_job, sonarr_config=sonarr_config
)

def start(self) -> None:
try:
config = configparser.get_config(
CONFIG_SCHEMA,
config_dir=path.relpath("/config"),
file_name="config.yml",
)
except (ConfigError, ConfigFileNotFoundError) as exc:
logger.error(exc)
exit(1)

Check warning on line 76 in src/main.py

View check run for this annotation

Codecov / codecov/patch

src/main.py#L74-L76

Added lines #L74 - L76 were not covered by tests

for sonarr_config in config.sonarr:
if sonarr_config.series_scanner.enabled:
self.__schedule_series_scanner(sonarr_config)
if sonarr_config.existing_renamer.enabled:
self.__schedule_existing_renamer(sonarr_config)

if schedule.get_jobs():
while True:
schedule.run_pending()
sleep(1)

Check warning on line 87 in src/main.py

View check run for this annotation

Codecov / codecov/patch

src/main.py#L85-L87

Added lines #L85 - L87 were not covered by tests


if __name__ == "__main__":
loguru_config()

try:
config = configparser.get_config(
CONFIG_SCHEMA,
config_dir=path.relpath("/config"),
file_name="config.yml",
)
except (ConfigError, ConfigFileNotFoundError) as exc:
logger.error(exc)
exit(1)

for sonarr_config in config.sonarr:
if sonarr_config.series_scanner.enabled:
schedule_series_scanner(sonarr_config)
if sonarr_config.existing_renamer.enabled:
schedule_existing_renamer(sonarr_config)

if schedule.get_jobs():
while True:
schedule.run_pending()
sleep(1)
Main().start()

Check warning on line 91 in src/main.py

View check run for this annotation

Codecov / codecov/patch

src/main.py#L91

Added line #L91 was not covered by tests
Loading