11from decimal import Decimal
2+ from unittest .mock import patch , PropertyMock
23
34from django .conf import settings
45from django .contrib .auth import get_user_model
5- from django .db import IntegrityError , DatabaseError
6- from django .test import TestCase
6+ from django .db import IntegrityError , DatabaseError , connections
7+ from django .test import TestCase , TransactionTestCase
78
89from demo .models import Book
910
1011# TODO: Fix sqlite
12+
1113User = get_user_model ()
1214
1315
14- class AnnotateCheckConstraintTestCase (TestCase ):
16+ class AnnotatedCheckConstraintNameTestCase (TransactionTestCase ):
17+ databases = settings .TEST_ENV_DB
18+
19+ @patch ("django.VERSION" , new_callable = PropertyMock (return_value = (2 , 2 )))
20+ def test_correct_name_is_generated_for_django_less_than_30 (self , version ):
21+ for db_alias in self ._databases_names (include_mirrors = False ):
22+ connection = connections [db_alias ]
23+
24+ connection .disable_constraint_checking ()
25+
26+ for constraint in Book ._meta .constraints :
27+ name = "%(app_label)s_%(class)s_optional_field_provided"
28+ constraint .model = "demo.Book"
29+ constraint .name = name
30+
31+ path , args , kwargs = constraint .deconstruct ()
32+
33+ self .assertEqual (kwargs ["name" ], "demo_book_optional_field_provided" )
34+
35+
36+ class AnnotatedCheckConstraintTestCase (TestCase ):
1537 databases = settings .TEST_ENV_DB
1638
1739 @classmethod
1840 def setUpTestData (cls ):
19- for db_name in cls ._databases_names (include_mirrors = False ):
20- cls .user = User .objects .db_manager (db_name ).create_superuser (
41+ for db_alias in cls ._databases_names (include_mirrors = False ):
42+ cls .user = User .objects .db_manager (db_alias ).create_superuser (
2143 username = "Admin" ,
email = "[email protected] " ,
password = "test" ,
2244 )
2345
2446 def test_create_passes_with_annotated_check_constraint (self ):
25- for db_name in self ._databases_names (include_mirrors = False ):
26- book = Book .objects .using (db_name ).create (
47+ for db_alias in self ._databases_names (include_mirrors = False ):
48+ book = Book .objects .using (db_alias ).create (
2749 name = "Business of the 21st Century" ,
2850 created_by = self .user ,
2951 amount = Decimal ("50" ),
@@ -34,17 +56,17 @@ def test_create_passes_with_annotated_check_constraint(self):
3456 self .assertEqual (book .created_by , self .user )
3557
3658 def test_create_is_invalid_with_annotated_check_constraint (self ):
37- for db_name in self ._databases_names (include_mirrors = False ):
38- if db_name == "mysql" :
59+ for db_alias in self ._databases_names (include_mirrors = False ):
60+ if db_alias == "mysql" :
3961 with self .assertRaises (DatabaseError ):
40- Book .objects .using (db_name ).create (
62+ Book .objects .using (db_alias ).create (
4163 name = "Business of the 21st Century" ,
4264 created_by = self .user ,
4365 amount = Decimal ("50" ),
4466 )
4567 else :
4668 with self .assertRaises (IntegrityError ):
47- Book .objects .using (db_name ).create (
69+ Book .objects .using (db_alias ).create (
4870 name = "Business of the 21st Century" ,
4971 created_by = self .user ,
5072 amount = Decimal ("50" ),
0 commit comments