Testing Tutorial

hubspot-connection offers utilities to simulate communication with HubSpot, thus allowing you to unit test your code on top of hubspot-contacts.

Imagine that you’d like to write unit tests for the function below:

from hubspot.contacts.lists import get_contact_lists

def get_contact_list_names(connection):
    return [list_.name for list_ in get_contact_lists(connection)]

One of the cases that you’d like to test would be when there’s exactly one list. You’d test this unit by passing a so-called “mock portal connection” instead of a regular connection, along with a so-called “simulator” for each of the end-points that you expect will get called; e.g.:

from hubspot.connection.testing import MockPortalConnection
from hubspot.contacts.lists import ContactList
from hubspot.contacts.testing import GetAllContactLists

def test_one_list():
    contact_list = ContactList(id=1, name='Your List', is_dynamic=False)
    simulator = GetAllContactLists([contact_list])
    with MockPortalConnection(simulator) as connection:
        contact_list_names = get_contact_list_names(connection)

    assert len(contact_list_names) == 1
    assert contact_list_names[0] == contact_list.name

The mock portal connection is initialized with zero or more so-called “API call simulators” (or simply “simulators”), which are passed by position. The purpose of a simulator is twofold:

  • On the one hand, it tells the connection what exactly you expect will get sent to HubSpot. If the wrong API end-point is called or the right one is called with the wrong arguments, an AssertionError would be raised by the connection.
  • On the other hand, you can use it to forge the response that HubSpot would give to a particular request. In the example above, we replicated the case where HubSpot responded that there were exactly one contact list in the portal.

By using the mock portal connection as a context manager, you also ensure that all of the specified API end-points are called and nothing else.

Read hubspot.connection.testing to learn more about the mock portal connection, or read on to learn about the individual simulators provided by this library.

Simulators

Each of the end-points supported by this library ships with a simulator for cases where the request completes successfully. In some cases, a simulator for error cases is also provided; their constructors largely share the same signature as their successful counterparts, but they also require the exception instance that is expected to be raised.

class hubspot.contacts.testing.GetAllContacts(contacts, available_properties, property_names=())

Bases: hubspot.contacts.testing._PaginatedObjectsRetriever

Simulator for a successful call to get_all_contacts().

Parameters:
  • contacts (iterable) – Contact instances for all the contacts supposedly in the portal.
  • available_properties (iterable) – Property instances for all the properties supposedly defined in the portal.
  • property_names (iterable) – The names of the properties that get_all_contacts() is expected to request.
class hubspot.contacts.testing.GetAllContactsByLastUpdate(contacts, available_properties, property_names=(), cutoff_datetime=None)

Bases: hubspot.contacts.testing.GetAllContacts

Simulator for a successful call to get_all_contacts_by_last_update().

Parameters:

When cutoff_datetime is unset, this simulator works exactly like GetAllContacts – Except that it uses a different API end-point. The order of the returned contacts match that of contacts.

Given that get_all_contacts_by_last_update() relies on the last modified time of each contact to filter out those which were last changed before cutoff_datetime, this simulator assigns a fake, decreasing value to each contact in the response.

Therefore, if you need to test the effects of cutoff_datetime, you ought to pre-compute such values with get_contact_added_at_datetime(). E.g.:

contacts = [
    Contact(vid=1, email_address='1@example.com', properties={}),
    Contact(vid=2, email_address='2@example.com', properties={}),
    Contact(vid=3, email_address='3@example.com', properties={}),
    ]
# We expect get_all_contacts_by_last_update to exclude the last
# contact:
cutoff_datetime = \
    GetAllContactsByLastUpdate.get_contact_added_at_datetime(
        contacts[1],
        contacts,
        )
simulator = GetAllContactsByLastUpdate(
    contacts,
    available_properties,
    cutoff_datetime=cutoff_datetime,
    )
classmethod get_contact_added_at_datetime(contact, contacts)

Compute the fake last modification time that would be assigned to contact inside contacts.

Parameters:
Returns:

datetime for the last modification time corresponding to contact

This method refers to the “added at” term, which is a piece of meta-data that HubSpot attaches to each contact returned via the end-point for the recently updated contacts. In spite of its name, this datum is effectively the last modified date, but not exactly the same as the “lastmodifieddate” property.

The cutoff_datetime passed to this simulator is compared against the “added at” value of the contact, not the “lastmodifieddate” property.

class hubspot.contacts.testing.SaveContacts(contacts, available_properties)

Bases: object

Simulator for a successful call to save_contacts().

Parameters:
  • contacts (iterable) – Contacts to be supposedly saved
  • available_properties (iterable) – Property instances for all the properties supposedly defined in the portal
class hubspot.contacts.testing.UnsuccessfulGetAllContacts

The unsuccessful counterpart to GetAllContacts.

__init__(contacts, exception, *args, **kwargs)

The additional arguments are passed to GetAllContacts.

class hubspot.contacts.testing.UnsuccessfulGetAllContactsByLastUpdate

The unsuccessful counterpart to GetAllContactsByLastUpdate.

__init__(contacts, exception, *args, **kwargs)

The additional arguments are passed to GetAllContactsByLastUpdate.

class hubspot.contacts.testing.UnsuccessfulSaveContacts(contacts, exception, available_properties)

