Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarconLP committed Oct 24, 2024
1 parent 91ee78f commit 73dae0a
Show file tree
Hide file tree
Showing 2 changed files with 356 additions and 11 deletions.
24 changes: 13 additions & 11 deletions posthog/cdp/templates/june/template_june.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
if (event.event in ('$identify', '$set')) {
type := 'identify'
} else if (event.event == '$pageview') {
} else if (event.event in ('$pageview', '$screen')) {
type := 'page'
} else if (event.event == '$screen') {
type := 'screen'
}
let context := {
Expand All @@ -30,12 +28,12 @@
if (not empty(event.properties.$app_build)) context.app.build := event.properties.$app_build
if (not empty(event.properties.$app_version)) context.app.version := event.properties.$app_version
if (not empty(event.properties.$app_name)) context.app.name := event.properties.$app_name
if (not empty(event.properties.utm_campaign)) context.device.name := event.properties.utm_campaign
if (not empty(event.properties.utm_content)) context.device.content := event.properties.utm_content
if (not empty(event.properties.utm_medium)) context.device.medium := event.properties.utm_medium
if (not empty(event.properties.utm_source)) context.device.source := event.properties.utm_source
if (not empty(event.properties.utm_term)) context.device.term := event.properties.utm_term
if (not empty(event.properties.$device_id)) context.device.id := event.properties.$device_id
if (not empty(event.properties.utm_campaign)) context.campaign.name := event.properties.utm_campaign
if (not empty(event.properties.utm_content)) context.campaign.content := event.properties.utm_content
if (not empty(event.properties.utm_medium)) context.campaign.medium := event.properties.utm_medium
if (not empty(event.properties.utm_source)) context.campaign.source := event.properties.utm_source
if (not empty(event.properties.utm_term)) context.campaign.term := event.properties.utm_term
if (not empty(event.properties.$device_id)) context.device.id := event.properties.$device_id
if (not empty(event.properties.$device_manufacturer)) context.device.manufacturer := event.properties.$device_manufacturer
if (not empty(event.properties.$device_model)) context.device.model := event.properties.$device_model
if (not empty(event.properties.$os_name)) context.device.name := event.properties.$os_name
Expand All @@ -57,11 +55,15 @@
if (not empty(event.properties.$pathname)) properties.path := event.properties.$pathname
if (not empty(event.properties.title)) properties.title := event.properties.title
if (not empty(event.properties.$referrer)) properties.referrer := event.properties.$referrer
if (not empty(event.properties.$current_url) and not empty(splitByString('?', event.properties.$current_url)[2])) properties := f'?{splitByString('?', event.properties.$current_url)[2]}'
if (not empty(event.properties.$current_url)) {
if (not empty(splitByString('?', event.properties.$current_url)[2])) {
properties.search := f'?{splitByString('?', event.properties.$current_url)[2]}'
}
}
let traits := {}
for (let key, value in properties) {
for (let key, value in inputs.properties) {
if (not empty(value)) {
traits[key] := value
}
Expand Down
343 changes: 343 additions & 0 deletions posthog/cdp/templates/june/test_june_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
from inline_snapshot import snapshot
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest
from posthog.cdp.templates.june.template_june import (
template as template_june,
)


def create_inputs(**kwargs):
inputs = {
"apiKey": "abcdef123456",
"include_all_properties": False,
"properties": {"name": "Max AI", "email": "[email protected]"},
}
inputs.update(kwargs)

return inputs


class TestTemplateJune(BaseHogFunctionTemplateTest):
template = template_june

def test_function_works(self):
self.run_function(
inputs=create_inputs(),
globals={
"event": {
"event": "$pageview",
"uuid": "151234234",
"distinct_id": "abc123",
"timestamp": "2024-10-24T23:03:50.941Z",
"properties": {
"$is_identified": True,
"$app_build": "1.0.0",
"$app_version": "2.0",
"$app_name": "PostHog",
"utm_campaign": "test1",
"utm_content": "test2",
"utm_medium": "test3",
"utm_source": "test4",
"utm_term": "test5",
"$device_id": "test6",
"$device_manufacturer": "test7",
"$device_model": "test8",
"$os_name": "test9",
"$os_version": "test10",
"$device_type": "test11",
"$ip": "test12",
"$browser_language": "test13",
"$os": "test14",
"$referrer": "test15",
"$screen_height": "test16",
"$screen_width": "test17",
"$geoip_time_zone": "test18",
"$raw_user_agent": "test19",
"$current_url": "https://hedgebox.net/faq?billing",
"$pathname": "/faq",
"title": "Hedgebox",
},
},
},
)

assert self.get_mock_fetch_calls()[0] == snapshot(
(
"https://api.june.so/sdk/page",
{
"method": "POST",
"headers": {
"Authorization": "Basic abcdef123456",
"Content-Type": "application/json",
},
"body": {
"properties": {
"url": "https://hedgebox.net/faq?billing",
"path": "/faq",
"title": "Hedgebox",
"referrer": "test15",
"search": "?billing",
},
"traits": {"name": "Max AI", "email": "[email protected]"},
"timestamp": "2024-10-24T23:03:50.941Z",
"context": {
"app": {"build": "1.0.0", "version": "2.0", "name": "PostHog"},
"campaign": {
"name": "test1",
"content": "test2",
"medium": "test3",
"source": "test4",
"term": "test5",
},
"device": {
"id": "test6",
"manufacturer": "test7",
"model": "test8",
"name": "test9",
"version": "test10",
"type": "test11",
},
"os": {"name": "test14", "version": "test10"},
"referrer": {"url": "test15"},
"screen": {
"height": "test16",
"width": "test17",
},
"ip": "test12",
"locale": "test13",
"timezone": "test18",
"userAgent": "test19",
},
"messageId": "151234234",
"userId": "abc123",
},
},
)
)

def test_body_includes_all_properties_if_set(self):
self.run_function(
inputs=create_inputs(include_all_properties=True),
globals={
"event": {
"event": "$pageview",
"uuid": "151234234",
"distinct_id": "abc123",
"timestamp": "2024-10-24T23:03:50.941Z",
"properties": {
"$is_identified": True,
"$current_url": "https://hedgebox.net/faq?billing",
"$pathname": "/faq",
"title": "Hedgebox",
},
},
},
)

assert self.get_mock_fetch_calls()[0] == snapshot(
(
"https://api.june.so/sdk/page",
{
"method": "POST",
"headers": {
"Authorization": "Basic abcdef123456",
"Content-Type": "application/json",
},
"body": {
"properties": {
"url": "https://hedgebox.net/faq?billing",
"path": "/faq",
"title": "Hedgebox",
"search": "?billing",
},
"traits": {"name": "Max AI", "email": "[email protected]", "title": "Hedgebox"},
"timestamp": "2024-10-24T23:03:50.941Z",
"context": {
"app": {},
"campaign": {},
"device": {},
"os": {},
"referrer": {},
"screen": {},
},
"messageId": "151234234",
"userId": "abc123",
},
},
)
)

self.run_function(
inputs=create_inputs(include_all_properties=False),
globals={
"event": {
"event": "$pageview",
"uuid": "151234234",
"distinct_id": "abc123",
"timestamp": "2024-10-24T23:03:50.941Z",
"properties": {
"$is_identified": True,
"$current_url": "https://hedgebox.net/faq?billing",
"$pathname": "/faq",
"title": "Hedgebox",
},
},
},
)

assert self.get_mock_fetch_calls()[0] == snapshot(
(
"https://api.june.so/sdk/page",
{
"method": "POST",
"headers": {
"Authorization": "Basic abcdef123456",
"Content-Type": "application/json",
},
"body": {
"properties": {
"url": "https://hedgebox.net/faq?billing",
"path": "/faq",
"title": "Hedgebox",
"search": "?billing",
},
"traits": {
"name": "Max AI",
"email": "[email protected]",
},
"timestamp": "2024-10-24T23:03:50.941Z",
"context": {
"app": {},
"campaign": {},
"device": {},
"os": {},
"referrer": {},
"screen": {},
},
"messageId": "151234234",
"userId": "abc123",
},
},
)
)

def test_automatic_type_mapping(self):
for event_name, expected_type in [
("$identify", "identify"),
("$set", "identify"),
("$pageview", "page"),
("$screen", "page"),
("$autocapture", "track"),
("custom", "track"),
]:
self.run_function(
inputs=create_inputs(),
globals={
"event": {"event": event_name, "properties": {"$current_url": "https://example.com"}},
},
)

assert self.get_mock_fetch_calls()[0][0] == "https://api.june.so/sdk/" + expected_type

def test_identified_tracking(self):
self.run_function(
inputs=create_inputs(),
globals={
"event": {
"event": "$pageview",
"uuid": "151234234",
"distinct_id": "abc123",
"timestamp": "2024-10-24T23:03:50.941Z",
"properties": {
"$is_identified": True,
"$current_url": "https://hedgebox.net/faq?billing",
"$pathname": "/faq",
"title": "Hedgebox",
},
},
},
)

assert self.get_mock_fetch_calls()[0] == snapshot(
(
"https://api.june.so/sdk/page",
{
"method": "POST",
"headers": {
"Authorization": "Basic abcdef123456",
"Content-Type": "application/json",
},
"body": {
"properties": {
"url": "https://hedgebox.net/faq?billing",
"path": "/faq",
"title": "Hedgebox",
"search": "?billing",
},
"traits": {"name": "Max AI", "email": "[email protected]"},
"timestamp": "2024-10-24T23:03:50.941Z",
"context": {
"app": {},
"campaign": {},
"device": {},
"os": {},
"referrer": {},
"screen": {},
},
"messageId": "151234234",
"userId": "abc123",
},
},
)
)

self.run_function(
inputs=create_inputs(),
globals={
"event": {
"event": "$pageview",
"uuid": "151234234",
"distinct_id": "abc123",
"timestamp": "2024-10-24T23:03:50.941Z",
"properties": {
"$is_identified": False,
"$current_url": "https://hedgebox.net/faq?billing",
"$pathname": "/faq",
"title": "Hedgebox",
"$anon_distinct_id": "12345678abc",
},
},
},
)

assert self.get_mock_fetch_calls()[0] == snapshot(
(
"https://api.june.so/sdk/page",
{
"method": "POST",
"headers": {
"Authorization": "Basic abcdef123456",
"Content-Type": "application/json",
},
"body": {
"properties": {
"url": "https://hedgebox.net/faq?billing",
"path": "/faq",
"title": "Hedgebox",
"search": "?billing",
},
"traits": {"name": "Max AI", "email": "[email protected]"},
"timestamp": "2024-10-24T23:03:50.941Z",
"context": {
"app": {},
"campaign": {},
"device": {},
"os": {},
"referrer": {},
"screen": {},
},
"messageId": "151234234",
"anonymousId": "abc123",
},
},
)
)

0 comments on commit 73dae0a

Please sign in to comment.