Skip to content

Commit

Permalink
Merge pull request #10 from deftinc/fix/feature-list-is-not-available
Browse files Browse the repository at this point in the history
fix KeyError by providing empty dict as default value
  • Loading branch information
vinbarnes authored Apr 11, 2024
2 parents 9e3ca13 + 8c2feac commit 1306fd5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion feature_gate/clients/posthog_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def create_feature(self, name, description, deleted=False, active=False):
return self._map_single_response("POST", path, response)

def fetch_feature(self, key):
features = self.list_features()["data"]
features = self.list_features().get("data", [])
for entry in features:
if "key" in entry and entry["key"] == key:
return entry
Expand Down
11 changes: 10 additions & 1 deletion tests/feature_gate/adapters/posthog_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from feature_gate.adapters.posthog import PosthogAdapter
from feature_gate.client import Client, FeatureNotFound
from feature_gate.feature import Feature
from tests.fixtures.posthog_api_client.mocks import build_feature_from_mocks, mock_add_feature_funnel, mock_disable_feature_funnel, mock_enable_feature_funnel, mock_features_when_empty, mock_features_when_funnel, mock_funnel_is_disabled, mock_funnel_is_enabled, mock_remove_feature_funnel
from tests.fixtures.posthog_api_client.mocks import build_feature_from_mocks, mock_add_feature_funnel, mock_disable_feature_funnel, mock_enable_feature_funnel, mock_features_when_empty, mock_features_when_error_returned, mock_features_when_funnel, mock_funnel_is_disabled, mock_funnel_is_enabled, mock_remove_feature_funnel
from unittest.mock import patch

def configured_client():
Expand Down Expand Up @@ -87,6 +87,15 @@ def test_is_enabled_raises_an_error_when_the_feature_does_not_exist():
except FeatureNotFound as e:
assert str(e) == "Feature funnel_test not found"

def test_is_enabled_raises_an_error_when_the_api_response_returns_an_error_status():
client = configured_client()
feature = build_feature_from_mocks()
with patch.object(requests, 'get', return_value=mock_features_when_error_returned()):
try:
resp = client.is_enabled(feature.key)
except FeatureNotFound as e:
assert str(e) == "Feature funnel_test not found"

def test_enable_returns_true_when_feature_exists():
client = configured_client()
feature = build_feature_from_mocks()
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/posthog_api_client/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ def mock_funnel_is_disabled():
return_value=load_response('funnel_is_disabled')
)
)

def mock_features_when_error_returned():
return Mock(
status_code=500,
json=Mock(
return_value=load_response('get_features_when_empty')
)
)

0 comments on commit 1306fd5

Please sign in to comment.