Skip to content

Commit 11f8b85

Browse files
surajitsurajit
authored andcommitted
add tests for hubspot utils
1 parent 6b37d7a commit 11f8b85

File tree

3 files changed

+111
-11
lines changed

3 files changed

+111
-11
lines changed

hubspot_api/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def create_contact(data):
3434
"email": "email",
3535
"phone": "phone",
3636
}
37-
3837
data = {contact_map[k]: v for k, v in data.items()}
3938
simple_public_object_input = SimplePublicObjectInput(properties=data)
4039
contact = api_client.crm.contacts.basic_api.create(
@@ -104,11 +103,12 @@ def get_contact_by_email(email):
104103
if contact.results:
105104
contact_detail = contact.results[0]
106105
contact_properties = contact_detail.properties
107-
return {"id": contact_detail.id,
108-
"email": contact_properties["email"],
109-
"firstname": contact_properties["firstname"],
110-
"lastname": contact_properties["lastname"],
111-
}
106+
return {
107+
"id": contact_detail.id,
108+
"email": contact_properties["email"],
109+
"firstname": contact_properties["firstname"],
110+
"lastname": contact_properties["lastname"],
111+
}
112112
else:
113113
return {}
114114
except contacts.exceptions.ApiException as e:

tests/integrations/test_hubspot.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from unittest import mock
2+
3+
import pytest
4+
5+
from hubspot_api.utils import (
6+
create_contact,
7+
ContactException,
8+
create_company,
9+
CompanyException,
10+
)
11+
12+
13+
class MockHubspotContact:
14+
def __init__(self, id):
15+
self.id = id
16+
17+
18+
class MockHubspotCompany:
19+
def __init__(self, id):
20+
self.id = id
21+
22+
23+
@mock.patch("hubspot_api.utils.associate_contact_to_organization")
24+
@mock.patch("hubspot_api.utils.get_company_by_name")
25+
@mock.patch("hubspot_api.utils.api_client")
26+
def test_create_contact(
27+
mock_api_basic_api_create,
28+
mock_get_company_by_name,
29+
mock_associate_contact_to_organization,
30+
):
31+
mock_api_basic_api_create.crm.contacts.basic_api.create.return_value = (
32+
MockHubspotContact(1234)
33+
)
34+
mock_get_company_by_name.return_value = {"id": 1234}
35+
mock_associate_contact_to_organization.return_value = "ok"
36+
contact = create_contact(
37+
dict(
38+
company_name="Test org",
39+
first_name="Test",
40+
last_name="User",
41+
42+
phone="111111111",
43+
)
44+
)
45+
assert contact == {"contact_id": 1234}
46+
47+
48+
@mock.patch("hubspot_api.utils.api_client")
49+
def test_create_contact_raises_contact_exception(mock_api_client):
50+
mock_api_client.crm.contacts.basic_api.create.side_effect = ContactException(
51+
"Contact Already " "exists"
52+
)
53+
54+
with pytest.raises(ContactException) as exc:
55+
create_contact(
56+
dict(
57+
company_name="Test org",
58+
first_name="Test",
59+
last_name="User",
60+
61+
phone="111111111",
62+
)
63+
)
64+
assert isinstance(exc.value, ContactException)
65+
assert exc.value.args[0] == "Contact Already exists"
66+
67+
68+
@mock.patch("hubspot_api.utils.api_client")
69+
def test_create_company(mock_api_client):
70+
mock_api_client.crm.companies.basic_api.create.return_value = MockHubspotCompany(
71+
777
72+
)
73+
company = create_company(
74+
dict(
75+
name="Test org",
76+
org_id="999",
77+
)
78+
)
79+
assert company == {"company_id": 777}
80+
81+
82+
@mock.patch("hubspot_api.utils.api_client")
83+
def test_create_company_raises_company_exception(mock_api_client):
84+
mock_api_client.crm.companies.basic_api.create.side_effect = CompanyException(
85+
"Company already exists"
86+
)
87+
with pytest.raises(CompanyException) as exc:
88+
create_company(
89+
dict(
90+
name="Test org",
91+
org_id="999",
92+
)
93+
)
94+
95+
assert isinstance(exc.value, CompanyException)
96+
assert exc.value.args[0] == "Company already exists"

tests/routes/test_email.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ def test_email_send(mock_logger_debug, mock_send_email, client, db):
3737

3838
@mock.patch("routes.v1.email.email.send_email")
3939
@mock.patch("logger.log.logger.debug")
40-
def test_email_send_with_sendgrid_unauthorizedexception(mock_logger_debug, mock_send_email, client):
40+
def test_email_send_with_sendgrid_unauthorizedexception(
41+
mock_logger_debug, mock_send_email, client
42+
):
4143
mock_logger_debug.return_value = "Ok"
4244
mock_send_email.side_effect = UnauthorizedException("Not allowed to access the API")
4345
response = client.post("/api/v1/email/send/", json=read_json("email_data.json"))
@@ -47,7 +49,9 @@ def test_email_send_with_sendgrid_unauthorizedexception(mock_logger_debug, mock_
4749

4850
@mock.patch("routes.v1.email.email.send_email")
4951
@mock.patch("logger.log.logger.debug")
50-
def test_email_send_with_sendgrid_bad_request_exception(mock_logger_debug, mock_send_email, client):
52+
def test_email_send_with_sendgrid_bad_request_exception(
53+
mock_logger_debug, mock_send_email, client
54+
):
5155
mock_logger_debug.return_value = "Ok"
5256
mock_send_email.side_effect = BadRequestException("Bad request")
5357
response = client.post("/api/v1/email/send/", json=read_json("email_data.json"))
@@ -58,7 +62,7 @@ def test_email_send_with_sendgrid_bad_request_exception(mock_logger_debug, mock_
5862
@mock.patch("routes.v1.email.email.send_email")
5963
@mock.patch("logger.log.logger.debug")
6064
def test_email_send_with_db_duplicate_message_id(
61-
mock_logger_debug, mock_send_email, client, message
65+
mock_logger_debug, mock_send_email, client, message
6266
):
6367
mock_logger_debug.return_value = "Ok"
6468
sendgrid_response_mock = SendGridResponseMock(
@@ -69,6 +73,6 @@ def test_email_send_with_db_duplicate_message_id(
6973
assert response.status_code == 200
7074
assert response.json() == {
7175
"detail": "duplicate key value violates unique constraint "
72-
'"ix_message_message_id"\nDETAIL: Key (message_id)=('
73-
"123657ab) already exists.\n"
76+
'"ix_message_message_id"\nDETAIL: Key (message_id)=('
77+
"123657ab) already exists.\n"
7478
}

0 commit comments

Comments
 (0)