Skip to content

Commit

Permalink
Test get_all_ancestors against UpdateView
Browse files Browse the repository at this point in the history
We previously tested against a couple of simple artificial examples,
but I didn't trust it to cover all the possible cases.

This change ensures that we're testing against a complicated real-world
example, UpdateView, which I believe to represent sufficient complexity.
  • Loading branch information
meshy committed Jun 13, 2024
1 parent f773611 commit 5e1dc15
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import pytest
from django.core.management import call_command
from django.views.generic import UpdateView

from cbv.models import Klass

from .factories import InheritanceFactory, KlassFactory

Expand Down Expand Up @@ -53,3 +57,26 @@ def test_diamond(self) -> None:
mro = d.get_all_ancestors()

assert mro == [b, c, a]

def test_real(self) -> None:
"""
Test the MRO of a real class hierarchy, taken from the Django's UpdateView.
"""
# The version of Django that we are using for this test is 3.1
# because that is the version we have in our dependencies when we run tests.
# If this fails in future, it is probably because the version of Django
# has been updated and the MRO of the UpdateView has changed.
# In that case, feel free to update the version we're loading here.
call_command("loaddata", "3.1.json")

mro = Klass.objects.get(name="UpdateView").get_all_ancestors()

# For the sake of comparison, we convert the Klass objects to a tuple of strings,
# where the first element is the module name and the second is the class name.
mro_strings = [(k.module.name, k.name) for k in mro]

# The first element is the class itself, so we skip it.
# The last element is object, so we skip it.
real_ancestors = UpdateView.__mro__[1:][:-1]

assert mro_strings == [(c.__module__, c.__name__) for c in real_ancestors]

0 comments on commit 5e1dc15

Please sign in to comment.