|
| 1 | +import io |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | +import respx |
| 6 | + |
| 7 | +from mpt_api_client.resources.notifications.batches import ( |
| 8 | + AsyncBatchesService, |
| 9 | + BatchesService, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def batches_service(http_client): |
| 15 | + return BatchesService(http_client=http_client) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def async_batches_service(async_http_client): |
| 20 | + return AsyncBatchesService(http_client=async_http_client) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize("method", ["get", "create", "iterate", "get_batch_attachment"]) |
| 24 | +def test_sync_batches_service_methods(batches_service, method): |
| 25 | + assert hasattr(batches_service, method) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize("method", ["get", "create", "iterate", "get_batch_attachment"]) |
| 29 | +def test_async_batches_service_methods(async_batches_service, method): |
| 30 | + assert hasattr(async_batches_service, method) |
| 31 | + |
| 32 | + |
| 33 | +def test_sync_get_batch_attachment(batches_service): |
| 34 | + attachment_content = b"Attachment file content or binary data" |
| 35 | + with respx.mock: |
| 36 | + mock_route = respx.get( |
| 37 | + "https://api.example.com/public/v1/notifications/batches/BAT-123/attachments/ATT-456" |
| 38 | + ).mock( |
| 39 | + return_value=httpx.Response( |
| 40 | + status_code=httpx.codes.OK, |
| 41 | + headers={ |
| 42 | + "content-type": "application/octet-stream", |
| 43 | + "content-disposition": ( |
| 44 | + 'form-data; name="file"; filename="batch_attachment.pdf"' |
| 45 | + ), |
| 46 | + }, |
| 47 | + content=attachment_content, |
| 48 | + ) |
| 49 | + ) |
| 50 | + downloaded_file = batches_service.get_batch_attachment("BAT-123", "ATT-456") |
| 51 | + |
| 52 | + assert mock_route.call_count == 1 |
| 53 | + assert downloaded_file.file_contents == attachment_content |
| 54 | + assert downloaded_file.content_type == "application/octet-stream" |
| 55 | + assert downloaded_file.filename == "batch_attachment.pdf" |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.asyncio |
| 59 | +async def test_async_get_batch_attachment(async_batches_service): |
| 60 | + attachment_content = b"Attachment file content or binary data" |
| 61 | + with respx.mock: |
| 62 | + mock_route = respx.get( |
| 63 | + "https://api.example.com/public/v1/notifications/batches/BAT-123/attachments/ATT-456" |
| 64 | + ).mock( |
| 65 | + return_value=httpx.Response( |
| 66 | + status_code=httpx.codes.OK, |
| 67 | + headers={ |
| 68 | + "content-type": "application/octet-stream", |
| 69 | + "content-disposition": ( |
| 70 | + 'form-data; name="file"; filename="batch_attachment.pdf"' |
| 71 | + ), |
| 72 | + }, |
| 73 | + content=attachment_content, |
| 74 | + ) |
| 75 | + ) |
| 76 | + downloaded_file = await async_batches_service.get_batch_attachment("BAT-123", "ATT-456") |
| 77 | + |
| 78 | + assert mock_route.call_count == 1 |
| 79 | + assert downloaded_file.file_contents == attachment_content |
| 80 | + assert downloaded_file.content_type == "application/octet-stream" |
| 81 | + assert downloaded_file.filename == "batch_attachment.pdf" |
| 82 | + |
| 83 | + |
| 84 | +def test_sync_batches_create_with_data(batches_service): |
| 85 | + batch_data = {"name": "Test Batch"} |
| 86 | + with respx.mock: |
| 87 | + mock_route = respx.post("https://api.example.com/public/v1/notifications/batches").mock( |
| 88 | + return_value=httpx.Response( |
| 89 | + status_code=httpx.codes.OK, |
| 90 | + json={"id": "BAT-133", "name": "Test Batch"}, |
| 91 | + ) |
| 92 | + ) |
| 93 | + files = {"attachment": ("test.pdf", io.BytesIO(b"PDF content"), "application/pdf")} |
| 94 | + new_batch = batches_service.create(batch_data, files=files) |
| 95 | + request = mock_route.calls[0].request |
| 96 | + assert b'Content-Disposition: form-data; name="_attachment_data"' in request.content |
| 97 | + assert mock_route.call_count == 1 |
| 98 | + assert new_batch.id == "BAT-133" |
| 99 | + assert new_batch.name == "Test Batch" |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.asyncio |
| 103 | +async def test_async_batches_create_with_data(async_batches_service): |
| 104 | + batch_data = {"name": "Test Batch"} |
| 105 | + with respx.mock: |
| 106 | + mock_route = respx.post("https://api.example.com/public/v1/notifications/batches").mock( |
| 107 | + return_value=httpx.Response( |
| 108 | + status_code=httpx.codes.OK, |
| 109 | + json={"id": "BAT-133", "name": "Test Batch"}, |
| 110 | + ) |
| 111 | + ) |
| 112 | + files = {"attachment": ("test.pdf", io.BytesIO(b"PDF content"), "application/pdf")} |
| 113 | + new_batch = await async_batches_service.create(batch_data, files=files) |
| 114 | + request = mock_route.calls[0].request |
| 115 | + assert b'Content-Disposition: form-data; name="_attachment_data"' in request.content |
| 116 | + assert mock_route.call_count == 1 |
| 117 | + assert new_batch.id == "BAT-133" |
| 118 | + assert new_batch.name == "Test Batch" |
0 commit comments