|
| 1 | +import asyncio |
1 | 2 | import time |
2 | 3 | import unittest |
3 | 4 | from datetime import datetime |
@@ -2361,6 +2362,35 @@ def test_device_id_from_context_is_used_in_flags_request(self, patch_flags): |
2361 | 2362 | flag_keys_to_evaluate=["random_key"], |
2362 | 2363 | ) |
2363 | 2364 |
|
| 2365 | + @mock.patch("posthog.client.flags") |
| 2366 | + def test_client_set_context_device_id_is_used_in_flags_request(self, patch_flags): |
| 2367 | + patch_flags.return_value = { |
| 2368 | + "featureFlags": { |
| 2369 | + "beta-feature": "random-variant", |
| 2370 | + } |
| 2371 | + } |
| 2372 | + client = Client( |
| 2373 | + FAKE_TEST_API_KEY, |
| 2374 | + on_error=self.set_fail, |
| 2375 | + ) |
| 2376 | + |
| 2377 | + with client.new_context(): |
| 2378 | + client.set_context_device_id("client-context-device-id") |
| 2379 | + client.get_feature_flag("random_key", "some_id") |
| 2380 | + |
| 2381 | + patch_flags.assert_called_with( |
| 2382 | + "random_key", |
| 2383 | + "https://us.i.posthog.com", |
| 2384 | + timeout=3, |
| 2385 | + distinct_id="some_id", |
| 2386 | + groups={}, |
| 2387 | + person_properties={"distinct_id": "some_id"}, |
| 2388 | + group_properties={}, |
| 2389 | + geoip_disable=True, |
| 2390 | + device_id="client-context-device-id", |
| 2391 | + flag_keys_to_evaluate=["random_key"], |
| 2392 | + ) |
| 2393 | + |
2364 | 2394 | @parameterized.expand( |
2365 | 2395 | [ |
2366 | 2396 | # name, sys_platform, version_info, expected_runtime, expected_version, expected_os, expected_os_version, expected_os_distro, platform_method, platform_return |
@@ -2534,6 +2564,77 @@ def test_set_context_session_with_capture(self): |
2534 | 2564 | msg["properties"]["$session_id"], "context-session-123" |
2535 | 2565 | ) |
2536 | 2566 |
|
| 2567 | + @parameterized.expand([("new_context",), ("scoped",)]) |
| 2568 | + def test_client_context_helpers_apply_to_capture(self, context_helper): |
| 2569 | + with mock.patch("posthog.client.batch_post") as mock_post: |
| 2570 | + client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True) |
| 2571 | + |
| 2572 | + def capture_in_context(): |
| 2573 | + client.tag("client_tag", "tag-value") |
| 2574 | + client.identify_context("context-user") |
| 2575 | + client.set_context_session("context-session-123") |
| 2576 | + |
| 2577 | + self.assertEqual(client.get_tags(), {"client_tag": "tag-value"}) |
| 2578 | + |
| 2579 | + return client.capture( |
| 2580 | + "test_event", |
| 2581 | + properties={"custom_prop": "value"}, |
| 2582 | + ) |
| 2583 | + |
| 2584 | + if context_helper == "new_context": |
| 2585 | + with client.new_context(fresh=True): |
| 2586 | + msg_uuid = capture_in_context() |
| 2587 | + else: |
| 2588 | + |
| 2589 | + @client.scoped(fresh=True) |
| 2590 | + def scoped_capture(): |
| 2591 | + return capture_in_context() |
| 2592 | + |
| 2593 | + msg_uuid = scoped_capture() |
| 2594 | + |
| 2595 | + self.assertIsNotNone(msg_uuid) |
| 2596 | + mock_post.assert_called_once() |
| 2597 | + batch_data = mock_post.call_args[1]["batch"] |
| 2598 | + msg = batch_data[0] |
| 2599 | + |
| 2600 | + self.assertEqual(msg["distinct_id"], "context-user") |
| 2601 | + self.assertEqual(msg["properties"]["client_tag"], "tag-value") |
| 2602 | + self.assertEqual(msg["properties"]["custom_prop"], "value") |
| 2603 | + self.assertEqual(msg["properties"]["$session_id"], "context-session-123") |
| 2604 | + self.assertCountEqual(msg["properties"]["$context_tags"], ["client_tag"]) |
| 2605 | + self.assertEqual(client.get_tags(), {}) |
| 2606 | + |
| 2607 | + def test_client_scoped_context_helpers_apply_to_capture_async(self): |
| 2608 | + with mock.patch("posthog.client.batch_post") as mock_post: |
| 2609 | + client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True) |
| 2610 | + |
| 2611 | + @client.scoped(fresh=True) |
| 2612 | + async def scoped_capture(): |
| 2613 | + client.tag("async_scoped_tag", "async-scoped-value") |
| 2614 | + client.identify_context("async-scoped-user") |
| 2615 | + client.set_context_session("async-scoped-session-123") |
| 2616 | + await asyncio.sleep(0) |
| 2617 | + return client.capture("async_scoped_event") |
| 2618 | + |
| 2619 | + msg_uuid = asyncio.run(scoped_capture()) |
| 2620 | + |
| 2621 | + self.assertIsNotNone(msg_uuid) |
| 2622 | + mock_post.assert_called_once() |
| 2623 | + batch_data = mock_post.call_args[1]["batch"] |
| 2624 | + msg = batch_data[0] |
| 2625 | + |
| 2626 | + self.assertEqual(msg["distinct_id"], "async-scoped-user") |
| 2627 | + self.assertEqual( |
| 2628 | + msg["properties"]["async_scoped_tag"], "async-scoped-value" |
| 2629 | + ) |
| 2630 | + self.assertEqual( |
| 2631 | + msg["properties"]["$session_id"], "async-scoped-session-123" |
| 2632 | + ) |
| 2633 | + self.assertCountEqual( |
| 2634 | + msg["properties"]["$context_tags"], ["async_scoped_tag"] |
| 2635 | + ) |
| 2636 | + self.assertEqual(client.get_tags(), {}) |
| 2637 | + |
2537 | 2638 | def test_set_context_session_with_page_explicit_properties(self): |
2538 | 2639 | with mock.patch("posthog.client.batch_post") as mock_post: |
2539 | 2640 | client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, sync_mode=True) |
|
0 commit comments