|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
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 |
11 | 13 |
|
12 | 14 |
|
13 | 15 | class MockHubspotContact: |
14 | | - def __init__(self, id): |
| 16 | + def __init__(self, id, properties=None): |
15 | 17 | self.id = id |
| 18 | + self.properties = properties |
16 | 19 |
|
17 | 20 |
|
18 | 21 | class MockHubspotCompany: |
19 | 22 | def __init__(self, id): |
20 | 23 | self.id = id |
21 | 24 |
|
22 | 25 |
|
| 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 | + |
23 | 36 | @mock.patch("hubspot_api.utils.associate_contact_to_organization") |
24 | 37 | @mock.patch("hubspot_api.utils.get_company_by_name") |
25 | 38 | @mock.patch("hubspot_api.utils.api_client") |
26 | 39 | def test_create_contact( |
27 | | - mock_api_basic_api_create, |
| 40 | + mock_api_client, |
28 | 41 | mock_get_company_by_name, |
29 | 42 | mock_associate_contact_to_organization, |
30 | 43 | ): |
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 |
33 | 46 | ) |
34 | 47 | mock_get_company_by_name.return_value = {"id": 1234} |
35 | 48 | mock_associate_contact_to_organization.return_value = "ok" |
@@ -94,3 +107,103 @@ def test_create_company_raises_company_exception(mock_api_client): |
94 | 107 |
|
95 | 108 | assert isinstance(exc.value, CompanyException) |
96 | 109 | 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 | + |
| 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 | + |
| 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