|
5 | 5 | from openfeature.provider import ProviderStatus |
6 | 6 |
|
7 | 7 |
|
| 8 | +# Mock feature response for testing cache functionality |
| 9 | +MOCK_FEATURE_RESPONSE = { |
| 10 | + "version": 1, |
| 11 | + "features": [ |
| 12 | + { |
| 13 | + "name": "testFlag", |
| 14 | + "description": "This is a test!", |
| 15 | + "enabled": True, |
| 16 | + "strategies": [{"name": "default", "parameters": {}}], |
| 17 | + "createdAt": "2018-10-04T01:27:28.477Z", |
| 18 | + "impressionData": True, |
| 19 | + }, |
| 20 | + { |
| 21 | + "name": "testFlag2", |
| 22 | + "description": "Test flag 2", |
| 23 | + "enabled": False, |
| 24 | + "strategies": [ |
| 25 | + {"name": "gradualRolloutRandom", "parameters": {"percentage": "50"}} |
| 26 | + ], |
| 27 | + "createdAt": "2018-10-04T11:03:56.062Z", |
| 28 | + "impressionData": False, |
| 29 | + }, |
| 30 | + ], |
| 31 | +} |
| 32 | + |
| 33 | + |
8 | 34 | def test_unleash_provider_import(): |
9 | 35 | """Test that UnleashProvider can be imported.""" |
10 | 36 | assert UnleashProvider is not None |
@@ -160,3 +186,49 @@ def test_unleash_provider_flag_metadata(): |
160 | 186 | assert result.flag_metadata["app_name"] == "test-app" |
161 | 187 |
|
162 | 188 | provider.shutdown() |
| 189 | + |
| 190 | + |
| 191 | +def test_unleash_provider_with_custom_cache(): |
| 192 | + """Test that UnleashProvider properly uses a custom cache with mocked features.""" |
| 193 | + from UnleashClient.cache import FileCache |
| 194 | + |
| 195 | + # Create a custom cache with mocked features |
| 196 | + custom_cache = FileCache("test-app") |
| 197 | + custom_cache.bootstrap_from_dict(MOCK_FEATURE_RESPONSE) |
| 198 | + |
| 199 | + # Create provider with custom cache |
| 200 | + provider = UnleashProvider( |
| 201 | + url="http://localhost:4242", |
| 202 | + app_name="test-app", |
| 203 | + api_token="test-token", |
| 204 | + cache=custom_cache, |
| 205 | + fetch_toggles=False, |
| 206 | + ) |
| 207 | + |
| 208 | + # Verify cache was stored |
| 209 | + assert provider.cache is custom_cache |
| 210 | + |
| 211 | + # Initialize the provider with fetch_toggles=False to prevent server connection |
| 212 | + provider.initialize() |
| 213 | + |
| 214 | + # Verify the provider is ready |
| 215 | + assert provider.get_status() == ProviderStatus.READY |
| 216 | + assert provider.client is not None |
| 217 | + |
| 218 | + # Test flag evaluation using the cached features |
| 219 | + # testFlag should be enabled (True in mock data) |
| 220 | + result = provider.resolve_boolean_details("testFlag", False) |
| 221 | + assert result.value is True |
| 222 | + assert result.reason.value == "TARGETING_MATCH" |
| 223 | + |
| 224 | + # testFlag2 should be disabled (False in mock data) |
| 225 | + result = provider.resolve_boolean_details("testFlag2", True) |
| 226 | + assert result.value is False |
| 227 | + assert result.reason.value == "DEFAULT" |
| 228 | + |
| 229 | + # Test string resolution with default value for non-existent flag |
| 230 | + result = provider.resolve_string_details("nonExistentFlag", "default_value") |
| 231 | + assert result.value == "default_value" |
| 232 | + assert result.reason.value == "DEFAULT" |
| 233 | + |
| 234 | + provider.shutdown() |
0 commit comments