|
3 | 3 |
|
4 | 4 | import pytest
|
5 | 5 | from django.contrib.auth.models import User
|
| 6 | +from rest_framework import status |
6 | 7 | from rest_framework.test import APIClient
|
7 | 8 |
|
8 | 9 | import jolpica.formula_one.models as f1
|
| 10 | +from jolpica_api.data_import.models import DataImportLog |
9 | 11 |
|
10 | 12 |
|
11 | 13 | @pytest.fixture(scope="function")
|
@@ -182,3 +184,126 @@ def test_data_import_2023_18_models_are_imported(client: APIClient):
|
182 | 184 | ).count()
|
183 | 185 | > 45
|
184 | 186 | )
|
| 187 | + |
| 188 | + |
| 189 | +@pytest.mark.django_db |
| 190 | +def test_successful_import(client): |
| 191 | + """Test successful data import.""" |
| 192 | + data = { |
| 193 | + "dry_run": False, |
| 194 | + "data": [ |
| 195 | + { |
| 196 | + "object_type": "Driver", |
| 197 | + "foreign_keys": {}, |
| 198 | + "objects": [{"reference": "max_verstappen", "forename": "Max"}], |
| 199 | + } |
| 200 | + ], |
| 201 | + } |
| 202 | + response = client.put("/data/import/", data, format="json") |
| 203 | + |
| 204 | + assert response.status_code == status.HTTP_200_OK |
| 205 | + assert DataImportLog.objects.count() == 1 |
| 206 | + log = DataImportLog.objects.first() |
| 207 | + assert log.completed_at is not None |
| 208 | + assert log.error_type is None |
| 209 | + assert log.updated_records == {"Driver": [831]} |
| 210 | + assert log.is_success |
| 211 | + assert log.error_type is None |
| 212 | + assert log.errors is None |
| 213 | + |
| 214 | + |
| 215 | +@pytest.mark.django_db |
| 216 | +def test_validation_error_has_logs(client): |
| 217 | + """Test import with deserialization errors.""" |
| 218 | + data = { |
| 219 | + "dry_run": False, |
| 220 | + "data": [ |
| 221 | + { |
| 222 | + "object_type": "Driver", |
| 223 | + "objects": [{"reference": "max_verstappen", "forename": "Max"}], |
| 224 | + } |
| 225 | + ], |
| 226 | + } |
| 227 | + response = client.put("/data/import/", data, format="json") |
| 228 | + |
| 229 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 230 | + assert DataImportLog.objects.count() == 1 |
| 231 | + log = DataImportLog.objects.first() |
| 232 | + assert not log.is_success |
| 233 | + assert log.error_type == "VALIDATION" |
| 234 | + assert log.errors[0]["type"] == "missing" |
| 235 | + |
| 236 | + |
| 237 | +@pytest.mark.django_db |
| 238 | +def test_deserialisation_error_has_log(client): |
| 239 | + """Test validation error during request data validation.""" |
| 240 | + data = { |
| 241 | + "dry_run": False, |
| 242 | + "data": [ |
| 243 | + { |
| 244 | + "object_type": "Round", |
| 245 | + "foreign_keys": {"year": 9999}, |
| 246 | + "objects": [{"number": 5}], |
| 247 | + } |
| 248 | + ], |
| 249 | + } |
| 250 | + response = client.put("/data/import/", data, format="json") |
| 251 | + |
| 252 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 253 | + assert DataImportLog.objects.count() == 1 |
| 254 | + log = DataImportLog.objects.first() |
| 255 | + assert not log.is_success |
| 256 | + assert log.error_type == "DESERIALISATION" |
| 257 | + assert response.json()["errors"][0] == { |
| 258 | + "index": 0, |
| 259 | + "message": ["DoesNotExist('Season matching query does not exist.')"], |
| 260 | + "type": "Round", |
| 261 | + } |
| 262 | + |
| 263 | + |
| 264 | +@pytest.mark.django_db |
| 265 | +def test_dry_run(client): |
| 266 | + """Test dry run.""" |
| 267 | + data = { |
| 268 | + "dry_run": True, |
| 269 | + "data": [ |
| 270 | + { |
| 271 | + "object_type": "Driver", |
| 272 | + "foreign_keys": {}, |
| 273 | + "objects": [{"reference": "max_verstappen", "forename": "Max"}], |
| 274 | + } |
| 275 | + ], |
| 276 | + } |
| 277 | + response = client.put("/data/import/", data, format="json") |
| 278 | + |
| 279 | + assert response.status_code == status.HTTP_200_OK |
| 280 | + assert DataImportLog.objects.count() == 0 # No log for dry run |
| 281 | + |
| 282 | + |
| 283 | +@pytest.mark.django_db |
| 284 | +def test_db_error(client): |
| 285 | + """Test database error during import.""" |
| 286 | + data = { |
| 287 | + "dry_run": False, |
| 288 | + "legacy_import": False, |
| 289 | + "data": [ |
| 290 | + { |
| 291 | + "object_type": "Lap", |
| 292 | + "foreign_keys": {"year": 2023, "round": 18, "session": "R", "car_number": 1}, |
| 293 | + "objects": [ |
| 294 | + {"number": 1, "position": 1, "average_speed": 200.0, "is_entry_fastest_lap": True}, |
| 295 | + { |
| 296 | + "number": 2, |
| 297 | + "position": 1, |
| 298 | + "average_speed": 201.0, |
| 299 | + }, |
| 300 | + ], |
| 301 | + }, |
| 302 | + ], |
| 303 | + } |
| 304 | + response = client.put("/data/import/", data, format="json") |
| 305 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 306 | + assert DataImportLog.objects.count() == 1 |
| 307 | + log = DataImportLog.objects.first() |
| 308 | + assert not log.is_success |
| 309 | + assert log.error_type == "IMPORT" |
0 commit comments