77
88import pytest
99from chromadb .api .client import Client as ClientCreator
10+ from chromadb .api .functions import Function , RecordCounterFunction
1011from chromadb .config import System
1112from chromadb .errors import ChromaError , NotFoundError
1213from chromadb .test .utils .wait_for_version_increase import (
@@ -29,10 +30,9 @@ def test_count_function_attach_and_detach(basic_http_client: System) -> None:
2930
3031 # Create a task that counts records in the collection
3132 attached_fn = collection .attach_function (
33+ function = RecordCounterFunction (),
3234 name = "count_my_docs" ,
33- function_id = "record_counter" , # Built-in operator that counts records
3435 output_collection = "my_documents_counts" ,
35- params = None ,
3636 )
3737
3838 # Verify task creation succeeded
@@ -66,6 +66,14 @@ def test_count_function_attach_and_detach(basic_http_client: System) -> None:
6666 assert success is True
6767
6868
69+ class InvalidFunction (Function ):
70+ """A function with an invalid name for testing error handling."""
71+
72+ @property
73+ def name (self ) -> str :
74+ return "nonexistent_function"
75+
76+
6977def test_task_with_invalid_function (basic_http_client : System ) -> None :
7078 """Test that creating a task with an invalid function raises an error"""
7179 client = ClientCreator .from_system (basic_http_client )
@@ -77,10 +85,9 @@ def test_task_with_invalid_function(basic_http_client: System) -> None:
7785 # Attempt to create task with non-existent function should raise ChromaError
7886 with pytest .raises (ChromaError , match = "function not found" ):
7987 collection .attach_function (
88+ function = InvalidFunction (),
8089 name = "invalid_task" ,
81- function_id = "nonexistent_function" ,
8290 output_collection = "output_collection" ,
83- params = None ,
8491 )
8592
8693
@@ -94,10 +101,9 @@ def test_attach_function_returns_function_name(basic_http_client: System) -> Non
94101
95102 # Attach a function and verify function_name field in response
96103 attached_fn = collection .attach_function (
104+ function = RecordCounterFunction (),
97105 name = "my_counter" ,
98- function_id = "record_counter" ,
99106 output_collection = "output_collection" ,
100- params = None ,
101107 )
102108
103109 # Verify the attached function has function_name (not function_id UUID)
@@ -122,10 +128,9 @@ def test_function_multiple_collections(basic_http_client: System) -> None:
122128 collection1 .add (ids = ["id1" , "id2" ], documents = ["doc1" , "doc2" ])
123129
124130 attached_fn1 = collection1 .attach_function (
131+ function = RecordCounterFunction (),
125132 name = "task_1" ,
126- function_id = "record_counter" ,
127133 output_collection = "output_1" ,
128- params = None ,
129134 )
130135
131136 assert attached_fn1 is not None
@@ -135,10 +140,9 @@ def test_function_multiple_collections(basic_http_client: System) -> None:
135140 collection2 .add (ids = ["id3" , "id4" ], documents = ["doc3" , "doc4" ])
136141
137142 attached_fn2 = collection2 .attach_function (
143+ function = RecordCounterFunction (),
138144 name = "task_2" ,
139- function_id = "record_counter" ,
140145 output_collection = "output_2" ,
141- params = None ,
142146 )
143147
144148 assert attached_fn2 is not None
@@ -170,10 +174,9 @@ def test_functions_one_attached_function_per_collection(
170174
171175 # Create first task on the collection
172176 attached_fn1 = collection .attach_function (
177+ function = RecordCounterFunction (),
173178 name = "task_1" ,
174- function_id = "record_counter" ,
175179 output_collection = "output_1" ,
176- params = None ,
177180 )
178181
179182 assert attached_fn1 is not None
@@ -182,19 +185,17 @@ def test_functions_one_attached_function_per_collection(
182185 # (only one attached function allowed per collection)
183186 with pytest .raises (ChromaError , match = "already has an attached function" ):
184187 collection .attach_function (
188+ function = RecordCounterFunction (),
185189 name = "task_2" ,
186- function_id = "record_counter" ,
187190 output_collection = "output_2" ,
188- params = None ,
189191 )
190192
191193 # Attempt to create a task with the same name but different params should also fail
192194 with pytest .raises (ChromaError , match = "already exists" ):
193195 collection .attach_function (
196+ function = RecordCounterFunction (),
194197 name = "task_1" ,
195- function_id = "record_counter" ,
196198 output_collection = "output_different" , # Different output collection
197- params = None ,
198199 )
199200
200201 # Detach the first function
@@ -205,10 +206,9 @@ def test_functions_one_attached_function_per_collection(
205206
206207 # Now we should be able to attach a new function
207208 attached_fn2 = collection .attach_function (
209+ function = RecordCounterFunction (),
208210 name = "task_2" ,
209- function_id = "record_counter" ,
210211 output_collection = "output_2" ,
211- params = None ,
212212 )
213213
214214 assert attached_fn2 is not None
@@ -229,10 +229,9 @@ def test_function_remove_nonexistent(basic_http_client: System) -> None:
229229 collection = client .create_collection (name = "test_collection" )
230230 collection .add (ids = ["id1" ], documents = ["test" ])
231231 attached_fn = collection .attach_function (
232+ function = RecordCounterFunction (),
232233 name = "test_function" ,
233- function_id = "record_counter" ,
234234 output_collection = "output_collection" ,
235- params = None ,
236235 )
237236
238237 collection .detach_function (attached_fn .name , delete_output_collection = True )
0 commit comments