|
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, |
| 8 | + BatchAttachment, |
5 | 9 | BatchesService, |
6 | 10 | ) |
7 | 11 |
|
8 | 12 |
|
9 | 13 | @pytest.fixture |
10 | | -def batches_service(http_client): |
| 14 | +def batches_service(http_client: HTTPClient) -> BatchesService: |
11 | 15 | return BatchesService(http_client=http_client) |
12 | 16 |
|
13 | 17 |
|
14 | 18 | @pytest.fixture |
15 | | -def async_batches_service(async_http_client): |
| 19 | +def async_batches_service(async_http_client: AsyncHTTPClient) -> AsyncBatchesService: |
16 | 20 | return AsyncBatchesService(http_client=async_http_client) |
17 | 21 |
|
18 | 22 |
|
19 | | -@pytest.mark.parametrize("method", ["get", "create", "iterate", "get_batch_attachment"]) |
20 | | -def test_sync_batches_service_methods(batches_service, method): |
| 23 | +@pytest.fixture |
| 24 | +def attachment_response() -> Response: |
| 25 | + return Response( |
| 26 | + headers={"content-disposition": 'attachment; filename="test.csv"'}, |
| 27 | + status_code=200, |
| 28 | + content=b"content", |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def batch_attachment_response() -> Response: |
| 34 | + return Response( |
| 35 | + headers={"Content-Type": "application/json"}, |
| 36 | + status_code=200, |
| 37 | + content=b'{"id": "A-123", "name": "test.csv"}', |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize( |
| 42 | + "method", ["get", "create", "iterate", "download_attachment", "get_attachment"] |
| 43 | +) |
| 44 | +def test_sync_batches_service_methods(batches_service: BatchesService, method: str) -> None: |
21 | 45 | result = hasattr(batches_service, method) |
22 | 46 |
|
23 | 47 | assert result is True |
24 | 48 |
|
25 | 49 |
|
26 | | -@pytest.mark.parametrize("method", ["get", "create", "iterate", "get_batch_attachment"]) |
27 | | -def test_async_batches_service_methods(async_batches_service, method): |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "method", ["get", "create", "iterate", "download_attachment", "get_attachment"] |
| 52 | +) |
| 53 | +def test_async_batches_service_methods( |
| 54 | + async_batches_service: AsyncBatchesService, method: str |
| 55 | +) -> None: |
28 | 56 | result = hasattr(async_batches_service, method) |
29 | 57 |
|
30 | 58 | assert result is True |
| 59 | + |
| 60 | + |
| 61 | +def test_get_attachment(mocker, batches_service, batch_attachment_response) -> None: |
| 62 | + batch_id = "B-123" |
| 63 | + attachment_id = "A-123" |
| 64 | + mock_request = mocker.patch.object( |
| 65 | + batches_service.http_client, "request", return_value=batch_attachment_response |
| 66 | + ) |
| 67 | + |
| 68 | + result = batches_service.get_attachment(batch_id, attachment_id) |
| 69 | + |
| 70 | + assert isinstance(result, BatchAttachment) |
| 71 | + assert result.id == "A-123" |
| 72 | + mock_request.assert_called_once_with( |
| 73 | + "get", |
| 74 | + f"/public/v1/notifications/batches/{batch_id}/attachments/{attachment_id}", |
| 75 | + headers={"Accept": "application/json"}, |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +async def test_async_get_attachment( |
| 80 | + mocker, async_batches_service, batch_attachment_response |
| 81 | +) -> None: |
| 82 | + batch_id = "B-123" |
| 83 | + attachment_id = "A-123" |
| 84 | + mock_request = mocker.patch.object( |
| 85 | + async_batches_service.http_client, "request", return_value=batch_attachment_response |
| 86 | + ) |
| 87 | + |
| 88 | + result = await async_batches_service.get_attachment(batch_id, attachment_id) |
| 89 | + |
| 90 | + assert isinstance(result, BatchAttachment) |
| 91 | + assert result.id == "A-123" |
| 92 | + mock_request.assert_called_once_with( |
| 93 | + "get", |
| 94 | + f"/public/v1/notifications/batches/{batch_id}/attachments/{attachment_id}", |
| 95 | + headers={"Accept": "application/json"}, |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def test_download_attachment(mocker, batches_service, attachment_response) -> None: |
| 100 | + batch_id = "B-123" |
| 101 | + attachment_id = "A-123" |
| 102 | + mock_request = mocker.patch.object( |
| 103 | + batches_service.http_client, "request", return_value=attachment_response |
| 104 | + ) |
| 105 | + |
| 106 | + result = batches_service.download_attachment(batch_id, attachment_id) |
| 107 | + |
| 108 | + assert isinstance(result, FileModel) |
| 109 | + mock_request.assert_called_once_with( |
| 110 | + "get", f"/public/v1/notifications/batches/{batch_id}/attachments/{attachment_id}" |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +async def test_async_download_attachment( # noqa: WPS210 |
| 115 | + mocker, async_batches_service, attachment_response |
| 116 | +) -> None: |
| 117 | + batch_id = "B-123" |
| 118 | + attachment_id = "A-123" |
| 119 | + mock_request = mocker.patch.object( |
| 120 | + async_batches_service.http_client, "request", return_value=attachment_response |
| 121 | + ) |
| 122 | + |
| 123 | + result = await async_batches_service.download_attachment(batch_id, attachment_id) |
| 124 | + |
| 125 | + assert isinstance(result, FileModel) |
| 126 | + |
| 127 | + mock_request.assert_called_once_with( |
| 128 | + "get", f"/public/v1/notifications/batches/{batch_id}/attachments/{attachment_id}" |
| 129 | + ) |
0 commit comments