@@ -2010,3 +2010,71 @@ def test_ivf_flat_taskgraph_query(tmp_path):
20102010 queries , k = k , nprobe = nprobe , nthreads = 8 , mode = Mode .LOCAL , num_partitions = 10
20112011 )
20122012 assert accuracy (result , gt_i ) > MINIMUM_ACCURACY
2013+
2014+
2015+ def test_ollama_embedding ():
2016+ """Test OllamaEmbedding class with mocked ollama library."""
2017+ from unittest .mock import MagicMock
2018+ from unittest .mock import Mock
2019+ from unittest .mock import patch
2020+
2021+ from tiledb .vector_search .embeddings import OllamaEmbedding
2022+
2023+ # Test initialization
2024+ dimensions = 384
2025+ embedding_class = "embed"
2026+ embedding_kwargs = {"model" : "nomic-embed-text" }
2027+
2028+ embedding = OllamaEmbedding (
2029+ dimensions = dimensions ,
2030+ embedding_class = embedding_class ,
2031+ embedding_kwargs = embedding_kwargs ,
2032+ )
2033+
2034+ # Test dimensions() method
2035+ assert embedding .dimensions () == dimensions
2036+
2037+ # Test vector_type() method
2038+ assert embedding .vector_type () == np .float32
2039+
2040+ # Test init_kwargs() method
2041+ init_kwargs = embedding .init_kwargs ()
2042+ assert init_kwargs ["dimensions" ] == dimensions
2043+ assert init_kwargs ["embedding_class" ] == embedding_class
2044+ assert init_kwargs ["embedding_kwargs" ] == embedding_kwargs
2045+
2046+ # Mock the ollama module
2047+ mock_ollama = MagicMock ()
2048+
2049+ # Create a mock embedding result with the expected structure
2050+ mock_embed_result = Mock ()
2051+ mock_embed_result .embeddings = [
2052+ [0.1 ] * dimensions , # 384 dimensions for first text
2053+ [0.2 ] * dimensions , # 384 dimensions for second text
2054+ ]
2055+
2056+ # Create a mock callable that will be returned by embed(**kwargs)
2057+ mock_callable = Mock (return_value = mock_embed_result )
2058+
2059+ # Mock the embed function to return our callable when called with **embedding_kwargs
2060+ mock_ollama .embed = Mock (return_value = mock_callable )
2061+
2062+ # Patch the importlib.import_module to return our mock
2063+ with patch ("importlib.import_module" , return_value = mock_ollama ):
2064+ # Test load() method
2065+ embedding .load ()
2066+
2067+ # Test embed() method with multiple texts
2068+ test_texts = ["hello world" , "test document" ]
2069+ result = embedding .embed (test_texts )
2070+
2071+ # Verify the result
2072+ assert isinstance (result , np .ndarray )
2073+ assert result .dtype == np .float32
2074+ assert result .shape == (2 , dimensions )
2075+
2076+ # Verify embed was called with correct kwargs during load
2077+ mock_ollama .embed .assert_called_once_with (model = "nomic-embed-text" )
2078+
2079+ # Verify the callable was called with correct input parameter
2080+ mock_callable .assert_called_once_with (input = test_texts )
0 commit comments