|
| 1 | +import resend |
| 2 | +from tests.conftest import ResendBaseTest |
| 3 | + |
| 4 | +# flake8: noqa |
| 5 | + |
| 6 | + |
| 7 | +class TestResponseDict(ResendBaseTest): |
| 8 | + |
| 9 | + def test_list_response_supports_dict_access(self) -> None: |
| 10 | + self.set_mock_json( |
| 11 | + { |
| 12 | + "object": "list", |
| 13 | + "has_more": False, |
| 14 | + "data": [ |
| 15 | + { |
| 16 | + "id": "att-1", |
| 17 | + "filename": "avatar.png", |
| 18 | + "content_type": "image/png", |
| 19 | + "content_disposition": "inline", |
| 20 | + "size": 1024, |
| 21 | + }, |
| 22 | + ], |
| 23 | + } |
| 24 | + ) |
| 25 | + |
| 26 | + attachments = resend.Emails.Receiving.Attachments.list( |
| 27 | + email_id="test-email-id" |
| 28 | + ) |
| 29 | + assert attachments["object"] == "list" |
| 30 | + assert attachments["has_more"] is False |
| 31 | + assert len(attachments["data"]) == 1 |
| 32 | + assert attachments["data"][0]["id"] == "att-1" |
| 33 | + |
| 34 | + def test_list_response_supports_attribute_access(self) -> None: |
| 35 | + self.set_mock_json( |
| 36 | + { |
| 37 | + "object": "list", |
| 38 | + "has_more": False, |
| 39 | + "data": [ |
| 40 | + { |
| 41 | + "id": "att-1", |
| 42 | + "filename": "avatar.png", |
| 43 | + "content_type": "image/png", |
| 44 | + "content_disposition": "inline", |
| 45 | + "size": 1024, |
| 46 | + }, |
| 47 | + ], |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + attachments = resend.Emails.Receiving.Attachments.list( |
| 52 | + email_id="test-email-id" |
| 53 | + ) |
| 54 | + assert attachments.object == "list" # type: ignore[attr-defined] |
| 55 | + assert attachments.has_more is False # type: ignore[attr-defined] |
| 56 | + assert len(attachments.data) == 1 # type: ignore[attr-defined] |
| 57 | + assert attachments.data[0]["id"] == "att-1" # type: ignore[attr-defined] |
| 58 | + |
| 59 | + def test_attribute_access_raises_for_missing_key(self) -> None: |
| 60 | + self.set_mock_json( |
| 61 | + { |
| 62 | + "object": "list", |
| 63 | + "has_more": False, |
| 64 | + "data": [], |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + attachments = resend.Emails.Receiving.Attachments.list( |
| 69 | + email_id="test-email-id" |
| 70 | + ) |
| 71 | + with self.assertRaises(AttributeError): |
| 72 | + _ = attachments.nonexistent # type: ignore[attr-defined] |
| 73 | + |
| 74 | + def test_single_response_supports_attribute_access(self) -> None: |
| 75 | + self.set_mock_json( |
| 76 | + { |
| 77 | + "id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794", |
| 78 | + } |
| 79 | + ) |
| 80 | + |
| 81 | + params: resend.Emails.SendParams = { |
| 82 | + "from": "from@email.io", |
| 83 | + "to": ["to@email.io"], |
| 84 | + "subject": "subject", |
| 85 | + "html": "<p>Hello</p>", |
| 86 | + } |
| 87 | + email = resend.Emails.send(params) |
| 88 | + assert email["id"] == "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794" |
| 89 | + assert email.id == "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794" # type: ignore[attr-defined] |
0 commit comments