|
19 | 19 | from audit.related_object_type import RelatedObjectType |
20 | 20 | from core.constants import STRING |
21 | 21 | from core.request_origin import RequestOrigin |
| 22 | +from environments.enums import EnvironmentDocumentCacheMode |
22 | 23 | from environments.identities.models import Identity |
23 | 24 | from environments.metrics import CACHE_HIT, CACHE_MISS |
24 | 25 | from environments.models import ( |
|
41 | 42 | from segments.models import Segment |
42 | 43 | from tests.types import EnableFeaturesFixture |
43 | 44 | from users.models import FFAdminUser |
44 | | -from util.mappers import map_environment_to_environment_document |
| 45 | +from util.mappers import ( |
| 46 | + map_environment_to_environment_document, |
| 47 | + map_environment_to_sdk_document, |
| 48 | +) |
45 | 49 |
|
46 | 50 | if typing.TYPE_CHECKING: |
47 | 51 | from django.db.models import Model |
@@ -1082,9 +1086,14 @@ def test_environment_save__api_key_changed__updates_environment_document_cache( |
1082 | 1086 |
|
1083 | 1087 | # Then |
1084 | 1088 | persistent_environment_document_cache.delete.assert_called_once_with(old_api_key) |
1085 | | - persistent_environment_document_cache.set_many.assert_called_once_with( |
1086 | | - {new_api_key: map_environment_to_environment_document(environment)} |
1087 | | - ) |
| 1089 | + persistent_environment_document_cache.set_many.assert_called_once() |
| 1090 | + |
| 1091 | + # Get what was actually cached and compare to sdk document |
| 1092 | + cache_payload = persistent_environment_document_cache.set_many.call_args[0][0] |
| 1093 | + cached_document = cache_payload[new_api_key] |
| 1094 | + expected_document = map_environment_to_sdk_document(environment) |
| 1095 | + |
| 1096 | + assert cached_document == expected_document |
1088 | 1097 |
|
1089 | 1098 |
|
1090 | 1099 | def test_get_environment_document__cache_hit__triggers_cache_hit_metric( |
@@ -1307,3 +1316,21 @@ def test_environment_clone__from_v1_with_v2_flag_enabled__upgrades_to_v2_version |
1307 | 1316 | latest_feature_states = get_environment_flags_queryset(new_environment) |
1308 | 1317 | assert latest_feature_states.count() == 2 |
1309 | 1318 | assert {fs.environment_feature_version for fs in latest_feature_states} == {efv} |
| 1319 | + |
| 1320 | + |
| 1321 | +@mock.patch("environments.models.environment_document_cache") |
| 1322 | +def test_write_environment_documents__persistent_caching_enabled__caches_sdk_document( |
| 1323 | + mock_document_cache: MagicMock, environment: Environment, settings: typing.Any |
| 1324 | +) -> None: |
| 1325 | + # Given |
| 1326 | + settings.CACHE_ENVIRONMENT_DOCUMENT_MODE = EnvironmentDocumentCacheMode.PERSISTENT |
| 1327 | + |
| 1328 | + # When |
| 1329 | + Environment.write_environment_documents(environment_id=environment.id) |
| 1330 | + |
| 1331 | + # Then |
| 1332 | + cache_payload = mock_document_cache.set_many.call_args[0][0] |
| 1333 | + cached_document = cache_payload[environment.api_key] |
| 1334 | + expected_document = map_environment_to_sdk_document(environment) |
| 1335 | + |
| 1336 | + assert cached_document == expected_document |
0 commit comments