11"""Tests for the Collections class."""
22
3-
43import sys
54
65from typesense .async_ .api_call import AsyncApiCall
1312
1413from tests .utils .object_assertions import assert_match_object , assert_object_lists_match
1514from typesense .sync .api_call import ApiCall
15+ from tests .utils .version import is_v30_or_above
1616from typesense .sync .collections import Collections
1717from typesense .async_ .collections import AsyncCollections
18+ from typesense .sync .client import Client
1819from typesense .types .collection import CollectionSchema
1920
2021
22+ IS_V30_OR_ABOVE = is_v30_or_above (
23+ Client (
24+ {
25+ "api_key" : "xyz" ,
26+ "nodes" : [{"host" : "localhost" , "port" : 8108 , "protocol" : "http" }],
27+ }
28+ )
29+ )
30+
31+
32+ def expected_collection_field (
33+ name : str ,
34+ type_ : str ,
35+ * ,
36+ sort : bool ,
37+ ) -> dict [str , typing .Any ]:
38+ field : dict [str , typing .Any ] = {
39+ "name" : name ,
40+ "type" : type_ ,
41+ "facet" : False ,
42+ "index" : True ,
43+ "optional" : False ,
44+ "locale" : "" ,
45+ "sort" : sort ,
46+ "infix" : False ,
47+ "stem" : False ,
48+ "stem_dictionary" : "" ,
49+ "store" : True ,
50+ }
51+ if IS_V30_OR_ABOVE :
52+ field ["truncate_len" ] = 100
53+ return field
54+
55+
56+ def expected_collection_schema (
57+ * ,
58+ default_sorting_field : str ,
59+ fields : typing .List [dict [str , typing .Any ]],
60+ name : str ,
61+ ) -> CollectionSchema :
62+ expected : CollectionSchema = {
63+ "default_sorting_field" : default_sorting_field ,
64+ "enable_nested_fields" : False ,
65+ "fields" : fields ,
66+ "name" : name ,
67+ "num_documents" : 0 ,
68+ "symbols_to_index" : [],
69+ "token_separators" : [],
70+ }
71+ if IS_V30_OR_ABOVE :
72+ expected ["synonym_sets" ] = []
73+ expected ["curation_sets" ] = []
74+ return expected
75+
76+
2177def test_init (fake_api_call : ApiCall ) -> None :
2278 """Test that the Collections object is initialized correctly."""
2379 collections = Collections (fake_api_call )
@@ -98,46 +154,14 @@ def test_get_existing_collection(fake_collections: Collections) -> None:
98154
99155def test_actual_create (actual_collections : Collections , delete_all : None ) -> None :
100156 """Test that the Collections object can create a collection on Typesense Server."""
101- expected : CollectionSchema = {
102- "default_sorting_field" : "" ,
103- "enable_nested_fields" : False ,
104- "fields" : [
105- {
106- "name" : "company_name" ,
107- "type" : "string" ,
108- "facet" : False ,
109- "index" : True ,
110- "optional" : False ,
111- "locale" : "" ,
112- "sort" : False ,
113- "infix" : False ,
114- "stem" : False ,
115- "stem_dictionary" : "" ,
116- "truncate_len" : 100 ,
117- "store" : True ,
118- },
119- {
120- "name" : "num_employees" ,
121- "type" : "int32" ,
122- "facet" : False ,
123- "index" : True ,
124- "optional" : False ,
125- "locale" : "" ,
126- "sort" : False ,
127- "infix" : False ,
128- "stem" : False ,
129- "stem_dictionary" : "" ,
130- "truncate_len" : 100 ,
131- "store" : True ,
132- },
157+ expected = expected_collection_schema (
158+ default_sorting_field = "" ,
159+ fields = [
160+ expected_collection_field ("company_name" , "string" , sort = False ),
161+ expected_collection_field ("num_employees" , "int32" , sort = False ),
133162 ],
134- "name" : "companies" ,
135- "num_documents" : 0 ,
136- "symbols_to_index" : [],
137- "token_separators" : [],
138- "synonym_sets" : [],
139- "curation_sets" : [],
140- }
163+ name = "companies" ,
164+ )
141165
142166 response = actual_collections .create (
143167 {
@@ -170,46 +194,14 @@ def test_actual_retrieve(
170194 response = actual_collections .retrieve ()
171195
172196 expected : typing .List [CollectionSchema ] = [
173- {
174- "default_sorting_field" : "num_employees" ,
175- "enable_nested_fields" : False ,
176- "fields" : [
177- {
178- "name" : "company_name" ,
179- "type" : "string" ,
180- "facet" : False ,
181- "index" : True ,
182- "optional" : False ,
183- "locale" : "" ,
184- "sort" : False ,
185- "infix" : False ,
186- "stem" : False ,
187- "stem_dictionary" : "" ,
188- "truncate_len" : 100 ,
189- "store" : True ,
190- },
191- {
192- "name" : "num_employees" ,
193- "type" : "int32" ,
194- "facet" : False ,
195- "index" : True ,
196- "optional" : False ,
197- "locale" : "" ,
198- "sort" : True ,
199- "infix" : False ,
200- "stem" : False ,
201- "stem_dictionary" : "" ,
202- "truncate_len" : 100 ,
203- "store" : True ,
204- },
197+ expected_collection_schema (
198+ default_sorting_field = "num_employees" ,
199+ fields = [
200+ expected_collection_field ("company_name" , "string" , sort = False ),
201+ expected_collection_field ("num_employees" , "int32" , sort = True ),
205202 ],
206- "name" : "companies" ,
207- "num_documents" : 0 ,
208- "symbols_to_index" : [],
209- "token_separators" : [],
210- "synonym_sets" : [],
211- "curation_sets" : [],
212- },
203+ name = "companies" ,
204+ ),
213205 ]
214206
215207 response [0 ].pop ("created_at" )
@@ -235,46 +227,14 @@ async def test_actual_create_async(
235227 actual_async_collections : AsyncCollections , delete_all : None
236228) -> None :
237229 """Test that the Collections object can create a collection on Typesense Server."""
238- expected : CollectionSchema = {
239- "default_sorting_field" : "" ,
240- "enable_nested_fields" : False ,
241- "fields" : [
242- {
243- "name" : "company_name" ,
244- "type" : "string" ,
245- "facet" : False ,
246- "index" : True ,
247- "optional" : False ,
248- "locale" : "" ,
249- "sort" : False ,
250- "infix" : False ,
251- "stem" : False ,
252- "stem_dictionary" : "" ,
253- "truncate_len" : 100 ,
254- "store" : True ,
255- },
256- {
257- "name" : "num_employees" ,
258- "type" : "int32" ,
259- "facet" : False ,
260- "index" : True ,
261- "optional" : False ,
262- "locale" : "" ,
263- "sort" : False ,
264- "infix" : False ,
265- "stem" : False ,
266- "stem_dictionary" : "" ,
267- "truncate_len" : 100 ,
268- "store" : True ,
269- },
230+ expected = expected_collection_schema (
231+ default_sorting_field = "" ,
232+ fields = [
233+ expected_collection_field ("company_name" , "string" , sort = False ),
234+ expected_collection_field ("num_employees" , "int32" , sort = False ),
270235 ],
271- "name" : "companies" ,
272- "num_documents" : 0 ,
273- "symbols_to_index" : [],
274- "token_separators" : [],
275- "synonym_sets" : [],
276- "curation_sets" : [],
277- }
236+ name = "companies" ,
237+ )
278238
279239 response = await actual_async_collections .create (
280240 {
@@ -307,46 +267,14 @@ async def test_actual_retrieve_async(
307267 response = await actual_async_collections .retrieve ()
308268
309269 expected : typing .List [CollectionSchema ] = [
310- {
311- "default_sorting_field" : "num_employees" ,
312- "enable_nested_fields" : False ,
313- "fields" : [
314- {
315- "name" : "company_name" ,
316- "type" : "string" ,
317- "facet" : False ,
318- "index" : True ,
319- "optional" : False ,
320- "locale" : "" ,
321- "sort" : False ,
322- "infix" : False ,
323- "stem" : False ,
324- "stem_dictionary" : "" ,
325- "truncate_len" : 100 ,
326- "store" : True ,
327- },
328- {
329- "name" : "num_employees" ,
330- "type" : "int32" ,
331- "facet" : False ,
332- "index" : True ,
333- "optional" : False ,
334- "locale" : "" ,
335- "sort" : True ,
336- "infix" : False ,
337- "stem" : False ,
338- "stem_dictionary" : "" ,
339- "truncate_len" : 100 ,
340- "store" : True ,
341- },
270+ expected_collection_schema (
271+ default_sorting_field = "num_employees" ,
272+ fields = [
273+ expected_collection_field ("company_name" , "string" , sort = False ),
274+ expected_collection_field ("num_employees" , "int32" , sort = True ),
342275 ],
343- "name" : "companies" ,
344- "num_documents" : 0 ,
345- "symbols_to_index" : [],
346- "token_separators" : [],
347- "synonym_sets" : [],
348- "curation_sets" : [],
349- },
276+ name = "companies" ,
277+ ),
350278 ]
351279
352280 response [0 ].pop ("created_at" )
0 commit comments