Skip to content

Commit

Permalink
setup tests (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroimpulcetto authored May 29, 2024
1 parent 752fe35 commit a3fef82
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 341 deletions.
53 changes: 9 additions & 44 deletions tests/api_keys_test.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -52,33 +30,20 @@ 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(
api_key_id="4ef9a417-02e9-4d39-ad75-9611e0fcc33c",
)
is None
)
patcher.stop()
77 changes: 14 additions & 63 deletions tests/audiences_test.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": [
{
Expand All @@ -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()
25 changes: 7 additions & 18 deletions tests/batch_emails_test.py
Original file line number Diff line number Diff line change
@@ -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] = [
{
Expand All @@ -42,8 +31,8 @@ def mock_json() -> Dict[Any, Any]:
"html": "<strong>hello, world!</strong>",
},
]

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()
30 changes: 30 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit a3fef82

Please sign in to comment.