authentik.root.tests.test_many_to_many

 1from django.apps import apps
 2from django.contrib.auth.models import Group as DjangoGroup
 3from django.db.models import ManyToManyField
 4from django.test import TestCase
 5
 6
 7class TestManyToMany(TestCase):
 8    def test_all_many_to_many_relations_are_explicit(self):
 9        implicit_m2ms = []
10        exceptions = [DjangoGroup]
11
12        for model in apps.get_models():
13            if model in exceptions:
14                continue
15            for field in model._meta.get_fields():
16                if not isinstance(field, ManyToManyField):
17                    continue
18                if field.remote_field.through._meta.auto_created:
19                    implicit_m2ms.append(f"{model.__name__}.{field.name}")
20
21        self.assertEqual(implicit_m2ms, [])
class TestManyToMany(django.test.testcases.TestCase):
 8class TestManyToMany(TestCase):
 9    def test_all_many_to_many_relations_are_explicit(self):
10        implicit_m2ms = []
11        exceptions = [DjangoGroup]
12
13        for model in apps.get_models():
14            if model in exceptions:
15                continue
16            for field in model._meta.get_fields():
17                if not isinstance(field, ManyToManyField):
18                    continue
19                if field.remote_field.through._meta.auto_created:
20                    implicit_m2ms.append(f"{model.__name__}.{field.name}")
21
22        self.assertEqual(implicit_m2ms, [])

Similar to TransactionTestCase, but use transaction.atomic() to achieve test isolation.

In most situations, TestCase should be preferred to TransactionTestCase as it allows faster execution. However, there are some situations where using TransactionTestCase might be necessary (e.g. testing some transactional behavior).

On database backends with no transaction support, TestCase behaves as TransactionTestCase.

def test_all_many_to_many_relations_are_explicit(self):
 9    def test_all_many_to_many_relations_are_explicit(self):
10        implicit_m2ms = []
11        exceptions = [DjangoGroup]
12
13        for model in apps.get_models():
14            if model in exceptions:
15                continue
16            for field in model._meta.get_fields():
17                if not isinstance(field, ManyToManyField):
18                    continue
19                if field.remote_field.through._meta.auto_created:
20                    implicit_m2ms.append(f"{model.__name__}.{field.name}")
21
22        self.assertEqual(implicit_m2ms, [])