Skip to content

Restructure code #176

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 4 commits into from
Nov 18, 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
5 changes: 2 additions & 3 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
24 changes: 5 additions & 19 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
17 changes: 9 additions & 8 deletions meilisearch/tests/index/test_index.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -18,23 +19,23 @@ 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

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'

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'
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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'

Expand Down