posthoganalytics 5.4.0__py3-none-any.whl → 6.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ import time
2
2
  import unittest
3
3
  from datetime import datetime
4
4
  from uuid import uuid4
5
- from posthoganalytics.scopes import get_context_session_id, set_context_session, new_context
5
+ from posthoganalytics.contexts import get_context_session_id, set_context_session, new_context
6
6
 
7
7
  import mock
8
8
  import six
@@ -39,74 +39,98 @@ class TestClient(unittest.TestCase):
39
39
  self.client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail)
40
40
 
41
41
  def test_requires_api_key(self):
42
- self.assertRaises(AssertionError, Client)
42
+ self.assertRaises(TypeError, Client)
43
43
 
44
44
  def test_empty_flush(self):
45
45
  self.client.flush()
46
46
 
47
47
  def test_basic_capture(self):
48
- client = self.client
49
- success, msg = client.capture("distinct_id", "python test event")
50
- client.flush()
51
- self.assertTrue(success)
52
- self.assertFalse(self.failed)
53
-
54
- self.assertEqual(msg["event"], "python test event")
55
- self.assertTrue(isinstance(msg["timestamp"], str))
56
- self.assertIsNone(msg.get("uuid"))
57
- self.assertEqual(msg["distinct_id"], "distinct_id")
58
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
59
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
60
- # these will change between platforms so just asssert on presence here
61
- assert msg["properties"]["$python_runtime"] == mock.ANY
62
- assert msg["properties"]["$python_version"] == mock.ANY
63
- assert msg["properties"]["$os"] == mock.ANY
64
- assert msg["properties"]["$os_version"] == mock.ANY
48
+ with mock.patch("posthog.client.batch_post") as mock_post:
49
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
50
+ msg_uuid = client.capture("python test event", distinct_id="distinct_id")
51
+ self.assertIsNotNone(msg_uuid)
52
+ self.assertFalse(self.failed)
53
+
54
+ # Get the enqueued message from the mock
55
+ mock_post.assert_called_once()
56
+ batch_data = mock_post.call_args[1]["batch"]
57
+ msg = batch_data[0]
58
+
59
+ self.assertEqual(msg["event"], "python test event")
60
+ self.assertTrue(isinstance(msg["timestamp"], str))
61
+ self.assertIsNotNone(msg.get("uuid"))
62
+ self.assertEqual(msg["distinct_id"], "distinct_id")
63
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
64
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
65
+ # these will change between platforms so just asssert on presence here
66
+ assert msg["properties"]["$python_runtime"] == mock.ANY
67
+ assert msg["properties"]["$python_version"] == mock.ANY
68
+ assert msg["properties"]["$os"] == mock.ANY
69
+ assert msg["properties"]["$os_version"] == mock.ANY
65
70
 
66
71
  def test_basic_capture_with_uuid(self):
67
- client = self.client
68
- uuid = str(uuid4())
69
- success, msg = client.capture("distinct_id", "python test event", uuid=uuid)
70
- client.flush()
71
- self.assertTrue(success)
72
- self.assertFalse(self.failed)
72
+ with mock.patch("posthog.client.batch_post") as mock_post:
73
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
74
+ uuid = str(uuid4())
75
+ msg_uuid = client.capture(
76
+ "python test event", distinct_id="distinct_id", uuid=uuid
77
+ )
78
+ self.assertEqual(msg_uuid, uuid)
79
+ self.assertFalse(self.failed)
80
+
81
+ # Get the enqueued message from the mock
82
+ mock_post.assert_called_once()
83
+ batch_data = mock_post.call_args[1]["batch"]
84
+ msg = batch_data[0]
73
85
 
74
- self.assertEqual(msg["event"], "python test event")
75
- self.assertTrue(isinstance(msg["timestamp"], str))
76
- self.assertEqual(msg["uuid"], uuid)
77
- self.assertEqual(msg["distinct_id"], "distinct_id")
78
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
79
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
86
+ self.assertEqual(msg["event"], "python test event")
87
+ self.assertTrue(isinstance(msg["timestamp"], str))
88
+ self.assertEqual(msg["uuid"], uuid)
89
+ self.assertEqual(msg["distinct_id"], "distinct_id")
90
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
91
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
80
92
 
81
93
  def test_basic_capture_with_project_api_key(self):
82
- client = Client(project_api_key=FAKE_TEST_API_KEY, on_error=self.set_fail)
94
+ with mock.patch("posthog.client.batch_post") as mock_post:
95
+ client = Client(
96
+ project_api_key=FAKE_TEST_API_KEY,
97
+ on_error=self.set_fail,
98
+ sync_mode=True,
99
+ )
83
100
 
84
- success, msg = client.capture("distinct_id", "python test event")
85
- client.flush()
86
- self.assertTrue(success)
87
- self.assertFalse(self.failed)
101
+ msg_uuid = client.capture("python test event", distinct_id="distinct_id")
102
+ self.assertIsNotNone(msg_uuid)
103
+ self.assertFalse(self.failed)
88
104
 
89
- self.assertEqual(msg["event"], "python test event")
90
- self.assertTrue(isinstance(msg["timestamp"], str))
91
- self.assertIsNone(msg.get("uuid"))
92
- self.assertEqual(msg["distinct_id"], "distinct_id")
93
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
94
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
105
+ # Get the enqueued message from the mock
106
+ mock_post.assert_called_once()
107
+ batch_data = mock_post.call_args[1]["batch"]
108
+ msg = batch_data[0]
95
109
 
96
- def test_basic_super_properties(self):
97
- client = Client(FAKE_TEST_API_KEY, super_properties={"source": "repo-name"})
110
+ self.assertEqual(msg["event"], "python test event")
111
+ self.assertTrue(isinstance(msg["timestamp"], str))
112
+ self.assertEqual(msg["distinct_id"], "distinct_id")
113
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
114
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
98
115
 
99
- _, msg = client.capture("distinct_id", "python test event")
100
- client.flush()
116
+ def test_basic_super_properties(self):
117
+ with mock.patch("posthog.client.batch_post") as mock_post:
118
+ client = Client(
119
+ FAKE_TEST_API_KEY,
120
+ super_properties={"source": "repo-name"},
121
+ sync_mode=True,
122
+ )
101
123
 
102
- self.assertEqual(msg["event"], "python test event")
103
- self.assertEqual(msg["properties"]["source"], "repo-name")
124
+ msg_uuid = client.capture("python test event", distinct_id="distinct_id")
125
+ self.assertIsNotNone(msg_uuid)
104
126
 
105
- _, msg = client.identify("distinct_id", {"trait": "value"})
106
- client.flush()
127
+ # Check the enqueued message
128
+ mock_post.assert_called_once()
129
+ batch_data = mock_post.call_args[1]["batch"]
130
+ msg = batch_data[0]
107
131
 
108
- self.assertEqual(msg["$set"]["trait"], "value")
109
- self.assertEqual(msg["properties"]["source"], "repo-name")
132
+ self.assertEqual(msg["event"], "python test event")
133
+ self.assertEqual(msg["properties"]["source"], "repo-name")
110
134
 
111
135
  def test_basic_capture_exception(self):
112
136
  with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
@@ -115,20 +139,20 @@ class TestClient(unittest.TestCase):
115
139
  client.capture_exception(exception, distinct_id="distinct_id")
116
140
 
117
141
  self.assertTrue(patch_capture.called)
118
- capture_call = patch_capture.call_args[0]
119
- self.assertEqual(capture_call[0], "distinct_id")
120
- self.assertEqual(capture_call[1], "$exception")
142
+ capture_call = patch_capture.call_args
143
+ self.assertEqual(capture_call[0][0], "$exception")
144
+ self.assertEqual(capture_call[1]["distinct_id"], "distinct_id")
121
145
 
122
146
  def test_basic_capture_exception_with_distinct_id(self):
123
147
  with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
124
148
  client = self.client
125
149
  exception = Exception("test exception")
126
- client.capture_exception(exception, "distinct_id")
150
+ client.capture_exception(exception, distinct_id="distinct_id")
127
151
 
128
152
  self.assertTrue(patch_capture.called)
129
- capture_call = patch_capture.call_args[0]
130
- self.assertEqual(capture_call[0], "distinct_id")
131
- self.assertEqual(capture_call[1], "$exception")
153
+ capture_call = patch_capture.call_args
154
+ self.assertEqual(capture_call[0][0], "$exception")
155
+ self.assertEqual(capture_call[1]["distinct_id"], "distinct_id")
132
156
 
133
157
  def test_basic_capture_exception_with_correct_host_generation(self):
134
158
  with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
@@ -136,12 +160,12 @@ class TestClient(unittest.TestCase):
136
160
  FAKE_TEST_API_KEY, on_error=self.set_fail, host="https://aloha.com"
137
161
  )
138
162
  exception = Exception("test exception")
139
- client.capture_exception(exception, "distinct_id")
163
+ client.capture_exception(exception, distinct_id="distinct_id")
140
164
 
141
165
  self.assertTrue(patch_capture.called)
142
- capture_call = patch_capture.call_args[0]
143
- self.assertEqual(capture_call[0], "distinct_id")
144
- self.assertEqual(capture_call[1], "$exception")
166
+ call = patch_capture.call_args
167
+ self.assertEqual(call[0][0], "$exception")
168
+ self.assertEqual(call[1]["distinct_id"], "distinct_id")
145
169
 
146
170
  def test_basic_capture_exception_with_correct_host_generation_for_server_hosts(
147
171
  self,
@@ -153,12 +177,12 @@ class TestClient(unittest.TestCase):
153
177
  host="https://app.posthog.com",
154
178
  )
155
179
  exception = Exception("test exception")
156
- client.capture_exception(exception, "distinct_id")
180
+ client.capture_exception(exception, distinct_id="distinct_id")
157
181
 
158
182
  self.assertTrue(patch_capture.called)
159
- capture_call = patch_capture.call_args[0]
160
- self.assertEqual(capture_call[0], "distinct_id")
161
- self.assertEqual(capture_call[1], "$exception")
183
+ capture_call = patch_capture.call_args
184
+ self.assertEqual(capture_call[0][0], "$exception")
185
+ self.assertEqual(capture_call[1]["distinct_id"], "distinct_id")
162
186
 
163
187
  def test_basic_capture_exception_with_no_exception_given(self):
164
188
  with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
@@ -166,51 +190,69 @@ class TestClient(unittest.TestCase):
166
190
  try:
167
191
  raise Exception("test exception")
168
192
  except Exception:
169
- client.capture_exception(distinct_id="distinct_id")
193
+ client.capture_exception(None, distinct_id="distinct_id")
170
194
 
171
195
  self.assertTrue(patch_capture.called)
