-
Notifications
You must be signed in to change notification settings - Fork 90
Improve tests #80
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
Improve tests #80
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import pytest | ||
import meilisearch | ||
from meilisearch.tests import BASE_URL, MASTER_KEY | ||
|
||
class TestClient: | ||
|
||
""" TESTS: Client class """ | ||
|
||
@staticmethod | ||
def test_get_client(): | ||
"""Tests getting a client instance""" | ||
client = meilisearch.Client(BASE_URL, MASTER_KEY) | ||
assert client.config | ||
response = client.health() | ||
assert response.status_code == 200 | ||
|
||
@staticmethod | ||
def test_get_client_without_master_key(): | ||
"""Tests getting a client instance without MASTER KEY""" | ||
client = meilisearch.Client(BASE_URL) | ||
with pytest.raises(Exception): | ||
client.get_version() | ||
|
||
@staticmethod | ||
def test_get_client_with_wrong_master_key(): | ||
"""Tests getting a client instance with an invalid MASTER KEY""" | ||
client = meilisearch.Client(BASE_URL, MASTER_KEY + "123") | ||
with pytest.raises(Exception): | ||
client.get_version() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import meilisearch | ||
from meilisearch.tests import BASE_URL, MASTER_KEY | ||
|
||
class TestStats: | ||
|
||
""" TESTS: client stats route """ | ||
|
||
client = meilisearch.Client(BASE_URL, MASTER_KEY) | ||
index = None | ||
index2 = None | ||
|
||
def setup_class(self): | ||
self.index = self.client.create_index(uid='indexUID') | ||
self.index_2 = self.client.create_index(uid='indexUID2') | ||
|
||
def teardown_class(self): | ||
self.index.delete() | ||
self.index_2.delete() | ||
|
||
def test_get_all_stats(self): | ||
"""Tests getting all stats after creating two indexes""" | ||
response = self.client.get_all_stats() | ||
assert isinstance(response, object) | ||
assert 'databaseSize' in response | ||
assert isinstance(response['databaseSize'], int) | ||
assert 'lastUpdate' in response | ||
assert 'indexes' in response | ||
assert 'indexUID' in response['indexes'] | ||
assert 'indexUID2' in response['indexes'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,61 +3,66 @@ | |
from meilisearch.tests import BASE_URL, MASTER_KEY | ||
|
||
class TestIndex: | ||
|
||
""" TESTS: all index routes """ | ||
|
||
client = meilisearch.Client(BASE_URL, MASTER_KEY) | ||
index_uid = 'indexUID' | ||
|
||
""" index route """ | ||
@pytest.mark.run(order=1) | ||
def test_create_index(self): | ||
"""Tests an API call to create an index in MeiliSearch""" | ||
index = self.client.create_index(uid='movies_uid') | ||
"""Tests creating an index""" | ||
index = self.client.create_index(uid=self.index_uid) | ||
assert isinstance(index, object) | ||
assert index.uid == 'movies_uid' | ||
assert index.uid == self.index_uid | ||
|
||
def test_get_indexes(self): | ||
"""Tests an API call to get all indexes in MeiliSearch""" | ||
"""Tests getting all indexes""" | ||
response = self.client.get_indexes() | ||
assert isinstance(response, list) | ||
assert response[0]['uid'] == self.index_uid | ||
|
||
def test_get_index_with_uid(self): | ||
"""Tests an API call to get one index with uid in MeiliSearch""" | ||
response = self.client.get_index(uid='movies_uid') | ||
"""Tests getting one index with uid""" | ||
response = self.client.get_index(uid=self.index_uid) | ||
assert isinstance(response, object) | ||
assert response.uid == self.index_uid | ||
|
||
def test_get_index_with_none_uid(self): | ||
"""Raises an exception if the index UID si None""" | ||
"""Test raising an exception if the index UID is None""" | ||
with pytest.raises(Exception): | ||
self.client.get_index(uid=None) | ||
|
||
def test_index_info(self): | ||
"""Tests an API call to get an index's info in MeiliSearch""" | ||
index = self.client.get_index(uid='movies_uid') | ||
"""Tests getting an index's info""" | ||
index = self.client.get_index(uid=self.index_uid) | ||
response = index.info() | ||
assert isinstance(response, object) | ||
assert response['uid'] == 'movies_uid' | ||
assert response['uid'] == self.index_uid | ||
assert response['primaryKey'] is None | ||
|
||
def test_index_info_with_wrong_uid(self): | ||
"""Tests an API call to get an index's info in MeiliSearch with a wrong UID""" | ||
"""Tests getting an index's info in MeiliSearch with a wrong UID""" | ||
with pytest.raises(Exception): | ||
self.client.get_index(uid='wrongUID').info() | ||
|
||
def test_get_primary_key(self): | ||
"""Tests an API call to get primary-key of an index in MeiliSearch""" | ||
index = self.client.get_index(uid='movies_uid') | ||
"""Tests getting the primary-key of an index""" | ||
index = self.client.get_index(uid=self.index_uid) | ||
response = index.get_primary_key() | ||
assert response is None | ||
|
||
def test_update_index(self): | ||
"""Tests an API call to update an index in MeiliSearch""" | ||
index = self.client.get_index(uid='movies_uid') | ||
"""Tests updating an index""" | ||
index = self.client.get_index(uid=self.index_uid) | ||
response = index.update(primaryKey='objectID') | ||
assert isinstance(response, object) | ||
assert index.get_primary_key() == 'objectID' | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add a tests that tries to update an already existing primaryKey. Which I don;t think should work just to see if it raises correctly an error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a good idea for a test 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like the SDK doesn't handle the exception, it just sets a primary_key if it was None befor, otherwise it deas nothing. This should be a separate Issue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
@pytest.mark.run(order=-1) | ||
def test_delete_index(self): | ||
"""Tests an API call to delete an index in MeiliSearch""" | ||
index = self.client.get_index(uid="movies_uid") | ||
"""Tests deleting an index""" | ||
index = self.client.get_index(uid=self.index_uid) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should check if index was deleted by doing a call on
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done with with pytest.raises(Exception):
self.client.get_index(uid=self.index_uid).info() |
||
response = index.delete() | ||
assert isinstance(response, object) | ||
assert response.status_code == 204 | ||
with pytest.raises(Exception): | ||
self.client.get_index(uid=self.index_uid).info() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import json | ||
import pytest | ||
import meilisearch | ||
from meilisearch.tests import BASE_URL, MASTER_KEY | ||
|
||
class TestDocument: | ||
|
||
""" TESTS: all documents routes and optional params """ | ||
|
||
client = meilisearch.Client(BASE_URL, MASTER_KEY) | ||
index = None | ||
dataset_file = None | ||
dataset_json = None | ||
|
||
def setup_class(self): | ||
self.index = self.client.create_index(uid='indexUID') | ||
self.dataset_file = open("./datasets/small_movies.json", "r") | ||
self.dataset_json = json.loads(self.dataset_file.read()) | ||
self.dataset_file.close() | ||
|
||
def teardown_class(self): | ||
self.index.delete() | ||
|
||
def test_get_documents_default(self): | ||
"""Tests getting documents on a clean index""" | ||
response = self.index.get_documents() | ||
assert isinstance(response, list) | ||
assert response == [] | ||
|
||
def test_add_documents(self): | ||
"""Tests adding new documents to a clean index""" | ||
response = self.index.add_documents(self.dataset_json) | ||
assert isinstance(response, object) | ||
assert 'updateId' in response | ||
assert self.index.get_primary_key() == 'id' | ||
update = self.index.wait_for_pending_update(response['updateId']) | ||
assert update['status'] == 'processed' | ||
|
||
def test_get_document(self): | ||
"""Tests getting one document on a populated index""" | ||
response = self.index.get_document("500682") | ||
assert isinstance(response, object) | ||
assert 'title' in response | ||
assert response['title'] == 'The Highwaymen' | ||
|
||
def test_get_document_inexistent(self): | ||
"""Tests getting one INEXISTENT document on a populated index""" | ||
with pytest.raises(Exception): | ||
self.index.get_document("123") | ||
|
||
def test_get_documents_populated(self): | ||
"""Tests getting documents on a populated index""" | ||
response = self.index.get_documents() | ||
assert isinstance(response, list) | ||
assert len(response) == 20 | ||
|
||
def test_get_documents_offset_optional_params(self): | ||
"""Tests getting documents on a populated index with optional parameters""" | ||
response = self.index.get_documents() | ||
assert isinstance(response, list) | ||
assert len(response) == 20 | ||
response_offset_limit = self.index.get_documents({ | ||
'limit': 3, | ||
'offset': 1, | ||
'attributesToRetrieve': 'title' | ||
}) | ||
assert len(response_offset_limit) == 3 | ||
assert response_offset_limit[0]['title'] == response[1]['title'] | ||
|
||
def test_update_documents(self): | ||
"""Tests updating a single document and a set of documents """ | ||
response = self.index.get_documents() | ||
response[0]['title'] = "Some title" | ||
update = self.index.update_documents([response[0]]) | ||
assert isinstance(update, object) | ||
assert 'updateId' in update | ||
self.index.wait_for_pending_update(update['updateId']) | ||
response = self.index.get_documents() | ||
assert response[0]['title'] == "Some title" | ||
update = self.index.update_documents(self.dataset_json) | ||
self.index.wait_for_pending_update(update['updateId']) | ||
response = self.index.get_documents() | ||
assert response[0]['title'] != "Some title" | ||
|
||
def test_delete_document(self): | ||
"""Tests deleting a single document""" | ||
response = self.index.delete_document("500682") | ||
assert isinstance(response, object) | ||
assert 'updateId' in response | ||
self.index.wait_for_pending_update(response['updateId']) | ||
with pytest.raises(Exception): | ||
self.index.get_document("500682") | ||
|
||
def test_delete_documents(self): | ||
"""Tests deleting a set of documents """ | ||
to_delete = ["522681", "450465", "329996"] | ||
response = self.index.delete_documents(to_delete) | ||
assert isinstance(response, object) | ||
assert 'updateId' in response | ||
self.index.wait_for_pending_update(response['updateId']) | ||
for document in to_delete: | ||
with pytest.raises(Exception): | ||
self.index.get_document(document) | ||
|
||
def test_delete_all_documents(self): | ||
"""Tests updating all the documents in the index""" | ||
response = self.index.delete_all_documents() | ||
assert isinstance(response, object) | ||
assert 'updateId' in response | ||
self.index.wait_for_pending_update(response['updateId']) | ||
response = self.index.get_documents() | ||
assert isinstance(response, list) | ||
assert response == [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this function present in all SDK's? It is not in the JS library.