|
| 1 | +import pytest |
| 2 | + |
| 3 | +from hubspot.crm.contacts import PublicObjectSearchRequest |
| 4 | + |
| 5 | +CONTACT_PROPERTIES = { |
| 6 | + |
| 7 | + "lastname": "Rocket", |
| 8 | + "firstname": "Racoon" |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def create_contact(api_client): |
| 14 | + contact = api_client.crm.contacts.basic_api.create(CONTACT_PROPERTIES) |
| 15 | + |
| 16 | + yield contact |
| 17 | + |
| 18 | + api_client.crm.contacts.basic_api.archive(contact_id=contact.id) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def create_company(api_client): |
| 23 | + properties = { |
| 24 | + "domain": "TestCompany.net", |
| 25 | + "name": "TestCompany", |
| 26 | + } |
| 27 | + |
| 28 | + company = api_client.crm.companies.basic_api.create(properties) |
| 29 | + |
| 30 | + yield company |
| 31 | + |
| 32 | + api_client.crm.companies.basic_api.archive(company_id=company.id) |
| 33 | + |
| 34 | + |
| 35 | +def get_or_create_contact_associated_with_company(api_client, create_company): |
| 36 | + contact_data = { |
| 37 | + "associations": [ |
| 38 | + { |
| 39 | + "to": {"id": create_company.id}, |
| 40 | + "types": [ |
| 41 | + { |
| 42 | + "associationCategory": "HUBSPOT_DEFINED", |
| 43 | + "associationTypeId": 1 |
| 44 | + } |
| 45 | + ] |
| 46 | + } |
| 47 | + ], |
| 48 | + "properties": CONTACT_PROPERTIES |
| 49 | + } |
| 50 | + public_object_search_request = PublicObjectSearchRequest( |
| 51 | + filter_groups=[ |
| 52 | + { |
| 53 | + "filters": [ |
| 54 | + { |
| 55 | + "value": contact_data["properties"]["email"], |
| 56 | + "propertyName": "email", |
| 57 | + "operator": "EQ" |
| 58 | + } |
| 59 | + ] |
| 60 | + } |
| 61 | + ] |
| 62 | + ) |
| 63 | + contact_response = api_client.crm.contacts.search_api.do_search(public_object_search_request) |
| 64 | + if contact_response.results: |
| 65 | + return contact_response.results[0] |
| 66 | + else: |
| 67 | + return api_client.crm.contacts.basic_api.create(contact_data) |
| 68 | + |
| 69 | + |
| 70 | +def test_create_contact_with_company_association(api_client, create_company): |
| 71 | + contact = get_or_create_contact_associated_with_company(api_client, create_company) |
| 72 | + |
| 73 | + assert contact |
| 74 | + |
| 75 | + |
| 76 | +def test_contact__create(api_client): |
| 77 | + contact = api_client.crm.contacts.basic_api.create(CONTACT_PROPERTIES) |
| 78 | + |
| 79 | + assert contact |
| 80 | + |
| 81 | + api_client.crm.contacts.basic_api.archive(contact_id=contact.id) |
| 82 | + |
| 83 | + |
| 84 | +def test_contact__get_page(api_client): |
| 85 | + result = api_client.crm.companies.basic_api.get_page() |
| 86 | + |
| 87 | + assert result |
| 88 | + |
| 89 | + |
| 90 | +def test_contact__get_by_id(api_client, create_contact): |
| 91 | + result = api_client.crm.contacts.basic_api.get_by_id(create_contact.id) |
| 92 | + |
| 93 | + assert result |
| 94 | + |
| 95 | + |
| 96 | +def test_contact__get_all(api_client): |
| 97 | + result = api_client.crm.contacts.get_all() |
| 98 | + |
| 99 | + assert result |
| 100 | + |
| 101 | + |
| 102 | +def test_contact__update(api_client, create_contact): |
| 103 | + simple_public_object_input = { |
| 104 | + "properties": { |
| 105 | + "firstname": "updated_name" |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + result = api_client.crm.contacts.basic_api.update(create_contact.id, simple_public_object_input) |
| 110 | + |
| 111 | + assert result.properties["firstname"] == simple_public_object_input["properties"]["firstname"] |
| 112 | + |
| 113 | + |
| 114 | +def test_contact__archive(api_client, create_contact): |
| 115 | + result = api_client.crm.contacts.basic_api.archive(create_contact.id) |
| 116 | + |
| 117 | + assert result is None |
| 118 | + |
| 119 | + |
| 120 | +def test_contact__do_search(api_client, create_contact): |
| 121 | + public_object_search_request = PublicObjectSearchRequest( |
| 122 | + filter_groups=[ |
| 123 | + { |
| 124 | + "filters": [ |
| 125 | + { |
| 126 | + |
| 127 | + "propertyName": "email", |
| 128 | + "operator": "EQ" |
| 129 | + } |
| 130 | + ] |
| 131 | + } |
| 132 | + ] |
| 133 | + ) |
| 134 | + contact = api_client.crm.contacts.search_api.do_search(public_object_search_request) |
| 135 | + |
| 136 | + assert contact.results |
0 commit comments