|
5 | 5 |
|
6 | 6 | import mock |
7 | 7 | import six |
| 8 | +from parameterized import parameterized |
8 | 9 |
|
9 | 10 | from posthog.client import Client |
10 | 11 | from posthog.request import APIError |
@@ -54,10 +55,11 @@ def test_basic_capture(self): |
54 | 55 | self.assertEqual(msg["distinct_id"], "distinct_id") |
55 | 56 | self.assertEqual(msg["properties"]["$lib"], "posthog-python") |
56 | 57 | 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 |
61 | 63 |
|
62 | 64 | def test_basic_capture_with_uuid(self): |
63 | 65 | client = self.client |
@@ -1128,3 +1130,88 @@ def test_default_properties_get_added_properly(self, patch_decide): |
1128 | 1130 | group_properties={}, |
1129 | 1131 | disable_geoip=False, |
1130 | 1132 | ) |
| 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) |
0 commit comments