1
1
from decimal import Decimal
2
+ from unittest .mock import patch , PropertyMock
2
3
3
4
from django .conf import settings
4
5
from 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
7
8
8
9
from demo .models import Book
9
10
10
11
# TODO: Fix sqlite
12
+
11
13
User = get_user_model ()
12
14
13
15
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 ):
15
37
databases = settings .TEST_ENV_DB
16
38
17
39
@classmethod
18
40
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 (
21
43
username = "Admin" ,
email = "[email protected] " ,
password = "test" ,
22
44
)
23
45
24
46
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 (
27
49
name = "Business of the 21st Century" ,
28
50
created_by = self .user ,
29
51
amount = Decimal ("50" ),
@@ -34,17 +56,17 @@ def test_create_passes_with_annotated_check_constraint(self):
34
56
self .assertEqual (book .created_by , self .user )
35
57
36
58
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" :
39
61
with self .assertRaises (DatabaseError ):
40
- Book .objects .using (db_name ).create (
62
+ Book .objects .using (db_alias ).create (
41
63
name = "Business of the 21st Century" ,
42
64
created_by = self .user ,
43
65
amount = Decimal ("50" ),
44
66
)
45
67
else :
46
68
with self .assertRaises (IntegrityError ):
47
- Book .objects .using (db_name ).create (
69
+ Book .objects .using (db_alias ).create (
48
70
name = "Business of the 21st Century" ,
49
71
created_by = self .user ,
50
72
amount = Decimal ("50" ),
0 commit comments