Skip to content

Commit 5e1dc15

Browse files
committed
Test get_all_ancestors against UpdateView
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.
1 parent f773611 commit 5e1dc15

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/test_models.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import pytest
2+
from django.core.management import call_command
3+
from django.views.generic import UpdateView
4+
5+
from cbv.models import Klass
26

37
from .factories import InheritanceFactory, KlassFactory
48

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

5559
assert mro == [b, c, a]
60+
61+
def test_real(self) -> None:
62+
"""
63+
Test the MRO of a real class hierarchy, taken from the Django's UpdateView.
64+
"""
65+
# The version of Django that we are using for this test is 3.1
66+
# because that is the version we have in our dependencies when we run tests.
67+
# If this fails in future, it is probably because the version of Django
68+
# has been updated and the MRO of the UpdateView has changed.
69+
# In that case, feel free to update the version we're loading here.
70+
call_command("loaddata", "3.1.json")
71+
72+
mro = Klass.objects.get(name="UpdateView").get_all_ancestors()
73+
74+
# For the sake of comparison, we convert the Klass objects to a tuple of strings,
75+
# where the first element is the module name and the second is the class name.
76+
mro_strings = [(k.module.name, k.name) for k in mro]
77+
78+
# The first element is the class itself, so we skip it.
79+
# The last element is object, so we skip it.
80+
real_ancestors = UpdateView.__mro__[1:][:-1]
81+
82+
assert mro_strings == [(c.__module__, c.__name__) for c in real_ancestors]

0 commit comments

Comments
 (0)