Skip to content

Commit

Permalink
Test compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Sep 27, 2023
1 parent af6072d commit a5827a8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
20 changes: 13 additions & 7 deletions tests/myapp/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import unittest

import django
from django.forms.models import modelform_factory
from myapp.models import Category, Genre, ReferencingModel
from myapp.tests import TreeTestCase
Expand Down Expand Up @@ -85,12 +88,15 @@ def test_movenodeform_save(self):
""",
)

@unittest.skipUnless(
django.VERSION > (4,), "Django 3.2 doesn't know Form.as_div() yet"
)
def test_movenodeform(self):
self.maxDiff = 2000
form = MoveNodeForm(Genre.objects.get(pk=7))
expected = (
'<tr><th><label for="id_target">Target:</label></th>'
'<td><select name="target" size="10" id="id_target" required>'
'<div><label for="id_target">Target:</label>'
'<select name="target" size="10" id="id_target" required>'
'<option value="" selected>---------</option>'
'<option value="1"> Action</option>'
'<option value="2">--- Platformer</option>'
Expand All @@ -102,16 +108,16 @@ def test_movenodeform(self):
'<option value="9"> Role-playing Game</option>'
'<option value="10">--- Action RPG</option>'
'<option value="11">--- Tactical RPG</option>'
"</select></td></tr>"
'<tr><th><label for="id_position">Position:</label></th>'
'<td><select name="position" id="id_position">'
"</select></div>"
'<div><label for="id_position">Position:</label>'
'<select name="position" id="id_position">'
'<option value="first-child">First child</option>'
'<option value="last-child">Last child</option>'
'<option value="left">Left sibling</option>'
'<option value="right">Right sibling</option>'
"</select></td></tr>"
"</select></div>"
)
self.assertHTMLEqual(str(form), expected)
self.assertHTMLEqual(str(form.as_div()), expected)
form = MoveNodeForm(
Genre.objects.get(pk=7), level_indicator="+--", target_select_size=5
)
Expand Down
34 changes: 21 additions & 13 deletions tests/myapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2565,10 +2565,8 @@ def get_changelist(self, request, model, modeladmin):
modeladmin.list_max_show_all,
modeladmin.list_editable,
modeladmin,
modeladmin.sortable_by,
]
if hasattr(modeladmin, "sortable_by"):
# New in Django 2.1
args.append(modeladmin.sortable_by)
if hasattr(modeladmin, "search_help_text"):
# New in Django 4.0
args.append(modeladmin.search_help_text)
Expand Down Expand Up @@ -3072,28 +3070,38 @@ def test_get_user_field_names_with_not_concrete_fields(self):
class BakeryTest(TestCase):
@override_settings(MPTT_ALLOW_TESTING_GENERATORS=True)
def test_create_by_bakery(self):
book = baker.make("Book")
self.assertQuerysetEqual(book.get_ancestors(), [])
self.assertQuerysetEqual(book.get_descendants(), [])
book_mommy = mommy.make("Book")
self.assertQuerysetEqual(book_mommy.get_ancestors(), [])
self.assertQuerysetEqual(book_mommy.get_descendants(), [])
if django.VERSION < (4,):
book = baker.make("Book")
self.assertQuerysetEqual(book.get_ancestors(), [])
self.assertQuerysetEqual(book.get_descendants(), [])
book_mommy = mommy.make("Book")
self.assertQuerysetEqual(book_mommy.get_ancestors(), [])
self.assertQuerysetEqual(book_mommy.get_descendants(), [])
else:
book = baker.make("Book")
self.assertQuerySetEqual(book.get_ancestors(), [])
self.assertQuerySetEqual(book.get_descendants(), [])
book_mommy = mommy.make("Book")
self.assertQuerySetEqual(book_mommy.get_ancestors(), [])
self.assertQuerySetEqual(book_mommy.get_descendants(), [])

@unittest.skipUnless(django.VERSION > (4,), "Django 3.2 is boring")
def test_create_by_bakery_exception(self):
with self.assertRaisesRegex(
Exception, "^The model_bakery populates django-mptt.*"
):
book = baker.make("Book")
self.assertQuerysetEqual(book.get_ancestors(), [])
self.assertQuerysetEqual(book.get_descendants(), [])
self.assertQuerySetEqual(book.get_ancestors(), [])
self.assertQuerySetEqual(book.get_descendants(), [])

@unittest.skipUnless(django.VERSION > (4,), "Django 3.2 is boring")
def test_create_by_mommy_exception(self):
with self.assertRaisesRegex(
Exception, "^The model_mommy populates django-mptt.*"
):
book = mommy.make("Book")
self.assertQuerysetEqual(book.get_ancestors(), [])
self.assertQuerysetEqual(book.get_descendants(), [])
self.assertQuerySetEqual(book.get_ancestors(), [])
self.assertQuerySetEqual(book.get_descendants(), [])


class FormTests(TestCase):
Expand Down

0 comments on commit a5827a8

Please sign in to comment.