|
1 | 1 | """Tests for download history endpoints.""" |
2 | 2 |
|
3 | 3 | from datetime import datetime, timedelta, timezone |
| 4 | +from types import SimpleNamespace |
| 5 | +from unittest.mock import AsyncMock |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 | from httpx import AsyncClient |
7 | 9 | from sqlalchemy.ext.asyncio import AsyncSession |
8 | 10 |
|
| 11 | +from app.api.v1.endpoints import history as history_endpoint |
9 | 12 | from app.db.models import DownloadHistory |
10 | 13 |
|
11 | 14 |
|
@@ -67,3 +70,96 @@ async def test_history_statistics_use_all_matching_records_not_current_page( |
67 | 70 | "count": 2, |
68 | 71 | "percentage": 66.67, |
69 | 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