diff --git a/flagsmith/flagsmith.py b/flagsmith/flagsmith.py index b07b1ed..226161c 100644 --- a/flagsmith/flagsmith.py +++ b/flagsmith/flagsmith.py @@ -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 @@ -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 " @@ -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() @@ -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) diff --git a/tests/test_flagsmith.py b/tests/test_flagsmith.py index b6f1cbd..baaa9cb 100644 --- a/tests/test_flagsmith.py +++ b/tests/test_flagsmith.py @@ -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() @@ -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( @@ -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."