Added black as pre-commit and applied it.

This commit is contained in:
Benjamin Harder
2024-09-08 18:47:57 +02:00
parent bba67f07c0
commit a926f10561
29 changed files with 1716 additions and 704 deletions

View File

@@ -5,49 +5,148 @@ from src.utils.nest_functions import nested_set, add_keys_nested_dict, nested_ge
# import asyncio
# Dictionary that is modified / queried as part of tests
input_dict = { 1: {'name': 'Breaking Bad 1', 'data': {'episodes': 3, 'year': 1991, 'actors': ['Peter', 'Paul', 'Ppacey']}},
2: {'name': 'Breaking Bad 2', 'data': {'episodes': 6, 'year': 1992, 'actors': ['Weter', 'Waul', 'Wpacey']}},
3: {'name': 'Breaking Bad 3', 'data': {'episodes': 9, 'year': 1993, 'actors': ['Zeter', 'Zaul', 'Zpacey']}}}
input_dict = {
1: {
"name": "Breaking Bad 1",
"data": {"episodes": 3, "year": 1991, "actors": ["Peter", "Paul", "Ppacey"]},
},
2: {
"name": "Breaking Bad 2",
"data": {"episodes": 6, "year": 1992, "actors": ["Weter", "Waul", "Wpacey"]},
},
3: {
"name": "Breaking Bad 3",
"data": {"episodes": 9, "year": 1993, "actors": ["Zeter", "Zaul", "Zpacey"]},
},
}
# @pytest.mark.asyncio
# async def test_nested_set():
def test_nested_set():
expected_output = { 1: {'name': 'Breaking Bad 1', 'data': {'episodes': 3, 'year': 1991, 'actors': ['Peter', 'Paul', 'Ppacey']}},
2: {'name': 'Breaking Bad 2', 'data': {'episodes': 6, 'year': 1994, 'actors': ['Weter', 'Waul', 'Wpacey']}},
3: {'name': 'Breaking Bad 3', 'data': {'episodes': 9, 'year': 1993, 'actors': ['Zeter', 'Zaul', 'Zpacey']}}}
expected_output = {
1: {
"name": "Breaking Bad 1",
"data": {
"episodes": 3,
"year": 1991,
"actors": ["Peter", "Paul", "Ppacey"],
},
},
2: {
"name": "Breaking Bad 2",
"data": {
"episodes": 6,
"year": 1994,
"actors": ["Weter", "Waul", "Wpacey"],
},
},
3: {
"name": "Breaking Bad 3",
"data": {
"episodes": 9,
"year": 1993,
"actors": ["Zeter", "Zaul", "Zpacey"],
},
},
}
output = input_dict
# await nested_set(output, [2, 'data' ,'year'], 1994)
nested_set(output, [2, 'data' ,'year'], 1994)
nested_set(output, [2, "data", "year"], 1994)
assert expected_output == output
def test_nested_set_conditions():
input = { 1: [{'year': 2001, 'rating': 'high'}, {'year': 2002, 'rating': 'high'}, {'year': 2003, 'rating': 'high'}],
2: [{'year': 2001, 'rating': 'high'}, {'year': 2002, 'rating': 'high'}, {'year': 2003, 'rating': 'high'}]}
expected_output = { 1: [{'year': 2001, 'rating': 'high'}, {'year': 2002, 'rating': 'high'}, {'year': 2003, 'rating': 'high'}],
2: [{'year': 2001, 'rating': 'high'}, {'year': 2002, 'rating': 'high'}, {'year': 2003, 'rating': 'LOW'}]}
input = {
1: [
{"year": 2001, "rating": "high"},
{"year": 2002, "rating": "high"},
{"year": 2003, "rating": "high"},
],
2: [
{"year": 2001, "rating": "high"},
{"year": 2002, "rating": "high"},
{"year": 2003, "rating": "high"},
],
}
expected_output = {
1: [
{"year": 2001, "rating": "high"},
{"year": 2002, "rating": "high"},
{"year": 2003, "rating": "high"},
],
2: [
{"year": 2001, "rating": "high"},
{"year": 2002, "rating": "high"},
{"year": 2003, "rating": "LOW"},
],
}
output = input
nested_set(output, [2, 'rating'], 'LOW', {'year': 2003})
nested_set(output, [2, "rating"], "LOW", {"year": 2003})
assert expected_output == output
def test_nested_set_conditions_multiple():
input = { 1: [{'rating': 'high', 'color': 1, 'stack': 1}, {'rating': 'high', 'color': 2, 'stack': 2}, {'rating': 'high', 'color': 2, 'stack': 1}]}
expected_output = { 1: [{'rating': 'high', 'color': 1, 'stack': 1}, {'rating': 'high', 'color': 2, 'stack': 2}, {'rating': 'LOW', 'color': 2, 'stack': 1}]}
input = {
1: [
{"rating": "high", "color": 1, "stack": 1},
{"rating": "high", "color": 2, "stack": 2},
{"rating": "high", "color": 2, "stack": 1},
]
}
expected_output = {
1: [
{"rating": "high", "color": 1, "stack": 1},
{"rating": "high", "color": 2, "stack": 2},
{"rating": "LOW", "color": 2, "stack": 1},
]
}
output = input
nested_set(output, [1, 'rating'], 'LOW', {'color': 2, 'stack': 1})
nested_set(output, [1, "rating"], "LOW", {"color": 2, "stack": 1})
assert expected_output == output
def test_add_keys_nested_dict():
expected_output = { 1: {'name': 'Breaking Bad 1', 'data': {'episodes': 3, 'year': 1991, 'actors': ['Peter', 'Paul', 'Ppacey']}},
2: {'name': 'Breaking Bad 2', 'data': {'episodes': 6, 'year': 1994, 'actors': ['Weter', 'Waul', 'Wpacey'], 'spaceship': True}},
3: {'name': 'Breaking Bad 3', 'data': {'episodes': 9, 'year': 1993, 'actors': ['Zeter', 'Zaul', 'Zpacey']}}}
expected_output = {
1: {
"name": "Breaking Bad 1",
"data": {
"episodes": 3,
"year": 1991,
"actors": ["Peter", "Paul", "Ppacey"],
},
},
2: {
"name": "Breaking Bad 2",
"data": {
"episodes": 6,
"year": 1994,
"actors": ["Weter", "Waul", "Wpacey"],
"spaceship": True,
},
},
3: {
"name": "Breaking Bad 3",
"data": {
"episodes": 9,
"year": 1993,
"actors": ["Zeter", "Zaul", "Zpacey"],
},
},
}
output = input_dict
add_keys_nested_dict(output, [2, 'data' ,'spaceship'], True)
add_keys_nested_dict(output, [2, "data", "spaceship"], True)
assert expected_output == output
def test_nested_get():
input = { 1: [{'name': 'A', 'color': 1, 'stack': 1}, {'name': 'B', 'color': 2, 'stack': 2}, {'name': 'C', 'color': 2, 'stack': 1}]}
expected_output = ['C']
output = nested_get(input[1], 'name', {'color': 2, 'stack': 1})
input = {
1: [
{"name": "A", "color": 1, "stack": 1},
{"name": "B", "color": 2, "stack": 2},
{"name": "C", "color": 2, "stack": 1},
]
}
expected_output = ["C"]
output = nested_get(input[1], "name", {"color": 2, "stack": 1})
assert expected_output == output

