55from datetime import datetime , timedelta , timezone
66
77import pytest
8- from dirty_equals import IsFloat
8+ from dirty_equals import IsFloat , IsStr
99from elasticsearch import Elasticsearch
1010from inline_snapshot import snapshot
1111from key_value .shared .stores .wait import wait_for_true
@@ -95,8 +95,7 @@ def test_managed_entry_document_conversion_native_storage():
9595 assert round_trip_managed_entry .expires_at == expires_at
9696
9797
98- @pytest .mark .skipif (should_skip_docker_tests (), reason = "Docker is not running" )
99- class TestElasticsearchStore (ContextManagerStoreTestMixin , BaseStoreTests ):
98+ class BaseTestElasticsearchStore (ContextManagerStoreTestMixin , BaseStoreTests ):
10099 @pytest .fixture (autouse = True , scope = "session" , params = ELASTICSEARCH_VERSIONS_TO_TEST )
101100 def setup_elasticsearch (self , request : pytest .FixtureRequest ) -> Generator [None , None , None ]:
102101 version = request .param
@@ -125,13 +124,6 @@ def _cleanup(self):
125124 for index in indices :
126125 _ = elasticsearch_client .options (ignore_status = 404 ).indices .delete (index = index )
127126
128- @override
129- @pytest .fixture
130- def store (self ) -> Generator [ElasticsearchStore , None , None ]:
131- self ._cleanup ()
132- with ElasticsearchStore (url = ES_URL , index_prefix = "kv-store-e2e-test" , native_storage = True ) as store :
133- yield store
134-
135127 @pytest .mark .skip (reason = "Distributed Caches are unbounded" )
136128 @override
137129 def test_not_unbounded (self , store : BaseStore ): ...
@@ -151,6 +143,17 @@ def test_put_put_two_indices(self, store: ElasticsearchStore, es_client: Elastic
151143 assert "kv-store-e2e-test-test_collection" in indices
152144 assert "kv-store-e2e-test-test_collection_2" in indices
153145
146+
147+ @pytest .mark .skipif (should_skip_docker_tests (), reason = "Docker is not running" )
148+ class TestElasticsearchStoreNativeMode (BaseTestElasticsearchStore ):
149+ """Test Elasticsearch store in native mode"""
150+
151+ @override
152+ @pytest .fixture
153+ def store (self ) -> ElasticsearchStore :
154+ self ._cleanup ()
155+ return ElasticsearchStore (url = ES_URL , index_prefix = "kv-store-e2e-test" , native_storage = True )
156+
154157 def test_value_stored_as_flattened_object (self , store : ElasticsearchStore , es_client : Elasticsearch ):
155158 """Verify values are stored as flattened objects, not JSON strings"""
156159 store .put (collection = "test" , key = "test_key" , value = {"name" : "Alice" , "age" : 30 })
@@ -165,8 +168,8 @@ def test_value_stored_as_flattened_object(self, store: ElasticsearchStore, es_cl
165168 {
166169 "collection" : "test" ,
167170 "key" : "test_key" ,
168- "value" : {"string " : ' {"name": "Alice", "age": 30}' },
169- "created_at" : "2025-10-27T15:33:31.795253+00:00" ,
171+ "value" : {"flattened " : {"name" : "Alice" , "age" : 30 }},
172+ "created_at" : IsStr ( min_length = 20 , max_length = 40 ) ,
170173 }
171174 )
172175
@@ -177,9 +180,9 @@ def test_value_stored_as_flattened_object(self, store: ElasticsearchStore, es_cl
177180 {
178181 "collection" : "test" ,
179182 "key" : "test_key" ,
180- "value" : {"string " : ' {"name": "Bob", "age": 25}' },
181- "created_at" : "2025-10-27T15:33:32.040020+00:00" ,
182- "expires_at" : "2025-10-27T15:33:42.040020+00:00" ,
183+ "value" : {"flattened " : {"name" : "Bob" , "age" : 25 }},
184+ "created_at" : IsStr ( min_length = 20 , max_length = 40 ) ,
185+ "expires_at" : IsStr ( min_length = 20 , max_length = 40 ) ,
183186 }
184187 )
185188
@@ -198,13 +201,14 @@ def test_migration_from_legacy_mode(self, store: ElasticsearchStore, es_client:
198201 assert result == snapshot (None )
199202
200203
201- class TestElasticsearchStoreNonNativeMode (TestElasticsearchStore ):
204+ class TestElasticsearchStoreNonNativeMode (BaseTestElasticsearchStore ):
205+ """Test Elasticsearch store in non-native mode"""
206+
202207 @override
203208 @pytest .fixture
204- def store (self ) -> Generator [ ElasticsearchStore , None , None ] :
209+ def store (self ) -> ElasticsearchStore :
205210 self ._cleanup ()
206- with ElasticsearchStore (url = ES_URL , index_prefix = "kv-store-e2e-test" , native_storage = False ) as store :
207- yield store
211+ return ElasticsearchStore (url = ES_URL , index_prefix = "kv-store-e2e-test" , native_storage = False )
208212
209213 def test_value_stored_as_json_string (self , store : ElasticsearchStore , es_client : Elasticsearch ):
210214 """Verify values are stored as flattened objects, not JSON strings"""
@@ -220,8 +224,8 @@ def test_value_stored_as_json_string(self, store: ElasticsearchStore, es_client:
220224 {
221225 "collection" : "test" ,
222226 "key" : "test_key" ,
223- "value" : {"string" : '{"name ": "Alice" , "age ": 30 }' },
224- "created_at" : "2025-10-27T15:33:32.631007+00:00" ,
227+ "value" : {"string" : '{"age ": 30 , "name ": "Alice" }' },
228+ "created_at" : IsStr ( min_length = 20 , max_length = 40 ) ,
225229 }
226230 )
227231
@@ -232,9 +236,9 @@ def test_value_stored_as_json_string(self, store: ElasticsearchStore, es_client:
232236 {
233237 "collection" : "test" ,
234238 "key" : "test_key" ,
235- "value" : {"string" : '{"name ": "Bob" , "age ": 25 }' },
236- "created_at" : "2025-10-27T15:33:32.650061+00:00" ,
237- "expires_at" : "2025-10-27T15:33:42.650061+00:00" ,
239+ "value" : {"string" : '{"age ": 25 , "name ": "Bob" }' },
240+ "created_at" : IsStr ( min_length = 20 , max_length = 40 ) ,
241+ "expires_at" : IsStr ( min_length = 20 , max_length = 40 ) ,
238242 }
239243 )
240244
0 commit comments