|
| 1 | +from typing import List |
| 2 | + |
| 3 | +import resend |
| 4 | +from resend.exceptions import NoContentError |
| 5 | +from tests.conftest import ResendBaseTest |
| 6 | + |
| 7 | +# flake8: noqa |
| 8 | + |
| 9 | + |
| 10 | +class TestResendBatchSendAsync(ResendBaseTest): |
| 11 | + async def test_batch_email_send_async(self) -> None: |
| 12 | + self.set_mock_json( |
| 13 | + { |
| 14 | + "data": [ |
| 15 | + {"id": "ae2014de-c168-4c61-8267-70d2662a1ce1"}, |
| 16 | + {"id": "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb"}, |
| 17 | + ] |
| 18 | + } |
| 19 | + ) |
| 20 | + |
| 21 | + params: List[resend.Emails.SendParams] = [ |
| 22 | + { |
| 23 | + "from": "from@resend.dev", |
| 24 | + "to": ["to@resend.dev"], |
| 25 | + "subject": "hey", |
| 26 | + "html": "<strong>hello, world!</strong>", |
| 27 | + }, |
| 28 | + { |
| 29 | + "from": "from@resend.dev", |
| 30 | + "to": ["to@resend.dev"], |
| 31 | + "subject": "hello", |
| 32 | + "html": "<strong>hello, world!</strong>", |
| 33 | + }, |
| 34 | + ] |
| 35 | + |
| 36 | + emails: resend.Batch.SendResponse = await resend.Batch.send_async(params) |
| 37 | + assert len(emails["data"]) == 2 |
| 38 | + assert emails["data"][0]["id"] == "ae2014de-c168-4c61-8267-70d2662a1ce1" |
| 39 | + assert emails["data"][1]["id"] == "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb" |
| 40 | + |
| 41 | + async def test_batch_email_send_async_with_options(self) -> None: |
| 42 | + self.set_mock_json( |
| 43 | + { |
| 44 | + "data": [ |
| 45 | + {"id": "ae2014de-c168-4c61-8267-70d2662a1ce1"}, |
| 46 | + {"id": "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb"}, |
| 47 | + ] |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + params: List[resend.Emails.SendParams] = [ |
| 52 | + { |
| 53 | + "from": "from@resend.dev", |
| 54 | + "to": ["to@resend.dev"], |
| 55 | + "subject": "hey", |
| 56 | + "html": "<strong>hello, world!</strong>", |
| 57 | + }, |
| 58 | + { |
| 59 | + "from": "from@resend.dev", |
| 60 | + "to": ["to@resend.dev"], |
| 61 | + "subject": "hello", |
| 62 | + "html": "<strong>hello, world!</strong>", |
| 63 | + }, |
| 64 | + ] |
| 65 | + |
| 66 | + options: resend.Batch.SendOptions = { |
| 67 | + "idempotency_key": "af477dc78aa9fa91fff3b8c0d4a2e1a5", |
| 68 | + } |
| 69 | + |
| 70 | + emails: resend.Batch.SendResponse = await resend.Batch.send_async( |
| 71 | + params, options=options |
| 72 | + ) |
| 73 | + assert len(emails["data"]) == 2 |
| 74 | + assert emails["data"][0]["id"] == "ae2014de-c168-4c61-8267-70d2662a1ce1" |
| 75 | + assert emails["data"][1]["id"] == "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb" |
| 76 | + |
| 77 | + async def test_should_send_batch_email_async_raise_exception_when_no_content( |
| 78 | + self, |
| 79 | + ) -> None: |
| 80 | + self.set_mock_json(None) |
| 81 | + params: List[resend.Emails.SendParams] = [ |
| 82 | + { |
| 83 | + "from": "from@resend.dev", |
| 84 | + "to": ["to@resend.dev"], |
| 85 | + "subject": "hey", |
| 86 | + "html": "<strong>hello, world!</strong>", |
| 87 | + }, |
| 88 | + { |
| 89 | + "from": "from@resend.dev", |
| 90 | + "to": ["to@resend.dev"], |
| 91 | + "subject": "hello", |
| 92 | + "html": "<strong>hello, world!</strong>", |
| 93 | + }, |
| 94 | + ] |
| 95 | + with self.assertRaises(NoContentError): |
| 96 | + _ = await resend.Batch.send_async(params) |
0 commit comments