33import django
44from django .conf import settings
55from django .db import models
6+ from django .db .models import CheckConstraint , Q
67from aurora_dsql_django .base import DatabaseWrapper
78from aurora_dsql_django .features import DatabaseFeatures
89from aurora_dsql_django .schema import DatabaseSchemaEditor
1617 django .setup ()
1718
1819
20+ def simple_quote_value (value ):
21+ return f"'{ value } '"
22+
23+
1924class TestWrapper (unittest .TestCase ):
2025 """Test Aurora DSQL wrapper behavior when all parts are working together"""
2126
2227 def setUp (self ):
2328 self .connection = DatabaseWrapper ({})
2429 self .connection .connection = MagicMock ()
25- self .connection .connection .encoding = 'utf8'
2630
2731 # Configure mock to use real components.
2832 self .connection .features = DatabaseFeatures (self .connection )
2933 self .schema_editor = DatabaseSchemaEditor (self .connection )
3034
31- def test_foreign_key_sql_generation (self ):
32- """Ensure foreign key SQL is not generated when the feature is disabled"""
35+ def _assert_sql_not_generated (self , operation_func , sql_patterns , message ):
36+ """Helper method to verify SQL patterns are not generated"""
37+ executed_sql = []
38+
39+ def mock_execute (sql , params = None ):
40+ executed_sql .append ((sql , params ))
41+
42+ # Capture SQL statements without running anything against a real DB.
43+ execute_patch = patch .object (self .schema_editor , 'execute' , side_effect = mock_execute )
44+
45+ # Work around issue caused by missing encoding configuration in test environment.
46+ quote_patch = patch .object (self .schema_editor , 'quote_value' , side_effect = simple_quote_value )
47+
48+ with execute_patch , quote_patch :
49+ operation_func ()
50+
51+ all_sql = [str (sql ) for sql , _ in executed_sql ]
52+ if hasattr (self .schema_editor , 'deferred_sql' ):
53+ all_sql += [str (sql ) for sql in self .schema_editor .deferred_sql ]
54+
55+ matching_statements = [sql for sql in all_sql if any (pattern in sql for pattern in sql_patterns )]
56+ self .assertListEqual ([], matching_statements , message )
57+
58+ def test_foreign_key_operations_ignored (self ):
59+ """Ensure foreign key constraint operations are ignored for model creation when the feature is disabled"""
3360
3461 class ParentModel (models .Model ):
3562 class Meta :
@@ -41,14 +68,77 @@ class ChildModel(models.Model):
4168 class Meta :
4269 app_label = 'test_app'
4370
44- # Mock execute to capture SQL without actually running it.
45- with patch .object (self .schema_editor , 'execute' ):
71+ def operation ():
4672 with self .schema_editor :
4773 self .schema_editor .create_model (ChildModel )
4874
49- # Check that no foreign key SQL was deferred.
50- foreign_key_statements = [sql for sql in self .schema_editor .deferred_sql if 'FOREIGN KEY' in str (sql )]
51- self .assertListEqual ([], foreign_key_statements , "Should not generate foreign key SQL" )
75+ self ._assert_sql_not_generated (
76+ operation ,
77+ ['FOREIGN KEY' , 'REFERENCES' ],
78+ "Should not generate foreign key SQL"
79+ )
80+
81+ def test_check_constraint_create_model_ignored (self ):
82+ """Ensure check constraint operations are ignored for model creation when the feature is disabled"""
83+
84+ class CheckConstraintModel (models .Model ):
85+ age = models .IntegerField ()
86+
87+ class Meta :
88+ app_label = 'test_app'
89+ constraints = [
90+ CheckConstraint (condition = Q (age__gte = 0 ), name = 'age_gte_0' )
91+ ]
92+
93+ def operation ():
94+ with self .schema_editor :
95+ self .schema_editor .create_model (CheckConstraintModel )
96+
97+ self ._assert_sql_not_generated (
98+ operation ,
99+ ['CHECK' ],
100+ "Should not generate check constraint SQL"
101+ )
102+
103+ def test_check_constraint_add_constraint_ignored (self ):
104+ """Ensure add_constraint operations ignore check constraints when the feature is disabled"""
105+
106+ class AddCheckConstraintModel (models .Model ):
107+ age = models .IntegerField ()
108+
109+ class Meta :
110+ app_label = 'test_app'
111+
112+ constraint = CheckConstraint (condition = Q (age__gte = 0 ), name = 'age_gte_0' )
113+
114+ def operation ():
115+ self .schema_editor .add_constraint (AddCheckConstraintModel , constraint )
116+
117+ self ._assert_sql_not_generated (
118+ operation ,
119+ ['CHECK' ],
120+ "Should not execute check constraint SQL"
121+ )
122+
123+ def test_check_constraint_remove_constraint_ignored (self ):
124+ """Ensure remove_constraint operations ignore check constraints when the feature is disabled"""
125+
126+ class RemoveCheckConstraintModel (models .Model ):
127+ age = models .IntegerField ()
128+
129+ class Meta :
130+ app_label = 'test_app'
131+
132+ constraint = CheckConstraint (condition = Q (age__gte = 0 ), name = 'age_gte_0' )
133+
134+ def operation ():
135+ self .schema_editor .remove_constraint (RemoveCheckConstraintModel , constraint )
136+
137+ self ._assert_sql_not_generated (
138+ operation ,
139+ ['CONSTRAINT' ],
140+ "Should not execute check constraint removal SQL"
141+ )
52142
53143
54144if __name__ == '__main__' :
0 commit comments