From d5292e3bc74bc4ba00b2e46891c59a16321a58a1 Mon Sep 17 00:00:00 2001 From: Neil Kakkar Date: Fri, 14 May 2021 12:49:12 +0100 Subject: [PATCH 1/2] fix feature flag issue with no % rollout --- posthog/client.py | 4 ++-- posthog/test/test_client.py | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/posthog/client.py b/posthog/client.py index 0588082..22157cf 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -330,8 +330,8 @@ def feature_enabled(self, key, distinct_id, default=False): except IndexError: return default - if feature_flag.get("is_simple_flag") and feature_flag.get("rollout_percentage"): - response = _hash(key, distinct_id) <= (feature_flag["rollout_percentage"] / 100) + if feature_flag.get("is_simple_flag"): + response = _hash(key, distinct_id) <= ((feature_flag.get("rollout_percentage", 100) or 100) / 100) else: try: request_data = { diff --git a/posthog/test/test_client.py b/posthog/test/test_client.py index e4971db..6806bbd 100644 --- a/posthog/test/test_client.py +++ b/posthog/test/test_client.py @@ -325,14 +325,30 @@ def test_feature_enabled_simple(self, patch_get): self.assertTrue(client.feature_enabled("beta-feature", "distinct_id")) @mock.patch("posthog.client.decide") - def test_feature_enabled_request(self, patch_get): - patch_get.return_value = {"featureFlags": ["beta-feature"]} + def test_feature_enabled_request(self, patch_decide): + patch_decide.return_value = {"featureFlags": ["beta-feature"]} client = Client(TEST_API_KEY) client.feature_flags = [ {"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": False, "rollout_percentage": 100} ] self.assertTrue(client.feature_enabled("beta-feature", "distinct_id")) + @mock.patch("posthog.client.get") + def test_feature_enabled_simple_without_rollout_percentage(self, patch_get): + client = Client(TEST_API_KEY) + client.feature_flags = [ + {"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True} + ] + self.assertTrue(client.feature_enabled("beta-feature", "distinct_id")) + + @mock.patch("posthog.client.get") + def test_feature_enabled_simple_with_none_rollout_percentage(self, patch_get): + client = Client(TEST_API_KEY) + client.feature_flags = [ + {"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True, "rollout_percantage": None} + ] + self.assertTrue(client.feature_enabled("beta-feature", "distinct_id")) + @mock.patch("posthog.client.Poller") @mock.patch("posthog.client.get") def test_feature_enabled_doesnt_exist(self, patch_get, patch_poll): From 6d4e6f330e95aa21c93ed3952a43e30872611890 Mon Sep 17 00:00:00 2001 From: Neil Kakkar Date: Fri, 14 May 2021 12:53:20 +0100 Subject: [PATCH 2/2] black --- posthog/test/test_client.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/posthog/test/test_client.py b/posthog/test/test_client.py index 6806bbd..9c867a0 100644 --- a/posthog/test/test_client.py +++ b/posthog/test/test_client.py @@ -336,9 +336,7 @@ def test_feature_enabled_request(self, patch_decide): @mock.patch("posthog.client.get") def test_feature_enabled_simple_without_rollout_percentage(self, patch_get): client = Client(TEST_API_KEY) - client.feature_flags = [ - {"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True} - ] + client.feature_flags = [{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True}] self.assertTrue(client.feature_enabled("beta-feature", "distinct_id")) @mock.patch("posthog.client.get")