From 8e89dfe1c24540d33b577377af633694ff57f505 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 14 Apr 2022 12:54:27 +0200 Subject: [PATCH] Fixed various tests on MySQL with MyISAM storage engine. --- tests/admin_filters/tests.py | 50 ++++++++++++------------- tests/admin_views/test_multidb.py | 3 ++ tests/auth_tests/test_admin_multidb.py | 3 ++ tests/backends/tests.py | 12 +++++- tests/contenttypes_tests/test_models.py | 3 ++ tests/model_forms/models.py | 3 ++ tests/raw_query/tests.py | 2 +- tests/serializers/test_natural.py | 12 +++--- 8 files changed, 54 insertions(+), 34 deletions(-) diff --git a/tests/admin_filters/tests.py b/tests/admin_filters/tests.py index 1fcb506b51..d542bcd3ec 100644 --- a/tests/admin_filters/tests.py +++ b/tests/admin_filters/tests.py @@ -1440,19 +1440,18 @@ class ListFiltersTests(TestCase): filterspec = changelist.get_filters(request)[0][-1] self.assertEqual(filterspec.title, "department") - choices = list(filterspec.choices(changelist)) - - self.assertEqual(choices[0]["display"], "All") - self.assertIs(choices[0]["selected"], True) - self.assertEqual(choices[0]["query_string"], "?") - - self.assertEqual(choices[1]["display"], "Development") - self.assertIs(choices[1]["selected"], False) - self.assertEqual(choices[1]["query_string"], "?department__code__exact=DEV") - - self.assertEqual(choices[2]["display"], "Design") - self.assertIs(choices[2]["selected"], False) - self.assertEqual(choices[2]["query_string"], "?department__code__exact=DSN") + choices = [ + (choice["display"], choice["selected"], choice["query_string"]) + for choice in filterspec.choices(changelist) + ] + self.assertCountEqual( + choices, + [ + ("All", True, "?"), + ("Development", False, "?department__code__exact=DEV"), + ("Design", False, "?department__code__exact=DSN"), + ], + ) # Filter by Department=='Development' -------------------------------- @@ -1466,19 +1465,18 @@ class ListFiltersTests(TestCase): filterspec = changelist.get_filters(request)[0][-1] self.assertEqual(filterspec.title, "department") - choices = list(filterspec.choices(changelist)) - - self.assertEqual(choices[0]["display"], "All") - self.assertIs(choices[0]["selected"], False) - self.assertEqual(choices[0]["query_string"], "?") - - self.assertEqual(choices[1]["display"], "Development") - self.assertIs(choices[1]["selected"], True) - self.assertEqual(choices[1]["query_string"], "?department__code__exact=DEV") - - self.assertEqual(choices[2]["display"], "Design") - self.assertIs(choices[2]["selected"], False) - self.assertEqual(choices[2]["query_string"], "?department__code__exact=DSN") + choices = [ + (choice["display"], choice["selected"], choice["query_string"]) + for choice in filterspec.choices(changelist) + ] + self.assertCountEqual( + choices, + [ + ("All", False, "?"), + ("Development", True, "?department__code__exact=DEV"), + ("Design", False, "?department__code__exact=DSN"), + ], + ) def test_lookup_with_dynamic_value(self): """ diff --git a/tests/admin_views/test_multidb.py b/tests/admin_views/test_multidb.py index da5b6b9a59..d868321a4b 100644 --- a/tests/admin_views/test_multidb.py +++ b/tests/admin_views/test_multidb.py @@ -16,6 +16,9 @@ class Router: db_for_write = db_for_read + def allow_relation(self, obj1, obj2, **hints): + return True + site = admin.AdminSite(name="test_adminsite") site.register(Book) diff --git a/tests/auth_tests/test_admin_multidb.py b/tests/auth_tests/test_admin_multidb.py index 20faab25d8..ce2ae6b103 100644 --- a/tests/auth_tests/test_admin_multidb.py +++ b/tests/auth_tests/test_admin_multidb.py @@ -15,6 +15,9 @@ class Router: db_for_write = db_for_read + def allow_relation(self, obj1, obj2, **hints): + return True + site = admin.AdminSite(name="test_adminsite") site.register(User, admin_class=UserAdmin) diff --git a/tests/backends/tests.py b/tests/backends/tests.py index 85b0e55cb0..9303089b51 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -694,8 +694,12 @@ class FkConstraintsTests(TransactionTestCase): a.reporter_id = 30 with connection.constraint_checks_disabled(): a.save() - with self.assertRaises(IntegrityError): + try: connection.check_constraints(table_names=[Article._meta.db_table]) + except IntegrityError: + pass + else: + self.skipTest("This backend does not support integrity checks.") transaction.set_rollback(True) def test_check_constraints_sql_keywords(self): @@ -705,8 +709,12 @@ class FkConstraintsTests(TransactionTestCase): obj.reporter_id = 30 with connection.constraint_checks_disabled(): obj.save() - with self.assertRaises(IntegrityError): + try: connection.check_constraints(table_names=["order"]) + except IntegrityError: + pass + else: + self.skipTest("This backend does not support integrity checks.") transaction.set_rollback(True) diff --git a/tests/contenttypes_tests/test_models.py b/tests/contenttypes_tests/test_models.py index a96e12e69f..2741556df7 100644 --- a/tests/contenttypes_tests/test_models.py +++ b/tests/contenttypes_tests/test_models.py @@ -288,6 +288,9 @@ class TestRouter: def db_for_write(self, model, **hints): return "default" + def allow_relation(self, obj1, obj2, **hints): + return True + @override_settings(DATABASE_ROUTERS=[TestRouter()]) class ContentTypesMultidbTests(TestCase): diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index a6f306c5b2..b6da15f48a 100644 --- a/tests/model_forms/models.py +++ b/tests/model_forms/models.py @@ -21,6 +21,9 @@ class Category(models.Model): slug = models.SlugField(max_length=20) url = models.CharField("The URL", max_length=40) + class Meta: + ordering = ("pk",) + def __str__(self): return self.name diff --git a/tests/raw_query/tests.py b/tests/raw_query/tests.py index ebbf31a8c8..1dcc7ce740 100644 --- a/tests/raw_query/tests.py +++ b/tests/raw_query/tests.py @@ -310,7 +310,7 @@ class RawQueryTests(TestCase): ("book_count", 1), ("book_count", 0), ) - authors = Author.objects.all() + authors = Author.objects.order_by("pk") self.assertSuccessfulRawQuery(Author, query, authors, expected_annotations) def test_white_space_query(self): diff --git a/tests/serializers/test_natural.py b/tests/serializers/test_natural.py index 6b6cfae6f3..b5b35708c6 100644 --- a/tests/serializers/test_natural.py +++ b/tests/serializers/test_natural.py @@ -78,11 +78,13 @@ def natural_key_test(self, format): # Deserialize and test. books = list(serializers.deserialize(format, string_data)) - self.assertEqual(len(books), 2) - self.assertEqual(books[0].object.title, book1["title"]) - self.assertEqual(books[0].object.pk, adrian.pk) - self.assertEqual(books[1].object.title, book2["title"]) - self.assertIsNone(books[1].object.pk) + self.assertCountEqual( + [(book.object.title, book.object.pk) for book in books], + [ + (book1["title"], adrian.pk), + (book2["title"], None), + ], + ) def natural_pk_mti_test(self, format):