|
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,6 +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)
|
| 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 |
57 | 63 |
|
58 | 64 | def test_basic_capture_with_uuid(self):
|
59 | 65 | client = self.client
|
@@ -101,7 +107,6 @@ def test_basic_super_properties(self):
|
101 | 107 | self.assertEqual(msg["properties"]["source"], "repo-name")
|
102 | 108 |
|
103 | 109 | def test_basic_capture_exception(self):
|
104 |
| - |
105 | 110 | with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
|
106 | 111 | client = self.client
|
107 | 112 | exception = Exception("test exception")
|
@@ -129,7 +134,6 @@ def test_basic_capture_exception(self):
|
129 | 134 | )
|
130 | 135 |
|
131 | 136 | def test_basic_capture_exception_with_distinct_id(self):
|
132 |
| - |
133 | 137 | with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
|
134 | 138 | client = self.client
|
135 | 139 | exception = Exception("test exception")
|
@@ -157,7 +161,6 @@ def test_basic_capture_exception_with_distinct_id(self):
|
157 | 161 | )
|
158 | 162 |
|
159 | 163 | def test_basic_capture_exception_with_correct_host_generation(self):
|
160 |
| - |
161 | 164 | with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
|
162 | 165 | client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, host="https://aloha.com")
|
163 | 166 | exception = Exception("test exception")
|
@@ -185,7 +188,6 @@ def test_basic_capture_exception_with_correct_host_generation(self):
|
185 | 188 | )
|
186 | 189 |
|
187 | 190 | def test_basic_capture_exception_with_correct_host_generation_for_server_hosts(self):
|
188 |
| - |
189 | 191 | with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
|
190 | 192 | client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, host="https://app.posthog.com")
|
191 | 193 | exception = Exception("test exception")
|
@@ -213,7 +215,6 @@ def test_basic_capture_exception_with_correct_host_generation_for_server_hosts(s
|
213 | 215 | )
|
214 | 216 |
|
215 | 217 | def test_basic_capture_exception_with_no_exception_given(self):
|
216 |
| - |
217 | 218 | with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
|
218 | 219 | client = self.client
|
219 | 220 | try:
|
@@ -250,10 +251,8 @@ def test_basic_capture_exception_with_no_exception_given(self):
|
250 | 251 | self.assertEqual(capture_call[2]["$exception_list"][0]["stacktrace"]["frames"][0]["in_app"], True)
|
251 | 252 |
|
252 | 253 | def test_basic_capture_exception_with_no_exception_happening(self):
|
253 |
| - |
254 | 254 | with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
|
255 | 255 | with self.assertLogs("posthog", level="WARNING") as logs:
|
256 |
| - |
257 | 256 | client = self.client
|
258 | 257 | client.capture_exception()
|
259 | 258 |
|
@@ -1124,3 +1123,92 @@ def test_default_properties_get_added_properly(self, patch_decide):
|
1124 | 1123 | group_properties={},
|
1125 | 1124 | disable_geoip=False,
|
1126 | 1125 | )
|
| 1126 | + |
| 1127 | + @parameterized.expand( |
| 1128 | + [ |
| 1129 | + # name, sys_platform, version_info, expected_runtime, expected_version, expected_os, expected_os_version, platform_method, platform_return, distro_info |
| 1130 | + ( |
| 1131 | + "macOS", |
| 1132 | + "darwin", |
| 1133 | + (3, 8, 10), |
| 1134 | + "MockPython", |
| 1135 | + "3.8.10", |
| 1136 | + "Mac OS X", |
| 1137 | + "10.15.7", |
| 1138 | + "mac_ver", |
| 1139 | + ("10.15.7", "", ""), |
| 1140 | + None, |
| 1141 | + ), |
| 1142 | + ( |
| 1143 | + "Windows", |
| 1144 | + "win32", |
| 1145 | + (3, 8, 10), |
| 1146 | + "MockPython", |
| 1147 | + "3.8.10", |
| 1148 | + "Windows", |
| 1149 | + "10", |
| 1150 | + "win32_ver", |
| 1151 | + ("10", "", "", ""), |
| 1152 | + None, |
| 1153 | + ), |
| 1154 | + ( |
| 1155 | + "Linux", |
| 1156 | + "linux", |
| 1157 | + (3, 8, 10), |
| 1158 | + "MockPython", |
| 1159 | + "3.8.10", |
| 1160 | + "Linux", |
| 1161 | + "20.04", |
| 1162 | + None, |
| 1163 | + None, |
| 1164 | + {"version": "20.04"}, |
| 1165 | + ), |
| 1166 | + ] |
| 1167 | + ) |
| 1168 | + def test_mock_system_context( |
| 1169 | + self, |
| 1170 | + _name, |
| 1171 | + sys_platform, |
| 1172 | + version_info, |
| 1173 | + expected_runtime, |
| 1174 | + expected_version, |
| 1175 | + expected_os, |
| 1176 | + expected_os_version, |
| 1177 | + platform_method, |
| 1178 | + platform_return, |
| 1179 | + distro_info, |
| 1180 | + ): |
| 1181 | + """Test that we can mock platform and sys for testing system_context""" |
| 1182 | + with mock.patch("posthog.client.platform") as mock_platform: |
| 1183 | + with mock.patch("posthog.client.sys") as mock_sys: |
| 1184 | + # Set up common mocks |
| 1185 | + mock_platform.python_implementation.return_value = expected_runtime |
| 1186 | + mock_sys.version_info = version_info |
| 1187 | + mock_sys.platform = sys_platform |
| 1188 | + |
| 1189 | + # Set up platform-specific mocks |
| 1190 | + if platform_method: |
| 1191 | + getattr(mock_platform, platform_method).return_value = platform_return |
| 1192 | + |
| 1193 | + # Special handling for Linux which uses distro module |
| 1194 | + if sys_platform == "linux": |
| 1195 | + # Directly patch the get_os_info function to return our expected values |
| 1196 | + with mock.patch("posthog.client.get_os_info", return_value=(expected_os, expected_os_version)): |
| 1197 | + from posthog.client import system_context |
| 1198 | + |
| 1199 | + context = system_context() |
| 1200 | + else: |
| 1201 | + # Get system context for non-Linux platforms |
| 1202 | + from posthog.client import system_context |
| 1203 | + |
| 1204 | + context = system_context() |
| 1205 | + |
| 1206 | + # Verify results |
| 1207 | + expected_context = { |
| 1208 | + "$python_runtime": expected_runtime, |
| 1209 | + "$python_version": expected_version, |
| 1210 | + "$os": expected_os, |
| 1211 | + "$os_version": expected_os_version, |
| 1212 | + } |
| 1213 | + |
| 1214 | + assert context == expected_context |
0 commit comments