11import time
22import unittest
33from datetime import date , datetime
4+ from unittest .mock import MagicMock
45from uuid import uuid4
56
67import mock
@@ -74,6 +75,71 @@ def test_basic_capture_with_project_api_key(self):
7475 self .assertEqual (msg ["properties" ]["$lib" ], "posthog-python" )
7576 self .assertEqual (msg ["properties" ]["$lib_version" ], VERSION )
7677
78+ @mock .patch ("posthog.client.decide" )
79+ def test_basic_capture_with_feature_flags (self , patch_decide ):
80+ patch_decide .return_value = {"featureFlags" : {"beta-feature" : "random-variant" }}
81+
82+ client = Client (TEST_API_KEY , on_error = self .set_fail , personal_api_key = TEST_API_KEY )
83+ success , msg = client .capture ("distinct_id" , "python test event" , send_feature_flags = True )
84+ client .flush ()
85+ self .assertTrue (success )
86+ self .assertFalse (self .failed )
87+
88+ self .assertEqual (msg ["event" ], "python test event" )
89+ self .assertTrue (isinstance (msg ["timestamp" ], str ))
90+ self .assertIsNone (msg .get ("uuid" ))
91+ self .assertEqual (msg ["distinct_id" ], "distinct_id" )
92+ self .assertEqual (msg ["properties" ]["$lib" ], "posthog-python" )
93+ self .assertEqual (msg ["properties" ]["$lib_version" ], VERSION )
94+ self .assertEqual (msg ["properties" ]["$feature/beta-feature" ], "random-variant" )
95+ self .assertEqual (msg ["properties" ]["$active_feature_flags" ], ["beta-feature" ])
96+
97+ self .assertEqual (patch_decide .call_count , 1 )
98+
99+ @mock .patch ("posthog.client.decide" )
100+ def test_basic_capture_with_feature_flags_switched_off_doesnt_send_them (self , patch_decide ):
101+ patch_decide .return_value = {"featureFlags" : {"beta-feature" : "random-variant" }}
102+
103+ client = Client (TEST_API_KEY , on_error = self .set_fail , personal_api_key = TEST_API_KEY )
104+ success , msg = client .capture ("distinct_id" , "python test event" , send_feature_flags = False )
105+ client .flush ()
106+ self .assertTrue (success )
107+ self .assertFalse (self .failed )
108+
109+ self .assertEqual (msg ["event" ], "python test event" )
110+ self .assertTrue (isinstance (msg ["timestamp" ], str ))
111+ self .assertIsNone (msg .get ("uuid" ))
112+ self .assertEqual (msg ["distinct_id" ], "distinct_id" )
113+ self .assertEqual (msg ["properties" ]["$lib" ], "posthog-python" )
114+ self .assertEqual (msg ["properties" ]["$lib_version" ], VERSION )
115+ self .assertTrue ("$feature/beta-feature" not in msg ["properties" ])
116+ self .assertTrue ("$active_feature_flags" not in msg ["properties" ])
117+
118+ self .assertEqual (patch_decide .call_count , 0 )
119+
120+ @mock .patch ("posthog.client.decide" )
121+ def test_basic_capture_with_feature_flags_without_api_key (self , patch_decide ):
122+ patch_decide .return_value = {"featureFlags" : {"beta-feature" : "random-variant" }}
123+
124+ client = Client (project_api_key = TEST_API_KEY , on_error = self .set_fail )
125+ client .log = MagicMock ()
126+ success , msg = client .capture ("distinct_id" , "python test event" , send_feature_flags = True )
127+ client .flush ()
128+ self .assertTrue (success )
129+ self .assertFalse (self .failed )
130+
131+ self .assertEqual (msg ["event" ], "python test event" )
132+ self .assertTrue (isinstance (msg ["timestamp" ], str ))
133+ self .assertIsNone (msg .get ("uuid" ))
134+ self .assertEqual (msg ["distinct_id" ], "distinct_id" )
135+ self .assertEqual (msg ["properties" ]["$lib" ], "posthog-python" )
136+ self .assertEqual (msg ["properties" ]["$lib_version" ], VERSION )
137+
138+ self .assertEqual (client .log .exception .call_count , 1 )
139+ client .log .exception .assert_called_with (
140+ "[FEATURE FLAGS] Unable to get feature variants: You have to specify a personal_api_key to use feature flags."
141+ )
142+
77143 def test_stringifies_distinct_id (self ):
78144 # A large number that loses precision in node:
79145 # node -e "console.log(157963456373623802 + 1)" > 157963456373623800
@@ -424,6 +490,16 @@ def test_feature_enabled_simple_is_false(self, patch_get, patch_decide):
424490 self .assertFalse (client .feature_enabled ("beta-feature" , "distinct_id" ))
425491 self .assertEqual (patch_decide .call_count , 0 )
426492
493+ @mock .patch ("posthog.client.decide" )
494+ @mock .patch ("posthog.client.get" )
495+ def test_feature_enabled_simple_is_true_when_rollout_is_undefined (self , patch_get , patch_decide ):
496+ client = Client (TEST_API_KEY )
497+ client .feature_flags = [
498+ {"id" : 1 , "name" : "Beta Feature" , "key" : "beta-feature" , "is_simple_flag" : True , "rollout_percentage" : None }
499+ ]
500+ self .assertTrue (client .feature_enabled ("beta-feature" , "distinct_id" ))
501+ self .assertEqual (patch_decide .call_count , 0 )
502+
427503 @mock .patch ("posthog.client.get" )
428504 def test_feature_enabled_simple_with_project_api_key (self , patch_get ):
429505 client = Client (project_api_key = TEST_API_KEY , on_error = self .set_fail )
@@ -435,7 +511,7 @@ def test_feature_enabled_simple_with_project_api_key(self, patch_get):
435511 @mock .patch ("posthog.client.decide" )
436512 def test_feature_enabled_request (self , patch_decide ):
437513 patch_decide .return_value = {"featureFlags" : {"beta-feature" : True }}
438- client = Client (TEST_API_KEY )
514+ client = Client (TEST_API_KEY , personal_api_key = "test" )
439515 client .feature_flags = [
440516 {"id" : 1 , "name" : "Beta Feature" , "key" : "beta-feature" , "is_simple_flag" : False , "rollout_percentage" : 100 }
441517 ]
@@ -444,7 +520,7 @@ def test_feature_enabled_request(self, patch_decide):
444520 @mock .patch ("posthog.client.decide" )
445521 def test_feature_enabled_request_multi_variate (self , patch_decide ):
446522 patch_decide .return_value = {"featureFlags" : {"beta-feature" : "variant-1" }}
447- client = Client (TEST_API_KEY )
523+ client = Client (TEST_API_KEY , personal_api_key = "test" )
448524 client .feature_flags = [
449525 {"id" : 1 , "name" : "Beta Feature" , "key" : "beta-feature" , "is_simple_flag" : False , "rollout_percentage" : 100 }
450526 ]
@@ -468,7 +544,7 @@ def test_feature_enabled_simple_with_none_rollout_percentage(self, patch_get):
468544 @mock .patch ("posthog.client.decide" )
469545 def test_feature_enabled_doesnt_exist (self , patch_decide , patch_poll ):
470546 patch_decide .return_value = {"featureFlags" : {}}
471- client = Client (TEST_API_KEY , personal_api_key = "test" )
547+ client = Client (TEST_API_KEY )
472548 client .feature_flags = []
473549
474550 self .assertFalse (client .feature_enabled ("doesnt-exist" , "distinct_id" ))
@@ -477,7 +553,7 @@ def test_feature_enabled_doesnt_exist(self, patch_decide, patch_poll):
477553 @mock .patch ("posthog.client.Poller" )
478554 @mock .patch ("posthog.client.decide" )
479555 def test_personal_api_key_doesnt_exist (self , patch_decide , patch_poll ):
480- client = Client (TEST_API_KEY )
556+ client = Client (TEST_API_KEY , personal_api_key = "test" )
481557 client .feature_flags = []
482558
483559 patch_decide .return_value = {"featureFlags" : {"feature-flag" : True }}
0 commit comments