Skip to content

Commit 310ee91

Browse files
committed
Add unit test for payloads
1 parent 68c7b39 commit 310ee91

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

posthog/test/test_feature_flags.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4075,6 +4075,62 @@ def test_capture_is_called_in_get_feature_flag_payload(
40754075

40764076
patch_capture.reset_mock()
40774077

4078+
@mock.patch("posthog.client.flags")
4079+
def test_fallback_to_api_in_get_feature_flag_payload_when_flag_has_static_cohort(
4080+
self, patch_flags
4081+
):
4082+
"""
4083+
Test that get_feature_flag_payload falls back to API when evaluating
4084+
a flag with static cohorts, similar to get_feature_flag behavior.
4085+
"""
4086+
client = Client(FAKE_TEST_API_KEY, personal_api_key=FAKE_TEST_API_KEY)
4087+
4088+
# Mock the local flags response - cohort 999 is NOT in cohorts map (static cohort)
4089+
client.feature_flags = [
4090+
{
4091+
"id": 1,
4092+
"name": "Multi-condition Flag",
4093+
"key": "multi-condition-flag",
4094+
"active": True,
4095+
"filters": {
4096+
"groups": [
4097+
{
4098+
"properties": [
4099+
{"key": "id", "value": 999, "type": "cohort"}
4100+
],
4101+
"rollout_percentage": 100,
4102+
"variant": "variant-1",
4103+
}
4104+
],
4105+
"multivariate": {
4106+
"variants": [
4107+
{"key": "variant-1", "rollout_percentage": 100}
4108+
]
4109+
},
4110+
"payloads": {"variant-1": '{"message": "local-payload"}'},
4111+
},
4112+
}
4113+
]
4114+
client.cohorts = {} # Note: cohort 999 is NOT here - it's a static cohort
4115+
4116+
# Mock the API response - user is in the static cohort
4117+
patch_flags.return_value = {
4118+
"featureFlags": {"multi-condition-flag": "variant-1"},
4119+
"featureFlagPayloads": {"multi-condition-flag": '{"message": "from-api"}'},
4120+
}
4121+
4122+
# Call get_feature_flag_payload without match_value to trigger evaluation
4123+
result = client.get_feature_flag_payload(
4124+
"multi-condition-flag",
4125+
"test-distinct-id",
4126+
)
4127+
4128+
# Should return the API payload, not local payload
4129+
self.assertEqual(result, {"message": "from-api"})
4130+
4131+
# Verify API was called (fallback occurred)
4132+
self.assertEqual(patch_flags.call_count, 1)
4133+
40784134
@mock.patch.object(Client, "capture")
40794135
@mock.patch("posthog.client.flags")
40804136
def test_disable_geoip_get_flag_capture_call(self, patch_flags, patch_capture):

0 commit comments

Comments
 (0)