-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from tjmlabs/main-better-errors
Main better errors
- Loading branch information
Showing
2 changed files
with
151 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,7 +198,12 @@ async def test_list_collection(async_client, user, collection): | |
) | ||
assert response.status_code == 200 | ||
assert response.json() == [ | ||
{"id": 1, "name": "Test Collection Fixture", "metadata": {"key": "value"}, "num_documents": 0} | ||
{ | ||
"id": 1, | ||
"name": "Test Collection Fixture", | ||
"metadata": {"key": "value"}, | ||
"num_documents": 0, | ||
} | ||
] | ||
|
||
|
||
|
@@ -1534,18 +1539,17 @@ async def test_document_fetch_failure_async(async_client, user): | |
await asyncio.gather(*pending_tasks) | ||
|
||
# Assert that both HEAD and GET were called | ||
mock_head.assert_called_once_with( | ||
"https://example.com/nonexistent.pdf", allow_redirects=True | ||
) | ||
mock_get.assert_called_once_with("https://example.com/nonexistent.pdf") | ||
mock_head.assert_called_once() | ||
mock_get.assert_called_once() | ||
|
||
# Assert that the email was sent | ||
MockEmailMessage.assert_called_once_with( | ||
subject="Document Upsertion Failed", | ||
body="There was an error processing your document: ['Failed to fetch document from URL']", | ||
body="There was an error processing your document: ['Failed to fetch document info from URL. Some documents are protected by anti-scrapping measures. We recommend you download them and send us base64.']", | ||
to=[user.email, "[email protected]"], | ||
from_email="[email protected]", | ||
) | ||
|
||
mock_email_instance.send.assert_called_once() | ||
|
||
|
||
|
@@ -1598,7 +1602,7 @@ async def test_document_file_too_big(async_client, user): | |
assert response.status_code == 400 | ||
|
||
|
||
async def test_gotenberg_service_down(async_client, user): | ||
async def test_gotenberg_service_down_with_file(async_client, user): | ||
GOTENBERG_POST_PATH = "api.models.aiohttp.ClientSession.post" | ||
# Create a mock response object with status 500 | ||
mock_response = AsyncMock() | ||
|
@@ -1639,6 +1643,29 @@ async def test_gotenberg_service_down(async_client, user): | |
assert response.status_code == 400 | ||
|
||
|
||
async def test_gotenberg_service_down_with_url(async_client, user): | ||
GOTENBERG_POST_PATH = "api.models.aiohttp.ClientSession.post" | ||
# Create a mock response object with status 500 | ||
mock_response = AsyncMock() | ||
mock_response.status = 500 | ||
mock_response.json.return_value = AsyncMock(return_value={"error": "Service Down"}) | ||
# Mock the context manager __aenter__ to return the mock_response | ||
mock_response.__aenter__.return_value = mock_response | ||
# Patch the aiohttp.ClientSession.post method to return the mock_response | ||
|
||
with patch(GOTENBERG_POST_PATH, return_value=mock_response): | ||
response = await async_client.post( | ||
"/documents/upsert-document/", | ||
json={ | ||
"name": "Test Document Fixture", | ||
"url": "https://example.com/largefile.pdf", | ||
"wait": True, | ||
}, | ||
headers={"Authorization": f"Bearer {user.token}"}, | ||
) | ||
assert response.status_code == 400 | ||
|
||
|
||
async def test_prep_document_document_data_too_large(): | ||
# Initialize Document without a URL or base64 (assuming document_data is handled internally) | ||
doc = Document() | ||
|
@@ -1650,6 +1677,17 @@ async def test_prep_document_document_data_too_large(): | |
await doc._prep_document(document_data=document_data) | ||
|
||
|
||
async def test_prep_document_pdf_conversion_failure(): | ||
CONVERT_FROM_BYTES_PATH = "api.models.convert_from_bytes" | ||
|
||
document = Document() # | ||
pdf_data = b"corrupted_pdf_data" | ||
|
||
with patch(CONVERT_FROM_BYTES_PATH, side_effect=Exception("PDF conversion failed")): | ||
with pytest.raises(DjangoValidationError): | ||
await document._prep_document(document_data=pdf_data) | ||
|
||
|
||
async def test_prep_document_with_disallowed_extension(collection): | ||
content = "bad base64 string" | ||
content_bytes = content.encode("utf-8") | ||
|
@@ -1714,3 +1752,39 @@ async def test_unknown_mime_type(collection): | |
|
||
# Cleanup | ||
await document.delete_s3_file() | ||
|
||
|
||
async def test_get_url_info_non_200_response(): | ||
AIOHTTP_HEAD_PATH = "api.models.aiohttp.ClientSession.head" | ||
|
||
# Mock response with non-200 status | ||
mock_response = AsyncMock() | ||
mock_response.status = 404 | ||
mock_response.__aenter__.return_value = mock_response | ||
|
||
document = Document(url="https://example.com/doc.pdf") | ||
|
||
with patch(AIOHTTP_HEAD_PATH, return_value=mock_response): | ||
with pytest.raises(DjangoValidationError): | ||
await document._get_url_info() | ||
|
||
|
||
async def test_get_url_info_empty_filename_fallback(): | ||
AIOHTTP_HEAD_PATH = "api.models.aiohttp.ClientSession.head" | ||
|
||
# Mock response with empty filename | ||
mock_response = AsyncMock() | ||
mock_response.status = 200 | ||
mock_response.headers = { | ||
"Content-Type": "application/pdf", | ||
"Content-Disposition": "", # Empty content disposition | ||
"Content-Length": "1000", | ||
} | ||
mock_response.__aenter__.return_value = mock_response | ||
|
||
document = Document(url="https://example.com/") # URL with no filename | ||
|
||
with patch(AIOHTTP_HEAD_PATH, return_value=mock_response): | ||
content_type, filename = await document._get_url_info() | ||
|
||
assert filename == "downloaded_file" |