From f93df85a6dce8e2d16370e2e2218dece7c30b611 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 9 Nov 2018 16:51:18 +0100 Subject: [PATCH 1/4] Adds replace_all_rules and replace_all_synonims --- algoliasearch/index.py | 36 ++++++++++++++ tests/helpers.py | 8 +++ tests/test_index.py | 110 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 153 insertions(+), 1 deletion(-) diff --git a/algoliasearch/index.py b/algoliasearch/index.py index 6dc7b4e07..bbf1c000c 100644 --- a/algoliasearch/index.py +++ b/algoliasearch/index.py @@ -672,6 +672,24 @@ def batch_synonyms(self, synonyms, forward_to_slaves=False, return self._req(False, '/synonyms/batch', 'POST', request_options, params, synonyms) + def replace_all_synonyms(self, synonyms, request_options=None): + """ + Replace all synonyms in the index. + @param synonyms the synonyms to upload as a python dictionary. + the dictionary must contain an objectID key. + """ + for synonym in synonyms: + if 'objectID' not in synonym: + raise AlgoliaException('missing objectID in synonym body') + if synonym['objectID'] == '': + raise AlgoliaException('objectID in synonym body cannot be empty') + + params = { + 'replaceExistingSynonyms': True + } + + return self._req(False, '/synonyms/batch', 'POST', request_options, params, data=synonyms) + def get_synonym(self, object_id, request_options=None): """ Get a synonym from this index. @@ -1057,6 +1075,24 @@ def save_rule(self, rule, forward_to_replicas=False, request_options=None): params = {'forwardToReplicas': forward_to_replicas} return self._req(False, '/rules/%s' % str(rule['objectID']), 'PUT', request_options, params, rule) + def replace_all_rules(self, rules, request_options=None): + """ + Replace all rules in the index. + @param rules the rules to upload as a python dictionary. + the dictionary must contain an objectID key. + """ + for rule in rules: + if 'objectID' not in rule: + raise AlgoliaException('missing objectID in rule body') + if rule['objectID'] == '': + raise AlgoliaException('objectID in rule body cannot be empty') + + params = { + 'clearExistingRules': True + } + + return self._req(False, '/rules/batch', 'POST', request_options, params, data=rules) + def batch_rules(self, rules, forward_to_replicas=False, clear_existing_rules=False, request_options=None): """ Save a batch of new rules diff --git a/tests/helpers.py b/tests/helpers.py index abe909731..9ef964f19 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -120,6 +120,14 @@ def __getattr__(self, name): return getattr(self._index, name) +def synonym_stub(objid='my-synonym'): + return { + 'objectID': objid, + 'type': 'synonym', + 'synonyms': ['San Francisco', 'SF'] + } + + def rule_stub(objid='my-rule'): return { 'objectID': objid, diff --git a/tests/test_index.py b/tests/test_index.py index 5bfba4816..6e6df09eb 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -7,7 +7,7 @@ from algoliasearch.client import RequestOptions, MAX_API_KEY_LENGTH from algoliasearch.helpers import AlgoliaException -from .helpers import Factory, rule_stub +from .helpers import Factory, rule_stub, synonym_stub def test_add_object(index): @@ -113,6 +113,60 @@ def test_synonyms(index): assert int(task['nbHits']) == 0 +def test_replace_all_synonyms(index): + synonym = synonym_stub('one') + synonym2 = synonym_stub('two') + + res = index.replace_all_synonyms([synonym, synonym2]) + index.wait_task(res['taskID']) + + rules = index.search_synonyms('') + assert rules['nbHits'] == 2 + assert index.get_synonym('one') == synonym + assert index.get_synonym('two') == synonym2 + + synonym3 = synonym_stub('three') + synonym4 = synonym_stub('four') + + res = index.replace_all_synonyms([synonym3, synonym4]) + index.wait_task(res['taskID']) + + rules = index.search_synonyms('') + assert rules['nbHits'] == 2 + assert index.get_synonym('three') == synonym3 + assert index.get_synonym('four') == synonym4 + + +def test_replace_all_synonyms_expects_object_id(index): + synonym = synonym_stub('one') + + synonym.pop('objectID', None) + + try: + index.replace_all_synonyms([synonym]) + except AlgoliaException: + assert True + return + + # We should not be able to save a rule with an empty objectID. + assert False + + +def test_replace_all_synonyms_expects_not_empty_object_id(index): + synonym = synonym_stub('one') + + synonym['objectID'] = '' + + try: + index.replace_all_synonyms([synonym]) + except AlgoliaException: + assert True + return + + # We should not be able to save a rule with an empty objectID. + assert False + + def test_iter_synonyms(index): synonyms = [{ 'objectID': 'city', @@ -268,6 +322,60 @@ def test_batch_and_clear_rules(index): assert rules['nbHits'] == 0 +def test_replace_all_rules(index): + rule = rule_stub('one') + rule2 = rule_stub('two') + + res = index.replace_all_rules([rule, rule2]) + index.wait_task(res['taskID']) + + rules = index.search_rules() + assert rules['nbHits'] == 2 + assert index.read_rule('one') == rule + assert index.read_rule('two') == rule2 + + rule3 = rule_stub('three') + rule4 = rule_stub('four') + + res = index.replace_all_rules([rule3, rule4]) + index.wait_task(res['taskID']) + + rules = index.search_rules() + assert rules['nbHits'] == 2 + assert index.read_rule('three') == rule3 + assert index.read_rule('four') == rule4 + + +def test_replace_all_rules_expects_object_id(index): + rule = rule_stub('one') + + rule.pop('objectID', None) + + try: + index.replace_all_rules([rule]) + except AlgoliaException: + assert True + return + + # We should not be able to save a rule with an empty objectID. + assert False + + +def test_replace_all_rules_expects_not_empty_object_id(index): + rule = rule_stub('one') + + rule['objectID'] = '' + + try: + index.replace_all_rules([rule]) + except AlgoliaException: + assert True + return + + # We should not be able to save a rule with an empty objectID. + assert False + + def test_batch_and_clear_existing(index): rule = rule_stub() rule2 = rule_stub('my-second-rule') From a0ced3e4850c817ed3176f44c7f4282635aac716 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 9 Nov 2018 16:53:29 +0100 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9bf0da16..0ae0e1d37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ * Adds `copy_settings`, `copy_synonyms` and `copy_rules` methods on client - PR [#389](https://github.com/algolia/algoliasearch-client-python/pull/389) +* Adds `replace_all_rules` and `replace_all_synonyms` methods on client - PR [#390](https://github.com/algolia/algoliasearch-client-python/pull/390) + ### 1.17.0 - 2018-06-19 * Introduce AB Testing feature - PR [#408](https://github.com/algolia/algoliasearch-client-php/pull/#408) From d14d0757fa2ea7b6957b86ac77f25ef1481ecfe3 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 15 Nov 2018 13:32:38 +0100 Subject: [PATCH 3/4] Fixes wording --- algoliasearch/index.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/algoliasearch/index.py b/algoliasearch/index.py index bbf1c000c..3f99f8179 100644 --- a/algoliasearch/index.py +++ b/algoliasearch/index.py @@ -675,7 +675,7 @@ def batch_synonyms(self, synonyms, forward_to_slaves=False, def replace_all_synonyms(self, synonyms, request_options=None): """ Replace all synonyms in the index. - @param synonyms the synonyms to upload as a python dictionary. + @param synonyms the synonyms to upload as a list of python dictionary. the dictionary must contain an objectID key. """ for synonym in synonyms: @@ -1078,7 +1078,7 @@ def save_rule(self, rule, forward_to_replicas=False, request_options=None): def replace_all_rules(self, rules, request_options=None): """ Replace all rules in the index. - @param rules the rules to upload as a python dictionary. + @param rules the rules to upload as a list of python dictionary. the dictionary must contain an objectID key. """ for rule in rules: From 4d471da18ec95007efaf84346373b3de66d52458 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 20 Nov 2018 17:41:21 +0100 Subject: [PATCH 4/4] Removes verfiication of object id replace related methods --- algoliasearch/index.py | 14 ---------- tests/test_index.py | 60 ------------------------------------------ 2 files changed, 74 deletions(-) diff --git a/algoliasearch/index.py b/algoliasearch/index.py index 3f99f8179..cbde3d6f5 100644 --- a/algoliasearch/index.py +++ b/algoliasearch/index.py @@ -676,14 +676,7 @@ def replace_all_synonyms(self, synonyms, request_options=None): """ Replace all synonyms in the index. @param synonyms the synonyms to upload as a list of python dictionary. - the dictionary must contain an objectID key. """ - for synonym in synonyms: - if 'objectID' not in synonym: - raise AlgoliaException('missing objectID in synonym body') - if synonym['objectID'] == '': - raise AlgoliaException('objectID in synonym body cannot be empty') - params = { 'replaceExistingSynonyms': True } @@ -1079,14 +1072,7 @@ def replace_all_rules(self, rules, request_options=None): """ Replace all rules in the index. @param rules the rules to upload as a list of python dictionary. - the dictionary must contain an objectID key. """ - for rule in rules: - if 'objectID' not in rule: - raise AlgoliaException('missing objectID in rule body') - if rule['objectID'] == '': - raise AlgoliaException('objectID in rule body cannot be empty') - params = { 'clearExistingRules': True } diff --git a/tests/test_index.py b/tests/test_index.py index 6e6df09eb..03e2719e6 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -137,36 +137,6 @@ def test_replace_all_synonyms(index): assert index.get_synonym('four') == synonym4 -def test_replace_all_synonyms_expects_object_id(index): - synonym = synonym_stub('one') - - synonym.pop('objectID', None) - - try: - index.replace_all_synonyms([synonym]) - except AlgoliaException: - assert True - return - - # We should not be able to save a rule with an empty objectID. - assert False - - -def test_replace_all_synonyms_expects_not_empty_object_id(index): - synonym = synonym_stub('one') - - synonym['objectID'] = '' - - try: - index.replace_all_synonyms([synonym]) - except AlgoliaException: - assert True - return - - # We should not be able to save a rule with an empty objectID. - assert False - - def test_iter_synonyms(index): synonyms = [{ 'objectID': 'city', @@ -346,36 +316,6 @@ def test_replace_all_rules(index): assert index.read_rule('four') == rule4 -def test_replace_all_rules_expects_object_id(index): - rule = rule_stub('one') - - rule.pop('objectID', None) - - try: - index.replace_all_rules([rule]) - except AlgoliaException: - assert True - return - - # We should not be able to save a rule with an empty objectID. - assert False - - -def test_replace_all_rules_expects_not_empty_object_id(index): - rule = rule_stub('one') - - rule['objectID'] = '' - - try: - index.replace_all_rules([rule]) - except AlgoliaException: - assert True - return - - # We should not be able to save a rule with an empty objectID. - assert False - - def test_batch_and_clear_existing(index): rule = rule_stub() rule2 = rule_stub('my-second-rule')