Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed Jul 18, 2023
1 parent f4f83e5 commit 3c03ee8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
7 changes: 4 additions & 3 deletions flagsmith/flagsmith.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(
"""

self.offline_mode = offline_mode
self.enable_local_evaluation = enable_local_evaluation
self.offline_handler = offline_handler
self.default_flag_handler = default_flag_handler
self._analytics_processor = None
Expand Down Expand Up @@ -117,7 +118,7 @@ def __init__(
self.identities_url = f"{self.api_url}identities/"
self.environment_url = f"{self.api_url}environment-document/"

if enable_local_evaluation:
if self.enable_local_evaluation:
if not environment_key.startswith("ser."):
raise ValueError(
"In order to use local evaluation, please generate a server key "
Expand All @@ -144,7 +145,7 @@ def get_environment_flags(self) -> Flags:
:return: Flags object holding all the flags for the current environment.
"""
if self._environment:
if (self.offline_mode or self.enable_local_evaluation) and self._environment:
return self._get_environment_flags_from_document()
return self._get_environment_flags_from_api()

Expand All @@ -163,7 +164,7 @@ def get_identity_flags(
:return: Flags object holding all the flags for the given identity.
"""
traits = traits or {}
if self._environment:
if (self.offline_mode or self.enable_local_evaluation) and self._environment:
return self._get_identity_flags_from_document(identifier, traits)
return self._get_identity_flags_from_api(identifier, traits)

Expand Down
40 changes: 40 additions & 0 deletions tests/test_flagsmith.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def test_get_environment_flags_uses_local_environment_when_available(
):
# Given
flagsmith._environment = environment_model
flagsmith.enable_local_evaluation = True

# When
all_flags = flagsmith.get_environment_flags().all_flags()
Expand Down Expand Up @@ -140,6 +141,7 @@ def test_get_identity_flags_uses_local_environment_when_available(
):
# Given
flagsmith._environment = environment_model
flagsmith.enable_local_evaluation = True
mock_engine = mocker.patch("flagsmith.flagsmith.engine")

feature_state = FeatureStateModel(
Expand Down Expand Up @@ -434,3 +436,41 @@ def test_flagsmith_uses_offline_handler_if_set_and_no_api_response(

assert identity_flags.is_feature_enabled("some_feature") is True
assert identity_flags.get_feature_value("some_feature") == "some-value"


def test_cannot_use_offline_mode_without_offline_handler():
with pytest.raises(ValueError) as e:
# When
Flagsmith(offline_mode=True, offline_handler=None)

# Then
assert (
e.exconly()
== "ValueError: offline_handler must be provided to use offline mode."
)


def test_cannot_use_default_handler_and_offline_handler(mocker):
# When
with pytest.raises(ValueError) as e:
Flagsmith(
offline_handler=mocker.MagicMock(spec=BaseOfflineHandler),
default_flag_handler=lambda flag_name: DefaultFlag(
enabled=True, value="foo"
),
)

# Then
assert (
e.exconly()
== "ValueError: Cannot use both default_flag_handler and offline_handler."
)


def test_cannot_create_flagsmith_client_in_remote_evaluation_without_api_key():
# When
with pytest.raises(ValueError) as e:
Flagsmith()

# Then
assert e.exconly() == "ValueError: environment_key is required."

0 comments on commit 3c03ee8

Please sign in to comment.