View File

@@ -1,5 +1,6 @@
import os
os.environ['IS_IN_PYTEST'] = 'true'
os.environ["IS_IN_PYTEST"] = "true"
import logging
import json
import pytest
@@ -8,45 +9,56 @@ from src.utils.shared import remove_download
from src.utils.trackers import Deleted_Downloads
# Utility function to load mock data
def load_mock_data(file_name):
with open(file_name, 'r') as file:
with open(file_name, "r") as file:
return json.load(file)
async def mock_rest_delete() -> None:
logger.debug(f"Mock rest_delete called with URL")
async def run_test(
settingsDict: Dict[str, Any],
settingsDict: Dict[str, Any],
expected_removal_messages: Set[str],
failType: str,
removeFromClient: bool,
mock_data_file: str,
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture
caplog: pytest.LogCaptureFixture,
) -> None:
# Load mock data
affectedItem = load_mock_data(mock_data_file)
# Mock the `rest_delete` function
monkeypatch.setattr('src.utils.shared.rest_delete', mock_rest_delete)
monkeypatch.setattr("src.utils.shared.rest_delete", mock_rest_delete)
# Call the function
with caplog.at_level(logging.INFO):
# Call the function and assert no exceptions
try:
deleted_downloads = Deleted_Downloads([])
await remove_download(settingsDict=settingsDict, BASE_URL='', API_KEY='', affectedItem=affectedItem, failType=failType, addToBlocklist=True, deleted_downloads=deleted_downloads, removeFromClient=removeFromClient)
try:
deleted_downloads = Deleted_Downloads([])
await remove_download(
settingsDict=settingsDict,
BASE_URL="",
API_KEY="",
affectedItem=affectedItem,
failType=failType,
addToBlocklist=True,
deleted_downloads=deleted_downloads,
removeFromClient=removeFromClient,
)
except Exception as e:
pytest.fail(f"remove_download raised an exception: {e}")
# Assertions:
# Check that expected log messages are in the captured log
log_messages = {record.message for record in caplog.records if record.levelname == 'INFO'}
log_messages = {
record.message for record in caplog.records if record.levelname == "INFO"
}
assert expected_removal_messages == log_messages
# Check that the affectedItem's downloadId was added to deleted_downloads
assert affectedItem['downloadId'] in deleted_downloads.dict
assert affectedItem["downloadId"] in deleted_downloads.dict

