|
1 | 1 | import pytest |
2 | 2 |
|
| 3 | +from mpt_api_client.http import AsyncHTTPClient, HTTPClient |
| 4 | +from mpt_api_client.http.types import Response |
| 5 | +from mpt_api_client.models import FileModel |
3 | 6 | from mpt_api_client.resources.notifications.batches import ( |
4 | 7 | AsyncBatchesService, |
5 | 8 | BatchesService, |
6 | 9 | ) |
7 | 10 |
|
8 | 11 |
|
9 | 12 | @pytest.fixture |
10 | | -def batches_service(http_client): |
| 13 | +def batches_service(http_client: HTTPClient) -> BatchesService: |
11 | 14 | return BatchesService(http_client=http_client) |
12 | 15 |
|
13 | 16 |
|
14 | 17 | @pytest.fixture |
15 | | -def async_batches_service(async_http_client): |
| 18 | +def async_batches_service(async_http_client: AsyncHTTPClient) -> AsyncBatchesService: |
16 | 19 | return AsyncBatchesService(http_client=async_http_client) |
17 | 20 |
|
18 | 21 |
|
19 | | -@pytest.mark.parametrize("method", ["get", "create", "iterate", "get_batch_attachment"]) |
20 | | -def test_sync_batches_service_methods(batches_service, method): |
| 22 | +@pytest.fixture |
| 23 | +def attachment_response() -> Response: |
| 24 | + return Response( |
| 25 | + headers={"content-disposition": 'attachment; filename="test.csv"'}, |
| 26 | + status_code=200, |
| 27 | + content=b"content", |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("method", ["get", "create", "iterate", "download_attachment"]) |
| 32 | +def test_sync_batches_service_methods(batches_service: BatchesService, method: str) -> None: |
21 | 33 | result = hasattr(batches_service, method) |
22 | 34 |
|
23 | 35 | assert result is True |
24 | 36 |
|
25 | 37 |
|
26 | | -@pytest.mark.parametrize("method", ["get", "create", "iterate", "get_batch_attachment"]) |
27 | | -def test_async_batches_service_methods(async_batches_service, method): |
| 38 | +@pytest.mark.parametrize("method", ["get", "create", "iterate", "download_attachment"]) |
| 39 | +def test_async_batches_service_methods( |
| 40 | + async_batches_service: AsyncBatchesService, method: str |
| 41 | +) -> None: |
28 | 42 | result = hasattr(async_batches_service, method) |
29 | 43 |
|
30 | 44 | assert result is True |
| 45 | + |
| 46 | + |
| 47 | +def test_download_attachment(mocker, batches_service, attachment_response): |
| 48 | + batch_id = "B-123" |
| 49 | + attachment_id = "A-123" |
| 50 | + mock_request = mocker.patch.object( |
| 51 | + batches_service.http_client, "request", return_value=attachment_response |
| 52 | + ) |
| 53 | + |
| 54 | + result = batches_service.download_attachment(batch_id, attachment_id) |
| 55 | + |
| 56 | + assert isinstance(result, FileModel) |
| 57 | + mock_request.assert_called_once_with( |
| 58 | + "get", f"/public/v1/notifications/batches/{batch_id}/attachments/{attachment_id}" |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +async def test_async_download_attachment( # noqa: WPS210 |
| 63 | + mocker, async_batches_service, attachment_response |
| 64 | +): |
| 65 | + batch_id = "B-123" |
| 66 | + attachment_id = "A-123" |
| 67 | + mock_request = mocker.patch.object( |
| 68 | + async_batches_service.http_client, "request", return_value=attachment_response |
| 69 | + ) |
| 70 | + |
| 71 | + result = await async_batches_service.download_attachment(batch_id, attachment_id) |
| 72 | + |
| 73 | + assert isinstance(result, FileModel) |
| 74 | + |
| 75 | + mock_request.assert_called_once_with( |
| 76 | + "get", f"/public/v1/notifications/batches/{batch_id}/attachments/{attachment_id}" |
| 77 | + ) |
0 commit comments