Bases: hubspot.contacts.testing.SaveContacts

Simulator for an usuccessful call to save_contacts().

Parameters:
  • contacts (iterable) – Contacts to be supposedly saved
  • exception (Exception) –
  • available_properties (iterable) – Property instances for all the properties supposedly defined in the portal

Contact Lists

class hubspot.contacts.testing.GetAllContactLists(objects)

Bases: hubspot.contacts.testing._PaginatedObjectsRetriever

Simulator for a successful call to get_all_contact_lists().

__init__(contact_lists)
Parameters:contact_lists (iterable) – ContactList instances for all the contact lists supposedly in the portal
class hubspot.contacts.testing.CreateStaticContactList(contact_list_name)

Bases: hubspot.contacts.testing._BaseCreateStaticContactList

Simulator for a successful call to create_static_contact_list().

class hubspot.contacts.testing.GetContactsFromList(contact_list, contacts, available_properties, property_names=())

Bases: hubspot.contacts.testing.GetAllContacts

Simulator for a successful call to get_all_contacts_from_list().

This behaves exactly like GetAllContacts, except that it works on a specific contact list and therefore uses a different API end-point.

class hubspot.contacts.testing.GetContactsFromListByAddedDate(contact_list, *args, **kwargs)

Bases: hubspot.contacts.testing.GetAllContactsByLastUpdate

Simulator for a successful call to get_all_contacts_from_list_by_added_date().

This behaves exactly like GetAllContactsByLastUpdate, except that it works on a specific contact list and therefore uses a different API end-point.

class hubspot.contacts.testing.DeleteContactList(contact_list_id)

Bases: object

Simulator for a successful call to delete_contact_list().

Parameters:contact_list_id (basestring) – The id of the static list to be supposedly deleted
class hubspot.contacts.testing.AddContactsToList(contact_list, contacts_to_add, updated_contacts)

Bases: hubspot.contacts.testing._UpdateContactListMembership

Simulator for a successful call to add_contacts_to_list().

Parameters:
  • contact_list (hubspot.contacts.lists.ContactList) – The list that would supposedly receive contacts_to_add
  • contacts_to_add (iterable) – The Contact instances to be supposedly added to contact_list
  • updated_contacts (iterable) – The Contact instances that would have been supposedly added to contact_list
class hubspot.contacts.testing.RemoveContactsFromList(contact_list, contacts_to_remove, updated_contacts)

Bases: hubspot.contacts.testing._UpdateContactListMembership

Simulator for a successful call to remove_contacts_from_list().

Parameters:
  • contact_list (hubspot.contacts.lists.ContactList) – The list that would supposedly have contacts_to_remove removed
  • contacts_to_remove (iterable) – The Contact instances to be supposedly removed from contact_list
  • updated_contacts (iterable) – The Contact instances that would have been supposedly removed from contact_list
class hubspot.contacts.testing.UnsuccessfulCreateStaticContactList(contact_list_name, exception)

Bases: hubspot.contacts.testing._BaseCreateStaticContactList

Simulator for an unsuccessful call to create_static_contact_list().

Parameters:
  • contact_list_name (basestring) – The name of the static list to be supposedly created
  • exception (Exception) –

Contact Properties

class hubspot.contacts.testing.GetAllProperties(properties)

Bases: object

Simulator for a successful call to get_all_properties().

Parameters:properties (iterable) – Property instances for all the properties supposedly defined in the portal.
class hubspot.contacts.testing.CreateProperty(property_)

Bases: hubspot.contacts.testing._BaseCreateProperty

Simulator for a successful call to create_property().

Parameters:property (hubspot.contacts.properties.Property) – The property to be supposedly created.
class hubspot.contacts.testing.DeleteProperty(property_name)

Bases: object

Simulator for a successful call to delete_property().

Parameters:property_name (basestring) – The name of the property to be supposedly deleted.
class hubspot.contacts.testing.UnsuccessfulCreateProperty(property_, exception)

Bases: hubspot.contacts.testing._BaseCreateProperty

Simulator for an unsuccessful call to create_property().

Parameters:

Contact Property Groups

class hubspot.contacts.testing.GetAllPropertyGroups(property_groups)

Bases: object

Simulator for a successful call to get_all_property_groups().

Parameters:property_groups (iterable) – PropertyGroup instances for all the property groups supposedly defined in the portal.
class hubspot.contacts.testing.CreatePropertyGroup(property_group)

Bases: hubspot.contacts.testing._BaseCreatePropertyGroup

Simulator for a successful call to create_property_group().

Parameters:property_group (hubspot.contacts.property_groups.PropertyGroup) – Property group to be supposedly created.
class hubspot.contacts.testing.DeletePropertyGroup(property_group_name)

Bases: object

Simulator for a successful call to delete_property_group().

Parameters:property_group_name (basestring) – The name of the property group to be supposedly deleted.
class hubspot.contacts.testing.UnsuccessfulCreatePropertyGroup(property_group, exception)

Bases: hubspot.contacts.testing._BaseCreatePropertyGroup

Simulator for an unsuccessful call to create_property_group().

Parameters:

Table Of Contents

Previous topic

API Documentation

Next topic

Changelog

This Page