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.
- posthoganalytics/__init__.py +93 -242
- posthoganalytics/ai/langchain/callbacks.py +2 -2
- posthoganalytics/args.py +68 -0
- posthoganalytics/client.py +142 -315
- posthoganalytics/{scopes.py → contexts.py} +27 -18
- posthoganalytics/exception_capture.py +3 -25
- posthoganalytics/exception_utils.py +85 -168
- posthoganalytics/integrations/django.py +46 -7
- posthoganalytics/test/test_before_send.py +124 -77
- posthoganalytics/test/test_client.py +903 -719
- posthoganalytics/test/{test_scopes.py → test_contexts.py} +2 -15
- posthoganalytics/test/test_exception_capture.py +0 -29
- posthoganalytics/test/test_feature_flag_result.py +12 -12
- posthoganalytics/test/test_feature_flags.py +30 -22
- posthoganalytics/test/test_module.py +2 -12
- posthoganalytics/utils.py +57 -0
- posthoganalytics/version.py +1 -1
- {posthoganalytics-5.4.0.dist-info → posthoganalytics-6.0.1.dist-info}/METADATA +5 -2
- {posthoganalytics-5.4.0.dist-info → posthoganalytics-6.0.1.dist-info}/RECORD +22 -23
- posthoganalytics/exception_integrations/__init__.py +0 -5
- posthoganalytics/exception_integrations/django.py +0 -117
- {posthoganalytics-5.4.0.dist-info → posthoganalytics-6.0.1.dist-info}/WHEEL +0 -0
- {posthoganalytics-5.4.0.dist-info → posthoganalytics-6.0.1.dist-info}/licenses/LICENSE +0 -0
- {posthoganalytics-5.4.0.dist-info → posthoganalytics-6.0.1.dist-info}/top_level.txt +0 -0
|
@@ -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.
|
|
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(
|
|
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
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
97
|
-
|
|
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
|
-
|
|
100
|
-
client.
|
|
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
|
-
|
|
103
|
-
|
|
124
|
+
msg_uuid = client.capture("python test event", distinct_id="distinct_id")
|
|
125
|
+
self.assertIsNotNone(msg_uuid)
|
|
104
126
|
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
109
|
-
|
|
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
|
|
119
|
-
self.assertEqual(capture_call[0], "
|
|
120
|
-
self.assertEqual(capture_call[1], "
|
|
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
|
|
130
|
-
self.assertEqual(capture_call[0], "
|
|
131
|
-
self.assertEqual(capture_call[1], "
|
|
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
|
-
|
|
143
|
-
self.assertEqual(
|
|
144
|
-
self.assertEqual(
|
|
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
|
|
160
|
-
self.assertEqual(capture_call[0], "
|
|
161
|
-
self.assertEqual(capture_call[1], "
|
|
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
|
|
173
|
-
|
|
174
|
-
self.assertEqual(capture_call[1], "
|
|
175
|
-
self.assertEqual(capture_call[
|
|
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[
|
|
201
|
+
capture_call[1]["properties"]["$exception_type"], "Exception"
|
|
179
202
|
)
|
|
180
203
|
self.assertEqual(
|
|
181
|
-
capture_call[
|
|
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[
|
|
207
|
+
capture_call[1]["properties"]["$exception_list"][0]["mechanism"][
|
|
208
|
+
"type"
|
|
209
|
+
],
|
|
210
|
+
"generic",
|
|
187
211
|
)
|
|
188
212
|
self.assertEqual(
|
|
189
|
-
capture_call[
|
|
190
|
-
|
|
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[
|
|
194
|
-
|
|
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[
|
|
200
|
-
"
|
|
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[
|
|
206
|
-
"
|
|
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[
|
|
212
|
-
"
|
|
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"),
|
|
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
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
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
|
-
|
|
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
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
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
|
-
|
|
441
|
+
self.assertEqual(patch_flags.call_count, 0)
|
|
384
442
|
|
|
385
443
|
# test that flags are not evaluated without local evaluation
|
|
386
|
-
client.
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
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
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
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
|
-
|
|
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
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
msg["properties"]["$
|
|
555
|
-
["
|
|
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
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
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
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
msg["
|
|
611
|
-
["
|
|
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
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
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
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
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
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
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
|
-
|
|
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
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
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
|
-
|
|
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
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
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
|
-
|
|
783
|
+
self.assertEqual(msg_uuid, "new-uuid")
|
|
679
784
|
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
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
|
-
|
|
690
|
-
|
|
691
|
-
"
|
|
692
|
-
"
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
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
|
|
703
|
-
client
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
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
|
-
|
|
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
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
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
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
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
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
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
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
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
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
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
|
-
|
|
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
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
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
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
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
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
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
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
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
|
-
|
|
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
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
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
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
"
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
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
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
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
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
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
|
-
|
|
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
|
-
|
|
862
|
-
|
|
863
|
-
"
|
|
864
|
-
|
|
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
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
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
|
|
889
|
-
client
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
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
|
-
|
|
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
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
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
|
-
|
|
921
|
-
|
|
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
|
-
|
|
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
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
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
|
|
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
|
-
|
|
973
|
-
|
|
974
|
-
|
|
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
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
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
|
-
|
|
987
|
-
|
|
988
|
-
self.assertEqual(msg["properties"][
|
|
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
|
|
992
|
-
|
|
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
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
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
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
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
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
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
|
-
|
|
1025
|
-
client = self.client
|
|
1026
|
-
session_id = "page-session-303"
|
|
1137
|
+
self.assertIsNotNone(msg_uuid)
|
|
1027
1138
|
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
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
|
-
|
|
1036
|
-
|
|
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
|
|
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
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
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
|
-
|
|
1126
|
-
self.assertEqual(msg["event"], event_name)
|
|
1127
|
-
self.assertEqual(msg["properties"]["$session_id"], session_id)
|
|
1229
|
+
self.assertIsNotNone(msg_uuid)
|
|
1128
1230
|
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
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
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
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
|
|
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
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
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
|
-
|
|
1186
|
-
|
|
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
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
1341
|
+
with mock.patch("posthog.client.batch_post") as mock_post:
|
|
1342
|
+
client = Client(FAKE_TEST_API_KEY, sync_mode=True)
|
|
1218
1343
|
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
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.
|
|
1358
|
+
client.capture("test event", distinct_id="distinct_id")
|
|
1231
1359
|
|
|
1232
|
-
|
|
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.
|
|
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(
|
|
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.
|
|
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.
|
|
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
|
-
|
|
1416
|
+
msg_uuid = client.capture("python test event", distinct_id="distinct_id")
|
|
1285
1417
|
client.flush()
|
|
1286
|
-
self.
|
|
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
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
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
|
-
|
|
1324
|
-
|
|
1325
|
-
self.assertEqual(msg["event"], "python test event")
|
|
1458
|
+
self.assertIsNotNone(msg_uuid)
|
|
1459
|
+
self.assertFalse(self.failed)
|
|
1326
1460
|
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
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
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
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
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
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
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
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
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
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.
|
|
1559
|
-
with mock.patch("posthog.
|
|
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.
|
|
1754
|
+
"posthog.utils.get_os_info",
|
|
1576
1755
|
return_value=(expected_os, expected_os_version),
|
|
1577
1756
|
):
|
|
1578
|
-
from posthoganalytics.
|
|
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.
|
|
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
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
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
|
-
|
|
1664
|
-
with new_context():
|
|
1665
|
-
set_context_session("page-context-session-456")
|
|
1842
|
+
self.assertIsNotNone(msg_uuid)
|
|
1666
1843
|
|
|
1667
|
-
|
|
1668
|
-
|
|
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
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
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
|
|
1677
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
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.
|
|
1696
|
-
|
|
1697
|
-
with
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
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
|
-
|
|
1713
|
-
with new_context(capture_exceptions=False):
|
|
1714
|
-
set_context_session("identify-session-555")
|
|
1896
|
+
self.assertIsNotNone(msg_uuid)
|
|
1715
1897
|
|
|
1716
|
-
|
|
1717
|
-
|
|
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
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1903
|
+
self.assertEqual(
|
|
1904
|
+
msg["properties"]["$session_id"], "explicit-session-override"
|
|
1905
|
+
)
|