172
- capture_call = patch_capture.call_args[0]
173
- self.assertEqual(capture_call[0], "distinct_id")
174
- self.assertEqual(capture_call[1], "$exception")
175
- self.assertEqual(capture_call[2]["$exception_type"], "Exception")
176
- self.assertEqual(capture_call[2]["$exception_message"], "test exception")
196
+ capture_call = patch_capture.call_args
197
+ print(capture_call)
198
+ self.assertEqual(capture_call[1]["distinct_id"], "distinct_id")
199
+ self.assertEqual(capture_call[0][0], "$exception")
177
200
  self.assertEqual(
178
- capture_call[2]["$exception_list"][0]["mechanism"]["type"], "generic"
201
+ capture_call[1]["properties"]["$exception_type"], "Exception"
179
202
  )
180
203
  self.assertEqual(
181
- capture_call[2]["$exception_list"][0]["mechanism"]["handled"], True
204
+ capture_call[1]["properties"]["$exception_message"], "test exception"
182
205
  )
183
- self.assertEqual(capture_call[2]["$exception_list"][0]["module"], None)
184
- self.assertEqual(capture_call[2]["$exception_list"][0]["type"], "Exception")
185
206
  self.assertEqual(
186
- capture_call[2]["$exception_list"][0]["value"], "test exception"
207
+ capture_call[1]["properties"]["$exception_list"][0]["mechanism"][
208
+ "type"
209
+ ],
210
+ "generic",
187
211
  )
188
212
  self.assertEqual(
189
- capture_call[2]["$exception_list"][0]["stacktrace"]["type"],
190
- "raw",
213
+ capture_call[1]["properties"]["$exception_list"][0]["mechanism"][
214
+ "handled"
215
+ ],
216
+ True,
217
+ )
218
+ self.assertEqual(
219
+ capture_call[1]["properties"]["$exception_list"][0]["module"], None
191
220
  )
192
221
  self.assertEqual(
193
- capture_call[2]["$exception_list"][0]["stacktrace"]["frames"][0][
194
- "filename"
222
+ capture_call[1]["properties"]["$exception_list"][0]["type"], "Exception"
223
+ )
224
+ self.assertEqual(
225
+ capture_call[1]["properties"]["$exception_list"][0]["value"],
226
+ "test exception",
227
+ )
228
+ self.assertEqual(
229
+ capture_call[1]["properties"]["$exception_list"][0]["stacktrace"][
230
+ "type"
195
231
  ],
232
+ "raw",
233
+ )
234
+ self.assertEqual(
235
+ capture_call[1]["properties"]["$exception_list"][0]["stacktrace"][
236
+ "frames"
237
+ ][0]["filename"],
196
238
  "posthog/test/test_client.py",
197
239
  )
198
240
  self.assertEqual(
199
- capture_call[2]["$exception_list"][0]["stacktrace"]["frames"][0][
200
- "function"
201
- ],
241
+ capture_call[1]["properties"]["$exception_list"][0]["stacktrace"][
242
+ "frames"
243
+ ][0]["function"],
202
244
  "test_basic_capture_exception_with_no_exception_given",
203
245
  )
204
246
  self.assertEqual(
205
- capture_call[2]["$exception_list"][0]["stacktrace"]["frames"][0][
206
- "module"
207
- ],
247
+ capture_call[1]["properties"]["$exception_list"][0]["stacktrace"][
248
+ "frames"
249
+ ][0]["module"],
208
250
  "posthog.test.test_client",
209
251
  )
210
252
  self.assertEqual(
211
- capture_call[2]["$exception_list"][0]["stacktrace"]["frames"][0][
212
- "in_app"
213
- ],
253
+ capture_call[1]["properties"]["$exception_list"][0]["stacktrace"][
254
+ "frames"
255
+ ][0]["in_app"],
214
256
  True,
215
257
  )
216
258
 
@@ -218,7 +260,7 @@ class TestClient(unittest.TestCase):
218
260
  with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
219
261
  with self.assertLogs("posthog", level="WARNING") as logs:
220
262
  client = self.client
221
- client.capture_exception()
263
+ client.capture_exception(None)
222
264
 
223
265
  self.assertFalse(patch_capture.called)
224
266
  self.assertEqual(
@@ -230,48 +272,52 @@ class TestClient(unittest.TestCase):
230
272
  client = Client(FAKE_TEST_API_KEY, log_captured_exceptions=True)
231
273
  with self.assertLogs("posthog", level="ERROR") as logs:
232
274
  client.capture_exception(
233
- Exception("test exception"), "distinct_id", path="one/two/three"
275
+ Exception("test exception"), distinct_id="distinct_id"
234
276
  )
235
277
  self.assertEqual(
236
278
  logs.output[0], "ERROR:posthog:test exception\nNoneType: None"
237
279
  )
238
- self.assertEqual(getattr(logs.records[0], "path"), "one/two/three")
239
280
 
240
281
  @mock.patch("posthog.client.flags")
241
282
  def test_basic_capture_with_feature_flags(self, patch_flags):
242
283
  patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
243
284
 
244
- client = Client(
245
- FAKE_TEST_API_KEY,
246
- on_error=self.set_fail,
247
- personal_api_key=FAKE_TEST_API_KEY,
248
- )
249
- success, msg = client.capture(
250
- "distinct_id", "python test event", send_feature_flags=True
251
- )
252
- client.flush()
253
- self.assertTrue(success)
254
- self.assertFalse(self.failed)
255
-
256
- self.assertEqual(msg["event"], "python test event")
257
- self.assertTrue(isinstance(msg["timestamp"], str))
258
- self.assertIsNone(msg.get("uuid"))
259
- self.assertEqual(msg["distinct_id"], "distinct_id")
260
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
261
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
262
- self.assertEqual(msg["properties"]["$feature/beta-feature"], "random-variant")
263
- self.assertEqual(msg["properties"]["$active_feature_flags"], ["beta-feature"])
285
+ with mock.patch("posthog.client.batch_post") as mock_post:
286
+ client = Client(
287
+ FAKE_TEST_API_KEY,
288
+ on_error=self.set_fail,
289
+ personal_api_key=FAKE_TEST_API_KEY,
290
+ sync_mode=True,
291
+ )
292
+ msg_uuid = client.capture(
293
+ "python test event", distinct_id="distinct_id", send_feature_flags=True
294
+ )
295
+ self.assertIsNotNone(msg_uuid)
296
+ self.assertFalse(self.failed)
297
+
298
+ # Get the enqueued message from the mock
299
+ mock_post.assert_called_once()
300
+ batch_data = mock_post.call_args[1]["batch"]
301
+ msg = batch_data[0]
302
+
303
+ self.assertEqual(msg["event"], "python test event")
304
+ self.assertTrue(isinstance(msg["timestamp"], str))
305
+ self.assertIsNotNone(msg.get("uuid"))
306
+ self.assertEqual(msg["distinct_id"], "distinct_id")
307
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
308
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
309
+ self.assertEqual(
310
+ msg["properties"]["$feature/beta-feature"], "random-variant"
311
+ )
312
+ self.assertEqual(
313
+ msg["properties"]["$active_feature_flags"], ["beta-feature"]
314
+ )
264
315
 
265
- self.assertEqual(patch_flags.call_count, 1)
316
+ self.assertEqual(patch_flags.call_count, 1)
266
317
 
267
318
  @mock.patch("posthog.client.flags")
268
319
  def test_basic_capture_with_locally_evaluated_feature_flags(self, patch_flags):
269
320
  patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
270
- client = Client(
271
- FAKE_TEST_API_KEY,
272
- on_error=self.set_fail,
273
- personal_api_key=FAKE_TEST_API_KEY,
274
- )
275
321
 
276
322
  multivariate_flag = {
277
323
  "id": 1,
@@ -358,40 +404,64 @@ class TestClient(unittest.TestCase):
358
404
  "payloads": {"true": 300},
359
405
  },
360
406
  }
361
- client.feature_flags = [multivariate_flag, basic_flag, false_flag]
362
-
363
- success, msg = client.capture("distinct_id", "python test event")
364
- client.flush()
365
- self.assertTrue(success)
366
- self.assertFalse(self.failed)
367
407
 
368
- self.assertEqual(msg["event"], "python test event")
369
- self.assertTrue(isinstance(msg["timestamp"], str))
370
- self.assertIsNone(msg.get("uuid"))
371
- self.assertEqual(msg["distinct_id"], "distinct_id")
372
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
373
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
374
- self.assertEqual(
375
- msg["properties"]["$feature/beta-feature-local"], "third-variant"
376
- )
377
- self.assertEqual(msg["properties"]["$feature/false-flag"], False)
378
- self.assertEqual(
379
- msg["properties"]["$active_feature_flags"], ["beta-feature-local"]
380
- )
381
- assert "$feature/beta-feature" not in msg["properties"]
408
+ with mock.patch("posthog.client.batch_post") as mock_post:
409
+ client = Client(
410
+ FAKE_TEST_API_KEY,
411
+ on_error=self.set_fail,
412
+ personal_api_key=FAKE_TEST_API_KEY,
413
+ sync_mode=True,
414
+ )
415
+ client.feature_flags = [multivariate_flag, basic_flag, false_flag]
416
+
417
+ msg_uuid = client.capture("python test event", distinct_id="distinct_id")
418
+ self.assertIsNotNone(msg_uuid)
419
+ self.assertFalse(self.failed)
420
+
421
+ # Get the enqueued message from the mock
422
+ mock_post.assert_called_once()
423
+ batch_data = mock_post.call_args[1]["batch"]
424
+ msg = batch_data[0]
425
+
426
+ self.assertEqual(msg["event"], "python test event")
427
+ self.assertTrue(isinstance(msg["timestamp"], str))
428
+ self.assertIsNotNone(msg.get("uuid"))
429
+ self.assertEqual(msg["distinct_id"], "distinct_id")
430
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
431
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
432
+ self.assertEqual(
433
+ msg["properties"]["$feature/beta-feature-local"], "third-variant"
434
+ )
435
+ self.assertEqual(msg["properties"]["$feature/false-flag"], False)
436
+ self.assertEqual(
437
+ msg["properties"]["$active_feature_flags"], ["beta-feature-local"]
438
+ )
439
+ assert "$feature/beta-feature" not in msg["properties"]
382
440
 
383
- self.assertEqual(patch_flags.call_count, 0)
441
+ self.assertEqual(patch_flags.call_count, 0)
384
442
 
385
443
  # test that flags are not evaluated without local evaluation
386
- client.feature_flags = []
387
- success, msg = client.capture("distinct_id", "python test event")
388
- client.flush()
389
- self.assertTrue(success)
390
- self.assertFalse(self.failed)
391
- assert "$feature/beta-feature" not in msg["properties"]
392
- assert "$feature/beta-feature-local" not in msg["properties"]
393
- assert "$feature/false-flag" not in msg["properties"]
394
- assert "$active_feature_flags" not in msg["properties"]
444
+ with mock.patch("posthog.client.batch_post") as mock_post:
445
+ client = Client(
446
+ FAKE_TEST_API_KEY,
447
+ on_error=self.set_fail,
448
+ personal_api_key=FAKE_TEST_API_KEY,
449
+ sync_mode=True,
450
+ )
451
+ client.feature_flags = []
452
+ msg_uuid = client.capture("python test event", distinct_id="distinct_id")
453
+ self.assertIsNotNone(msg_uuid)
454
+ self.assertFalse(self.failed)
455
+
456
+ # Get the enqueued message from the mock
457
+ mock_post.assert_called_once()
458
+ batch_data = mock_post.call_args[1]["batch"]
459
+ msg = batch_data[0]
460
+
461
+ assert "$feature/beta-feature" not in msg["properties"]
462
+ assert "$feature/beta-feature-local" not in msg["properties"]
463
+ assert "$feature/false-flag" not in msg["properties"]
464
+ assert "$active_feature_flags" not in msg["properties"]
395
465
 
396
466
  @mock.patch("posthog.client.get")
397
467
  def test_load_feature_flags_quota_limited(self, patch_get):
@@ -415,11 +485,6 @@ class TestClient(unittest.TestCase):
415
485
  @mock.patch("posthog.client.flags")
416
486
  def test_dont_override_capture_with_local_flags(self, patch_flags):
417
487
  patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
418
- client = Client(
419
- FAKE_TEST_API_KEY,
420
- on_error=self.set_fail,
421
- personal_api_key=FAKE_TEST_API_KEY,
422
- )
423
488
 
424
489
  multivariate_flag = {
425
490
  "id": 1,
@@ -491,33 +556,45 @@ class TestClient(unittest.TestCase):
491
556
  "payloads": {"true": 300},
492
557
  },
493
558
  }
494
- client.feature_flags = [multivariate_flag, basic_flag]
495
559
 
496
- success, msg = client.capture(
497
- "distinct_id",
498
- "python test event",
499
- {"$feature/beta-feature-local": "my-custom-variant"},
500
- )
501
- client.flush()
502
- self.assertTrue(success)
503
- self.assertFalse(self.failed)
560
+ with mock.patch("posthog.client.batch_post") as mock_post:
561
+ client = Client(
562
+ FAKE_TEST_API_KEY,
563
+ on_error=self.set_fail,
564
+ personal_api_key=FAKE_TEST_API_KEY,
565
+ sync_mode=True,
566
+ )
567
+ client.feature_flags = [multivariate_flag, basic_flag]
504
568
 
505
- self.assertEqual(msg["event"], "python test event")
506
- self.assertTrue(isinstance(msg["timestamp"], str))
507
- self.assertIsNone(msg.get("uuid"))
508
- self.assertEqual(msg["distinct_id"], "distinct_id")
509
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
510
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
511
- self.assertEqual(
512
- msg["properties"]["$feature/beta-feature-local"], "my-custom-variant"
513
- )
514
- self.assertEqual(
515
- msg["properties"]["$active_feature_flags"], ["beta-feature-local"]
516
- )
517
- assert "$feature/beta-feature" not in msg["properties"]
518
- assert "$feature/person-flag" not in msg["properties"]
569
+ msg_uuid = client.capture(
570
+ "python test event",
571
+ distinct_id="distinct_id",
572
+ properties={"$feature/beta-feature-local": "my-custom-variant"},
573
+ )
574
+ self.assertIsNotNone(msg_uuid)
575
+ self.assertFalse(self.failed)
576
+
577
+ # Get the enqueued message from the mock
578
+ mock_post.assert_called_once()
579
+ batch_data = mock_post.call_args[1]["batch"]
580
+ msg = batch_data[0]
581
+
582
+ self.assertEqual(msg["event"], "python test event")
583
+ self.assertTrue(isinstance(msg["timestamp"], str))
584
+ self.assertIsNotNone(msg.get("uuid"))
585
+ self.assertEqual(msg["distinct_id"], "distinct_id")
586
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
587
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
588
+ self.assertEqual(
589
+ msg["properties"]["$feature/beta-feature-local"], "my-custom-variant"
590
+ )
591
+ self.assertEqual(
592
+ msg["properties"]["$active_feature_flags"], ["beta-feature-local"]
593
+ )
594
+ assert "$feature/beta-feature" not in msg["properties"]
595
+ assert "$feature/person-flag" not in msg["properties"]
519
596
 
520
- self.assertEqual(patch_flags.call_count, 0)
597
+ self.assertEqual(patch_flags.call_count, 0)
521
598
 
522
599
  @mock.patch("posthog.client.flags")
523
600
  def test_basic_capture_with_feature_flags_returns_active_only(self, patch_flags):
@@ -529,43 +606,51 @@ class TestClient(unittest.TestCase):
529
606
  }
530
607
  }
531
608
 
532
- client = Client(
533
- FAKE_TEST_API_KEY,
534
- on_error=self.set_fail,
535
- personal_api_key=FAKE_TEST_API_KEY,
536
- )
537
- success, msg = client.capture(
538
- "distinct_id", "python test event", send_feature_flags=True
539
- )
540
- client.flush()
541
- self.assertTrue(success)
542
- self.assertFalse(self.failed)
543
-
544
- self.assertEqual(msg["event"], "python test event")
545
- self.assertTrue(isinstance(msg["timestamp"], str))
546
- self.assertIsNone(msg.get("uuid"))
547
- self.assertEqual(msg["distinct_id"], "distinct_id")
548
- self.assertTrue(msg["properties"]["$geoip_disable"])
549
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
550
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
551
- self.assertEqual(msg["properties"]["$feature/beta-feature"], "random-variant")
552
- self.assertEqual(msg["properties"]["$feature/alpha-feature"], True)
553
- self.assertEqual(
554
- msg["properties"]["$active_feature_flags"],
555
- ["beta-feature", "alpha-feature"],
556
- )
609
+ with mock.patch("posthog.client.batch_post") as mock_post:
610
+ client = Client(
611
+ FAKE_TEST_API_KEY,
612
+ on_error=self.set_fail,
613
+ personal_api_key=FAKE_TEST_API_KEY,
614
+ sync_mode=True,
615
+ )
616
+ msg_uuid = client.capture(
617
+ "python test event", distinct_id="distinct_id", send_feature_flags=True
618
+ )
619
+ self.assertIsNotNone(msg_uuid)
620
+ self.assertFalse(self.failed)
621
+
622
+ # Get the enqueued message from the mock
623
+ mock_post.assert_called_once()
624
+ batch_data = mock_post.call_args[1]["batch"]
625
+ msg = batch_data[0]
626
+
627
+ self.assertEqual(msg["event"], "python test event")
628
+ self.assertTrue(isinstance(msg["timestamp"], str))
629
+ self.assertIsNotNone(msg.get("uuid"))
630
+ self.assertEqual(msg["distinct_id"], "distinct_id")
631
+ self.assertTrue(msg["properties"]["$geoip_disable"])
632
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
633
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
634
+ self.assertEqual(
635
+ msg["properties"]["$feature/beta-feature"], "random-variant"
636
+ )
637
+ self.assertEqual(msg["properties"]["$feature/alpha-feature"], True)
638
+ self.assertEqual(
639
+ msg["properties"]["$active_feature_flags"],
640
+ ["beta-feature", "alpha-feature"],
641
+ )
557
642
 
558
- self.assertEqual(patch_flags.call_count, 1)
559
- patch_flags.assert_called_with(
560
- "random_key",
561
- "https://us.i.posthog.com",
562
- timeout=3,
563
- distinct_id="distinct_id",
564
- groups={},
565
- person_properties=None,
566
- group_properties=None,
567
- geoip_disable=True,
568
- )
643
+ self.assertEqual(patch_flags.call_count, 1)
644
+ patch_flags.assert_called_with(
645
+ "random_key",
646
+ "https://us.i.posthog.com",
647
+ timeout=3,
648
+ distinct_id="distinct_id",
649
+ groups={},
650
+ person_properties=None,
651
+ group_properties=None,
652
+ geoip_disable=True,
653
+ )
569
654
 
570
655
  @mock.patch("posthog.client.flags")
571
656
  def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(
@@ -579,49 +664,57 @@ class TestClient(unittest.TestCase):
579
664
  }
580
665
  }
581
666
 
582
- client = Client(
583
- FAKE_TEST_API_KEY,
584
- host="https://app.posthog.com",
585
- on_error=self.set_fail,
586
- personal_api_key=FAKE_TEST_API_KEY,
587
- disable_geoip=True,
588
- feature_flags_request_timeout_seconds=12,
589
- )
590
- success, msg = client.capture(
591
- "distinct_id",
592
- "python test event",
593
- send_feature_flags=True,
594
- disable_geoip=False,
595
- )
596
- client.flush()
597
- self.assertTrue(success)
598
- self.assertFalse(self.failed)
599
-
600
- self.assertEqual(msg["event"], "python test event")
601
- self.assertTrue(isinstance(msg["timestamp"], str))
602
- self.assertIsNone(msg.get("uuid"))
603
- self.assertTrue("$geoip_disable" not in msg["properties"])
604
- self.assertEqual(msg["distinct_id"], "distinct_id")
605
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
606
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
607
- self.assertEqual(msg["properties"]["$feature/beta-feature"], "random-variant")
608
- self.assertEqual(msg["properties"]["$feature/alpha-feature"], True)
609
- self.assertEqual(
610
- msg["properties"]["$active_feature_flags"],
611
- ["beta-feature", "alpha-feature"],
612
- )
667
+ with mock.patch("posthog.client.batch_post") as mock_post:
668
+ client = Client(
669
+ FAKE_TEST_API_KEY,
670
+ host="https://app.posthog.com",
671
+ on_error=self.set_fail,
672
+ personal_api_key=FAKE_TEST_API_KEY,
673
+ disable_geoip=True,
674
+ feature_flags_request_timeout_seconds=12,
675
+ sync_mode=True,
676
+ )
677
+ msg_uuid = client.capture(
678
+ "python test event",
679
+ distinct_id="distinct_id",
680
+ send_feature_flags=True,
681
+ disable_geoip=False,
682
+ )
683
+ self.assertIsNotNone(msg_uuid)
684
+ self.assertFalse(self.failed)
685
+
686
+ # Get the enqueued message from the mock
687
+ mock_post.assert_called_once()
688
+ batch_data = mock_post.call_args[1]["batch"]
689
+ msg = batch_data[0]
690
+
691
+ self.assertEqual(msg["event"], "python test event")
692
+ self.assertTrue(isinstance(msg["timestamp"], str))
693
+ self.assertIsNotNone(msg.get("uuid"))
694
+ self.assertTrue("$geoip_disable" not in msg["properties"])
695
+ self.assertEqual(msg["distinct_id"], "distinct_id")
696
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
697
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
698
+ self.assertEqual(
699
+ msg["properties"]["$feature/beta-feature"], "random-variant"
700
+ )
701
+ self.assertEqual(msg["properties"]["$feature/alpha-feature"], True)
702
+ self.assertEqual(
703
+ msg["properties"]["$active_feature_flags"],
704
+ ["beta-feature", "alpha-feature"],
705
+ )
613
706
 
