-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathcollection_test.py
More file actions
296 lines (255 loc) · 8.51 KB
/
collection_test.py
File metadata and controls
296 lines (255 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""Tests for the Collection class."""
from __future__ import annotations
import logging
import time
import requests_mock
from tests.utils.object_assertions import (
assert_match_object,
assert_object_lists_match,
assert_to_contain_object,
)
from typesense.api_call import ApiCall
from typesense.collection import Collection
from typesense.collections import Collections
from typesense.types.collection import CollectionSchema
import typesense.logger as typesense_logger
def test_init(fake_api_call: ApiCall) -> None:
"""Test that the Collection object is initialized correctly."""
collection = Collection(fake_api_call, "companies")
assert collection.name == "companies"
assert_match_object(collection.api_call, fake_api_call)
assert_object_lists_match(
collection.api_call.node_manager.nodes,
fake_api_call.node_manager.nodes,
)
assert_match_object(
collection.api_call.config.nearest_node,
fake_api_call.config.nearest_node,
)
assert collection._overrides is None
assert collection._synonyms is None
assert collection._endpoint_path == "/collections/companies" # noqa: WPS437
def test_retrieve(fake_collection: Collection) -> None:
"""Test that the Collection object can retrieve a collection."""
time_now = int(time.time())
json_response: CollectionSchema = {
"created_at": time_now,
"default_sorting_field": "num_employees",
"enable_nested_fields": False,
"fields": [
{
"name": "company_name",
"type": "string",
},
{
"name": "num_employees",
"type": "int32",
},
],
"name": "companies",
"num_documents": 0,
"symbols_to_index": [],
"token_separators": [],
"synonym_sets": [],
"curation_sets": [],
}
with requests_mock.mock() as mock:
mock.get(
"http://nearest:8108/collections/companies",
json=json_response,
)
response = fake_collection.retrieve()
assert len(mock.request_history) == 1
assert mock.request_history[0].method == "GET"
assert (
mock.request_history[0].url == "http://nearest:8108/collections/companies"
)
assert response == json_response
def test_update(fake_collection: Collection) -> None:
"""Test that the Collection object can update a collection."""
json_response: CollectionSchema = {
"created_at": 1619711487,
"default_sorting_field": "num_employees",
"enable_nested_fields": False,
"fields": [
{
"name": "company_name",
"type": "string",
},
{
"name": "num_employees",
"type": "int32",
},
{
"name": "num_locations",
"type": "int32",
},
],
"name": "companies",
"num_documents": 0,
"symbols_to_index": [],
"token_separators": [],
"synonym_sets": [],
"curation_sets": [],
}
with requests_mock.mock() as mock:
mock.patch(
"http://nearest:8108/collections/companies",
json=json_response,
)
response = fake_collection.update(
schema_change={
"fields": [
{
"name": "num_locations",
"type": "int32",
},
],
},
)
assert mock.call_count == 1
assert mock.called is True
assert mock.last_request.method == "PATCH"
assert mock.last_request.url == "http://nearest:8108/collections/companies"
assert mock.last_request.json() == {
"fields": [
{
"name": "num_locations",
"type": "int32",
},
],
}
assert response == json_response
def test_delete(fake_collection: Collection) -> None:
"""Test that the Collection object can delete a collection."""
json_response: CollectionSchema = {
"created_at": 1619711487,
"default_sorting_field": "num_employees",
"enable_nested_fields": False,
"fields": [
{
"name": "company_name",
"type": "string",
},
{
"name": "num_employees",
"type": "int32",
},
{
"name": "num_locations",
"type": "int32",
},
],
"name": "companies",
"num_documents": 0,
"symbols_to_index": [],
"token_separators": [],
"synonym_sets": [],
"curation_sets": [],
}
with requests_mock.mock() as mock:
mock.delete(
"http://nearest:8108/collections/companies",
json=json_response,
)
response = fake_collection.delete()
assert mock.call_count == 1
assert mock.called is True
assert mock.last_request.method == "DELETE"
assert mock.last_request.url == "http://nearest:8108/collections/companies"
assert response == json_response
def test_actual_retrieve(
actual_collections: Collections,
delete_all: None,
create_collection: None,
) -> None:
"""Test that the Collection object can retrieve a collection."""
response = actual_collections["companies"].retrieve()
expected: CollectionSchema = {
"default_sorting_field": "num_employees",
"enable_nested_fields": False,
"fields": [
{
"name": "company_name",
"type": "string",
"facet": False,
"index": True,
"optional": False,
"locale": "",
"sort": False,
"infix": False,
"stem": False,
"stem_dictionary": "",
"truncate_len": 100,
"store": True,
},
{
"name": "num_employees",
"type": "int32",
"facet": False,
"index": True,
"optional": False,
"locale": "",
"sort": True,
"infix": False,
"stem": False,
"stem_dictionary": "",
"truncate_len": 100,
"store": True,
},
],
"name": "companies",
"num_documents": 0,
"symbols_to_index": [],
"token_separators": [],
"synonym_sets": [],
"curation_sets": [],
}
response.pop("created_at")
assert response == expected
def test_actual_update(
actual_collections: Collections,
delete_all: None,
create_collection: None,
) -> None:
"""Test that the Collection object can update a collection."""
response = actual_collections["companies"].update(
{"fields": [{"name": "num_locations", "type": "int32"}]},
)
expected: CollectionSchema = {
"fields": [
{"name": "num_locations", "truncate_len": 100, "type": "int32"},
],
}
assert_to_contain_object(response.get("fields")[0], expected.get("fields")[0])
def test_deprecated_resources_not_logged_on_init(
fake_api_call: ApiCall,
caplog,
) -> None:
"""Test that deprecated resources are not logged on collection init."""
typesense_logger._deprecation_warnings.clear()
caplog.set_level(logging.WARNING, logger="typesense")
Collection(fake_api_call, "companies")
assert "Deprecation warning:" not in caplog.text
def test_deprecated_resources_logged_once_on_use(
fake_api_call: ApiCall,
caplog,
) -> None:
"""Test that deprecated resources are logged once when used."""
typesense_logger._deprecation_warnings.clear()
caplog.set_level(logging.WARNING, logger="typesense")
collection = Collection(fake_api_call, "companies")
_ = collection.synonyms
_ = collection.synonyms
_ = collection.overrides
_ = collection.overrides
synonyms_message = (
"Deprecation warning: The synonyms API (collections/{collection}/synonyms) "
"is deprecated is removed on v30+. Use synonym sets (synonym_sets) instead."
)
overrides_message = (
"Deprecation warning: Overrides is deprecated on v30+. "
"Use client.curation_sets instead."
)
assert caplog.text.count(synonyms_message) == 1
assert caplog.text.count(overrides_message) == 1