Skip to content

Add tests for filtering on django-treebeard tree operations#91

Open
gasman wants to merge 2 commits intowagtail:mainfrom
gasman:feature/treebeard
Open

Add tests for filtering on django-treebeard tree operations#91
gasman wants to merge 2 commits intowagtail:mainfrom
gasman:feature/treebeard

Conversation

@gasman
Copy link
Copy Markdown
Contributor

@gasman gasman commented Mar 30, 2026

Description

Wanted to investigate what was needed to support searching querysets from django-treebeard operations such as get_descendants, and was pleasantly surprised to discover that they already worked :-) So, add unit tests to formalise that.

(The trickier part is to ensure that the path field stored in the Elasticsearch/OpenSearch index is kept in sync with the database, even after nodes have been updated with queryset.update - have opened django-treebeard/django-treebeard#381 to tackle that...)

AI usage

Github Copilot code completion for tests and fixtures.

Comment on lines +797 to +821
def test_get_ancestors_filter_mp_node(self):
dog = models.MPAnimal.objects.get(name="Dog")
results = self.backend.search("mammal", dog.get_ancestors())
self.assertCountEqual(
[r.name for r in results],
["Mammal"],
)
results = self.backend.search("reptile", dog.get_ancestors())
self.assertCountEqual(
[r.name for r in results],
[],
)

def test_get_ancestors_filter_ns_node(self):
dog = models.NSAnimal.objects.get(name="Dog")
results = self.backend.search("mammal", dog.get_ancestors())
self.assertCountEqual(
[r.name for r in results],
["Mammal"],
)
results = self.backend.search("reptile", dog.get_ancestors())
self.assertCountEqual(
[r.name for r in results],
[],
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since they share the same APIs and fixtures, I think my preference would be to avoid duplicating the tests somehow. For example, extracting these into a separate test class with model = models.MPAnimal and use self.model to refer to it, and then create a subclass that only adds model = models.NSAnimal. It would help running a subset of the tests when you only care about the treebeard ones, as well as making sure we apply any new tests to both MP and NS trees.

Alternatively, use subTest() with cases = [models.MPAnimal, models.NSAnimal], but that needs to be done for each case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants