|
1 | 1 | from pathlib import Path |
| 2 | +from unittest.mock import MagicMock, patch |
2 | 3 |
|
| 4 | +import requests |
3 | 5 | from django.contrib.auth import get_user_model |
4 | 6 | from django.test import TestCase |
5 | 7 |
|
6 | 8 | from app.models import ( |
7 | 9 | Book, |
| 10 | + Sources, |
8 | 11 | Status, |
9 | 12 | ) |
| 13 | +from app.providers import services |
10 | 14 | from integrations.imports import ( |
11 | 15 | goodreads, |
12 | 16 | ) |
@@ -45,3 +49,32 @@ def test_stored_progress(self): |
45 | 49 | read_book = Book.objects.get(status=Status.IN_PROGRESS.value) |
46 | 50 | self.assertEqual(read_book.status, Status.IN_PROGRESS.value) |
47 | 51 | self.assertEqual(read_book.progress, 0) |
| 52 | + |
| 53 | + |
| 54 | +class ImportGoodreadsProviderErrors(TestCase): |
| 55 | + """Test GoodReads provider error handling.""" |
| 56 | + |
| 57 | + def setUp(self): |
| 58 | + """Create user for the tests.""" |
| 59 | + self.credentials = {"username": "test", "password": "12345"} |
| 60 | + self.user = get_user_model().objects.create_user(**self.credentials) |
| 61 | + |
| 62 | + @patch("integrations.imports.goodreads.GoodReadsImporter._process_row") |
| 63 | + def test_provider_error_warns_with_goodreads_fields(self, mock_process_row): |
| 64 | + """Test provider failures warn and continue without requiring media_id.""" |
| 65 | + response = MagicMock(status_code=408, text='{"error":"Request timeout"}') |
| 66 | + error = requests.exceptions.HTTPError(response=response) |
| 67 | + mock_process_row.side_effect = services.ProviderAPIError( |
| 68 | + Sources.HARDCOVER.value, |
| 69 | + error, |
| 70 | + ) |
| 71 | + |
| 72 | + with Path(mock_path / "import_goodreads.csv").open("rb") as file: |
| 73 | + imported_counts, warnings = goodreads.importer(file, self.user, "new") |
| 74 | + |
| 75 | + self.assertEqual(imported_counts, {}) |
| 76 | + self.assertIn( |
| 77 | + "Ghosts of the Tristan Basin (Powder Mage, #0.8) (Goodreads ID 28825810)", |
| 78 | + warnings, |
| 79 | + ) |
| 80 | + self.assertIn("There was an error contacting the Hardcover API", warnings) |
0 commit comments