-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathconftest.py
238 lines (187 loc) · 6.56 KB
/
conftest.py
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
# pylint: disable=redefined-outer-name
import json
from typing import Optional
import requests
from pytest import fixture
import meilisearch
from meilisearch.errors import MeilisearchApiError
from meilisearch.models.index import OpenAiEmbedder, UserProvidedEmbedder
from tests import common
@fixture(scope="session")
def client():
return meilisearch.Client(common.BASE_URL, common.MASTER_KEY)
@fixture(autouse=True)
def clear_indexes(client):
"""
Auto-clears the indexes after each test function run.
Makes all the test functions independent.
"""
# Yields back to the test function.
yield
# Deletes all the indexes in the Meilisearch instance.
indexes = client.get_indexes()
for index in indexes["results"]:
task = client.index(index.uid).delete()
client.wait_for_task(task.task_uid)
@fixture(autouse=True)
def clear_all_tasks(client):
"""
Auto-clears the tasks after each test function run.
Makes all the test functions independent.
"""
client.delete_tasks({"statuses": ["succeeded", "failed", "canceled"]})
@fixture(scope="function")
def indexes_sample(client):
indexes = []
for index_args in common.INDEX_FIXTURE:
task = client.create_index(**index_args)
client.wait_for_task(task.task_uid)
indexes.append(client.get_index(index_args["uid"]))
# Yields the indexes to the test to make them accessible.
yield indexes
@fixture(scope="session")
def small_movies():
"""
Runs once per session. Provides the content of small_movies.json.
"""
with open("./datasets/small_movies.json", encoding="utf-8") as movie_file:
yield json.loads(movie_file.read())
@fixture(scope="session")
def small_movies_json_file():
"""
Runs once per session. Provides the content of small_movies.json from read.
"""
with open("./datasets/small_movies.json", encoding="utf-8") as movie_json_file:
return movie_json_file.read().encode("utf-8")
@fixture(scope="session")
def songs_csv():
"""
Runs once per session. Provides the content of songs.csv from read.
"""
with open("./datasets/songs.csv", encoding="utf-8") as song_csv_file:
return song_csv_file.read().encode("utf-8")
@fixture(scope="session")
def songs_csv_custom_separator():
"""
Runs once per session. Provides the content of songs_custom_delimiter.csv from read.
"""
with open("./datasets/songs_custom_delimiter.csv", encoding="utf-8") as song_csv_file:
return song_csv_file.read().encode("utf-8")
@fixture(scope="session")
def songs_ndjson():
"""
Runs once per session. Provides the content of songs.ndjson from read.
"""
with open("./datasets/songs.ndjson", encoding="utf-8") as song_ndjson_file:
return song_ndjson_file.read().encode("utf-8")
@fixture(scope="session")
def nested_movies():
"""
Runs once per session. Provides the content of nested_movies.json.
"""
with open("./datasets/nested_movies.json", encoding="utf-8") as nested_movie_file:
yield json.loads(nested_movie_file.read())
@fixture(scope="function")
def empty_index(client, index_uid: Optional[str] = None):
index_uid = index_uid if index_uid else common.INDEX_UID
def index_maker(index_uid=index_uid):
task = client.create_index(uid=index_uid)
client.wait_for_task(task.task_uid)
return client.get_index(uid=index_uid)
return index_maker
@fixture(scope="function")
def index_with_documents(empty_index, small_movies):
def index_maker(index_uid=common.INDEX_UID, documents=small_movies):
index = empty_index(index_uid)
task = index.add_documents(documents)
index.wait_for_task(task.task_uid)
return index
return index_maker
@fixture(scope="function")
def index_with_documents_and_vectors(empty_index, small_movies):
small_movies[0]["_vectors"] = {"default": [0.1, 0.2]}
for movie in small_movies[1:]:
movie["_vectors"] = {"default": [0.9, 0.9]}
def index_maker(index_uid=common.INDEX_UID, documents=small_movies):
index = empty_index(index_uid)
settings_update_task = index.update_embedders(
{
"default": {
"source": "userProvided",
"dimensions": 2,
}
}
)
index.wait_for_task(settings_update_task.task_uid)
document_addition_task = index.add_documents(documents)
index.wait_for_task(document_addition_task.task_uid)
return index
return index_maker
@fixture(scope="function")
def index_with_documents_and_facets(empty_index, small_movies):
def index_maker(index_uid=common.INDEX_UID, documents=small_movies):
index = empty_index(index_uid)
task_1 = index.update_filterable_attributes(["genre"])
index.wait_for_task(task_1.task_uid)
task_2 = index.add_documents(documents)
index.wait_for_task(task_2.task_uid)
return index
return index_maker
@fixture(scope="function")
def test_key(client):
key_info = {
"description": "test",
"actions": ["search"],
"indexes": ["movies"],
"expiresAt": None,
}
key = client.create_key(key_info)
yield key
try:
client.delete_key(key.key)
except MeilisearchApiError:
pass
@fixture(scope="function")
def test_key_info(client):
key_info = {
"name": "testKeyName",
"description": "test",
"actions": ["search"],
"indexes": [common.INDEX_UID],
"expiresAt": None,
}
yield key_info
try:
keys = client.get_keys().results
key = next(x for x in keys if x.description == key_info["description"])
client.delete_key(key.key)
except MeilisearchApiError:
pass
except StopIteration:
pass
@fixture(scope="function")
def get_private_key(client):
keys = client.get_keys().results
key = next(x for x in keys if "Default Search API" in x.name)
return key
@fixture
def enable_vector_search():
requests.patch(
f"{common.BASE_URL}/experimental-features",
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
json={"vectorStore": True},
timeout=10,
)
yield
requests.patch(
f"{common.BASE_URL}/experimental-features",
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
json={"vectorStore": False},
timeout=10,
)
@fixture
def new_embedders():
return {
"default": UserProvidedEmbedder(dimensions=1).model_dump(by_alias=True),
"open_ai": OpenAiEmbedder().model_dump(by_alias=True),
}