mirror of
https://github.com/ManiMatter/decluttarr.git
synced 2026-04-19 14:54:12 +02:00
Bug Fixes & Automated Testing - remove_failed_imports.py #patch
This commit is contained in:
11
tests/utils/remove_download/mock_data/mock_data_1.json
Normal file
11
tests/utils/remove_download/mock_data/mock_data_1.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": 1,
|
||||
"downloadId": "A",
|
||||
"title": "Sonarr Title 1",
|
||||
"removal_messages": [
|
||||
">>>>> 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"
|
||||
]
|
||||
}
|
||||
52
tests/utils/remove_download/remove_download_utils.py
Normal file
52
tests/utils/remove_download/remove_download_utils.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import os
|
||||
os.environ['IS_IN_PYTEST'] = 'true'
|
||||
import logging
|
||||
import json
|
||||
import pytest
|
||||
from typing import Dict, Set, Any
|
||||
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:
|
||||
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],
|
||||
expected_removal_messages: Set[str],
|
||||
failType: str,
|
||||
removeFromClient: bool,
|
||||
mock_data_file: str,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
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)
|
||||
|
||||
# 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)
|
||||
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'}
|
||||
|
||||
assert expected_removal_messages == log_messages
|
||||
|
||||
# Check that the affectedItem's downloadId was added to deleted_downloads
|
||||
assert affectedItem['downloadId'] in deleted_downloads.dict
|
||||
33
tests/utils/remove_download/test_remove_download_1.py
Normal file
33
tests/utils/remove_download/test_remove_download_1.py
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
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'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_removal_with_removal_messages(monkeypatch, caplog):
|
||||
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'
|
||||
}
|
||||
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}
|
||||
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)
|
||||
Reference in New Issue
Block a user