diff --git a/meilisearch/client.py b/meilisearch/client.py index 7b493686..c7ac574d 100644 --- a/meilisearch/client.py +++ b/meilisearch/client.py @@ -45,8 +45,7 @@ def create_index(self, uid, options=None): HTTPError In case of any other error found here https://docs.meilisearch.com/references/#errors-status-code """ - index_dict = Index.create(self.config, uid, options) - return Index(self.config, index_dict['uid'], index_dict['primaryKey']) + return Index.create(self.config, uid, options) def get_indexes(self): """Get all indexes. @@ -60,7 +59,7 @@ def get_indexes(self): list List of indexes in dictionnary format. (e.g [{ 'uid': 'movies' 'primaryKey': 'objectID' }]) """ - return Index.get_indexes(self.config) + return self.http.get(self.config.paths.index) def get_index(self, uid): """Get the index. diff --git a/meilisearch/index.py b/meilisearch/index.py index a397729e..e89a8221 100644 --- a/meilisearch/index.py +++ b/meilisearch/index.py @@ -64,7 +64,7 @@ def update(self, **body): payload['primaryKey'] = primary_key response = self.http.put('{}/{}'.format(self.config.paths.index, self.uid), payload) self.primary_key = response['primaryKey'] - return response + return self def fetch_info(self): """Fetch the information of the index. @@ -88,8 +88,8 @@ def get_primary_key(self): """ return self.fetch_info().primary_key - @staticmethod - def create(config, uid, options=None): + @classmethod + def create(cls, config, uid, options=None): """Create the index. Parameters @@ -112,22 +112,8 @@ def create(config, uid, options=None): if options is None: options = {} payload = {**options, 'uid': uid} - return HttpRequests(config).post(config.paths.index, payload) - - @staticmethod - def get_indexes(config): - """Get all indexes from meilisearch. - - Returns - ------- - indexes : list - List of indexes (dict) - Raises - ------ - HTTPError - In case of any error found here https://docs.meilisearch.com/references/#errors-status-code - """ - return HttpRequests(config).get(config.paths.index) + index_dict = HttpRequests(config).post(config.paths.index, payload) + return cls(config, index_dict['uid'], index_dict['primaryKey']) def get_all_update_status(self): """Get all update status from MeiliSearch diff --git a/meilisearch/tests/index/test_index.py b/meilisearch/tests/index/test_index.py index 81315509..f3932bd9 100644 --- a/meilisearch/tests/index/test_index.py +++ b/meilisearch/tests/index/test_index.py @@ -1,5 +1,6 @@ import pytest import meilisearch +from meilisearch.index import Index from meilisearch.tests import BASE_URL, MASTER_KEY, clear_all_indexes class TestIndex: @@ -18,7 +19,7 @@ def setup_class(self): def test_create_index(self): """Tests creating an index""" index = self.client.create_index(uid=self.index_uid) - assert isinstance(index, object) + assert isinstance(index, Index) assert index.uid == self.index_uid assert index.primary_key is None assert index.get_primary_key() is None @@ -26,7 +27,7 @@ def test_create_index(self): def test_create_index_with_primary_key(self): """Tests creating an index with a primary key""" index = self.client.create_index(uid=self.index_uid2, options={'primaryKey': 'book_id'}) - assert isinstance(index, object) + assert isinstance(index, Index) assert index.uid == self.index_uid2 assert index.primary_key == 'book_id' assert index.get_primary_key() == 'book_id' @@ -34,7 +35,7 @@ def test_create_index_with_primary_key(self): def test_create_index_with_uid_in_options(self): """Tests creating an index with a primary key""" index = self.client.create_index(uid=self.index_uid3, options={'uid': 'wrong', 'primaryKey': 'book_id'}) - assert isinstance(index, object) + assert isinstance(index, Index) assert index.uid == self.index_uid3 assert index.primary_key == 'book_id' assert index.get_primary_key() == 'book_id' @@ -51,7 +52,7 @@ def test_get_indexes(self): def test_index_with_any_uid(self): index = self.client.index('anyUID') - assert isinstance(index, object) + assert isinstance(index, Index) assert index.uid == 'anyUID' assert index.primary_key is None assert index.config is not None @@ -64,7 +65,7 @@ def test_index_with_none_uid(self): def test_get_index_with_valid_uid(self): """Tests getting one index with uid""" response = self.client.get_index(uid=self.index_uid) - assert isinstance(response, object) + assert isinstance(response, Index) assert response.uid == self.index_uid def test_get_index_with_none_uid(self): @@ -109,7 +110,7 @@ def test_index_fetch_info(self): """Tests getting the index info""" index = self.client.index(uid=self.index_uid) response = index.fetch_info() - assert isinstance(response, object) + assert isinstance(response, Index) assert response.uid == self.index_uid assert response.primary_key is None assert response.primary_key == index.primary_key @@ -119,7 +120,7 @@ def test_index_fetch_info_containing_primary_key(self): """Tests getting the index info""" index = self.client.index(uid=self.index_uid3) response = index.fetch_info() - assert isinstance(response, object) + assert isinstance(response, Index) assert response.uid == self.index_uid3 assert response.primary_key == 'book_id' assert response.primary_key == index.primary_key @@ -138,7 +139,7 @@ def test_update_index(self): """Tests updating an index""" index = self.client.index(uid=self.index_uid) response = index.update(primaryKey='objectID') - assert isinstance(response, object) + assert isinstance(response, Index) assert index.primary_key == 'objectID' assert index.get_primary_key() == 'objectID'