-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
surajit
authored and
surajit
committed
May 24, 2022
1 parent
11f8b85
commit c9605f1
Showing
1 changed file
with
123 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,34 +2,47 @@ | |
|
||
import pytest | ||
|
||
from hubspot_api.utils import ( | ||
create_contact, | ||
ContactException, | ||
create_company, | ||
CompanyException, | ||
) | ||
from hubspot_api.utils import CompanyException | ||
from hubspot_api.utils import ContactAssociationOrganizationException | ||
from hubspot_api.utils import ContactException | ||
from hubspot_api.utils import create_contact | ||
from hubspot_api.utils import create_company | ||
from hubspot_api.utils import get_contact_by_email | ||
from hubspot_api.utils import get_company_by_name | ||
from hubspot_api.utils import associate_contact_to_organization | ||
|
||
|
||
class MockHubspotContact: | ||
def __init__(self, id): | ||
def __init__(self, id, properties=None): | ||
self.id = id | ||
self.properties = properties | ||
|
||
|
||
class MockHubspotCompany: | ||
def __init__(self, id): | ||
self.id = id | ||
|
||
|
||
class MockHubspotCompanySearch: | ||
def __init__(self, results=None): | ||
self.results = results | ||
|
||
|
||
class MockHubspotContactSearch: | ||
def __init__(self, results): | ||
self.results = results | ||
|
||
|
||
@mock.patch("hubspot_api.utils.associate_contact_to_organization") | ||
@mock.patch("hubspot_api.utils.get_company_by_name") | ||
@mock.patch("hubspot_api.utils.api_client") | ||
def test_create_contact( | ||
mock_api_basic_api_create, | ||
mock_api_client, | ||
mock_get_company_by_name, | ||
mock_associate_contact_to_organization, | ||
): | ||
mock_api_basic_api_create.crm.contacts.basic_api.create.return_value = ( | ||
MockHubspotContact(1234) | ||
mock_api_client.crm.contacts.basic_api.create.return_value = MockHubspotContact( | ||
1234 | ||
) | ||
mock_get_company_by_name.return_value = {"id": 1234} | ||
mock_associate_contact_to_organization.return_value = "ok" | ||
|
@@ -94,3 +107,103 @@ def test_create_company_raises_company_exception(mock_api_client): | |
|
||
assert isinstance(exc.value, CompanyException) | ||
assert exc.value.args[0] == "Company already exists" | ||
|
||
|
||
@mock.patch("hubspot_api.utils.api_client") | ||
def test_get_company_by_name(mock_api_client): | ||
mock_hubspot_company = MockHubspotCompany(id=1234) | ||
mock_api_client.crm.companies.search_api.do_search.return_value = ( | ||
MockHubspotCompanySearch(results=[mock_hubspot_company]) | ||
) | ||
company = get_company_by_name("Test org") | ||
assert company == {"id": 1234} | ||
|
||
|
||
@mock.patch("hubspot_api.utils.api_client") | ||
def test_get_company_by_name_with_empty_results(mock_api_client): | ||
mock_api_client.crm.companies.search_api.do_search.return_value = ( | ||
MockHubspotCompanySearch() | ||
) | ||
company = get_company_by_name("Test org1") | ||
assert company == {} | ||
|
||
|
||
@mock.patch("hubspot_api.utils.api_client") | ||
def test_get_company_by_name_raises_company_exception(mock_api_client): | ||
mock_api_client.crm.companies.search_api.do_search.side_effect = CompanyException( | ||
"Something went wrong" | ||
) | ||
with pytest.raises(CompanyException) as exc: | ||
get_company_by_name("Test org2") | ||
|
||
assert isinstance(exc.value, CompanyException) | ||
assert exc.value.args[0] == "Something went wrong" | ||
|
||
|
||
@mock.patch("hubspot_api.utils.api_client") | ||
def test_get_contact_by_email(mock_api_client): | ||
mock_api_client.crm.contacts.search_api.do_search.return_value = ( | ||
MockHubspotContactSearch( | ||
results=[ | ||
MockHubspotContact( | ||
1234, | ||
properties={ | ||
"email": "[email protected]", | ||
"firstname": "test", | ||
"lastname": "user", | ||
}, | ||
) | ||
], | ||
) | ||
) | ||
contact = get_contact_by_email("[email protected]") | ||
assert contact == { | ||
"id": 1234, | ||
"email": "[email protected]", | ||
"firstname": "test", | ||
"lastname": "user", | ||
} | ||
|
||
|
||
@mock.patch("hubspot_api.utils.api_client") | ||
def test_get_contact_by_email_for_no_result(mock_api_client): | ||
mock_api_client.crm.contacts.search_api.do_search.return_value = ( | ||
MockHubspotContactSearch(results=None) | ||
) | ||
contact = get_contact_by_email("[email protected]") | ||
assert contact == {} | ||
|
||
|
||
@mock.patch("hubspot_api.utils.api_client") | ||
def test_get_contact_by_email_raises_contact_exception(mock_api_client): | ||
mock_api_client.crm.contacts.search_api.do_search.side_effect = ContactException( | ||
"Something went wrong" | ||
) | ||
with pytest.raises(ContactException) as exc: | ||
get_contact_by_email("[email protected]") | ||
assert isinstance(exc.value, ContactException) | ||
assert exc.value.args[0] == "Something went wrong" | ||
|
||
|
||
@mock.patch("hubspot_api.utils.api_client") | ||
def test_associate_contact_to_organization(mock_api_client): | ||
mock_api_client.crm.contacts.associations_api.create.return_value = True | ||
contact_association = associate_contact_to_organization( | ||
contact_id=1234, company_id=777 | ||
) | ||
assert contact_association is True | ||
|
||
|
||
@mock.patch("hubspot_api.utils.api_client") | ||
def test_associate_contact_to_organization_raises_company_organization_association_exception( | ||
mock_api_client, | ||
): | ||
mock_api_client.crm.contacts.associations_api.create.side_effect = ( | ||
ContactAssociationOrganizationException( | ||
"Something went wrong in linking contact to company" | ||
) | ||
) | ||
with pytest.raises(ContactAssociationOrganizationException) as exc: | ||
associate_contact_to_organization(contact_id=123, company_id=999) | ||
assert isinstance(exc.value, ContactAssociationOrganizationException) | ||
assert exc.value.args[0] == "Something went wrong in linking contact to company" |