Skip to content

Commit 6eecb90

Browse files
committed
test: gate v30 collection schema expectations
- keep v29.0 and earlier tests passing
1 parent f6989c8 commit 6eecb90

2 files changed

Lines changed: 106 additions & 167 deletions

File tree

tests/collection_test.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@
55
assert_object_lists_match,
66
assert_to_contain_object,
77
)
8+
from tests.utils.version import is_v30_or_above
89
from typesense.sync.api_call import ApiCall
10+
from typesense.sync.client import Client
911
from typesense.sync.collection import Collection
1012
from typesense.sync.collections import Collections
1113
from typesense.types.collection import CollectionSchema
1214

1315

16+
is_v30_or_above_server = is_v30_or_above(
17+
Client(
18+
{
19+
"api_key": "xyz",
20+
"nodes": [{"host": "localhost", "port": 8108, "protocol": "http"}],
21+
}
22+
)
23+
)
24+
25+
1426
def test_init(fake_api_call: ApiCall) -> None:
1527
"""Test that the Collection object is initialized correctly."""
1628
collection = Collection(fake_api_call, "companies")
@@ -52,7 +64,6 @@ def test_actual_retrieve(
5264
"infix": False,
5365
"stem": False,
5466
"stem_dictionary": "",
55-
"truncate_len": 100,
5667
"store": True,
5768
},
5869
{
@@ -66,17 +77,19 @@ def test_actual_retrieve(
6677
"infix": False,
6778
"stem": False,
6879
"stem_dictionary": "",
69-
"truncate_len": 100,
7080
"store": True,
7181
},
7282
],
7383
"name": "companies",
7484
"num_documents": 0,
7585
"symbols_to_index": [],
7686
"token_separators": [],
77-
"synonym_sets": [],
78-
"curation_sets": [],
7987
}
88+
if is_v30_or_above_server:
89+
expected["synonym_sets"] = []
90+
expected["curation_sets"] = []
91+
expected["fields"][0]["truncate_len"] = 100
92+
expected["fields"][1]["truncate_len"] = 100
8093

8194
response.pop("created_at")
8295

@@ -93,10 +106,8 @@ def test_actual_update(
93106
{"fields": [{"name": "num_locations", "type": "int32"}]},
94107
)
95108

96-
expected: CollectionSchema = {
97-
"fields": [
98-
{"name": "num_locations", "truncate_len": 100, "type": "int32"},
99-
],
100-
}
109+
expected_field = {"name": "num_locations", "type": "int32"}
110+
if is_v30_or_above_server:
111+
expected_field["truncate_len"] = 100
101112

102-
assert_to_contain_object(response.get("fields")[0], expected.get("fields")[0])
113+
assert_to_contain_object(response.get("fields")[0], expected_field)

tests/collections_test.py

Lines changed: 85 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Tests for the Collections class."""
22

3-
43
import sys
54

65
from typesense.async_.api_call import AsyncApiCall
@@ -13,11 +12,68 @@
1312

1413
from tests.utils.object_assertions import assert_match_object, assert_object_lists_match
1514
from typesense.sync.api_call import ApiCall
15+
from tests.utils.version import is_v30_or_above
1616
from typesense.sync.collections import Collections
1717
from typesense.async_.collections import AsyncCollections
18+
from typesense.sync.client import Client
1819
from 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+
2177
def 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

99155
def 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

Comments
 (0)