@@ -43,12 +43,17 @@ class TestElasticsearchDocumentStoreAsync(
4343):
4444 @pytest_asyncio .fixture
4545 async def document_store (self , request ):
46+ """
47+ Basic fixture providing a document store instance for async tests
48+ """
4649 hosts = ["http://localhost:9200" ]
50+ # Use a different index for each test so we can run them in parallel
4751 index = f"{ request .node .name } "
4852
4953 store = ElasticsearchDocumentStore (hosts = hosts , index = index )
5054 yield store
5155 store .client .options (ignore_status = [400 , 404 ]).indices .delete (index = index )
56+
5257 await store .async_client .close ()
5358
5459 def assert_documents_are_equal (self , received : list [Document ], expected : list [Document ]):
@@ -63,37 +68,6 @@ async def test_count_not_empty_async(self, document_store):
6368 )
6469 assert await document_store .count_documents_async () == 3
6570
66- @pytest .mark .asyncio
67- async def test_delete_documents_empty_document_store_async (self , document_store ):
68- # Elasticsearch raises DocumentStoreError when deleting a non-existent document
69- # rather than silently ignoring it, so we override the mixin test here.
70- with pytest .raises (DocumentStoreError ):
71- await document_store .delete_documents_async (["non_existing_id" ])
72-
73- @pytest .mark .asyncio
74- async def test_delete_documents_non_existing_document_async (self , document_store ):
75- # Same as above: Elasticsearch raises on missing IDs rather than a no-op.
76- doc = Document (content = "test doc" )
77- await document_store .write_documents_async ([doc ])
78- assert await document_store .count_documents_async () == 1
79- with pytest .raises (DocumentStoreError ):
80- await document_store .delete_documents_async (["non_existing_id" ])
81-
82- @pytest .mark .asyncio
83- async def test_write_documents_duplicate_fail_async (self , document_store ):
84- # Elasticsearch raises DocumentStoreError instead of DuplicateDocumentError on duplicate FAIL
85- doc = Document (content = "test doc" )
86- assert await document_store .write_documents_async ([doc ], policy = DuplicatePolicy .FAIL ) == 1
87- with pytest .raises (DocumentStoreError ):
88- await document_store .write_documents_async (documents = [doc ], policy = DuplicatePolicy .FAIL )
89-
90- @pytest .mark .asyncio
91- async def test_write_documents_duplicate_skip_async (self , document_store ):
92- # Elasticsearch returns 1 (not 0) when skipping an already-existing document
93- doc = Document (content = "test doc" )
94- assert await document_store .write_documents_async ([doc ], policy = DuplicatePolicy .SKIP ) == 1
95- assert await document_store .write_documents_async (documents = [doc ], policy = DuplicatePolicy .SKIP ) == 1
96-
9771 @pytest .mark .asyncio
9872 async def test_write_documents_async (self , document_store ):
9973 docs = [Document (id = "1" , content = "test" )]
@@ -104,12 +78,14 @@ async def test_write_documents_async(self, document_store):
10478
10579 @pytest .mark .asyncio
10680 async def test_write_documents_async_invalid_document_type (self , document_store ):
81+ """Test write_documents with invalid document type"""
10782 invalid_docs = [{"id" : "1" , "content" : "test" }]
10883 with pytest .raises (ValueError , match = "param 'documents' must contain a list of objects of type Document" ):
10984 await document_store .write_documents_async (invalid_docs )
11085
11186 @pytest .mark .asyncio
11287 async def test_write_documents_async_with_sparse_embedding_warning (self , document_store , caplog ):
88+ """Test write_documents with document containing sparse_embedding field"""
11389 doc = Document (id = "1" , content = "test" , sparse_embedding = SparseEmbedding (indices = [0 , 1 ], values = [0.5 , 0.5 ]))
11490 await document_store .write_documents_async ([doc ])
11591 assert "but `sparse_vector_field` is not configured" in caplog .text
@@ -121,6 +97,7 @@ async def test_write_documents_async_with_sparse_embedding_warning(self, documen
12197
12298 @pytest .mark .asyncio
12399 async def test_write_documents_async_with_sparse_vectors (self ):
100+ """Test write_documents with document containing sparse_embedding field"""
124101 store = ElasticsearchDocumentStore (
125102 hosts = ["http://localhost:9200" ], index = "test_async_sparse" , sparse_vector_field = "sparse_vec"
126103 )
@@ -129,9 +106,11 @@ async def test_write_documents_async_with_sparse_vectors(self):
129106 doc = Document (id = "1" , content = "test" , sparse_embedding = SparseEmbedding (indices = [0 , 1 ], values = [0.5 , 0.5 ]))
130107 await store .write_documents_async ([doc ])
131108
109+ # check ES natively
132110 raw_doc = await store .async_client .get (index = "test_async_sparse" , id = "1" )
133111 assert raw_doc ["_source" ]["sparse_vec" ] == {"0" : 0.5 , "1" : 0.5 }
134112
113+ # check retrieval
135114 results = await store .filter_documents_async ()
136115 assert len (results ) == 1
137116 assert results [0 ].sparse_embedding is not None
0 commit comments