-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdp): upsert in the hubspot function (#25362)
- Loading branch information
1 parent
a1abaf5
commit 1f7f5d6
Showing
4 changed files
with
37 additions
and
238 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 |
---|---|---|
|
@@ -4,94 +4,10 @@ | |
from inline_snapshot import snapshot | ||
|
||
from posthog.cdp.templates.google_cloud_storage.template_google_cloud_storage import TemplateGoogleCloudStorageMigrator | ||
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest | ||
from posthog.cdp.templates.hubspot.template_hubspot import template as template_hubspot | ||
from posthog.models import PluginConfig, PluginAttachment, Plugin, Integration | ||
from posthog.test.base import BaseTest | ||
|
||
|
||
class TestTemplateGoogleCloudStorage(BaseHogFunctionTemplateTest): | ||
template = template_hubspot | ||
|
||
def _inputs(self, **kwargs): | ||
inputs = { | ||
"oauth": {"access_token": "TOKEN"}, | ||
"email": "[email protected]", | ||
"properties": { | ||
"company": "PostHog", | ||
}, | ||
} | ||
inputs.update(kwargs) | ||
return inputs | ||
|
||
def test_function_works(self): | ||
self.mock_fetch_response = lambda *args: {"status": 200, "body": {"status": "success"}} # type: ignore | ||
|
||
res = self.run_function(inputs=self._inputs()) | ||
|
||
assert res.result is None | ||
|
||
assert self.get_mock_fetch_calls() == [ | ||
( | ||
"https://api.hubapi.com/crm/v3/objects/contacts", | ||
{ | ||
"method": "POST", | ||
"headers": {"Authorization": "Bearer TOKEN", "Content-Type": "application/json"}, | ||
"body": {"properties": {"company": "PostHog", "email": "[email protected]"}}, | ||
}, | ||
) | ||
] | ||
assert self.get_mock_print_calls() == [("Contact created successfully!",)] | ||
|
||
def test_exits_if_no_email(self): | ||
for email in [None, ""]: | ||
self.mock_print.reset_mock() | ||
res = self.run_function(inputs=self._inputs(email=email)) | ||
|
||
assert res.result is None | ||
assert self.get_mock_fetch_calls() == [] | ||
assert self.get_mock_print_calls() == [("`email` input is empty. Not creating a contact.",)] | ||
|
||
def test_handles_updates(self): | ||
call_count = 0 | ||
|
||
# First call respond with 409, second one 200 and increment call_count | ||
def mock_fetch(*args): | ||
nonlocal call_count | ||
call_count += 1 | ||
return ( | ||
{"status": 409, "body": {"message": "Contact already exists. Existing ID: 12345"}} | ||
if call_count == 1 | ||
else {"status": 200, "body": {"status": "success"}} | ||
) | ||
|
||
self.mock_fetch_response = mock_fetch # type: ignore | ||
|
||
res = self.run_function(inputs=self._inputs()) | ||
|
||
assert res.result is None | ||
|
||
assert len(self.get_mock_fetch_calls()) == 2 | ||
|
||
assert self.get_mock_fetch_calls()[0] == ( | ||
"https://api.hubapi.com/crm/v3/objects/contacts", | ||
{ | ||
"method": "POST", | ||
"headers": {"Authorization": "Bearer TOKEN", "Content-Type": "application/json"}, | ||
"body": {"properties": {"company": "PostHog", "email": "[email protected]"}}, | ||
}, | ||
) | ||
|
||
assert self.get_mock_fetch_calls()[1] == ( | ||
"https://api.hubapi.com/crm/v3/objects/contacts/12345", | ||
{ | ||
"method": "PATCH", | ||
"headers": {"Authorization": "Bearer TOKEN", "Content-Type": "application/json"}, | ||
"body": {"properties": {"company": "PostHog", "email": "[email protected]"}}, | ||
}, | ||
) | ||
|
||
|
||
class TestTemplateMigration(BaseTest): | ||
def get_plugin_config(self, config: dict): | ||
_config = { | ||
|
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 |
---|---|---|
|
@@ -4,94 +4,10 @@ | |
from inline_snapshot import snapshot | ||
|
||
from posthog.cdp.templates.google_pubsub.template_google_pubsub import TemplateGooglePubSubMigrator | ||
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest | ||
from posthog.cdp.templates.hubspot.template_hubspot import template as template_hubspot | ||
from posthog.models import PluginConfig, PluginAttachment, Plugin, Integration | ||
from posthog.test.base import BaseTest | ||
|
||
|
||
class TestTemplateGooglePubSub(BaseHogFunctionTemplateTest): | ||
template = template_hubspot | ||
|
||
def _inputs(self, **kwargs): | ||
inputs = { | ||
"oauth": {"access_token": "TOKEN"}, | ||
"email": "[email protected]", | ||
"properties": { | ||
"company": "PostHog", | ||
}, | ||
} | ||
inputs.update(kwargs) | ||
return inputs | ||
|
||
def test_function_works(self): | ||
self.mock_fetch_response = lambda *args: {"status": 200, "body": {"status": "success"}} # type: ignore | ||
|
||
res = self.run_function(inputs=self._inputs()) | ||
|
||
assert res.result is None | ||
|
||
assert self.get_mock_fetch_calls() == [ | ||
( | ||
"https://api.hubapi.com/crm/v3/objects/contacts", | ||
{ | ||
"method": "POST", | ||
"headers": {"Authorization": "Bearer TOKEN", "Content-Type": "application/json"}, | ||
"body": {"properties": {"company": "PostHog", "email": "[email protected]"}}, | ||
}, | ||
) | ||
] | ||
assert self.get_mock_print_calls() == [("Contact created successfully!",)] | ||
|
||
def test_exits_if_no_email(self): | ||
for email in [None, ""]: | ||
self.mock_print.reset_mock() | ||
res = self.run_function(inputs=self._inputs(email=email)) | ||
|
||
assert res.result is None | ||
assert self.get_mock_fetch_calls() == [] | ||
assert self.get_mock_print_calls() == [("`email` input is empty. Not creating a contact.",)] | ||
|
||
def test_handles_updates(self): | ||
call_count = 0 | ||
|
||
# First call respond with 409, second one 200 and increment call_count | ||
def mock_fetch(*args): | ||
nonlocal call_count | ||
call_count += 1 | ||
return ( | ||
{"status": 409, "body": {"message": "Contact already exists. Existing ID: 12345"}} | ||
if call_count == 1 | ||
else {"status": 200, "body": {"status": "success"}} | ||
) | ||
|
||
self.mock_fetch_response = mock_fetch # type: ignore | ||
|
||
res = self.run_function(inputs=self._inputs()) | ||
|
||
assert res.result is None | ||
|
||
assert len(self.get_mock_fetch_calls()) == 2 | ||
|
||
assert self.get_mock_fetch_calls()[0] == ( | ||
"https://api.hubapi.com/crm/v3/objects/contacts", | ||
{ | ||
"method": "POST", | ||
"headers": {"Authorization": "Bearer TOKEN", "Content-Type": "application/json"}, | ||
"body": {"properties": {"company": "PostHog", "email": "[email protected]"}}, | ||
}, | ||
) | ||
|
||
assert self.get_mock_fetch_calls()[1] == ( | ||
"https://api.hubapi.com/crm/v3/objects/contacts/12345", | ||
{ | ||
"method": "PATCH", | ||
"headers": {"Authorization": "Bearer TOKEN", "Content-Type": "application/json"}, | ||
"body": {"properties": {"company": "PostHog", "email": "[email protected]"}}, | ||
}, | ||
) | ||
|
||
|
||
class TestTemplateMigration(BaseTest): | ||
def get_plugin_config(self, config: dict): | ||
_config = { | ||
|
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
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 |
---|---|---|
|
@@ -29,15 +29,23 @@ def test_function_works(self): | |
|
||
assert self.get_mock_fetch_calls() == [ | ||
( | ||
"https://api.hubapi.com/crm/v3/objects/contacts", | ||
"https://api.hubapi.com/crm/v3/objects/contacts/batch/upsert", | ||
{ | ||
"method": "POST", | ||
"headers": {"Authorization": "Bearer TOKEN", "Content-Type": "application/json"}, | ||
"body": {"properties": {"company": "PostHog", "email": "[email protected]"}}, | ||
"body": { | ||
"inputs": [ | ||
{ | ||
"properties": {"company": "PostHog", "email": "[email protected]"}, | ||
"id": "[email protected]", | ||
"idProperty": "email", | ||
} | ||
] | ||
}, | ||
}, | ||
) | ||
] | ||
assert self.get_mock_print_calls() == [("Contact created successfully!",)] | ||
assert self.get_mock_print_calls() == [("Contact [email protected] updated successfully!",)] | ||
|
||
def test_exits_if_no_email(self): | ||
for email in [None, ""]: | ||
|
@@ -48,45 +56,6 @@ def test_exits_if_no_email(self): | |
assert self.get_mock_fetch_calls() == [] | ||
assert self.get_mock_print_calls() == [("`email` input is empty. Not creating a contact.",)] | ||
|
||
def test_handles_updates(self): | ||
call_count = 0 | ||
|
||
# First call respond with 409, second one 200 and increment call_count | ||
def mock_fetch(*args): | ||
nonlocal call_count | ||
call_count += 1 | ||
return ( | ||
{"status": 409, "body": {"message": "Contact already exists. Existing ID: 12345"}} | ||
if call_count == 1 | ||
else {"status": 200, "body": {"status": "success"}} | ||
) | ||
|
||
self.mock_fetch_response = mock_fetch # type: ignore | ||
|
||
res = self.run_function(inputs=self._inputs()) | ||
|
||
assert res.result is None | ||
|
||
assert len(self.get_mock_fetch_calls()) == 2 | ||
|
||
assert self.get_mock_fetch_calls()[0] == ( | ||
"https://api.hubapi.com/crm/v3/objects/contacts", | ||
{ | ||
"method": "POST", | ||
"headers": {"Authorization": "Bearer TOKEN", "Content-Type": "application/json"}, | ||
"body": {"properties": {"company": "PostHog", "email": "[email protected]"}}, | ||
}, | ||
) | ||
|
||
assert self.get_mock_fetch_calls()[1] == ( | ||
"https://api.hubapi.com/crm/v3/objects/contacts/12345", | ||
{ | ||
"method": "PATCH", | ||
"headers": {"Authorization": "Bearer TOKEN", "Content-Type": "application/json"}, | ||
"body": {"properties": {"company": "PostHog", "email": "[email protected]"}}, | ||
}, | ||
) | ||
|
||
|
||
class TestTemplateMigration(BaseTest): | ||
def get_plugin_config(self, config: dict): | ||
|