|
| 1 | +"""Tests for download history endpoints.""" |
| 2 | + |
| 3 | +from datetime import datetime, timedelta, timezone |
| 4 | +from types import SimpleNamespace |
| 5 | +from unittest.mock import AsyncMock |
| 6 | + |
| 7 | +import pytest |
| 8 | +from httpx import AsyncClient |
| 9 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 10 | + |
| 11 | +from app.api.v1.endpoints import history as history_endpoint |
| 12 | +from app.db.models import DownloadHistory |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.asyncio |
| 16 | +async def test_history_statistics_use_all_matching_records_not_current_page( |
| 17 | + client: AsyncClient, db_session: AsyncSession |
| 18 | +): |
| 19 | + now = datetime.now(timezone.utc) |
| 20 | + records = [ |
| 21 | + DownloadHistory( |
| 22 | + id="history-1", |
| 23 | + download_id="download-1", |
| 24 | + url="https://example.test/one", |
| 25 | + status="completed", |
| 26 | + duration=10, |
| 27 | + file_size=100, |
| 28 | + extractor="youtube", |
| 29 | + started_at=now - timedelta(minutes=3), |
| 30 | + completed_at=now - timedelta(minutes=2), |
| 31 | + ), |
| 32 | + DownloadHistory( |
| 33 | + id="history-2", |
| 34 | + download_id="download-2", |
| 35 | + url="https://example.test/two", |
| 36 | + status="failed", |
| 37 | + duration=20, |
| 38 | + file_size=0, |
| 39 | + extractor="youtube", |
| 40 | + started_at=now - timedelta(minutes=2), |
| 41 | + completed_at=now - timedelta(minutes=1), |
| 42 | + ), |
| 43 | + DownloadHistory( |
| 44 | + id="history-3", |
| 45 | + download_id="download-3", |
| 46 | + url="https://example.test/three", |
| 47 | + status="completed", |
| 48 | + duration=30, |
| 49 | + file_size=300, |
| 50 | + extractor="vimeo", |
| 51 | + started_at=now - timedelta(minutes=1), |
| 52 | + completed_at=now, |
| 53 | + ), |
| 54 | + ] |
| 55 | + db_session.add_all(records) |
| 56 | + await db_session.commit() |
| 57 | + |
| 58 | + response = await client.get("/api/v1/history/", params={"limit": 1}) |
| 59 | + |
| 60 | + assert response.status_code == 200 |
| 61 | + data = response.json() |
| 62 | + assert len(data["items"]) == 1 |
| 63 | + assert data["totalDownloads"] == 3 |
| 64 | + assert data["totalItems"] == 3 |
| 65 | + assert data["successRate"] == 0.667 |
| 66 | + assert data["averageDownloadTime"] == 20 |
| 67 | + assert data["totalSize"] == 400 |
| 68 | + assert data["popularExtractors"][0] == { |
| 69 | + "extractor": "youtube", |
| 70 | + "count": 2, |
| 71 | + "percentage": 66.67, |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.asyncio |
| 76 | +async def test_history_statistics_are_zero_without_matching_records( |
| 77 | + client: AsyncClient, db_session: AsyncSession |
| 78 | +): |
| 79 | + db_session.add( |
| 80 | + DownloadHistory( |
| 81 | + id="history-other-extractor", |
| 82 | + download_id="download-other-extractor", |
| 83 | + url="https://example.test/other", |
| 84 | + status="completed", |
| 85 | + duration=12, |
| 86 | + file_size=120, |
| 87 | + extractor="youtube", |
| 88 | + started_at=datetime.now(timezone.utc) - timedelta(minutes=1), |
| 89 | + completed_at=datetime.now(timezone.utc), |
| 90 | + ) |
| 91 | + ) |
| 92 | + await db_session.commit() |
| 93 | + |
| 94 | + response = await client.get( |
| 95 | + "/api/v1/history/", params={"extractor": "vimeo", "limit": 1} |
| 96 | + ) |
| 97 | + |
| 98 | + assert response.status_code == 200 |
| 99 | + data = response.json() |
| 100 | + assert data["items"] == [] |
| 101 | + assert data["totalDownloads"] == 0 |
| 102 | + assert data["totalItems"] == 0 |
| 103 | + assert data["successRate"] == 0 |
| 104 | + assert data["averageDownloadTime"] == 0 |
| 105 | + assert data["totalSize"] == 0 |
| 106 | + assert data["popularExtractors"] == [] |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.asyncio |
| 110 | +async def test_history_endpoint_maps_aggregate_query_results(monkeypatch): |
| 111 | + now = datetime.now(timezone.utc) |
| 112 | + history_record = SimpleNamespace( |
| 113 | + download_id="download-aggregate", |
| 114 | + url="https://example.test/aggregate", |
| 115 | + status="completed", |
| 116 | + started_at=now - timedelta(minutes=1), |
| 117 | + completed_at=now, |
| 118 | + duration=None, |
| 119 | + file_size=25, |
| 120 | + extractor=None, |
| 121 | + error_message=None, |
| 122 | + ) |
| 123 | + |
| 124 | + count_result = SimpleNamespace(scalar=lambda: 1) |
| 125 | + history_result = SimpleNamespace( |
| 126 | + scalars=lambda: SimpleNamespace(all=lambda: [history_record]) |
| 127 | + ) |
| 128 | + stats_result = SimpleNamespace( |
| 129 | + one=lambda: SimpleNamespace( |
| 130 | + total=1, |
| 131 | + successful=1, |
| 132 | + avg_duration=None, |
| 133 | + total_size=25, |
| 134 | + ) |
| 135 | + ) |
| 136 | + extractor_result = SimpleNamespace(all=lambda: [(None, 1)]) |
| 137 | + db_session = SimpleNamespace( |
| 138 | + execute=AsyncMock( |
| 139 | + side_effect=[count_result, history_result, stats_result, extractor_result] |
| 140 | + ) |
| 141 | + ) |
| 142 | + monkeypatch.setattr( |
| 143 | + history_endpoint, "calculate_daily_stats", AsyncMock(return_value=[]) |
| 144 | + ) |
| 145 | + |
| 146 | + history = await history_endpoint.get_download_history( |
| 147 | + start_date=None, |
| 148 | + end_date=None, |
| 149 | + extractor="youtube", |
| 150 | + status="completed", |
| 151 | + limit=1, |
| 152 | + offset=0, |
| 153 | + db_session=db_session, |
| 154 | + principal=SimpleNamespace(), |
| 155 | + ) |
| 156 | + |
| 157 | + assert history.total_downloads == 1 |
| 158 | + assert history.total_items == 1 |
| 159 | + assert history.success_rate == 1 |
| 160 | + assert history.average_download_time == 0 |
| 161 | + assert history.total_size == 25 |
| 162 | + assert history.items[0].duration == 0 |
| 163 | + assert history.items[0].extractor == "unknown" |
| 164 | + assert history.popular_extractors[0].extractor == "unknown" |
| 165 | + assert history.popular_extractors[0].percentage == 100 |
0 commit comments