Skip to content
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
45 changes: 14 additions & 31 deletions algoliasearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,6 @@ def _load_next_page(self):
self.pos = 0
self.cursor = self.answer.get('cursor', None)

class SynonymIterator:
"""Iterator on the synonyms of an index"""

def __init__(self, index, hits_per_page=1000):
self.index = index
self.hits_per_page = hits_per_page
self.page = 0

def __iter__(self):
self._load_next_page()
return self

def __next__(self):
return self.next()

def next(self):
if self.pos >= len(self.response['hits']):
self._load_next_page()
if self.pos < len(self.response['hits']):
result = self.response['hits'][self.pos]
self.pos += 1
# Remove highlighting.
if '_highlightResult' in result: del result['_highlightResult']
return result
else:
raise StopIteration

def _load_next_page(self):
self.response = self.index.search_synonyms('', page=self.page, hits_per_page=self.hits_per_page)
self.page += 1
self.pos = 0

class RuleIterator:
"""Iterator on the rules of an index"""
Expand Down Expand Up @@ -714,6 +683,20 @@ def search_synonyms(self, query, types=[], page=0, hits_per_page=100, request_op

return self._req(True, '/synonyms/search', 'POST', request_options, data=data)

def iter_synonyms(self, hits_per_page=1000):
page = 0
response = self.search_synonyms('', page=page, hits_per_page=hits_per_page)

while response['hits']:
for hit in response['hits']:
if '_highlightResult' in hit:
del hit['_highlightResult']

yield hit

page += 1
response = self.search_synonyms('', page=page, hits_per_page=hits_per_page)

@deprecated
def waitTask(self, task_id, time_before_retry=100):
return self.wait_task(task_id, time_before_retry)
Expand Down
5 changes: 5 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys

# Use local algoliasearch package.
sys.path.append('..')

4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from helpers import Factory
from helpers import create_client, create_index, IndexWithData
from .helpers import Factory
from .helpers import create_client, create_index, IndexWithData


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def check_credentials():

for credential in credentials:
if credential not in os.environ:
print('environement variable {} not defined')
print('environement variable {} not defined'.format(credential))
assert False


Expand Down
4 changes: 2 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from algoliasearch.client import RequestOptions, MAX_API_KEY_LENGTH, Client
from algoliasearch.helpers import AlgoliaException
from fake_session import FakeSession
from helpers import Factory, check_credentials
from .fake_session import FakeSession
from .helpers import Factory, check_credentials


def test_request_options(client):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from helpers import wait_key, wait_missing_key
from .helpers import wait_key, wait_missing_key

def test_addObject(index):
task = index.addObject({'name': 'Paris'}, 'a')
Expand Down
33 changes: 20 additions & 13 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from algoliasearch.client import MAX_API_KEY_LENGTH
from algoliasearch.helpers import AlgoliaException

from helpers import Factory, rule_stub
from .helpers import Factory, rule_stub


def test_add_object(index):
Expand Down Expand Up @@ -113,18 +113,25 @@ def test_synonyms(index):
assert int(task['nbHits']) == 0


# def tests_synonym_iterator(index):
# synonyms = [
# {'objectID': 'city', 'type': 'synonym',
# 'synonyms': ['San Francisco', 'SF']},
# {'objectID': 'street', 'type': 'altCorrection1',
# 'word': 'Street', 'corrections': ['St']}
# ]
# task = index.batch_synonyms(synonyms)
# index.wait_task(task['taskID'])
# it = SynonymIterator(index)
# for got, expected in zip(it, synonyms):
# self.assertEqual(got, expected)
def test_iter_synonyms(index):
synonyms = [{
'objectID': 'city',
'type': 'synonym',
'synonyms': ['San Francisco', 'SF']
}, {
'objectID': 'street',
'type': 'altCorrection1',
'word': 'Street', 'corrections': ['St']
}]

task = index.batch_synonyms(synonyms)
index.wait_task(task['taskID'])

res = list(index.iter_synonyms(hits_per_page=1))
assert len(res) == 2

for synonym in synonyms:
assert synonym in res


def test_facet_search(index):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_keys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from helpers import wait_key, wait_missing_key
from .helpers import wait_key, wait_missing_key


def test_list_user_keys(client):
Expand Down