1818# pytype: skip-file
1919# pylint: disable=line-too-long
2020
21- import os
2221import unittest
2322from io import StringIO
2423
2524import mock
26- import pytest
2725
2826# pylint: disable=unused-import
2927try :
30- from apache_beam .examples .snippets .transforms .elementwise .enrichment import (
31- enrichment_with_bigtable ,
32- enrichment_with_vertex_ai ,
33- enrichment_with_vertex_ai_legacy ,
34- enrichment_with_milvus )
35- from apache_beam .ml .rag .enrichment .milvus_search import (
36- MilvusConnectionParameters )
37- from apache_beam .ml .rag .enrichment .milvus_search_it_test import (
38- MilvusEnrichmentTestHelper ,
39- MilvusDBContainerInfo ,
40- parse_chunk_strings ,
41- assert_chunks_equivalent )
28+ from apache_beam .examples .snippets .transforms .elementwise .enrichment import enrichment_with_bigtable , \
29+ enrichment_with_vertex_ai_legacy
30+ from apache_beam .examples .snippets .transforms .elementwise .enrichment import enrichment_with_vertex_ai
4231 from apache_beam .io .requestresponse import RequestResponseIO
4332except ImportError :
4433 raise unittest .SkipTest ('RequestResponseIO dependencies are not installed' )
@@ -71,15 +60,7 @@ def validate_enrichment_with_vertex_ai_legacy():
7160 return expected
7261
7362
74- def validate_enrichment_with_milvus ():
75- expected = '''[START enrichment_with_milvus]
76- Chunk(content=Content(text=None), id='query1', index=0, metadata={'enrichment_data': defaultdict(<class 'list'>, {'id': [1], 'distance': [1.0], 'fields': [{'content': 'This is a test document', 'cost': 49, 'domain': 'medical', 'id': 1, 'metadata': {'language': 'en'}}]})}, embedding=Embedding(dense_embedding=[0.1, 0.2, 0.3], sparse_embedding=None))
77- [END enrichment_with_milvus]''' .splitlines ()[1 :- 1 ]
78- return expected
79-
80-
8163@mock .patch ('sys.stdout' , new_callable = StringIO )
82- @pytest .mark .uses_testcontainer
8364class EnrichmentTest (unittest .TestCase ):
8465 def test_enrichment_with_bigtable (self , mock_stdout ):
8566 enrichment_with_bigtable ()
@@ -102,63 +83,6 @@ def test_enrichment_with_vertex_ai_legacy(self, mock_stdout):
10283 self .maxDiff = None
10384 self .assertEqual (output , expected )
10485
105- def test_enrichment_with_milvus (self , mock_stdout ):
106- milvus_db = None
107- try :
108- milvus_db = EnrichmentTestHelpers .pre_milvus_enrichment ()
109- enrichment_with_milvus ()
110- output = mock_stdout .getvalue ().splitlines ()
111- expected = validate_enrichment_with_milvus ()
112- self .maxDiff = None
113- output = parse_chunk_strings (output )
114- expected = parse_chunk_strings (expected )
115- assert_chunks_equivalent (output , expected )
116- except Exception as e :
117- self .fail (f"Test failed with unexpected error: { e } " )
118- finally :
119- if milvus_db :
120- EnrichmentTestHelpers .post_milvus_enrichment (milvus_db )
121-
122-
123- class EnrichmentTestHelpers :
124- @staticmethod
125- def pre_milvus_enrichment () -> MilvusDBContainerInfo :
126- # Create Milvus db container and make sure it is up and running.
127- db = MilvusEnrichmentTestHelper .start_db_container ()
128-
129- # Construct connection parameters.
130- connection_params = MilvusConnectionParameters (
131- uri = db .uri ,
132- user = db .user ,
133- password = db .password ,
134- db_id = db .id ,
135- token = db .token )
136-
137- # Initialize db with data required for testing.
138- collection_name = MilvusEnrichmentTestHelper .initialize_db_with_data (
139- connection_params )
140-
141- # Setup environment variables for db and collection configuration. This will
142- # be used downstream by the milvus enrichment handler.
143- os .environ ['MILVUS_VECTOR_DB_URI' ] = db .uri
144- os .environ ['MILVUS_VECTOR_DB_USER' ] = db .user
145- os .environ ['MILVUS_VECTOR_DB_PASSWORD' ] = db .password
146- os .environ ['MILVUS_VECTOR_DB_ID' ] = db .id
147- os .environ ['MILVUS_VECTOR_DB_TOKEN' ] = db .token
148- os .environ ['MILVUS_VECTOR_DB_COLLECTION_NAME' ] = collection_name
149-
150- return db
151-
152- @staticmethod
153- def post_milvus_enrichment (db : MilvusDBContainerInfo ):
154- MilvusEnrichmentTestHelper .stop_db_container (db )
155- os .environ .pop ('MILVUS_VECTOR_DB_URI' , None )
156- os .environ .pop ('MILVUS_VECTOR_DB_USER' , None )
157- os .environ .pop ('MILVUS_VECTOR_DB_PASSWORD' , None )
158- os .environ .pop ('MILVUS_VECTOR_DB_ID' , None )
159- os .environ .pop ('MILVUS_VECTOR_DB_TOKEN' , None )
160- os .environ .pop ('MILVUS_VECTOR_DB_COLLECTION_NAME' , None )
161-
16286
16387if __name__ == '__main__' :
16488 unittest .main ()
0 commit comments