Skip to content

Commit

Permalink
fix: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Nov 26, 2024
1 parent 893f3f4 commit 999f115
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
22 changes: 17 additions & 5 deletions algoliasearch_django/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion algoliasearch_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
6 changes: 4 additions & 2 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
37 changes: 21 additions & 16 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,33 +256,38 @@ 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)

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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 999f115

Please sign in to comment.