Skip to content

Commit 78f2967

Browse files
committed
Closer
1 parent fa0350b commit 78f2967

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

posthog/test/test_client.py

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import mock
77
import six
8+
from parameterized import parameterized
89

910
from posthog.client import Client
1011
from posthog.request import APIError
@@ -54,10 +55,11 @@ def test_basic_capture(self):
5455
self.assertEqual(msg["distinct_id"], "distinct_id")
5556
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
5657
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
57-
assert msg["properties"]["$python_runtime"] == "CPython"
58-
assert msg["properties"]["$python_version"] == "3.11.11"
59-
assert msg["properties"]["$os"] == "Mac OS X"
60-
assert msg["properties"]["$os_version"] == "15.3.1"
58+
# these will change between platforms so just asssert on presence here
59+
assert msg["properties"]["$python_runtime"] == mock.ANY
60+
assert msg["properties"]["$python_version"] == mock.ANY
61+
assert msg["properties"]["$os"] == mock.ANY
62+
assert msg["properties"]["$os_version"] == mock.ANY
6163

6264
def test_basic_capture_with_uuid(self):
6365
client = self.client
@@ -1128,3 +1130,88 @@ def test_default_properties_get_added_properly(self, patch_decide):
11281130
group_properties={},
11291131
disable_geoip=False,
11301132
)
1133+
1134+
@parameterized.expand([
1135+
# name, sys_platform, version_info, expected_runtime, expected_version, expected_os, expected_os_version, platform_method, platform_return, distro_info
1136+
(
1137+
"macOS",
1138+
"darwin",
1139+
(3, 8, 10),
1140+
"MockPython",
1141+
"3.8.10",
1142+
"Mac OS X",
1143+
"10.15.7",
1144+
"mac_ver",
1145+
("10.15.7", "", ""),
1146+
None,
1147+
),
1148+
(
1149+
"Windows",
1150+
"win32",
1151+
(3, 8, 10),
1152+
"MockPython",
1153+
"3.8.10",
1154+
"Windows",
1155+
"10",
1156+
"win32_ver",
1157+
("10", "", "", ""),
1158+
None,
1159+
),
1160+
(
1161+
"Linux",
1162+
"linux",
1163+
(3, 8, 10),
1164+
"MockPython",
1165+
"3.8.10",
1166+
"Linux",
1167+
"20.04",
1168+
None,
1169+
None,
1170+
{"version": "20.04"},
1171+
),
1172+
])
1173+
def test_mock_system_context(
1174+
self,
1175+
_name,
1176+
sys_platform,
1177+
version_info,
1178+
expected_runtime,
1179+
expected_version,
1180+
expected_os,
1181+
expected_os_version,
1182+
platform_method,
1183+
platform_return,
1184+
distro_info
1185+
):
1186+
"""Test that we can mock platform and sys for testing system_context"""
1187+
with mock.patch('posthog.client.platform') as mock_platform:
1188+
with mock.patch('posthog.client.sys') as mock_sys:
1189+
# Set up common mocks
1190+
mock_platform.python_implementation.return_value = expected_runtime
1191+
mock_sys.version_info = version_info
1192+
mock_sys.platform = sys_platform
1193+
1194+
# Set up platform-specific mocks
1195+
if platform_method:
1196+
getattr(mock_platform, platform_method).return_value = platform_return
1197+
1198+
# Special handling for Linux which uses distro module
1199+
if sys_platform == "linux":
1200+
with mock.patch.dict('sys.modules', {'distro': mock.MagicMock()}):
1201+
import sys
1202+
mock_distro = sys.modules['distro']
1203+
mock_distro.info.return_value = distro_info
1204+
1205+
# Get system context
1206+
from posthog.client import system_context
1207+
context = system_context()
1208+
else:
1209+
# Get system context for non-Linux platforms
1210+
from posthog.client import system_context
1211+
context = system_context()
1212+
1213+
# Verify results
1214+
self.assertEqual(context["$python_runtime"], expected_runtime)
1215+
self.assertEqual(context["$python_version"], expected_version)
1216+
self.assertEqual(context["$os"], expected_os)
1217+
self.assertEqual(context["$os_version"], expected_os_version)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"langchain-openai>=0.2.0",
5959
"langchain-anthropic>=0.2.0",
6060
"pydantic",
61+
"parameterized>=0.8.1",
6162
],
6263
"sentry": ["sentry-sdk", "django"],
6364
"langchain": ["langchain>=0.2.0"],

0 commit comments

Comments
 (0)