|
1 | 1 | import pytest
|
| 2 | +from django.core.management import call_command |
| 3 | +from django.views.generic import UpdateView |
| 4 | + |
| 5 | +from cbv.models import Klass |
2 | 6 |
|
3 | 7 | from .factories import InheritanceFactory, KlassFactory
|
4 | 8 |
|
@@ -53,3 +57,26 @@ def test_diamond(self) -> None:
|
53 | 57 | mro = d.get_all_ancestors()
|
54 | 58 |
|
55 | 59 | 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