View File

@@ -1,33 +1,50 @@
import pytest
from remove_download_utils import run_test
# Parameters identical across all tests
mock_data_file = 'tests/utils/remove_download/mock_data/mock_data_1.json'
failType = 'failed import'
mock_data_file = "tests/utils/remove_download/mock_data/mock_data_1.json"
failType = "failed import"
@pytest.mark.asyncio
async def test_removal_with_removal_messages(monkeypatch, caplog):
settingsDict = {'TEST_RUN': True}
settingsDict = {"TEST_RUN": True}
removeFromClient = True
expected_removal_messages = {
'>>> Removing failed import download: Sonarr Title 1',
'>>>>> Tracked Download State: importBlocked',
'>>>>> Status Messages (matching specified patterns):',
'>>>>> - Episode XYZ was not found in the grabbed release: Sonarr Title 2.mkv',
'>>>>> - And yet another message'
">>> Removing failed import download: Sonarr Title 1",
">>>>> Tracked Download State: importBlocked",
">>>>> Status Messages (matching specified patterns):",
">>>>> - Episode XYZ was not found in the grabbed release: Sonarr Title 2.mkv",
">>>>> - And yet another message",
}
await run_test(settingsDict=settingsDict, expected_removal_messages=expected_removal_messages, failType=failType, removeFromClient=removeFromClient, mock_data_file=mock_data_file, monkeypatch=monkeypatch, caplog=caplog)
await run_test(
settingsDict=settingsDict,
expected_removal_messages=expected_removal_messages,
failType=failType,
removeFromClient=removeFromClient,
mock_data_file=mock_data_file,
monkeypatch=monkeypatch,
caplog=caplog,
)
@pytest.mark.asyncio
async def test_schizophrenic_removal_with_removal_messages(monkeypatch, caplog):
settingsDict = {'TEST_RUN': True}
settingsDict = {"TEST_RUN": True}
removeFromClient = False
expected_removal_messages = {
'>>> Removing failed import download (without removing from torrent client): Sonarr Title 1',
'>>>>> Tracked Download State: importBlocked',
'>>>>> Status Messages (matching specified patterns):',
'>>>>> - Episode XYZ was not found in the grabbed release: Sonarr Title 2.mkv',
'>>>>> - And yet another message'
}
await run_test(settingsDict=settingsDict, expected_removal_messages=expected_removal_messages, failType=failType, removeFromClient=removeFromClient, mock_data_file=mock_data_file, monkeypatch=monkeypatch, caplog=caplog)
">>> Removing failed import download (without removing from torrent client): Sonarr Title 1",
">>>>> Tracked Download State: importBlocked",
">>>>> Status Messages (matching specified patterns):",
">>>>> - Episode XYZ was not found in the grabbed release: Sonarr Title 2.mkv",
">>>>> - And yet another message",
}
await run_test(
settingsDict=settingsDict,
expected_removal_messages=expected_removal_messages,
failType=failType,
removeFromClient=removeFromClient,
mock_data_file=mock_data_file,
monkeypatch=monkeypatch,
caplog=caplog,
)