614
- self.assertEqual(patch_flags.call_count, 1)
615
- patch_flags.assert_called_with(
616
- "random_key",
617
- "https://us.i.posthog.com",
618
- timeout=12,
619
- distinct_id="distinct_id",
620
- groups={},
621
- person_properties=None,
622
- group_properties=None,
623
- geoip_disable=False,
624
- )
707
+ self.assertEqual(patch_flags.call_count, 1)
708
+ patch_flags.assert_called_with(
709
+ "random_key",
710
+ "https://us.i.posthog.com",
711
+ timeout=12,
712
+ distinct_id="distinct_id",
713
+ groups={},
714
+ person_properties=None,
715
+ group_properties=None,
716
+ geoip_disable=False,
717
+ )
625
718
 
626
719
  @mock.patch("posthog.client.flags")
627
720
  def test_basic_capture_with_feature_flags_switched_off_doesnt_send_them(
@@ -629,316 +722,325 @@ class TestClient(unittest.TestCase):
629
722
  ):
630
723
  patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
631
724
 
632
- client = Client(
633
- FAKE_TEST_API_KEY,
634
- on_error=self.set_fail,
635
- personal_api_key=FAKE_TEST_API_KEY,
636
- )
637
- success, msg = client.capture(
638
- "distinct_id", "python test event", send_feature_flags=False
639
- )
640
- client.flush()
641
- self.assertTrue(success)
642
- self.assertFalse(self.failed)
725
+ with mock.patch("posthog.client.batch_post") as mock_post:
726
+ client = Client(
727
+ FAKE_TEST_API_KEY,
728
+ on_error=self.set_fail,
729
+ personal_api_key=FAKE_TEST_API_KEY,
730
+ sync_mode=True,
731
+ )
732
+ msg_uuid = client.capture(
733
+ "python test event", distinct_id="distinct_id", send_feature_flags=False
734
+ )
735
+ self.assertIsNotNone(msg_uuid)
736
+ self.assertFalse(self.failed)
643
737
 
644
- self.assertEqual(msg["event"], "python test event")
645
- self.assertTrue(isinstance(msg["timestamp"], str))
646
- self.assertIsNone(msg.get("uuid"))
647
- self.assertEqual(msg["distinct_id"], "distinct_id")
648
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
649
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
650
- self.assertTrue("$feature/beta-feature" not in msg["properties"])
651
- self.assertTrue("$active_feature_flags" not in msg["properties"])
738
+ # Get the enqueued message from the mock
739
+ mock_post.assert_called_once()
740
+ batch_data = mock_post.call_args[1]["batch"]
741
+ msg = batch_data[0]
652
742
 
653
- self.assertEqual(patch_flags.call_count, 0)
743
+ self.assertEqual(msg["event"], "python test event")
744
+ self.assertTrue(isinstance(msg["timestamp"], str))
745
+ self.assertIsNotNone(msg.get("uuid"))
746
+ self.assertEqual(msg["distinct_id"], "distinct_id")
747
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
748
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
749
+ self.assertTrue("$feature/beta-feature" not in msg["properties"])
750
+ self.assertTrue("$active_feature_flags" not in msg["properties"])
751
+
752
+ self.assertEqual(patch_flags.call_count, 0)
654
753
 
655
754
  def test_stringifies_distinct_id(self):
656
755
  # A large number that loses precision in node:
657
756
  # node -e "console.log(157963456373623802 + 1)" > 157963456373623800
658
- client = self.client
659
- success, msg = client.capture(
660
- distinct_id=157963456373623802, event="python test event"
661
- )
662
- client.flush()
663
- self.assertTrue(success)
664
- self.assertFalse(self.failed)
757
+ with mock.patch("posthog.client.batch_post") as mock_post:
758
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
759
+ msg_uuid = client.capture(
760
+ "python test event", distinct_id=157963456373623802
761
+ )
762
+ self.assertIsNotNone(msg_uuid)
763
+ self.assertFalse(self.failed)
665
764
 
666
- self.assertEqual(msg["distinct_id"], "157963456373623802")
765
+ # Get the enqueued message from the mock
766
+ mock_post.assert_called_once()
767
+ batch_data = mock_post.call_args[1]["batch"]
768
+ msg = batch_data[0]
769
+
770
+ self.assertEqual(msg["distinct_id"], "157963456373623802")
667
771
 
668
772
  def test_advanced_capture(self):
669
- client = self.client
670
- success, msg = client.capture(
671
- "distinct_id",
672
- "python test event",
673
- {"property": "value"},
674
- timestamp=datetime(2014, 9, 3),
675
- uuid="new-uuid",
676
- )
773
+ with mock.patch("posthog.client.batch_post") as mock_post:
774
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
775
+ msg_uuid = client.capture(
776
+ "python test event",
777
+ distinct_id="distinct_id",
778
+ properties={"property": "value"},
779
+ timestamp=datetime(2014, 9, 3),
780
+ uuid="new-uuid",
781
+ )
677
782
 
678
- self.assertTrue(success)
783
+ self.assertEqual(msg_uuid, "new-uuid")
679
784
 
680
- self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
681
- self.assertEqual(msg["properties"]["property"], "value")
682
- self.assertEqual(msg["event"], "python test event")
683
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
684
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
685
- self.assertEqual(msg["uuid"], "new-uuid")
686
- self.assertEqual(msg["distinct_id"], "distinct_id")
687
- self.assertTrue("$groups" not in msg["properties"])
785
+ # Get the enqueued message from the mock
786
+ mock_post.assert_called_once()
787
+ batch_data = mock_post.call_args[1]["batch"]
788
+ msg = batch_data[0]
688
789
 
689
- def test_groups_capture(self):
690
- success, msg = self.client.capture(
691
- "distinct_id",
692
- "test_event",
693
- groups={"company": "id:5", "instance": "app.posthog.com"},
694
- )
695
-
696
- self.assertTrue(success)
697
- self.assertEqual(
698
- msg["properties"]["$groups"],
699
- {"company": "id:5", "instance": "app.posthog.com"},
700
- )
790
+ self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
791
+ self.assertEqual(msg["properties"]["property"], "value")
792
+ self.assertEqual(msg["event"], "python test event")
793
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
794
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
795
+ self.assertEqual(msg["uuid"], "new-uuid")
796
+ self.assertEqual(msg["distinct_id"], "distinct_id")
797
+ self.assertTrue("$groups" not in msg["properties"])
701
798
 
702
- def test_basic_identify(self):
703
- client = self.client
704
- success, msg = client.identify("distinct_id", {"trait": "value"})
705
- client.flush()
706
- self.assertTrue(success)
707
- self.assertFalse(self.failed)
799
+ def test_groups_capture(self):
800
+ with mock.patch("posthog.client.batch_post") as mock_post:
801
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
802
+ msg_uuid = client.capture(
803
+ "test_event",
804
+ distinct_id="distinct_id",
805
+ groups={"company": "id:5", "instance": "app.posthog.com"},
806
+ )
708
807
 
709
- self.assertEqual(msg["$set"]["trait"], "value")
710
- self.assertTrue(isinstance(msg["timestamp"], str))
711
- self.assertIsNone(msg.get("uuid"))
712
- self.assertEqual(msg["distinct_id"], "distinct_id")
808
+ self.assertIsNotNone(msg_uuid)
713
809
 
714
- def test_advanced_identify(self):
715
- client = self.client
716
- success, msg = client.identify(
717
- "distinct_id",
718
- {"trait": "value"},
719
- timestamp=datetime(2014, 9, 3),
720
- uuid="new-uuid",
721
- )
810
+ # Get the enqueued message from the mock
811
+ mock_post.assert_called_once()
812
+ batch_data = mock_post.call_args[1]["batch"]
813
+ msg = batch_data[0]
722
814
 
723
- self.assertTrue(success)
724
-
725
- self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
726
- self.assertEqual(msg["$set"]["trait"], "value")
727
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
728
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
729
- self.assertTrue(isinstance(msg["timestamp"], str))
730
- self.assertEqual(msg["uuid"], "new-uuid")
731
- self.assertEqual(msg["distinct_id"], "distinct_id")
815
+ self.assertEqual(
816
+ msg["properties"]["$groups"],
817
+ {"company": "id:5", "instance": "app.posthog.com"},
818
+ )
732
819
 
733
820
  def test_basic_set(self):
734
- client = self.client
735
- success, msg = client.set("distinct_id", {"trait": "value"})
736
- client.flush()
737
- self.assertTrue(success)
738
- self.assertFalse(self.failed)
821
+ with mock.patch("posthog.client.batch_post") as mock_post:
822
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
823
+ msg_uuid = client.set(
824
+ distinct_id="distinct_id", properties={"trait": "value"}
825
+ )
826
+ self.assertIsNotNone(msg_uuid)
827
+ self.assertFalse(self.failed)
828
+
829
+ # Get the enqueued message from the mock
830
+ mock_post.assert_called_once()
831
+ batch_data = mock_post.call_args[1]["batch"]
832
+ msg = batch_data[0]
739
833
 
740
- self.assertEqual(msg["$set"]["trait"], "value")
741
- self.assertTrue(isinstance(msg["timestamp"], str))
742
- self.assertIsNone(msg.get("uuid"))
743
- self.assertEqual(msg["distinct_id"], "distinct_id")
834
+ self.assertEqual(msg["$set"]["trait"], "value")
835
+ self.assertTrue(isinstance(msg["timestamp"], str))
836
+ self.assertIsNotNone(msg.get("uuid"))
837
+ self.assertEqual(msg["distinct_id"], "distinct_id")
744
838
 
745
839
  def test_advanced_set(self):
