zenith-backend / tests /unit /test_backup_mocks.py
teoat's picture
Upload folder using huggingface_hub
4ae946d verified
from unittest.mock import MagicMock, patch
import pytest
# Now we import the REAL service to ensure the file exists and works
from app.services.infrastructure.storage.backup_service import BackupService
def test_create_backup_mock_fs():
"""Test backup creation with file system mocked"""
# Mock shutil and os to prevent actual file creation
with patch(
"shutil.make_archive", return_value="/tmp/backup.zip"
) as mock_shutil, patch("os.makedirs"), patch(
"os.path.exists", return_value=True
), patch(
"builtins.open", MagicMock()
), patch(
"shutil.rmtree"
):
service = BackupService()
# Override backup_dir for safety
service.backup_dir = "/tmp/backups"
result = service.create_backup(include_db=False)
assert result == "/tmp/backup.zip"
# Verify make_archive was called
assert mock_shutil.called
@pytest.mark.asyncio
async def test_upload_to_s3_mock_boto():
"""Test S3 upload with mocked boto3 client"""
# Simpler: Mock the `boto3` module in sys.modules so the import works
mock_boto = MagicMock()
with patch.dict("sys.modules", {"boto3": mock_boto}), patch(
"os.path.exists", return_value=True
), patch("os.path.basename", return_value="backup.zip"), patch(
"core.config.settings.S3_BUCKET_NAME", "test-bucket"
):
service = BackupService()
success = await service.upload_to_s3("backup.zip", "my-bucket")
assert success is True
mock_boto.client.return_value.upload_file.assert_called_once()