Skip to content

Commit 2ef214a

Browse files
committed
tests
1 parent fdf8f54 commit 2ef214a

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

posthog/test/test_feature_flags.py

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,9 @@ def test_boolean_feature_flag_payloads_local(self, patch_decide):
16321632
)
16331633
self.assertEqual(patch_decide.call_count, 0)
16341634

1635+
@mock.patch.object(Client, "capture")
16351636
@mock.patch("posthog.client.decide")
1636-
def test_boolean_feature_flag_payload_decide(self, patch_decide):
1637+
def test_boolean_feature_flag_payload_decide(self, patch_decide, patch_capture):
16371638
patch_decide.return_value = {"featureFlagPayloads": {"person-flag": 300}}
16381639
self.assertEqual(
16391640
self.client.get_feature_flag_payload(
@@ -1649,6 +1650,20 @@ def test_boolean_feature_flag_payload_decide(self, patch_decide):
16491650
300,
16501651
)
16511652
self.assertEqual(patch_decide.call_count, 3)
1653+
self.assertEqual(patch_capture.call_count, 1)
1654+
# patch_capture.assert_called_with(
1655+
# "some-distinct-id",
1656+
# "$feature_flag_called",
1657+
# {
1658+
# "$feature_flag": "person-flag",
1659+
# "$feature_flag_response": True,
1660+
# "locally_evaluated": False,
1661+
# "$feature/complex-flag": True,
1662+
# },
1663+
# groups={},
1664+
# disable_geoip=None,
1665+
# )
1666+
patch_capture.reset_mock()
16521667

16531668
@mock.patch("posthog.client.decide")
16541669
def test_multivariate_feature_flag_payloads(self, patch_decide):
@@ -2334,6 +2349,97 @@ def test_capture_is_called(self, patch_decide, patch_capture):
23342349
disable_geoip=None,
23352350
)
23362351

2352+
2353+
@mock.patch.object(Client, "capture")
2354+
@mock.patch("posthog.client.decide")
2355+
def test_capture_is_called_in_get_feature_flag_payload(self, patch_decide, patch_capture):
2356+
# Mock the decide response
2357+
patch_decide.return_value = {"featureFlags": {"decide-flag": "decide-value"}, "featureFlagPayloads": {"person-flag": 300}}
2358+
2359+
# Initialize the Client with necessary keys
2360+
client = Client(api_key=FAKE_TEST_API_KEY, personal_api_key=FAKE_TEST_API_KEY)
2361+
2362+
# Set up feature flags
2363+
client.feature_flags = [
2364+
{
2365+
"id": 1,
2366+
"name": "Beta Feature",
2367+
"key": "complex-flag",
2368+
"is_simple_flag": False,
2369+
"active": True,
2370+
"filters": {
2371+
"groups": [
2372+
{
2373+
"properties": [{"key": "region", "value": "USA"}],
2374+
"rollout_percentage": 100,
2375+
}
2376+
],
2377+
},
2378+
}
2379+
]
2380+
2381+
# Call get_feature_flag_payload with match_value=None to trigger get_feature_flag
2382+
client.get_feature_flag_payload(
2383+
key="complex-flag",
2384+
distinct_id="some-distinct-id",
2385+
person_properties={"region": "USA", "name": "Aloha"}
2386+
)
2387+
2388+
# Assert that capture was called once
2389+
self.assertEqual(patch_capture.call_count, 1)
2390+
2391+
# Verify the capture was called with the correct parameters
2392+
patch_capture.assert_called_with(
2393+
"some-distinct-id",
2394+
"$feature_flag_called",
2395+
{
2396+
"$feature_flag": "complex-flag",
2397+
"$feature_flag_response": True,
2398+
"locally_evaluated": True,
2399+
"$feature/complex-flag": True,
2400+
},
2401+
groups={},
2402+
disable_geoip=None,
2403+
)
2404+
2405+
# Reset mocks for further tests
2406+
patch_capture.reset_mock()
2407+
patch_decide.reset_mock()
2408+
2409+
# Call get_feature_flag_payload again for the same user; capture should not be called again
2410+
client.get_feature_flag_payload(
2411+
key="complex-flag",
2412+
distinct_id="some-distinct-id",
2413+
person_properties={"region": "USA", "name": "Aloha"}
2414+
)
2415+
2416+
self.assertEqual(patch_capture.call_count, 0)
2417+
patch_capture.reset_mock()
2418+
2419+
# Call get_feature_flag_payload for a different user; capture should be called
2420+
client.get_feature_flag_payload(
2421+
key="complex-flag",
2422+
distinct_id="some-distinct-id2",
2423+
person_properties={"region": "USA", "name": "Aloha"}
2424+
)
2425+
2426+
# self.assertIsNotNone(payload)
2427+
self.assertEqual(patch_capture.call_count, 1)
2428+
patch_capture.assert_called_with(
2429+
"some-distinct-id2",
2430+
"$feature_flag_called",
2431+
{
2432+
"$feature_flag": "complex-flag",
2433+
"$feature_flag_response": True,
2434+
"locally_evaluated": True,
2435+
"$feature/complex-flag": True,
2436+
},
2437+
groups={},
2438+
disable_geoip=None,
2439+
)
2440+
2441+
patch_capture.reset_mock()
2442+
23372443
@mock.patch.object(Client, "capture")
23382444
@mock.patch("posthog.client.decide")
23392445
def test_disable_geoip_get_flag_capture_call(self, patch_decide, patch_capture):

0 commit comments

Comments
 (0)