Skip to content

Commit c9605f1

Browse files
surajitsurajit
authored andcommitted
update test for hubspot
1 parent 11f8b85 commit c9605f1

File tree

1 file changed

+123
-10
lines changed

1 file changed

+123
-10
lines changed

tests/integrations/test_hubspot.py

Lines changed: 123 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,47 @@
22

33
import pytest
44

5-
from hubspot_api.utils import (
6-
create_contact,
7-
ContactException,
8-
create_company,
9-
CompanyException,
10-
)
5+
from hubspot_api.utils import CompanyException
6+
from hubspot_api.utils import ContactAssociationOrganizationException
7+
from hubspot_api.utils import ContactException
8+
from hubspot_api.utils import create_contact
9+
from hubspot_api.utils import create_company
10+
from hubspot_api.utils import get_contact_by_email
11+
from hubspot_api.utils import get_company_by_name
12+
from hubspot_api.utils import associate_contact_to_organization
1113

1214

1315
class MockHubspotContact:
14-
def __init__(self, id):
16+
def __init__(self, id, properties=None):
1517
self.id = id
18+
self.properties = properties
1619

1720

1821
class MockHubspotCompany:
1922
def __init__(self, id):
2023
self.id = id
2124

2225

26+
class MockHubspotCompanySearch:
27+
def __init__(self, results=None):
28+
self.results = results
29+
30+
31+
class MockHubspotContactSearch:
32+
def __init__(self, results):
33+
self.results = results
34+
35+
2336
@mock.patch("hubspot_api.utils.associate_contact_to_organization")
2437
@mock.patch("hubspot_api.utils.get_company_by_name")
2538
@mock.patch("hubspot_api.utils.api_client")
2639
def test_create_contact(
27-
mock_api_basic_api_create,
40+
mock_api_client,
2841
mock_get_company_by_name,
2942
mock_associate_contact_to_organization,
3043
):
31-
mock_api_basic_api_create.crm.contacts.basic_api.create.return_value = (
32-
MockHubspotContact(1234)
44+
mock_api_client.crm.contacts.basic_api.create.return_value = MockHubspotContact(
45+
1234
3346
)
3447
mock_get_company_by_name.return_value = {"id": 1234}
3548
mock_associate_contact_to_organization.return_value = "ok"
@@ -94,3 +107,103 @@ def test_create_company_raises_company_exception(mock_api_client):
94107

95108
assert isinstance(exc.value, CompanyException)
96109
assert exc.value.args[0] == "Company already exists"
110+
111+
112+
@mock.patch("hubspot_api.utils.api_client")
113+
def test_get_company_by_name(mock_api_client):
114+
mock_hubspot_company = MockHubspotCompany(id=1234)
115+
mock_api_client.crm.companies.search_api.do_search.return_value = (
116+
MockHubspotCompanySearch(results=[mock_hubspot_company])
117+
)
118+
company = get_company_by_name("Test org")
119+
assert company == {"id": 1234}
120+
121+
122+
@mock.patch("hubspot_api.utils.api_client")
123+
def test_get_company_by_name_with_empty_results(mock_api_client):
124+
mock_api_client.crm.companies.search_api.do_search.return_value = (
125+
MockHubspotCompanySearch()
126+
)
127+
company = get_company_by_name("Test org1")
128+
assert company == {}
129+
130+
131+
@mock.patch("hubspot_api.utils.api_client")
132+
def test_get_company_by_name_raises_company_exception(mock_api_client):
133+
mock_api_client.crm.companies.search_api.do_search.side_effect = CompanyException(
134+
"Something went wrong"
135+
)
136+
with pytest.raises(CompanyException) as exc:
137+
get_company_by_name("Test org2")
138+
139+
assert isinstance(exc.value, CompanyException)
140+
assert exc.value.args[0] == "Something went wrong"
141+
142+
143+
@mock.patch("hubspot_api.utils.api_client")
144+
def test_get_contact_by_email(mock_api_client):
145+
mock_api_client.crm.contacts.search_api.do_search.return_value = (
146+
MockHubspotContactSearch(
147+
results=[
148+
MockHubspotContact(
149+
1234,
150+
properties={
151+
"email": "[email protected]",
152+
"firstname": "test",
153+
"lastname": "user",
154+
},
155+
)
156+
],
157+
)
158+
)
159+
contact = get_contact_by_email("[email protected]")
160+
assert contact == {
161+
"id": 1234,
162+
"email": "[email protected]",
163+
"firstname": "test",
164+
"lastname": "user",
165+
}
166+
167+
168+
@mock.patch("hubspot_api.utils.api_client")
169+
def test_get_contact_by_email_for_no_result(mock_api_client):
170+
mock_api_client.crm.contacts.search_api.do_search.return_value = (
171+
MockHubspotContactSearch(results=None)
172+
)
173+
contact = get_contact_by_email("[email protected]")
174+
assert contact == {}
175+
176+
177+
@mock.patch("hubspot_api.utils.api_client")
178+
def test_get_contact_by_email_raises_contact_exception(mock_api_client):
179+
mock_api_client.crm.contacts.search_api.do_search.side_effect = ContactException(
180+
"Something went wrong"
181+
)
182+
with pytest.raises(ContactException) as exc:
183+
get_contact_by_email("[email protected]")
184+
assert isinstance(exc.value, ContactException)
185+
assert exc.value.args[0] == "Something went wrong"
186+
187+
188+
@mock.patch("hubspot_api.utils.api_client")
189+
def test_associate_contact_to_organization(mock_api_client):
190+
mock_api_client.crm.contacts.associations_api.create.return_value = True
191+
contact_association = associate_contact_to_organization(
192+
contact_id=1234, company_id=777
193+
)
194+
assert contact_association is True
195+
196+
197+
@mock.patch("hubspot_api.utils.api_client")
198+
def test_associate_contact_to_organization_raises_company_organization_association_exception(
199+
mock_api_client,
200+
):
201+
mock_api_client.crm.contacts.associations_api.create.side_effect = (
202+
ContactAssociationOrganizationException(
203+
"Something went wrong in linking contact to company"
204+
)
205+
)
206+
with pytest.raises(ContactAssociationOrganizationException) as exc:
207+
associate_contact_to_organization(contact_id=123, company_id=999)
208+
assert isinstance(exc.value, ContactAssociationOrganizationException)
209+
assert exc.value.args[0] == "Something went wrong in linking contact to company"

0 commit comments

Comments
 (0)