746
- client = self.client
747
- success, msg = client.set(
748
- "distinct_id",
749
- {"trait": "value"},
750
- timestamp=datetime(2014, 9, 3),
751
- uuid="new-uuid",
752
- )
840
+ with mock.patch("posthog.client.batch_post") as mock_post:
841
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
842
+ msg_uuid = client.set(
843
+ distinct_id="distinct_id",
844
+ properties={"trait": "value"},
845
+ timestamp=datetime(2014, 9, 3),
846
+ uuid="new-uuid",
847
+ )
848
+
849
+ self.assertEqual(msg_uuid, "new-uuid")
753
850
 
754
- self.assertTrue(success)
851
+ # Get the enqueued message from the mock
852
+ mock_post.assert_called_once()
853
+ batch_data = mock_post.call_args[1]["batch"]
854
+ msg = batch_data[0]
755
855
 
756
- self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
757
- self.assertEqual(msg["$set"]["trait"], "value")
758
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
759
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
760
- self.assertTrue(isinstance(msg["timestamp"], str))
761
- self.assertEqual(msg["uuid"], "new-uuid")
762
- self.assertEqual(msg["distinct_id"], "distinct_id")
856
+ self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
857
+ self.assertEqual(msg["$set"]["trait"], "value")
858
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
859
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
860
+ self.assertTrue(isinstance(msg["timestamp"], str))
861
+ self.assertEqual(msg["uuid"], "new-uuid")
862
+ self.assertEqual(msg["distinct_id"], "distinct_id")
763
863
 
764
864
  def test_basic_set_once(self):
765
- client = self.client
766
- success, msg = client.set_once("distinct_id", {"trait": "value"})
767
- client.flush()
768
- self.assertTrue(success)
769
- self.assertFalse(self.failed)
865
+ with mock.patch("posthog.client.batch_post") as mock_post:
866
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
867
+ msg_uuid = client.set_once(
868
+ distinct_id="distinct_id", properties={"trait": "value"}
869
+ )
870
+ self.assertIsNotNone(msg_uuid)
871
+ self.assertFalse(self.failed)
872
+
873
+ # Get the enqueued message from the mock
874
+ mock_post.assert_called_once()
875
+ batch_data = mock_post.call_args[1]["batch"]
876
+ msg = batch_data[0]
770
877
 
771
- self.assertEqual(msg["$set_once"]["trait"], "value")
772
- self.assertTrue(isinstance(msg["timestamp"], str))
773
- self.assertIsNone(msg.get("uuid"))
774
- self.assertEqual(msg["distinct_id"], "distinct_id")
878
+ self.assertEqual(msg["$set_once"]["trait"], "value")
879
+ self.assertTrue(isinstance(msg["timestamp"], str))
880
+ self.assertIsNotNone(msg.get("uuid"))
881
+ self.assertEqual(msg["distinct_id"], "distinct_id")
775
882
 
776
883
  def test_advanced_set_once(self):
777
- client = self.client
778
- success, msg = client.set_once(
779
- "distinct_id",
780
- {"trait": "value"},
781
- timestamp=datetime(2014, 9, 3),
782
- uuid="new-uuid",
783
- )
884
+ with mock.patch("posthog.client.batch_post") as mock_post:
885
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
886
+ msg_uuid = client.set_once(
887
+ distinct_id="distinct_id",
888
+ properties={"trait": "value"},
889
+ timestamp=datetime(2014, 9, 3),
890
+ uuid="new-uuid",
891
+ )
892
+
893
+ self.assertEqual(msg_uuid, "new-uuid")
784
894
 
785
- self.assertTrue(success)
895
+ # Get the enqueued message from the mock
896
+ mock_post.assert_called_once()
897
+ batch_data = mock_post.call_args[1]["batch"]
898
+ msg = batch_data[0]
786
899
 
787
- self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
788
- self.assertEqual(msg["$set_once"]["trait"], "value")
789
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
790
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
791
- self.assertTrue(isinstance(msg["timestamp"], str))
792
- self.assertEqual(msg["uuid"], "new-uuid")
793
- self.assertEqual(msg["distinct_id"], "distinct_id")
900
+ self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
901
+ self.assertEqual(msg["$set_once"]["trait"], "value")
902
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
903
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
904
+ self.assertTrue(isinstance(msg["timestamp"], str))
905
+ self.assertEqual(msg["uuid"], "new-uuid")
906
+ self.assertEqual(msg["distinct_id"], "distinct_id")
794
907
 
795
908
  def test_basic_group_identify(self):
796
- success, msg = self.client.group_identify("organization", "id:5")
797
-
798
- self.assertTrue(success)
799
- self.assertEqual(msg["event"], "$groupidentify")
800
- self.assertEqual(msg["distinct_id"], "$organization_id:5")
801
- self.assertEqual(
802
- msg["properties"],
803
- {
804
- "$group_type": "organization",
805
- "$group_key": "id:5",
806
- "$group_set": {},
807
- "$lib": "posthog-python",
808
- "$lib_version": VERSION,
809
- "$geoip_disable": True,
810
- },
811
- )
812
- self.assertTrue(isinstance(msg["timestamp"], str))
813
- self.assertIsNone(msg.get("uuid"))
909
+ with mock.patch("posthog.client.batch_post") as mock_post:
910
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
911
+ msg_uuid = client.group_identify("organization", "id:5")
912
+
913
+ self.assertIsNotNone(msg_uuid)
914
+
915
+ # Get the enqueued message from the mock
916
+ mock_post.assert_called_once()
917
+ batch_data = mock_post.call_args[1]["batch"]
918
+ msg = batch_data[0]
919
+
920
+ self.assertEqual(msg["event"], "$groupidentify")
921
+ self.assertEqual(
922
+ msg["properties"],
923
+ {
924
+ "$group_type": "organization",
925
+ "$group_key": "id:5",
926
+ "$group_set": {},
927
+ "$lib": "posthog-python",
928
+ "$lib_version": VERSION,
929
+ "$geoip_disable": True,
930
+ },
931
+ )
932
+ self.assertTrue(isinstance(msg["timestamp"], str))
933
+ self.assertIsNotNone(msg.get("uuid"))
814
934
 
815
935
  def test_basic_group_identify_with_distinct_id(self):
816
- success, msg = self.client.group_identify(
817
- "organization", "id:5", distinct_id="distinct_id"
818
- )
819
- self.assertTrue(success)
820
- self.assertEqual(msg["event"], "$groupidentify")
821
- self.assertEqual(msg["distinct_id"], "distinct_id")
822
- self.assertEqual(
823
- msg["properties"],
824
- {
825
- "$group_type": "organization",
826
- "$group_key": "id:5",
827
- "$group_set": {},
828
- "$lib": "posthog-python",
829
- "$lib_version": VERSION,
830
- "$geoip_disable": True,
831
- },
832
- )
833
- self.assertTrue(isinstance(msg["timestamp"], str))
834
- self.assertIsNone(msg.get("uuid"))
936
+ with mock.patch("posthog.client.batch_post") as mock_post:
937
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
938
+ msg_uuid = client.group_identify(
939
+ "organization", "id:5", distinct_id="distinct_id"
940
+ )
941
+ self.assertIsNotNone(msg_uuid)
942
+
943
+ # Get the enqueued message from the mock
944
+ mock_post.assert_called_once()
945
+ batch_data = mock_post.call_args[1]["batch"]
946
+ msg = batch_data[0]
947
+
948
+ self.assertEqual(msg["event"], "$groupidentify")
949
+ self.assertEqual(msg["distinct_id"], "distinct_id")
950
+ self.assertEqual(
951
+ msg["properties"],
952
+ {
953
+ "$group_type": "organization",
954
+ "$group_key": "id:5",
955
+ "$group_set": {},
956
+ "$lib": "posthog-python",
957
+ "$lib_version": VERSION,
958
+ "$geoip_disable": True,
959
+ },
960
+ )
961
+ self.assertTrue(isinstance(msg["timestamp"], str))
962
+ self.assertIsNotNone(msg.get("uuid"))
835
963
 
836
964
  def test_advanced_group_identify(self):
837
- success, msg = self.client.group_identify(
838
- "organization",
839
- "id:5",
840
- {"trait": "value"},
841
- timestamp=datetime(2014, 9, 3),
842
- uuid="new-uuid",
843
- )
965
+ with mock.patch("posthog.client.batch_post") as mock_post:
966
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
967
+ msg_uuid = client.group_identify(
968
+ "organization",
969
+ "id:5",
970
+ {"trait": "value"},
971
+ timestamp=datetime(2014, 9, 3),
972
+ uuid="new-uuid",
973
+ )
844
974
 
845
- self.assertTrue(success)
846
- self.assertEqual(msg["event"], "$groupidentify")
847
- self.assertEqual(msg["distinct_id"], "$organization_id:5")
848
- self.assertEqual(
849
- msg["properties"],
850
- {
851
- "$group_type": "organization",
852
- "$group_key": "id:5",
853
- "$group_set": {"trait": "value"},
854
- "$lib": "posthog-python",
855
- "$lib_version": VERSION,
856
- "$geoip_disable": True,
857
- },
858
- )
859
- self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
975
+ self.assertEqual(msg_uuid, "new-uuid")
860
976
 
861
- def test_advanced_group_identify_with_distinct_id(self):
862
- success, msg = self.client.group_identify(
863
- "organization",
864
- "id:5",
865
- {"trait": "value"},
866
- timestamp=datetime(2014, 9, 3),
867
- uuid="new-uuid",
868
- distinct_id="distinct_id",
869
- )
977
+ # Get the enqueued message from the mock
978
+ mock_post.assert_called_once()
979
+ batch_data = mock_post.call_args[1]["batch"]
980
+ msg = batch_data[0]
870
981
 
871
- self.assertTrue(success)
872
- self.assertEqual(msg["event"], "$groupidentify")
873
- self.assertEqual(msg["distinct_id"], "distinct_id")
874
-
875
- self.assertEqual(
876
- msg["properties"],
877
- {
878
- "$group_type": "organization",
879
- "$group_key": "id:5",
880
- "$group_set": {"trait": "value"},
881
- "$lib": "posthog-python",
882
- "$lib_version": VERSION,
883
- "$geoip_disable": True,
884
- },
885
- )
886
- self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
982
+ self.assertEqual(msg["event"], "$groupidentify")
983
+ self.assertEqual(
984
+ msg["properties"],
985
+ {
986
+ "$group_type": "organization",
987
+ "$group_key": "id:5",
988
+ "$group_set": {"trait": "value"},
989
+ "$lib": "posthog-python",
990
+ "$lib_version": VERSION,
991
+ "$geoip_disable": True,
992
+ },
993
+ )
994
+ self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
887
995
 
