| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- # -*- coding: utf-8 -*-
- """
- Tests für das Download-Modul.
- """
- import pytest
- from pathlib import Path
- from unittest.mock import MagicMock, AsyncMock, patch
- from trixy_core.utils.download import (
- DownloadProgress,
- download_file,
- download_from_huggingface,
- download_from_github,
- download_and_extract,
- format_size,
- )
- class TestFormatSize:
- """Tests für die format_size Funktion."""
- def test_bytes(self):
- """Testet Byte-Formatierung."""
- assert format_size(500) == "500.0 B"
- def test_kilobytes(self):
- """Testet KB-Formatierung."""
- assert format_size(1024) == "1.0 KB"
- assert format_size(2048) == "2.0 KB"
- def test_megabytes(self):
- """Testet MB-Formatierung."""
- assert format_size(1024 * 1024) == "1.0 MB"
- assert format_size(5 * 1024 * 1024) == "5.0 MB"
- def test_gigabytes(self):
- """Testet GB-Formatierung."""
- assert format_size(1024 * 1024 * 1024) == "1.0 GB"
- def test_terabytes(self):
- """Testet TB-Formatierung."""
- assert format_size(1024 * 1024 * 1024 * 1024) == "1.0 TB"
- class TestDownloadProgress:
- """Tests für die DownloadProgress Klasse."""
- def test_initial_state(self):
- """Testet den initialen Zustand."""
- progress = DownloadProgress(total_size=1000)
- assert progress.downloaded == 0
- assert progress.total_size == 1000
- assert progress.percent == 0.0
- def test_update(self):
- """Testet das Update."""
- progress = DownloadProgress(total_size=1000)
- progress.update(500)
- assert progress.downloaded == 500
- assert progress.percent == 50.0
- def test_callback_called(self):
- """Testet, dass der Callback aufgerufen wird."""
- callback = MagicMock()
- progress = DownloadProgress(total_size=1000, callback=callback)
- progress.update(500)
- callback.assert_called_once_with(500, 1000, 50.0)
- def test_zero_total_size(self):
- """Testet mit total_size=0."""
- progress = DownloadProgress(total_size=0)
- progress.update(100)
- assert progress.percent == 0.0
- class TestDownloadFile:
- """Tests für die download_file Funktion."""
- @pytest.mark.asyncio
- async def test_creates_parent_directories(self, tmp_path):
- """Testet, dass Parent-Verzeichnisse erstellt werden."""
- dest_path = tmp_path / "subdir" / "file.txt"
- # Mock aiohttp
- with patch("trixy_core.utils.download.download_file") as mock_download:
- mock_download.return_value = True
- # Verzeichnis sollte erstellt werden
- dest_path.parent.mkdir(parents=True, exist_ok=True)
- assert dest_path.parent.exists()
- @pytest.mark.asyncio
- async def test_invalid_url_returns_false(self, tmp_path):
- """Testet, dass ungültige URLs False zurückgeben."""
- dest_path = tmp_path / "file.txt"
- result = await download_file(
- url="https://invalid.url.that.does.not.exist/file.txt",
- dest_path=dest_path,
- timeout=5,
- )
- assert result is False
- class TestDownloadFromGithub:
- """Tests für download_from_github."""
- def test_url_construction(self):
- """Testet, dass die GitHub URL korrekt konstruiert wird."""
- # Die Funktion konstruiert intern:
- # https://github.com/{repo}/releases/download/{tag}/{asset_name}
- expected_url = "https://github.com/owner/repo/releases/download/v1.0.0/file.zip"
- # Wir können die URL-Konstruktion nicht direkt testen ohne die Funktion aufzurufen,
- # aber wir können die Parameter-Validierung prüfen
- assert "github.com" in expected_url
- assert "releases/download" in expected_url
- class TestDownloadEvents:
- """Tests für Download-Events."""
- @pytest.fixture
- def mock_event_manager(self):
- """Erstellt einen Mock-EventManager."""
- em = MagicMock()
- em.emit = AsyncMock()
- return em
- @pytest.mark.asyncio
- async def test_download_started_event(self, tmp_path, mock_event_manager):
- """Testet, dass download_started Event emittiert wird."""
- dest_path = tmp_path / "file.txt"
- # Download wird fehlschlagen, aber das Event sollte trotzdem emittiert werden
- await download_file(
- url="https://example.com/nonexistent.txt",
- dest_path=dest_path,
- event_manager=mock_event_manager,
- download_id="test-download",
- timeout=5,
- )
- # Prüfen, dass Events emittiert wurden
- calls = mock_event_manager.emit.call_args_list
- event_names = [call[0][0] for call in calls]
- assert "download_started" in event_names
- # Da der Download fehlschlägt, sollte auch download_failed emittiert werden
- assert "download_failed" in event_names
- class TestDownloadAndExtract:
- """Tests für download_and_extract."""
- @pytest.mark.asyncio
- async def test_unsupported_format_fails(self, tmp_path):
- """Testet, dass nicht unterstützte Formate fehlschlagen."""
- # Diese URL hat ein unbekanntes Format (.rar)
- result = await download_and_extract(
- url="https://example.com/file.rar",
- dest_dir=tmp_path,
- )
- # Entweder der Download schlägt fehl oder das Entpacken
- # In beiden Fällen sollte False zurückkommen
- assert result is False
|