Skip to content

Commit 6c2e832

Browse files
committed
Changes due to the review
1 parent a1daa4e commit 6c2e832

File tree

7 files changed

+39
-37
lines changed

7 files changed

+39
-37
lines changed

meilisearch/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,13 @@ def get_dump_status(self, uid: str) -> Dict[str, str]:
356356
self.config.paths.dumps + '/' + str(uid) + '/status'
357357
)
358358

359-
def get_tasks(self) -> List[Dict[str, Any]]:
359+
def get_tasks(self) -> Dict[str, List[Dict[str, Any]]]:
360360
"""Get all tasks.
361361
362362
Returns
363363
-------
364364
task:
365-
List of all enqueued, processing, succeeded or failed actions of the index.
365+
Dictionary containing a list of all enqueued, processing, succeeded or failed actions of the index.
366366
367367
Raises
368368
------

meilisearch/index.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ def get_primary_key(self) -> Optional[str]:
117117
"""
118118
return self.fetch_info().primary_key
119119

120-
@classmethod
121-
def create(cls, config: Config, uid: str, options: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
120+
@staticmethod
121+
def create(config: Config, uid: str, options: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
122122
"""Create the index.
123123
124124
Parameters
@@ -144,13 +144,13 @@ def create(cls, config: Config, uid: str, options: Optional[Dict[str, Any]] = No
144144
payload = {**options, 'uid': uid}
145145
return HttpRequests(config).post(config.paths.index, payload)
146146

147-
def get_tasks(self) -> List[Dict[str, Any]]:
147+
def get_tasks(self) -> Dict[str, List[Dict[str, Any]]]:
148148
"""Get all tasks of a specific index from the last one.
149149
150150
Returns
151151
-------
152152
task:
153-
List of all enqueued, processing, succeeded or failed actions of the index.
153+
Dictionary containing a list of all enqueued, processing, succeeded or failed actions of the index.
154154
155155
Raises
156156
------
@@ -216,7 +216,7 @@ def wait_for_task(
216216
f'{self.config.paths.task}/{uid}'
217217
)
218218

219-
if get_task['status'] != 'enqueued' and get_task['status'] != 'processing':
219+
if get_task['status'] not in ('enqueued', 'processing'):
220220
return get_task
221221
sleep(interval_in_ms / 1000)
222222
time_delta = datetime.now() - start_time

tests/client/test_client_task_meilisearch.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ def test_get_tasks_default(client):
77
"""Tests getting the global tasks list."""
88
tasks = client.get_tasks()
99
assert isinstance(tasks, dict)
10-
assert len(tasks) == 1
11-
assert len(tasks['results']) != 0
10+
assert 'results' in tasks
1211

13-
def test_get_tasks(client, empty_index, small_movies):
12+
def test_get_tasks(client, empty_index):
1413
"""Tests getting the global tasks list after populated an index."""
15-
index = empty_index()
16-
response = index.add_documents(small_movies)
17-
assert 'uid' in response
18-
response = index.add_documents(small_movies)
19-
assert 'uid' in response
14+
current_tasks = client.get_tasks()
15+
pre_count = len(current_tasks["results"])
16+
empty_index()
2017
tasks = client.get_tasks()
21-
assert len(tasks) == 1
22-
assert len(tasks['results']) != 0
18+
assert isinstance(tasks, dict)
19+
assert len(tasks['results']) == pre_count + 1
2320

2421
def test_get_task(client):
2522
"""Tests getting the tasks list of an empty index."""
@@ -29,13 +26,16 @@ def test_get_task(client):
2926
assert isinstance(task, dict)
3027
assert len(task) == 9
3128
assert 'uid' in task
29+
assert 'indexUid' in task
3230
assert 'status' in task
3331
assert 'type' in task
3432
assert 'duration' in task
3533
assert 'enqueuedAt' in task
3634
assert 'finishedAt' in task
35+
assert 'details' in task
36+
assert 'startedAt' in task
3737

3838
def test_get_task_inexistent(client):
3939
"""Tests getting a task of an inexistent operation."""
4040
with pytest.raises(Exception):
41-
client.get_task('999')
41+
client.get_task('abc')

tests/conftest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ def clear_indexes(client):
2020
# Deletes all the indexes in the MeiliSearch instance.
2121
indexes = client.get_indexes()
2222
for index in indexes:
23-
client.index(index.uid).delete()
23+
task = client.index(index.uid).delete()
24+
client.wait_for_task(task['uid'])
2425

2526
@fixture(scope='function')
2627
def indexes_sample(client):
2728
indexes = []
2829
for index_args in common.INDEX_FIXTURE:
29-
response = client.create_index(**index_args)
30-
client.wait_for_task(response['uid'])
30+
task = client.create_index(**index_args)
31+
client.wait_for_task(task['uid'])
3132
indexes.append(client.get_index(index_args['uid']))
3233
# Yields the indexes to the test to make them accessible.
3334
yield indexes
@@ -76,7 +77,7 @@ def index_maker(index_name=common.INDEX_UID):
7677
def index_with_documents(empty_index, small_movies):
7778
def index_maker(index_name=common.INDEX_UID, documents=small_movies):
7879
index = empty_index(index_name)
79-
response = index.add_documents(documents)
80-
index.wait_for_task(response['uid'])
80+
task = index.add_documents(documents)
81+
index.wait_for_task(task['uid'])
8182
return index
8283
return index_maker

tests/index/test_index.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,19 @@ def test_get_primary_key(client):
163163
assert index.primary_key == 'book_id'
164164
assert index.get_primary_key() == 'book_id'
165165

166-
@pytest.mark.usefixtures("indexes_sample")
167-
def test_update_index(client, empty_index):
166+
def test_update_index(empty_index):
168167
"""Tests updating an index."""
169168
index = empty_index()
170-
response = client.get_index(uid=common.INDEX_UID)
169+
response = index.update(primary_key='objectID')
170+
index.wait_for_task(response['uid'])
171+
response = index.fetch_info()
171172
assert isinstance(response, Index)
172173
assert index.get_primary_key() == 'objectID'
173174
assert isinstance(index.created_at, datetime)
174175
assert isinstance(index.updated_at, datetime)
175176

176177
@pytest.mark.usefixtures("indexes_sample")
177-
def test_delete_index(client):
178+
def test_delete_index_by_client(client):
178179
"""Tests deleting an index."""
179180
response = client.index(uid=common.INDEX_UID).delete()
180181
assert response['status'] == 'enqueued'

tests/index/test_index_task_meilisearch.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@ def test_get_tasks_default(empty_index):
77
"""Tests getting the tasks list of an empty index."""
88
task = empty_index().get_tasks()
99
assert isinstance(task, dict)
10-
assert len(task) == 1
11-
assert len(task['results']) == 1
10+
assert 'results' in task
11+
assert len(task['results']) != 0
1212

1313
def test_get_tasks(empty_index, small_movies):
1414
"""Tests getting the tasks list of a populated index."""
1515
index = empty_index()
16+
current_tasks = index.get_tasks()
17+
pre_count = len(current_tasks["results"])
1618
response = index.add_documents(small_movies)
1719
assert 'uid' in response
18-
response = index.add_documents(small_movies)
19-
assert 'uid' in response
20-
assert 'status' in response
2120
task = index.get_tasks()
22-
assert len(task) == 1
23-
assert len(task['results']) > 2
21+
assert len(task['results']) == pre_count + 1
2422

2523
def test_get_task(client):
2624
"""Tests getting a task of a operation."""
@@ -31,13 +29,16 @@ def test_get_task(client):
3129
assert isinstance(task, dict)
3230
assert len(task) == 9
3331
assert 'uid' in task
32+
assert 'indexUid' in task
3433
assert 'status' in task
3534
assert 'type' in task
3635
assert 'duration' in task
3736
assert 'enqueuedAt' in task
3837
assert 'finishedAt' in task
38+
assert 'details' in task
39+
assert 'startedAt' in task
3940

4041
def test_get_task_inexistent(empty_index):
4142
"""Tests getting a task of an inexistent operation."""
4243
with pytest.raises(Exception):
43-
empty_index().get_task('999')
44+
empty_index().get_task('abc')

tests/index/test_index_wait_for_task.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ def test_wait_for_task_default(index_with_documents):
1212
update = index.wait_for_task(response['uid'])
1313
assert isinstance(update, dict)
1414
assert 'status' in update
15-
assert update['status'] != 'enqueued'
16-
assert update['status'] != 'processing'
15+
assert update['status'] not in ('enqueued', 'processing')
1716

1817
def test_wait_for_task_timeout(index_with_documents):
1918
"""Tests timeout risen by waiting for an update."""

0 commit comments

Comments
 (0)