888
- def test_basic_alias(self):
889
- client = self.client
890
- success, msg = client.alias("previousId", "distinct_id")
891
- client.flush()
892
- self.assertTrue(success)
893
- self.assertFalse(self.failed)
894
- self.assertEqual(msg["properties"]["distinct_id"], "previousId")
895
- self.assertEqual(msg["properties"]["alias"], "distinct_id")
996
+ def test_advanced_group_identify_with_distinct_id(self):
997
+ with mock.patch("posthog.client.batch_post") as mock_post:
998
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
999
+ msg_uuid = client.group_identify(
1000
+ "organization",
1001
+ "id:5",
1002
+ {"trait": "value"},
1003
+ timestamp=datetime(2014, 9, 3),
1004
+ uuid="new-uuid",
1005
+ distinct_id="distinct_id",
1006
+ )
896
1007
 
897
- def test_basic_page(self):
898
- client = self.client
899
- success, msg = client.page("distinct_id", url="https://posthog.com/contact")
900
- self.assertFalse(self.failed)
901
- client.flush()
902
- self.assertTrue(success)
903
- self.assertEqual(msg["distinct_id"], "distinct_id")
904
- self.assertEqual(
905
- msg["properties"]["$current_url"], "https://posthog.com/contact"
906
- )
1008
+ self.assertEqual(msg_uuid, "new-uuid")
907
1009
 
908
- def test_basic_page_distinct_uuid(self):
909
- client = self.client
910
- distinct_id = uuid4()
911
- success, msg = client.page(distinct_id, url="https://posthog.com/contact")
912
- self.assertFalse(self.failed)
913
- client.flush()
914
- self.assertTrue(success)
915
- self.assertEqual(msg["distinct_id"], str(distinct_id))
916
- self.assertEqual(
917
- msg["properties"]["$current_url"], "https://posthog.com/contact"
918
- )
1010
+ # Get the enqueued message from the mock
1011
+ mock_post.assert_called_once()
1012
+ batch_data = mock_post.call_args[1]["batch"]
1013
+ msg = batch_data[0]
919
1014
 
920
- def test_advanced_page(self):
921
- client = self.client
922
- success, msg = client.page(
923
- "distinct_id",
924
- "https://posthog.com/contact",
925
- {"property": "value"},
926
- timestamp=datetime(2014, 9, 3),
927
- uuid="new-uuid",
928
- )
1015
+ self.assertEqual(msg["event"], "$groupidentify")
1016
+ self.assertEqual(msg["distinct_id"], "distinct_id")
929
1017
 
930
- self.assertTrue(success)
1018
+ self.assertEqual(
1019
+ msg["properties"],
1020
+ {
1021
+ "$group_type": "organization",
1022
+ "$group_key": "id:5",
1023
+ "$group_set": {"trait": "value"},
1024
+ "$lib": "posthog-python",
1025
+ "$lib_version": VERSION,
1026
+ "$geoip_disable": True,
1027
+ },
1028
+ )
1029
+ self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
931
1030
 
932
- self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
933
- self.assertEqual(
934
- msg["properties"]["$current_url"], "https://posthog.com/contact"
935
- )
936
- self.assertEqual(msg["properties"]["property"], "value")
937
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
938
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
939
- self.assertTrue(isinstance(msg["timestamp"], str))
940
- self.assertEqual(msg["uuid"], "new-uuid")
941
- self.assertEqual(msg["distinct_id"], "distinct_id")
1031
+ def test_basic_alias(self):
1032
+ with mock.patch("posthog.client.batch_post") as mock_post:
1033
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
1034
+ msg_uuid = client.alias("previousId", "distinct_id")
1035
+ self.assertIsNotNone(msg_uuid)
1036
+ self.assertFalse(self.failed)
1037
+
1038
+ # Get the enqueued message from the mock
1039
+ mock_post.assert_called_once()
1040
+ batch_data = mock_post.call_args[1]["batch"]
1041
+ msg = batch_data[0]
1042
+ self.assertEqual(msg["properties"]["distinct_id"], "previousId")
1043
+ self.assertEqual(msg["properties"]["alias"], "distinct_id")
942
1044
 
