Skip to content

Add dumps methods #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,7 @@ faceted_search_walkthrough_facets_distribution_1: |-
client.get_index('movies').search('Batman', {
'facetsDistribution': ['genres']
})
post_dump_1: |-
client.create_dump()
get_dump_status_1: |-
client.get_dump_status('20201101-110357260')
29 changes: 29 additions & 0 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,32 @@ def version(self):
Information about the version of MeiliSearch.
"""
return self.get_version()

def create_dump(self):
"""Triggers the creation of a MeiliSearch dump

Returns
----------
Dump: dict
Information about the dump.
https://docs.meilisearch.com/references/dump.html#create-a-dump
"""
return self.http.post(self.config.paths.dumps)

def get_dump_status(self, uid):
"""Retrieves the status of a MeiliSearch dump creation

Parameters
----------
uid: str
UID of the dump

Returns
----------
Dump status: dict
Information about the dump status.
https://docs.meilisearch.com/references/dump.html#get-dump-status
"""
return self.http.get(
self.config.paths.dumps + '/' + str(uid) + '/status'
)
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Paths():
synonyms = 'synonyms'
accept_new_fields = 'accept-new-fields'
attributes_for_faceting = 'attributes-for-faceting'
dumps = 'dumps'

def __init__(self, url, api_key=None):
"""
Expand Down
8 changes: 8 additions & 0 deletions meilisearch/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import time

MASTER_KEY = 'masterKey'
BASE_URL = 'http://127.0.0.1:7700'

def clear_all_indexes(client):
indexes = client.get_indexes()
for index in indexes:
client.get_index(index['uid']).delete()

def wait_for_dump_creation(client, dump_uid):
dump_status = client.get_dump_status(dump_uid)
while dump_status['status'] == 'processing':
time.sleep(0.1)
dump_status = client.get_dump_status(dump_uid)
55 changes: 55 additions & 0 deletions meilisearch/tests/client/test_client_dumps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import json
import pytest
import meilisearch
from meilisearch.tests import BASE_URL, MASTER_KEY, clear_all_indexes, wait_for_dump_creation
from meilisearch.errors import MeiliSearchApiError

class TestClientDumps:

""" TESTS: Client dumps creation and status """

client = meilisearch.Client(BASE_URL, MASTER_KEY)
index = None
dataset_file = None
dataset_json = None

def setup_class(self):
clear_all_indexes(self.client)
self.dataset_file = open('./datasets/small_movies.json', 'r')
self.dataset_json = json.loads(self.dataset_file.read())
self.dataset_file.close()

def setup_method(self, method):
"""Creates and populates an index before each test is run"""
self.index = self.client.create_index(uid='indexUID-' + method.__name__)
response = self.index.add_documents(self.dataset_json, primary_key='id')
self.index.wait_for_pending_update(response['updateId'])

def teardown_method(self, method):
self.client.get_index('indexUID-' + method.__name__).delete()

def teardown_class(self):
"""Cleans all indexes in MEiliSearch when all the test are done"""
clear_all_indexes(self.client)

def test_dump_creation(self):
"""Tests the creation of a MeiliSearch dump"""
dump = self.client.create_dump()
assert dump['uid'] is not None
assert dump['status'] == 'processing'
wait_for_dump_creation(self.client, dump['uid'])

def test_dump_status_route(self):
"""Tests the route for getting a MeiliSearch dump status"""
dump = self.client.create_dump()
assert dump['uid'] is not None
assert dump['status'] == 'processing'
dump_status = self.client.get_dump_status(dump['uid'])
assert dump_status['uid'] is not None
assert dump_status['status'] == 'processing'
wait_for_dump_creation(self.client, dump['uid'])

def test_dump_status_nonexistent_uid_raises_error(self):
"""Tests the route for getting a nonexistent dump status"""
with pytest.raises(MeiliSearchApiError):
self.client.get_dump_status('uid_not_exists')