Skip to content

Commit dc3d2bf

Browse files
surajitsurajit
authored andcommitted
add test for sendgrid api
1 parent fab938d commit dc3d2bf

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ typing_extensions==4.2.0
3434
urllib3==1.26.9
3535
zipp==3.8.0
3636
sqlalchemy-utils==0.38.2
37-
37+
pytest-asyncio==0.18.3
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import json
2+
from unittest import mock
3+
4+
import pytest
5+
6+
from email_api.email import send_email
7+
from email_api.email import UnauthorizedException
8+
from email_api.email import BadRequestException
9+
from settings import TEST_DATA_DIR
10+
11+
12+
def read_json(filename):
13+
with open(TEST_DATA_DIR.joinpath(filename)) as resp_file:
14+
return json.load(resp_file)
15+
16+
17+
@mock.patch("email_api.email.SendGridAPIClient.send")
18+
@pytest.mark.asyncio
19+
async def test_send_email(mock_email_client):
20+
mock_email_client.return_value = {"status_code": 202}
21+
response = await send_email(data=read_json("email_data.json"))
22+
assert response["status_code"] == 202
23+
24+
25+
@mock.patch("email_api.email.SendGridAPIClient.send")
26+
@pytest.mark.asyncio
27+
async def test_send_email_raises_unauthorized_exception(mock_email_client):
28+
mock_email_client.side_effect = UnauthorizedException("Invalid api key")
29+
with pytest.raises(UnauthorizedException) as exc:
30+
await send_email(data=read_json("email_data.json"))
31+
assert isinstance(exc.value, UnauthorizedException)
32+
assert exc.value.args[0] == "Invalid api key"
33+
34+
35+
@mock.patch("email_api.email.SendGridAPIClient.send")
36+
@pytest.mark.asyncio
37+
async def test_send_email_raises_bad_request_exception(mock_email_client):
38+
mock_email_client.side_effect = BadRequestException("Bad request")
39+
with pytest.raises(BadRequestException) as exc:
40+
await send_email(data=read_json("email_data.json"))
41+
assert isinstance(exc.value, BadRequestException)
42+
assert exc.value.args[0] == "Bad request"

0 commit comments

Comments
 (0)