Tests constraints

class ConstraintsTests(TestCase):

 1 import unittest
 2
 3 from django.conf import settings
 4 from django.core.checks import Error, Warning
 5 from django.core.checks.model_checks import _check_lazy_references
 6 from django.db import connection, connections, models
 7 from django.db.models.functions import Lower
 8 from django.db.models.signals import post_init
 9 from django.test import SimpleTestCase, TestCase
10 from django.test.utils import isolate_apps, override_settings, register_lookup
11
12
13 @isolate_apps('invalid_models_tests')
14 class ConstraintsTests(TestCase):
15     def test_check_constraints(self):
16         class Model(models.Model):
17             age = models.IntegerField()
18
19             class Meta:
20                 constraints = [models.CheckConstraint(check=models.Q(age__gte=18), name='is_adult')]
21
22         errors = Model.check(databases=self.databases)
23         warn = Warning(
24             '%s does not support check constraints.' % connection.display_name,
25             hint=(
26                 "A constraint won't be created. Silence this warning if you "
27                 "don't care about it."
28             ),
29             obj=Model,
30             id='models.W027',
31         )
32         expected = [] if connection.features.supports_table_check_constraints else [warn]
33         self.assertCountEqual(errors, expected)
34
35     def test_check_constraints_required_db_features(self):
36         class Model(models.Model):
37             age = models.IntegerField()
38
39             class Meta:
40                 required_db_features = {'supports_table_check_constraints'}
41                 constraints = [models.CheckConstraint(check=models.Q(age__gte=18), name='is_adult')]
42
43         self.assertEqual(Model.check(databases=self.databases), [])