943
1045
  @parameterized.expand(
944
1046
  [
@@ -967,80 +1069,80 @@ class TestClient(unittest.TestCase):
967
1069
  def test_capture_with_session_id_variations(
968
1070
  self, test_name, session_id, additional_properties, expected_properties
969
1071
  ):
970
- client = self.client
1072
+ with mock.patch("posthog.client.batch_post") as mock_post:
1073
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
971
1074
 
972
- properties = {"$session_id": session_id, **additional_properties}
973
- success, msg = client.capture(
974
- "distinct_id", "python test event", properties=properties
975
- )
976
- client.flush()
1075
+ properties = {"$session_id": session_id, **additional_properties}
1076
+ msg_uuid = client.capture(
1077
+ "python test event", distinct_id="distinct_id", properties=properties
1078
+ )
977
1079
 
978
- self.assertTrue(success)
979
- self.assertFalse(self.failed)
980
- self.assertEqual(msg["event"], "python test event")
981
- self.assertEqual(msg["distinct_id"], "distinct_id")
982
- self.assertEqual(msg["properties"]["$session_id"], session_id)
983
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
984
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
1080
+ self.assertIsNotNone(msg_uuid)
1081
+ self.assertFalse(self.failed)
1082
+
1083
+ # Get the enqueued message from the mock
1084
+ mock_post.assert_called_once()
1085
+ batch_data = mock_post.call_args[1]["batch"]
1086
+ msg = batch_data[0]
985
1087
 
986
- # Check additional expected properties
987
- for key, value in expected_properties.items():
988
- self.assertEqual(msg["properties"][key], value)
1088
+ self.assertEqual(msg["event"], "python test event")
1089
+ self.assertEqual(msg["distinct_id"], "distinct_id")
1090
+ self.assertEqual(msg["properties"]["$session_id"], session_id)
1091
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
1092
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
1093
+
1094
+ # Check additional expected properties
1095
+ for key, value in expected_properties.items():
1096
+ self.assertEqual(msg["properties"][key], value)
989
1097
 
990
1098
  def test_session_id_preserved_with_groups(self):
991
- client = self.client
992
- session_id = "group-session-101"
1099
+ with mock.patch("posthog.client.batch_post") as mock_post:
1100
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
1101
+ session_id = "group-session-101"
993
1102
 
994
- success, msg = client.capture(
995
- "distinct_id",
996
- "test_event",
997
- properties={"$session_id": session_id},
998
- groups={"company": "id:5", "instance": "app.posthog.com"},
999
- )
1000
- client.flush()
1103
+ msg_uuid = client.capture(
1104
+ "test_event",
1105
+ distinct_id="distinct_id",
1106
+ properties={"$session_id": session_id},
1107
+ groups={"company": "id:5", "instance": "app.posthog.com"},
1108
+ )
1001
1109
 
1002
- self.assertTrue(success)
1003
- self.assertEqual(msg["properties"]["$session_id"], session_id)
1004
- self.assertEqual(
1005
- msg["properties"]["$groups"],
1006
- {"company": "id:5", "instance": "app.posthog.com"},
1007
- )
1110
+ self.assertIsNotNone(msg_uuid)
1008
1111
 
1009
- def test_session_id_with_anonymous_event(self):
1010
- client = self.client
1011
- session_id = "anonymous-session-202"
1112
+ # Get the enqueued message from the mock
1113
+ mock_post.assert_called_once()
1114
+ batch_data = mock_post.call_args[1]["batch"]
1115
+ msg = batch_data[0]
1012
1116
 
1013
- success, msg = client.capture(
1014
- "distinct_id",
1015
- "anonymous_event",
1016
- properties={"$session_id": session_id, "$process_person_profile": False},
1017
- )
1018
- client.flush()
1117
+ self.assertEqual(msg["properties"]["$session_id"], session_id)
1118
+ self.assertEqual(
1119
+ msg["properties"]["$groups"],
1120
+ {"company": "id:5", "instance": "app.posthog.com"},
1121
+ )
1019
1122
 
1020
- self.assertTrue(success)
1021
- self.assertEqual(msg["properties"]["$session_id"], session_id)
1022
- self.assertEqual(msg["properties"]["$process_person_profile"], False)
1123
+ def test_session_id_with_anonymous_event(self):
1124
+ with mock.patch("posthog.client.batch_post") as mock_post:
1125
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
1126
+ session_id = "anonymous-session-202"
1127
+
1128
+ msg_uuid = client.capture(
1129
+ "anonymous_event",
1130
+ distinct_id="distinct_id",
1131
+ properties={
1132
+ "$session_id": session_id,
1133
+ "$process_person_profile": False,
1134
+ },
1135
+ )
1023
1136
 
1024
- def test_page_with_session_id(self):
1025
- client = self.client
1026
- session_id = "page-session-303"
1137
+ self.assertIsNotNone(msg_uuid)
1027
1138
 
1028
- success, msg = client.page(
1029
- "distinct_id",
1030
- "https://posthog.com/contact",
1031
- properties={"$session_id": session_id, "page_type": "contact"},
1032
- )
1033
- client.flush()
1139
+ # Get the enqueued message from the mock
1140
+ mock_post.assert_called_once()
1141
+ batch_data = mock_post.call_args[1]["batch"]
1142
+ msg = batch_data[0]
1034
1143
 
1035
- self.assertTrue(success)
1036
- self.assertFalse(self.failed)
1037
- self.assertEqual(msg["event"], "$pageview")
1038
- self.assertEqual(msg["distinct_id"], "distinct_id")
1039
- self.assertEqual(msg["properties"]["$session_id"], session_id)
1040
- self.assertEqual(
1041
- msg["properties"]["$current_url"], "https://posthog.com/contact"
1042
- )
1043
- self.assertEqual(msg["properties"]["page_type"], "contact")
1144
+ self.assertEqual(msg["properties"]["$session_id"], session_id)
1145
+ self.assertEqual(msg["properties"]["$process_person_profile"], False)
1044
1146
 
1045
1147
  @parameterized.expand(
1046
1148
  [
@@ -1116,23 +1218,31 @@ class TestClient(unittest.TestCase):
1116
1218
  additional_properties,
1117
1219
  expected_additional_properties,
1118
1220
  ):
1119
- client = self.client
1221
+ with mock.patch("posthog.client.batch_post") as mock_post:
1222
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
1120
1223
 
1121
- properties = {"$session_id": session_id, **additional_properties}
1122
- success, msg = client.capture("distinct_id", event_name, properties=properties)
1123
- client.flush()
1224
+ properties = {"$session_id": session_id, **additional_properties}
1225
+ msg_uuid = client.capture(
1226
+ event_name, distinct_id="distinct_id", properties=properties
1227
+ )
1124
1228
 
1125
- self.assertTrue(success)
1126
- self.assertEqual(msg["event"], event_name)
1127
- self.assertEqual(msg["properties"]["$session_id"], session_id)
1229
+ self.assertIsNotNone(msg_uuid)
1128
1230
 
1129
- # Check additional expected properties
1130
- for key, value in expected_additional_properties.items():
1131
- self.assertEqual(msg["properties"][key], value)
1231
+ # Get the enqueued message from the mock
1232
+ mock_post.assert_called_once()
1233
+ batch_data = mock_post.call_args[1]["batch"]
1234
+ msg = batch_data[0]
1132
1235
 
1133
- # Verify system properties are still added
1134
- self.assertEqual(msg["properties"]["$lib"], "posthog-python")
1135
- self.assertEqual(msg["properties"]["$lib_version"], VERSION)
1236
+ self.assertEqual(msg["event"], event_name)
1237
+ self.assertEqual(msg["properties"]["$session_id"], session_id)
1238
+
1239
+ # Check additional expected properties
1240
+ for key, value in expected_additional_properties.items():
1241
+ self.assertEqual(msg["properties"][key], value)
1242
+
1243
+ # Verify system properties are still added
1244
+ self.assertEqual(msg["properties"]["$lib"], "posthog-python")
1245
+ self.assertEqual(msg["properties"]["$lib_version"], VERSION)
1136
1246
 
1137
1247
  @parameterized.expand(
1138
1248
  [
@@ -1175,25 +1285,37 @@ class TestClient(unittest.TestCase):
1175
1285
  expected_session_id,
1176
1286
  expected_super_props,
1177
1287
  ):
1178
- client = Client(FAKE_TEST_API_KEY, super_properties=super_properties)
1288
+ with mock.patch("posthog.client.batch_post") as mock_post:
1289
+ client = Client(
1290
+ FAKE_TEST_API_KEY, super_properties=super_properties, sync_mode=True
1291
+ )
1179
1292
 
1180
- success, msg = client.capture(
1181
- "distinct_id", "test_event", properties={"$session_id": event_session_id}
1182
- )
1183
- client.flush()
1293
+ msg_uuid = client.capture(
1294
+ "test_event",
1295
+ distinct_id="distinct_id",
1296
+ properties={"$session_id": event_session_id},
1297
+ )
1298
+
1299
+ self.assertIsNotNone(msg_uuid)
1184
1300
 
1185
- self.assertTrue(success)
1186
- self.assertEqual(msg["properties"]["$session_id"], expected_session_id)
1301
+ # Get the enqueued message from the mock
1302
+ mock_post.assert_called_once()
1303
+ batch_data = mock_post.call_args[1]["batch"]
1304
+ msg = batch_data[0]
1187
1305
 
1188
- # Check expected super properties are present
1189
- for key, value in expected_super_props.items():
1190
- self.assertEqual(msg["properties"][key], value)
1306
+ self.assertEqual(msg["properties"]["$session_id"], expected_session_id)
1307
+
1308
+ # Check expected super properties are present
1309
+ for key, value in expected_super_props.items():
1310
+ self.assertEqual(msg["properties"][key], value)
1191
1311
 
1192
1312
  def test_flush(self):
1193
1313
  client = self.client
1194
1314
  # set up the consumer with more requests than a single batch will allow
1195
1315
  for i in range(1000):
1196
- success, msg = client.identify("distinct_id", {"trait": "value"})
1316
+ client.capture(
1317
+ "event", distinct_id="distinct_id", properties={"trait": "value"}
1318
+ )
1197
1319
  # We can't reliably assert that the queue is non-empty here; that's
1198
1320
  # a race condition. We do our best to load it up though.
1199
1321
  client.flush()
@@ -1204,7 +1326,9 @@ class TestClient(unittest.TestCase):
1204
1326
  client = self.client
1205
1327
  # set up the consumer with more requests than a single batch will allow
1206
1328
  for i in range(1000):
1207
- success, msg = client.identify("distinct_id", {"trait": "value"})
1329
+ client.capture(
1330
+ "test event", distinct_id="distinct_id", properties={"trait": "value"}
1331
+ )
1208
1332
  client.shutdown()
1209
1333
  # we expect two things after shutdown:
1210
1334
  # 1. client queue is empty
@@ -1214,12 +1338,16 @@ class TestClient(unittest.TestCase):
1214
1338
  self.assertFalse(consumer.is_alive())
1215
1339
 
1216
1340
  def test_synchronous(self):
1217
- client = Client(FAKE_TEST_API_KEY, sync_mode=True)
1341
+ with mock.patch("posthog.client.batch_post") as mock_post:
1342
+ client = Client(FAKE_TEST_API_KEY, sync_mode=True)
1218
1343
 
1219
- success, message = client.identify("distinct_id")
1220
- self.assertFalse(client.consumers)
1221
- self.assertTrue(client.queue.empty())
1222
- self.assertTrue(success)
1344
+ msg_uuid = client.capture("test event", distinct_id="distinct_id")
1345
+ self.assertFalse(client.consumers)
1346
+ self.assertTrue(client.queue.empty())
1347
+ self.assertIsNotNone(msg_uuid)
1348
+
1349
+ # Verify the message was sent immediately
1350
+ mock_post.assert_called_once()
1223
1351
 
1224
1352
  def test_overflow(self):
1225
1353
  client = Client(FAKE_TEST_API_KEY, max_queue_size=1)
@@ -1227,17 +1355,17 @@ class TestClient(unittest.TestCase):
1227
1355
  client.join()
1228
1356
 
1229
1357
  for i in range(10):
1230
- client.identify("distinct_id")
1358
+ client.capture("test event", distinct_id="distinct_id")
1231
1359
 
1232
- success, msg = client.identify("distinct_id")
1360
+ msg_uuid = client.capture("test event", distinct_id="distinct_id")
1233
1361
  # Make sure we are informed that the queue is at capacity
1234
- self.assertFalse(success)
1362
+ self.assertIsNone(msg_uuid)
1235
1363
 
1236
1364
  def test_unicode(self):
1237
1365
  Client(six.u("unicode_key"))
1238
1366
 
1239
1367
  def test_numeric_distinct_id(self):
1240
- self.client.capture(1234, "python event")
1368
+ self.client.capture("python event", distinct_id=1234)
1241
1369
  self.client.flush()
1242
1370
  self.assertFalse(self.failed)
1243
1371
 
@@ -1247,7 +1375,9 @@ class TestClient(unittest.TestCase):
1247
1375
  def test_gzip(self):
1248
1376
  client = Client(FAKE_TEST_API_KEY, on_error=self.fail, gzip=True)
1249
1377
  for _ in range(10):
1250
- client.identify("distinct_id", {"trait": "value"})
1378
+ client.capture(
1379
+ "event", distinct_id="distinct_id", properties={"trait": "value"}
1380
+ )
1251
1381
  client.flush()
1252
1382
  self.assertFalse(self.failed)
1253
1383
 
@@ -1265,7 +1395,9 @@ class TestClient(unittest.TestCase):
1265
1395
  "posthog.consumer.batch_post", side_effect=mock_post_fn
1266
1396
  ) as mock_post:
1267
1397
  for _ in range(20):
1268
- client.identify("distinct_id", {"trait": "value"})
1398
+ client.capture(
1399
+ "event", distinct_id="distinct_id", properties={"trait": "value"}
1400
+ )
1269
1401
  time.sleep(1)
1270
1402
  self.assertEqual(mock_post.call_count, 2)
1271
1403
 
@@ -1281,13 +1413,11 @@ class TestClient(unittest.TestCase):
1281
1413
 
1282
1414
  def test_disabled(self):
1283
1415
  client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disabled=True)
1284
- success, msg = client.capture("distinct_id", "python test event")
1416
+ msg_uuid = client.capture("python test event", distinct_id="distinct_id")
1285
1417
  client.flush()
1286
- self.assertFalse(success)
1418
+ self.assertIsNone(msg_uuid)
1287
1419
  self.assertFalse(self.failed)
1288
1420
 
1289
- self.assertEqual(msg, "disabled")
1290
-
1291
1421
  @mock.patch("posthog.client.flags")
1292
1422
  def test_disabled_with_feature_flags(self, patch_flags):
1293
1423
  client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disabled=True)
@@ -1316,51 +1446,100 @@ class TestClient(unittest.TestCase):
1316
1446
  self.assertTrue(client.queue.empty())
1317
1447
 
1318
1448
  def test_enabled_to_disabled(self):
1319
- client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disabled=False)
1320
- success, msg = client.capture("distinct_id", "python test event")
1321
- client.flush()
1449
+ with mock.patch("posthog.client.batch_post") as mock_post:
1450
+ client = Client(
1451
+ FAKE_TEST_API_KEY,
1452
+ on_error=self.set_fail,
1453
+ disabled=False,
1454
+ sync_mode=True,
1455
+ )
1456
+ msg_uuid = client.capture("python test event", distinct_id="distinct_id")
1322
1457
 
1323
- self.assertTrue(success)
1324
- self.assertFalse(self.failed)
1325
- self.assertEqual(msg["event"], "python test event")
1458
+ self.assertIsNotNone(msg_uuid)
1459
+ self.assertFalse(self.failed)
1326
1460
 
1327
- client.disabled = True
1328
- success, msg = client.capture("distinct_id", "python test event")
1329
- client.flush()
1330
- self.assertFalse(success)
1331
- self.assertFalse(self.failed)
1461
+ # Get the enqueued message from the mock
1462
+ mock_post.assert_called_once()
1463
+ batch_data = mock_post.call_args[1]["batch"]
1464
+ msg = batch_data[0]
1465
+ self.assertEqual(msg["event"], "python test event")
1332
1466
 
1333
- self.assertEqual(msg, "disabled")
1467
+ client.disabled = True
1468
+ msg_uuid = client.capture("python test event", distinct_id="distinct_id")
1469
+ self.assertIsNone(msg_uuid)
1470
+ self.assertFalse(self.failed)
1334
1471
 
1335
1472
  def test_disable_geoip_default_on_events(self):
1336
- client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disable_geoip=True)
1337
- _, capture_msg = client.capture("distinct_id", "python test event")
1338
- client.flush()
1339
- self.assertEqual(capture_msg["properties"]["$geoip_disable"], True)
1473
+ with mock.patch("posthog.client.batch_post") as mock_post:
1474
+ client = Client(
1475
+ FAKE_TEST_API_KEY,
1476
+ on_error=self.set_fail,
1477
+ disable_geoip=True,
1478
+ sync_mode=True,
1479
+ )
1480
+ msg_uuid = client.capture("python test event", distinct_id="distinct_id")
1481
+ self.assertIsNotNone(msg_uuid)
1340
1482
 
