11import unittest
22from parameterized import parameterized
3- from posthog .types import FeatureFlag , FlagMetadata , FlagReason , LegacyFlagMetadata , normalize_decide_response , to_flags_and_payloads
3+ from posthog .types import (
4+ FeatureFlag ,
5+ FlagMetadata ,
6+ FlagReason ,
7+ LegacyFlagMetadata ,
8+ normalize_decide_response ,
9+ to_flags_and_payloads ,
10+ )
11+
412
513class TestTypes (unittest .TestCase ):
614 @parameterized .expand ([(True ,), (False ,)])
@@ -22,7 +30,7 @@ def test_normalize_decide_response_v4(self, has_errors: bool):
2230 }
2331
2432 result = normalize_decide_response (resp )
25-
33+
2634 flag = result ["flags" ]["my-flag" ]
2735 self .assertEqual (flag .key , "my-flag" )
2836 self .assertTrue (flag .enabled )
@@ -41,7 +49,7 @@ def test_normalize_decide_response_legacy(self):
4149 # Test legacy response format with "featureFlags" and "featureFlagPayloads"
4250 resp = {
4351 "featureFlags" : {"my-flag" : "test-variant" },
44- "featureFlagPayloads" : {"my-flag" : "{ \ " some\ " : \ " json-payload\" }" },
52+ "featureFlagPayloads" : {"my-flag" : '{ "some": "json-payload"}' },
4553 "errorsWhileComputingFlags" : False ,
4654 "requestId" : "test-id" ,
4755 }
@@ -54,9 +62,7 @@ def test_normalize_decide_response_legacy(self):
5462 self .assertEqual (flag .variant , "test-variant" )
5563 self .assertEqual (flag .get_value (), "test-variant" )
5664 self .assertIsNone (flag .reason )
57- self .assertEqual (
58- flag .metadata , LegacyFlagMetadata (payload = '{"some": "json-payload"}' )
59- )
65+ self .assertEqual (flag .metadata , LegacyFlagMetadata (payload = '{"some": "json-payload"}' ))
6066 self .assertFalse (result ["errorsWhileComputingFlags" ])
6167 self .assertEqual (result ["requestId" ], "test-id" )
6268 # Verify legacy fields are removed
@@ -65,10 +71,7 @@ def test_normalize_decide_response_legacy(self):
6571
6672 def test_normalize_decide_response_boolean_flag (self ):
6773 # Test legacy response with boolean flag
68- resp = {
69- "featureFlags" : {"my-flag" : True },
70- "errorsWhileComputingFlags" : False
71- }
74+ resp = {"featureFlags" : {"my-flag" : True }, "errorsWhileComputingFlags" : False }
7275
7376 result = normalize_decide_response (resp )
7477
@@ -80,9 +83,7 @@ def test_normalize_decide_response_boolean_flag(self):
8083 self .assertTrue (flag .enabled )
8184 self .assertIsNone (flag .variant )
8285 self .assertIsNone (flag .reason )
83- self .assertEqual (
84- flag .metadata , LegacyFlagMetadata (payload = None )
85- )
86+ self .assertEqual (flag .metadata , LegacyFlagMetadata (payload = None ))
8687 self .assertFalse (result ["errorsWhileComputingFlags" ])
8788 self .assertNotIn ("featureFlags" , result )
8889 self .assertNotIn ("featureFlagPayloads" , result )
@@ -115,22 +116,21 @@ def test_to_flags_and_payloads_v4(self):
115116 variant = None ,
116117 reason = None ,
117118 metadata = LegacyFlagMetadata (payload = None ),
118- )
119+ ),
119120 },
120121 "errorsWhileComputingFlags" : False ,
121122 "requestId" : "test-id" ,
122123 }
123124
124125 result = to_flags_and_payloads (resp )
125-
126+
126127 self .assertEqual (result ["featureFlags" ]["my-variant-flag" ], "test-variant" )
127128 self .assertEqual (result ["featureFlags" ]["my-boolean-flag" ], True )
128129 self .assertEqual (result ["featureFlags" ]["disabled-flag" ], False )
129130 self .assertEqual (result ["featureFlagPayloads" ]["my-variant-flag" ], '{"some": "json"}' )
130131 self .assertEqual (result ["featureFlagPayloads" ]["my-boolean-flag" ], None )
131132 self .assertNotIn ("disabled-flag" , result ["featureFlagPayloads" ])
132133
133-
134134 def test_to_flags_and_payloads_empty (self ):
135135 # Test empty response
136136 resp = {
@@ -140,7 +140,7 @@ def test_to_flags_and_payloads_empty(self):
140140 }
141141
142142 result = to_flags_and_payloads (resp )
143-
143+
144144 self .assertEqual (result ["featureFlags" ], {})
145145 self .assertEqual (result ["featureFlagPayloads" ], {})
146146
@@ -159,7 +159,7 @@ def test_to_flags_and_payloads_with_payload(self):
159159 "metadata" : {
160160 "id" : 23 ,
161161 "version" : 42 ,
162- "payload" : "{ \ " foo\ " : \ " bar\" }" ,
162+ "payload" : '{ "foo": "bar"}' ,
163163 },
164164 }
165165 },
@@ -170,25 +170,16 @@ def test_to_flags_and_payloads_with_payload(self):
170170 result = to_flags_and_payloads (normalized )
171171
172172 self .assertEqual (result ["featureFlags" ]["decide-flag" ], "decide-variant" )
173- self .assertEqual (result ["featureFlagPayloads" ]["decide-flag" ], "{ \ " foo\ " : \ " bar\" }" )
173+ self .assertEqual (result ["featureFlagPayloads" ]["decide-flag" ], '{ "foo": "bar"}' )
174174
175175 def test_feature_flag_from_json (self ):
176176 # Test with full metadata
177177 resp = {
178178 "key" : "test-flag" ,
179179 "enabled" : True ,
180180 "variant" : "test-variant" ,
181- "reason" : {
182- "code" : "matched_condition" ,
183- "condition_index" : 0 ,
184- "description" : "Matched condition set 1"
185- },
186- "metadata" : {
187- "id" : 1 ,
188- "payload" : '{"some": "json"}' ,
189- "version" : 2 ,
190- "description" : "test-description"
191- }
181+ "reason" : {"code" : "matched_condition" , "condition_index" : 0 , "description" : "Matched condition set 1" },
182+ "metadata" : {"id" : 1 , "payload" : '{"some": "json"}' , "version" : 2 , "description" : "test-description" },
192183 }
193184
194185 flag = FeatureFlag .from_json (resp )
@@ -205,10 +196,7 @@ def test_feature_flag_from_json(self):
205196
206197 def test_feature_flag_from_json_minimal (self ):
207198 # Test with minimal required fields
208- resp = {
209- "key" : "test-flag" ,
210- "enabled" : True
211- }
199+ resp = {"key" : "test-flag" , "enabled" : True }
212200
213201 flag = FeatureFlag .from_json (resp )
214202 self .assertEqual (flag .key , "test-flag" )
@@ -224,11 +212,7 @@ def test_feature_flag_from_json_without_metadata(self):
224212 "key" : "test-flag" ,
225213 "enabled" : True ,
226214 "variant" : "test-variant" ,
227- "reason" : {
228- "code" : "matched_condition" ,
229- "condition_index" : 0 ,
230- "description" : "Matched condition set 1"
231- }
215+ "reason" : {"code" : "matched_condition" , "condition_index" : 0 , "description" : "Matched condition set 1" },
232216 }
233217
234218 flag = FeatureFlag .from_json (resp )
0 commit comments