Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix feature flag issue with no % rollout #30

Merged
merged 2 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
18 changes: 16 additions & 2 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,28 @@ 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):
Expand Down