1341
- _, identify_msg = client.identify("distinct_id", {"trait": "value"})
1342
- client.flush()
1343
- self.assertEqual(identify_msg["properties"]["$geoip_disable"], True)
1483
+ # Get the enqueued message from the mock
1484
+ mock_post.assert_called_once()
1485
+ batch_data = mock_post.call_args[1]["batch"]
1486
+ capture_msg = batch_data[0]
1487
+ self.assertEqual(capture_msg["properties"]["$geoip_disable"], True)
1344
1488
 
1345
1489
  def test_disable_geoip_override_on_events(self):
1346
- client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disable_geoip=False)
1347
- _, capture_msg = client.set(
1348
- "distinct_id", {"a": "b", "c": "d"}, disable_geoip=True
1349
- )
1350
- client.flush()
1351
- self.assertEqual(capture_msg["properties"]["$geoip_disable"], True)
1490
+ with mock.patch("posthog.client.batch_post") as mock_post:
1491
+ client = Client(
1492
+ FAKE_TEST_API_KEY,
1493
+ on_error=self.set_fail,
1494
+ disable_geoip=False,
1495
+ sync_mode=True,
1496
+ )
1497
+ msg_uuid = client.set(
1498
+ distinct_id="distinct_id",
1499
+ properties={"a": "b", "c": "d"},
1500
+ disable_geoip=True,
1501
+ )
1502
+ self.assertIsNotNone(msg_uuid)
1352
1503
 
1353
- _, identify_msg = client.page(
1354
- "distinct_id", "http://a.com", {"trait": "value"}, disable_geoip=False
1355
- )
1356
- client.flush()
1357
- self.assertEqual("$geoip_disable" not in identify_msg["properties"], True)
1504
+ msg_uuid = client.capture(
1505
+ "event",
1506
+ distinct_id="distinct_id",
1507
+ properties={"trait": "value"},
1508
+ disable_geoip=False,
1509
+ )
1510
+ self.assertIsNotNone(msg_uuid)
1511
+
1512
+ # Check both calls were made
1513
+ self.assertEqual(mock_post.call_count, 2)
1514
+
1515
+ # Check set event
1516
+ set_batch = mock_post.call_args_list[0][1]["batch"]
1517
+ capture_msg = set_batch[0]
1518
+ self.assertEqual(capture_msg["properties"]["$geoip_disable"], True)
1519
+
1520
+ # Check page event
1521
+ page_batch = mock_post.call_args_list[1][1]["batch"]
1522
+ identify_msg = page_batch[0]
1523
+ self.assertEqual("$geoip_disable" not in identify_msg["properties"], True)
1358
1524
 
1359
1525
  def test_disable_geoip_method_overrides_init_on_events(self):
1360
- client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disable_geoip=True)
1361
- _, msg = client.capture("distinct_id", "python test event", disable_geoip=False)
1362
- client.flush()
1363
- self.assertTrue("$geoip_disable" not in msg["properties"])
1526
+ with mock.patch("posthog.client.batch_post") as mock_post:
1527
+ client = Client(
1528
+ FAKE_TEST_API_KEY,
1529
+ on_error=self.set_fail,
1530
+ disable_geoip=True,
1531
+ sync_mode=True,
1532
+ )
1533
+ msg_uuid = client.capture(
1534
+ "python test event", distinct_id="distinct_id", disable_geoip=False
1535
+ )
1536
+ self.assertIsNotNone(msg_uuid)
1537
+
1538
+ # Get the enqueued message from the mock
1539
+ mock_post.assert_called_once()
1540
+ batch_data = mock_post.call_args[1]["batch"]
1541
+ msg = batch_data[0]
1542
+ self.assertTrue("$geoip_disable" not in msg["properties"])
1364
1543
 
1365
1544
  @mock.patch("posthog.client.flags")
1366
1545
  def test_disable_geoip_default_on_decide(self, patch_flags):
@@ -1555,8 +1734,8 @@ class TestClient(unittest.TestCase):
1555
1734
  distro_info,
1556
1735
  ):
1557
1736
  """Test that we can mock platform and sys for testing system_context"""
1558
- with mock.patch("posthog.client.platform") as mock_platform:
1559
- with mock.patch("posthog.client.sys") as mock_sys:
1737
+ with mock.patch("posthog.utils.platform") as mock_platform:
1738
+ with mock.patch("posthog.utils.sys") as mock_sys:
1560
1739
  # Set up common mocks
1561
1740
  mock_platform.python_implementation.return_value = expected_runtime
1562
1741
  mock_sys.version_info = version_info
@@ -1572,15 +1751,15 @@ class TestClient(unittest.TestCase):
1572
1751
  if sys_platform == "linux":
1573
1752
  # Directly patch the get_os_info function to return our expected values
1574
1753
  with mock.patch(
1575
- "posthog.client.get_os_info",
1754
+ "posthog.utils.get_os_info",
1576
1755
  return_value=(expected_os, expected_os_version),
1577
1756
  ):
1578
- from posthoganalytics.client import system_context
1757
+ from posthoganalytics.utils import system_context
1579
1758
 
1580
1759
  context = system_context()
1581
1760
  else:
1582
1761
  # Get system context for non-Linux platforms
1583
- from posthoganalytics.client import system_context
1762
+ from posthoganalytics.utils import system_context
1584
1763
 
1585
1764
  context = system_context()
1586
1765
 
@@ -1649,73 +1828,78 @@ class TestClient(unittest.TestCase):
1649
1828
  }
1650
1829
 
1651
1830
  def test_set_context_session_with_capture(self):
1652
- with new_context():
1653
- set_context_session("context-session-123")
1654
-
1655
- success, msg = self.client.capture(
1656
- "distinct_id", "test_event", {"custom_prop": "value"}
1657
- )
1658
- self.client.flush()
1659
-
1660
- self.assertTrue(success)
1661
- self.assertEqual(msg["properties"]["$session_id"], "context-session-123")
1831
+ with mock.patch("posthog.client.batch_post") as mock_post:
1832
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
1833
+ with new_context():
1834
+ set_context_session("context-session-123")
1835
+
1836
+ msg_uuid = client.capture(
1837
+ "test_event",
1838
+ distinct_id="distinct_id",
1839
+ properties={"custom_prop": "value"},
1840
+ )
1662
1841
 
1663
- def test_set_context_session_with_page(self):
1664
- with new_context():
1665
- set_context_session("page-context-session-456")
1842
+ self.assertIsNotNone(msg_uuid)
1666
1843
 
1667
- success, msg = self.client.page("distinct_id", "https://example.com/page")
1668
- self.client.flush()
1844
+ # Get the enqueued message from the mock
1845
+ mock_post.assert_called_once()
1846
+ batch_data = mock_post.call_args[1]["batch"]
1847
+ msg = batch_data[0]
1669
1848
 
1670
- self.assertTrue(success)
1671
- self.assertEqual(
1672
- msg["properties"]["$session_id"], "page-context-session-456"
1673
- )
1849
+ self.assertEqual(
1850
+ msg["properties"]["$session_id"], "context-session-123"
1851
+ )
1674
1852
 
1675
1853
  def test_set_context_session_with_page_explicit_properties(self):
1676
- with new_context():
1677
- set_context_session("page-explicit-session-789")
1854
+ with mock.patch("posthog.client.batch_post") as mock_post:
1855
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
1856
+ with new_context():
1857
+ set_context_session("page-explicit-session-789")
1858
+
1859
+ properties = {
1860
+ "$session_id": get_context_session_id(),
1861
+ "page_type": "landing",
1862
+ }
1863
+ msg_uuid = client.capture(
1864
+ "$page", distinct_id="distinct_id", properties=properties
1865
+ )
1678
1866
 
1679
- properties = {
1680
- "$session_id": get_context_session_id(),
1681
- "page_type": "landing",
1682
- }
1683
- success, msg = self.client.page(
1684
- "distinct_id", "https://example.com/landing", properties
1685
- )
1686
- self.client.flush()
1867
+ self.assertIsNotNone(msg_uuid)
1687
1868
 
1688
- self.assertTrue(success)
1689
- self.assertEqual(
1690
- msg["properties"]["$session_id"], "page-explicit-session-789"
1691
- )
1869
+ # Get the enqueued message from the mock
1870
+ mock_post.assert_called_once()
1871
+ batch_data = mock_post.call_args[1]["batch"]
1872
+ msg = batch_data[0]
1873
+
1874
+ self.assertEqual(
1875
+ msg["properties"]["$session_id"], "page-explicit-session-789"
1876
+ )
1692
1877
 
1693
1878
  def test_set_context_session_override_in_capture(self):
1694
1879
  """Test that explicit session ID overrides context session ID in capture"""
1695
- from posthoganalytics.scopes import set_context_session, new_context
1696
-
1697
- with new_context():
1698
- set_context_session("context-session-override")
1699
-
1700
- success, msg = self.client.capture(
1701
- "distinct_id",
1702
- "test_event",
1703
- {"$session_id": "explicit-session-override", "custom_prop": "value"},
1704
- )
1705
- self.client.flush()
1706
-
1707
- self.assertTrue(success)
1708
- self.assertEqual(
1709
- msg["properties"]["$session_id"], "explicit-session-override"
1710
- )
1880
+ from posthoganalytics.contexts import set_context_session, new_context
1881
+
1882
+ with mock.patch("posthog.client.batch_post") as mock_post:
1883
+ client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True)
1884
+ with new_context():
1885
+ set_context_session("context-session-override")
1886
+
1887
+ msg_uuid = client.capture(
1888
+ "test_event",
1889
+ distinct_id="distinct_id",
1890
+ properties={
1891
+ "$session_id": "explicit-session-override",
1892
+ "custom_prop": "value",
1893
+ },
1894
+ )
1711
1895
 
1712
- def test_set_context_session_with_identify(self):
1713
- with new_context(capture_exceptions=False):
1714
- set_context_session("identify-session-555")
1896
+ self.assertIsNotNone(msg_uuid)
1715
1897
 
1716
- success, msg = self.client.identify("distinct_id", {"trait": "value"})
1717
- self.client.flush()
1898
+ # Get the enqueued message from the mock
1899
+ mock_post.assert_called_once()
1900
+ batch_data = mock_post.call_args[1]["batch"]
1901
+ msg = batch_data[0]
1718
1902
 
1719
- self.assertTrue(success)
1720
- # In identify, the session ID is added to the $set payload
1721
- self.assertEqual(msg["$set"]["$session_id"], "identify-session-555")
1903
+ self.assertEqual(
1904
+ msg["properties"]["$session_id"], "explicit-session-override"
1905
+ )