From 999f1158aa19951011b98a45808e7d631aef6d1a Mon Sep 17 00:00:00 2001 From: shortcuts Date: Tue, 26 Nov 2024 17:27:45 +0100 Subject: [PATCH] fix: more tests --- algoliasearch_django/decorators.py | 22 ++++++++++++++---- algoliasearch_django/models.py | 2 +- runtests.py | 6 ++++- tests/test_engine.py | 6 +++-- tests/test_index.py | 37 +++++++++++++++++------------- 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/algoliasearch_django/decorators.py b/algoliasearch_django/decorators.py index d1a075c..b5e422e 100644 --- a/algoliasearch_django/decorators.py +++ b/algoliasearch_django/decorators.py @@ -53,14 +53,26 @@ def __init__(self, model=None): if model is not None: self.models = [model] else: - self.models = algolia_engine.get_registered_models() + self.models = algolia_engine._AlgoliaEngine__registered_models # pyright: ignore def __enter__(self): for model in self.models: - post_save.disconnect(algolia_engine.__post_save_receiver, sender=model) - pre_delete.disconnect(algolia_engine.__pre_delete_receiver, sender=model) + post_save.disconnect( + algolia_engine._AlgoliaEngine__post_save_receiver, + sender=model, # pyright: ignore + ) + pre_delete.disconnect( + algolia_engine._AlgoliaEngine__pre_delete_receiver, + sender=model, # pyright: ignore + ) def __exit__(self, exc_type, exc_value, traceback): for model in self.models: - post_save.connect(algolia_engine.__post_save_receiver, sender=model) - pre_delete.connect(algolia_engine.__pre_delete_receiver, sender=model) + post_save.connect( + algolia_engine._AlgoliaEngine__post_save_receiver, + sender=model, # pyright: ignore + ) + pre_delete.connect( + algolia_engine._AlgoliaEngine__pre_delete_receiver, + sender=model, # pyright: ignore + ) diff --git a/algoliasearch_django/models.py b/algoliasearch_django/models.py index af4050c..2d12355 100644 --- a/algoliasearch_django/models.py +++ b/algoliasearch_django/models.py @@ -559,7 +559,7 @@ def reindex_all(self, batch_size=1000): logger.debug("RESTORE REPLICAS") if should_keep_replicas: _resp = self.__client.set_settings(self.index_name, self.settings) - self.__client.wait_for_task(self.tmp_index_name, _resp.task_id) + self.__client.wait_for_task(self.index_name, _resp.task_id) if should_keep_rules: _resp = self.__client.save_rules(self.index_name, rules, True) self.__client.wait_for_task(self.index_name, _resp.task_id) diff --git a/runtests.py b/runtests.py index f05758f..a2de077 100755 --- a/runtests.py +++ b/runtests.py @@ -14,7 +14,11 @@ def main(): TestRunner = get_runner(settings) test_runner = TestRunner(failfase=True) # kept here to run a single test - # failures = test_runner.run_tests(["tests.test_index.IndexTestCase.test_reindex_with_synonyms"]) + # failures = test_runner.run_tests( + # [ + # "tests.test_index.IndexTestCase.test_reindex_with_rules" + # ] + # ) failures = test_runner.run_tests(["tests"]) sys.exit(bool(failures)) diff --git a/tests/test_engine.py b/tests/test_engine.py index c67672e..97be25c 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -32,8 +32,10 @@ def test_init_exception(self): def test_user_agent(self): self.assertIn( - "Algolia for Django (%s); Django (%s)" % __version__ % __django__version__, - self.engine.client._config.user_agent.get(), + "Algolia for Django ({}); Django ({})".format( + __version__, __django__version__ + ), + self.engine.client._config._user_agent.get(), ) def test_auto_discover_indexes(self): diff --git a/tests/test_index.py b/tests/test_index.py index 6dc5e80..684d0af 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -256,7 +256,7 @@ class WebsiteIndex(AlgoliaIndex): rule = { "objectID": "my-rule", "condition": {"pattern": "some text", "anchoring": "is"}, - "consequence": {"params": {"query": "other text"}}, + "consequence": {"params": {"hitsPerPage": 42}}, } self.assertIsNotNone(self.index.index_name) @@ -264,25 +264,30 @@ class WebsiteIndex(AlgoliaIndex): if self.index.index_name is None: return - self.client.save_rule(self.index.index_name, rule["objectID"], rule) + self.client.save_rule_with_http_info( + self.index.index_name, rule["objectID"], rule + ) # When reindexing with no settings on the instance self.index = WebsiteIndex(Website, self.client, settings.ALGOLIA) self.index.reindex_all() - # Expect the rules to be kept across reindex - def remove_metadata(rule): - copy = dict(rule) - del copy["_metadata"] - return copy - rules = [] - self.index.__client.browse_rules( - self.index.index_name, lambda _resp: rules.extend(_resp.hits) + self.client.browse_rules( + self.index.index_name, + lambda _resp: rules.extend([_hit.to_dict() for _hit in _resp.hits]), ) - rules = list(map(remove_metadata, rules)) self.assertEqual(len(rules), 1, "There should only be one rule") - self.assertIn(rule, rules, "The existing rule should be kept over reindex") + self.assertEqual( + rules[0]["consequence"], + rule["consequence"], + "The existing rule should be kept over reindex", + ) + self.assertEqual( + rules[0]["objectID"], + rule["objectID"], + "The existing rule should be kept over reindex", + ) def test_reindex_with_synonyms(self): # Given an existing index defined with settings @@ -313,11 +318,11 @@ class WebsiteIndex(AlgoliaIndex): self.index.reindex_all() # Expect the synonyms to be kept across reindex - resp = self.client.search_synonyms(index_name=self.index.index_name) - print(resp, self.index.index_name) - synonyms = [] - self.client.browse_synonyms(self.index.index_name, lambda _resp: print(_resp)) + self.client.browse_synonyms( + self.index.index_name, + lambda _resp: synonyms.extend([_hit.to_dict() for _hit in _resp.hits]), + ) self.assertEqual(len(synonyms), 1, "There should only be one synonym") self.assertIn( synonym, synonyms, "The existing synonym should be kept over reindex"