Files
realesrgan-api/tests/test_file_manager.py

45 lines
1.5 KiB
Python
Raw Normal View History

2026-02-16 19:56:25 +01:00
"""Unit tests for file manager."""
import tempfile
from pathlib import Path
from unittest.mock import patch
import pytest
from app.services import file_manager
@pytest.fixture
def temp_data_dir():
"""Create temporary data directory."""
with tempfile.TemporaryDirectory() as tmpdir:
yield Path(tmpdir)
class TestFileManager:
def test_create_request_dir(self, temp_data_dir):
"""Test request directory creation."""
with patch('app.services.file_manager.settings.upload_dir', str(temp_data_dir)):
req_dir = file_manager.create_request_dir()
assert Path(req_dir).exists()
assert Path(req_dir).parent == temp_data_dir
def test_directory_size(self, temp_data_dir):
"""Test directory size calculation."""
test_file = temp_data_dir / 'test.txt'
test_file.write_text('x' * 1024) # 1KB
size_mb = file_manager.get_directory_size_mb(str(temp_data_dir))
assert size_mb > 0
def test_cleanup_old_jobs(self, temp_data_dir):
"""Test old jobs cleanup."""
with patch('app.services.file_manager.settings.jobs_dir', str(temp_data_dir)):
# Create old directory
old_job = temp_data_dir / 'old-job'
old_job.mkdir()
(old_job / 'file.txt').write_text('test')
# Should be cleaned up with hours=0
cleaned = file_manager.cleanup_old_jobs(hours=0)
assert cleaned >= 1