diff --git a/tests/api_keys_test.py b/tests/api_keys_test.py index 30d1145..92a35bb 100644 --- a/tests/api_keys_test.py +++ b/tests/api_keys_test.py @@ -1,49 +1,27 @@ -import unittest -from typing import Any, Dict, List -from unittest.mock import MagicMock, patch - import resend +from tests.conftest import ResendBaseTest # flake8: noqa -class TestResendApiKeys(unittest.TestCase): +class TestResendApiKeys(ResendBaseTest): def test_api_keys_create(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "id": "dacf4072-4119-4d88-932f-6202748ac7c8", "token": "re_c1tpEyD8_NKFusih9vKVQknRAQfmFcWCv", } - - m.json = mock_json - mock.return_value = m + ) params: resend.ApiKeys.CreateParams = { "name": "prod", } key: resend.ApiKey = resend.ApiKeys.create(params) assert key["id"] == "dacf4072-4119-4d88-932f-6202748ac7c8" - patcher.stop() def test_api_keys_list(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "data": [ { "id": "91f3200a-df72-4654-b0cd-f202395f5354", @@ -52,28 +30,16 @@ def mock_json() -> Dict[Any, Any]: } ] } - - m.json = mock_json - mock.return_value = m + ) keys: resend.ApiKeys.ListResponse = resend.ApiKeys.list() for key in keys["data"]: assert key["id"] == "91f3200a-df72-4654-b0cd-f202395f5354" assert key["name"] == "Production" assert key["created_at"] == "2023-04-08T00:11:13.110779+00:00" - patcher.stop() def test_api_keys_remove(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - m.text = "" - mock.return_value = m + self.set_mock_text("") assert ( resend.ApiKeys.remove( @@ -81,4 +47,3 @@ def test_api_keys_remove(self) -> None: ) is None ) - patcher.stop() diff --git a/tests/audiences_test.py b/tests/audiences_test.py index e10dba6..da3c52a 100644 --- a/tests/audiences_test.py +++ b/tests/audiences_test.py @@ -1,31 +1,18 @@ -import unittest -from typing import Any, Dict, List -from unittest.mock import MagicMock, patch - import resend +from tests.conftest import ResendBaseTest # flake8: noqa -class TestResendAudiences(unittest.TestCase): +class TestResendAudiences(ResendBaseTest): def test_audiences_create(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "audience", "id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", "name": "Registered Users", } - - m.json = mock_json - mock.return_value = m + ) params: resend.Audiences.CreateParams = { "name": "Python SDK Audience", @@ -34,70 +21,37 @@ def mock_json() -> Dict[Any, Any]: assert audience["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf" assert audience["name"] == "Registered Users" - patcher.stop() - def test_audiences_get(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "audience", "id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", "name": "Registered Users", "created_at": "2023-10-06T22:59:55.977Z", } - - m.json = mock_json - mock.return_value = m + ) audience = resend.Audiences.get(id="78261eea-8f8b-4381-83c6-79fa7120f1cf") assert audience["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf" assert audience["name"] == "Registered Users" assert audience["created_at"] == "2023-10-06T22:59:55.977Z" - patcher.stop() def test_audiences_remove(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "audience", "id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", "deleted": True, } - - m.json = mock_json - mock.return_value = m + ) rmed = resend.Audiences.remove("78261eea-8f8b-4381-83c6-79fa7120f1cf") assert rmed["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf" assert rmed["deleted"] is True - patcher.stop() - def test_audiences_list(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "list", "data": [ { @@ -107,11 +61,8 @@ def mock_json() -> Dict[Any, Any]: } ], } - - m.json = mock_json - mock.return_value = m + ) audiences: resend.Audiences.ListResponse = resend.Audiences.list() assert audiences["data"][0]["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf" assert audiences["data"][0]["name"] == "Registered Users" - patcher.stop() diff --git a/tests/batch_emails_test.py b/tests/batch_emails_test.py index 7f9a784..3ed4ca7 100644 --- a/tests/batch_emails_test.py +++ b/tests/batch_emails_test.py @@ -1,32 +1,21 @@ -import unittest -from typing import Any, Dict, List -from unittest.mock import MagicMock, patch +from typing import List import resend +from tests.conftest import ResendBaseTest # flake8: noqa -class TestResendBatchSend(unittest.TestCase): +class TestResendBatchSend(ResendBaseTest): def test_batch_email_send(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "data": [ {"id": "ae2014de-c168-4c61-8267-70d2662a1ce1"}, {"id": "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb"}, ] } - - m.json = mock_json - mock.return_value = m + ) params: List[resend.Emails.SendParams] = [ { @@ -42,8 +31,8 @@ def mock_json() -> Dict[Any, Any]: "html": "hello, world!", }, ] + emails: resend.Batch.SendResponse = resend.Batch.send(params) assert len(emails["data"]) == 2 assert emails["data"][0]["id"] == "ae2014de-c168-4c61-8267-70d2662a1ce1" assert emails["data"][1]["id"] == "faccb7a5-8a28-4e9a-ac64-8da1cc3bc1cb" - patcher.stop() diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..0918cbd --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,30 @@ +from typing import Any, Dict +from unittest import TestCase +from unittest.mock import MagicMock, patch + +import resend + +# flake8: noqa + + +class ResendBaseTest(TestCase): + def setUp(self) -> None: + resend.api_key = "re_123" + + self.patcher = patch("resend.Request.make_request") + self.mock = self.patcher.start() + self.mock.status_code = 200 + self.m = MagicMock() + self.m.status_code = 200 + self.mock.return_value = self.m + + def tearDown(self) -> None: + self.patcher.stop() + + def set_mock_json(self, mock_json: Dict[Any, Any]) -> None: + """Auxiliary function to set the mock json return value""" + self.m.json = lambda: mock_json + + def set_mock_text(self, mock_text: str) -> None: + """Auxiliary function to set the mock text return value""" + self.m.text = mock_text diff --git a/tests/contacts_test.py b/tests/contacts_test.py index 394d875..f1e73bc 100644 --- a/tests/contacts_test.py +++ b/tests/contacts_test.py @@ -1,27 +1,14 @@ -import unittest -from typing import Any, Dict, List -from unittest.mock import MagicMock, patch - import resend +from tests.conftest import ResendBaseTest # flake8: noqa -class TestResendContacts(unittest.TestCase): +class TestResendContacts(ResendBaseTest): def test_contacts_create(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return {"object": "contact", "id": "479e3145-dd38-476b-932c-529ceb705947"} - - m.json = mock_json - mock.return_value = m + self.set_mock_json( + {"object": "contact", "id": "479e3145-dd38-476b-932c-529ceb705947"} + ) params: resend.Contacts.CreateParams = { "audience_id": "48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8", @@ -33,25 +20,13 @@ def mock_json() -> Dict[Any, Any]: contact: resend.Contact = resend.Contacts.create(params) assert contact["id"] == "479e3145-dd38-476b-932c-529ceb705947" - patcher.stop() - def test_contacts_update(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "contact", "id": "479e3145-dd38-476b-932c-529ceb705947", } - - m.json = mock_json - mock.return_value = m + ) params: resend.Contacts.UpdateParams = { "audience_id": "48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8", @@ -62,19 +37,9 @@ def mock_json() -> Dict[Any, Any]: contact = resend.Contacts.update(params) assert contact["id"] == "479e3145-dd38-476b-932c-529ceb705947" - patcher.stop() - def test_contacts_get(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "contact", "id": "e169aa45-1ecf-4183-9955-b1499d5701d3", "email": "steve.wozniak@gmail.com", @@ -83,9 +48,7 @@ def mock_json() -> Dict[Any, Any]: "created_at": "2023-10-06T23:47:56.678Z", "unsubscribed": False, } - - m.json = mock_json - mock.return_value = m + ) contact: resend.Contact = resend.Contacts.get( id="e169aa45-1ecf-4183-9955-b1499d5701d3", @@ -97,26 +60,15 @@ def mock_json() -> Dict[Any, Any]: assert contact["last_name"] == "Wozniak" assert contact["created_at"] == "2023-10-06T23:47:56.678Z" assert contact["unsubscribed"] is False - patcher.stop() def test_contacts_remove_by_id(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "contact", "id": "520784e2-887d-4c25-b53c-4ad46ad38100", "deleted": True, } - - m.json = mock_json - mock.return_value = m + ) rmed = resend.Contacts.remove( audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8", @@ -124,26 +76,15 @@ def mock_json() -> Dict[Any, Any]: ) assert rmed["id"] == "520784e2-887d-4c25-b53c-4ad46ad38100" assert rmed["deleted"] is True - patcher.stop() def test_contacts_remove_by_email(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "contact", "id": "520784e2-887d-4c25-b53c-4ad46ad38100", "deleted": True, } - - m.json = mock_json - mock.return_value = m + ) rmed: resend.Contact = resend.Contacts.remove( audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8", @@ -151,7 +92,6 @@ def mock_json() -> Dict[Any, Any]: ) assert rmed["id"] == "520784e2-887d-4c25-b53c-4ad46ad38100" assert rmed["deleted"] is True - patcher.stop() def test_contacts_remove_raises(self) -> None: resend.api_key = "re_123" @@ -164,16 +104,8 @@ def test_contacts_remove_raises(self) -> None: self.assertEqual("id or email must be provided", str(context.exception)) def test_contacts_list(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "list", "data": [ { @@ -186,9 +118,7 @@ def mock_json() -> Dict[Any, Any]: } ], } - - m.json = mock_json - mock.return_value = m + ) contacts: resend.Contacts.ListResponse = resend.Contacts.list( audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8" @@ -199,4 +129,3 @@ def mock_json() -> Dict[Any, Any]: assert contacts["data"][0]["last_name"] == "Wozniak" assert contacts["data"][0]["created_at"] == "2023-10-06T23:47:56.678Z" assert contacts["data"][0]["unsubscribed"] is False - patcher.stop() diff --git a/tests/domains_test.py b/tests/domains_test.py index de187da..36803a6 100644 --- a/tests/domains_test.py +++ b/tests/domains_test.py @@ -1,24 +1,13 @@ -import unittest -from typing import Any, Dict -from unittest.mock import MagicMock, patch - import resend +from tests.conftest import ResendBaseTest # flake8: noqa -class TestResendDomains(unittest.TestCase): +class TestResendDomains(ResendBaseTest): def test_domains_create(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "id": "4dd369bc-aa82-4ff3-97de-514ae3000ee0", "name": "example.com", "created_at": "2023-03-28T17:12:02.059593+00:00", @@ -68,9 +57,7 @@ def mock_json() -> Dict[Any, Any]: ], "region": "us-east-1", } - - m.json = mock_json - mock.return_value = m + ) create_params: resend.Domains.CreateParams = { "name": "example.com", @@ -82,19 +69,9 @@ def mock_json() -> Dict[Any, Any]: assert domain["created_at"] == "2023-03-28T17:12:02.059593+00:00" assert domain["region"] == "us-east-1" - patcher.stop() - def test_domains_get(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "domain", "id": "d91cd9bd-1176-453e-8fc1-35364d380206", "name": "example.com", @@ -102,9 +79,7 @@ def mock_json() -> Dict[Any, Any]: "created_at": "2023-04-26T20:21:26.347412+00:00", "region": "us-east-1", } - - m.json = mock_json - mock.return_value = m + ) domain = resend.Domains.get( domain_id="d91cd9bd-1176-453e-8fc1-35364d380206", @@ -114,19 +89,10 @@ def mock_json() -> Dict[Any, Any]: assert domain["status"] == "not_started" assert domain["created_at"] == "2023-04-26T20:21:26.347412+00:00" assert domain["region"] == "us-east-1" - patcher.stop() def test_domains_list(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "data": [ { "id": "d91cd9bd-1176-453e-8fc1-35364d380206", @@ -137,9 +103,7 @@ def mock_json() -> Dict[Any, Any]: } ] } - - m.json = mock_json - mock.return_value = m + ) domains = resend.Domains.list() assert domains["data"][0]["id"] == "d91cd9bd-1176-453e-8fc1-35364d380206" @@ -147,73 +111,39 @@ def mock_json() -> Dict[Any, Any]: assert domains["data"][0]["status"] == "not_started" assert domains["data"][0]["created_at"] == "2023-04-26T20:21:26.347412+00:00" assert domains["data"][0]["region"] == "us-east-1" - patcher.stop() def test_domains_remove(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "domain", "id": "4ef9a417-02e9-4d39-ad75-9611e0fcc33c", "deleted": True, } - - m.json = mock_json - mock.return_value = m + ) domain = resend.Domains.remove( domain_id="4ef9a417-02e9-4d39-ad75-9611e0fcc33c", ) assert domain["deleted"] is True assert domain["id"] == "4ef9a417-02e9-4d39-ad75-9611e0fcc33c" - patcher.stop() def test_domains_verify(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return {"object": "domain", "id": "d91cd9bd-1176-453e-8fc1-35364d380206"} - - m.json = mock_json - mock.return_value = m + self.set_mock_json( + {"object": "domain", "id": "d91cd9bd-1176-453e-8fc1-35364d380206"} + ) domain = resend.Domains.verify( domain_id="d91cd9bd-1176-453e-8fc1-35364d380206", ) assert domain["id"] == "d91cd9bd-1176-453e-8fc1-35364d380206" - patcher.stop() - def test_domains_update(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "domain", "id": "479e3145-dd38-476b-932c-529ceb705947", } - - m.json = mock_json - mock.return_value = m + ) params: resend.Domains.UpdateParams = { "id": "479e3145-dd38-476b-932c-529ceb705947", @@ -222,5 +152,3 @@ def mock_json() -> Dict[Any, Any]: } domain = resend.Domains.update(params) assert domain["id"] == "479e3145-dd38-476b-932c-529ceb705947" - - patcher.stop() diff --git a/tests/emails_test.py b/tests/emails_test.py index 963f64b..e8dedff 100644 --- a/tests/emails_test.py +++ b/tests/emails_test.py @@ -1,30 +1,17 @@ -import unittest -from typing import Any, Dict -from unittest.mock import MagicMock, patch - import resend +from tests.conftest import ResendBaseTest # flake8: noqa -class TestResendEmail(unittest.TestCase): +class TestResendEmail(ResendBaseTest): def test_email_send_with_from(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794", } - - m.json = mock_json - mock.return_value = m + ) params: resend.Emails.SendParams = { "to": "to@email.com", @@ -34,19 +21,10 @@ def mock_json() -> Dict[Any, Any]: } email: resend.Email = resend.Emails.send(params) assert email["id"] == "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794" - patcher.stop() def test_email_get(self) -> None: - resend.api_key = "re_123" - - patcher = patch("resend.Request.make_request") - mock = patcher.start() - mock.status_code = 200 - m = MagicMock() - m.status_code = 200 - - def mock_json() -> Dict[Any, Any]: - return { + self.set_mock_json( + { "object": "email", "id": "4ef9a417-02e9-4d39-ad75-9611e0fcc33c", "to": ["james@bond.com"], @@ -60,12 +38,9 @@ def mock_json() -> Dict[Any, Any]: "reply_to": [None], "last_event": "delivered", } - - m.json = mock_json - mock.return_value = m + ) email: resend.Email = resend.Emails.get( email_id="4ef9a417-02e9-4d39-ad75-9611e0fcc33c", ) assert email["id"] == "4ef9a417-02e9-4d39-ad75-9611e0fcc33c" - patcher.stop()