Skip to content

Commit e1a0f76

Browse files
Fix test failures with older Python versions
1 parent 24639eb commit e1a0f76

File tree

4 files changed

+102
-8
lines changed

4 files changed

+102
-8
lines changed

aurora_dsql_django/schema.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
2121
from django.db.backends.postgresql import schema
22+
from django.db.models import CheckConstraint
2223

2324

2425
class DatabaseSchemaEditor(schema.DatabaseSchemaEditor):
@@ -64,13 +65,13 @@ def __enter__(self):
6465

6566
def add_index(self, model, index, concurrently=False):
6667
if index.contains_expressions and not self.connection.features.supports_expression_indexes:
67-
return None
68-
return super().add_index(model, index, concurrently)
68+
return
69+
super().add_index(model, index, concurrently)
6970

7071
def remove_index(self, model, index, concurrently=False):
7172
if index.contains_expressions and not self.connection.features.supports_expression_indexes:
72-
return None
73-
return super().remove_index(model, index, concurrently)
73+
return
74+
super().remove_index(model, index, concurrently)
7475

7576
def _check_sql(self, name, check):
7677
# There is no feature check in the upstream implementation when creating
@@ -79,6 +80,18 @@ def _check_sql(self, name, check):
7980
return None
8081
return super()._check_sql(name, check)
8182

83+
def add_constraint(self, model, constraint):
84+
# Older versions of Django don't reference supports_table_check_constraints, so we add this as a backup.
85+
if isinstance(constraint, CheckConstraint) and not self.connection.features.supports_table_check_constraints:
86+
return
87+
super().add_constraint(model, constraint)
88+
89+
def remove_constraint(self, model, constraint):
90+
# Older versions of Django don't reference supports_table_check_constraints, so we add this as a backup.
91+
if isinstance(constraint, CheckConstraint) and not self.connection.features.supports_table_check_constraints:
92+
return
93+
super().remove_constraint(model, constraint)
94+
8295
def _index_columns(self, table, columns, col_suffixes, opclasses):
8396
# Aurora DSQL doesn't support PostgreSQL opclasses.
8497
return BaseDatabaseSchemaEditor._index_columns(

aurora_dsql_django/tests/unit/test_schema.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import unittest
22
from unittest.mock import patch, MagicMock
3-
from aurora_dsql_django.schema import DatabaseSchemaEditor
3+
44
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
5+
from django.db.models.constraints import UniqueConstraint
6+
from django.db.models.query_utils import Q
7+
8+
from aurora_dsql_django.schema import DatabaseSchemaEditor
9+
from aurora_dsql_django.tests.utils import create_check_constraint
510

611

712
class TestDatabaseSchemaEditor(unittest.TestCase):
@@ -110,6 +115,68 @@ def test_check_sql_feature_enabled(self, mock_super_check_sql):
110115
mock_super_check_sql.assert_called_once_with("test_check", "age >= 0")
111116
self.assertEqual(result, "CHECK (age >= 0)")
112117

118+
@patch('aurora_dsql_django.schema.schema.DatabaseSchemaEditor.add_constraint')
119+
def test_add_constraint_check_disabled(self, mock_super_add_constraint):
120+
model = MagicMock()
121+
constraint = create_check_constraint(Q(age__gte=0), 'age_check')
122+
self.connection.features.supports_table_check_constraints = False
123+
124+
result = self.schema_editor.add_constraint(model, constraint)
125+
126+
self.assertIsNone(result)
127+
mock_super_add_constraint.assert_not_called()
128+
129+
@patch('aurora_dsql_django.schema.schema.DatabaseSchemaEditor.add_constraint')
130+
def test_add_constraint_check_enabled(self, mock_super_add_constraint):
131+
model = MagicMock()
132+
constraint = create_check_constraint(Q(age__gte=0), 'age_check')
133+
self.connection.features.supports_table_check_constraints = True
134+
135+
self.schema_editor.add_constraint(model, constraint)
136+
137+
mock_super_add_constraint.assert_called_once_with(model, constraint)
138+
139+
@patch('aurora_dsql_django.schema.schema.DatabaseSchemaEditor.add_constraint')
140+
def test_add_constraint_non_check(self, mock_super_add_constraint):
141+
model = MagicMock()
142+
constraint = UniqueConstraint(fields=['name'], name='unique_name')
143+
self.connection.features.supports_table_check_constraints = False
144+
145+
self.schema_editor.add_constraint(model, constraint)
146+
147+
mock_super_add_constraint.assert_called_once_with(model, constraint)
148+
149+
@patch('aurora_dsql_django.schema.schema.DatabaseSchemaEditor.remove_constraint')
150+
def test_remove_constraint_check_disabled(self, mock_super_remove_constraint):
151+
model = MagicMock()
152+
constraint = create_check_constraint(Q(age__gte=0), 'age_check')
153+
self.connection.features.supports_table_check_constraints = False
154+
155+
result = self.schema_editor.remove_constraint(model, constraint)
156+
157+
self.assertIsNone(result)
158+
mock_super_remove_constraint.assert_not_called()
159+
160+
@patch('aurora_dsql_django.schema.schema.DatabaseSchemaEditor.remove_constraint')
161+
def test_remove_constraint_check_enabled(self, mock_super_remove_constraint):
162+
model = MagicMock()
163+
constraint = create_check_constraint(Q(age__gte=0), 'age_check')
164+
self.connection.features.supports_table_check_constraints = True
165+
166+
self.schema_editor.remove_constraint(model, constraint)
167+
168+
mock_super_remove_constraint.assert_called_once_with(model, constraint)
169+
170+
@patch('aurora_dsql_django.schema.schema.DatabaseSchemaEditor.remove_constraint')
171+
def test_remove_constraint_non_check(self, mock_super_remove_constraint):
172+
model = MagicMock()
173+
constraint = UniqueConstraint(fields=['name'], name='unique_name')
174+
self.connection.features.supports_table_check_constraints = False
175+
176+
self.schema_editor.remove_constraint(model, constraint)
177+
178+
mock_super_remove_constraint.assert_called_once_with(model, constraint)
179+
113180

114181
if __name__ == '__main__':
115182
unittest.main()

aurora_dsql_django/tests/unit/test_wrapper.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from aurora_dsql_django.base import DatabaseWrapper
88
from aurora_dsql_django.features import DatabaseFeatures
99
from aurora_dsql_django.schema import DatabaseSchemaEditor
10+
from aurora_dsql_django.tests.utils import create_check_constraint
1011

1112
if not settings.configured:
1213
settings.configure(
@@ -87,7 +88,7 @@ class CheckConstraintModel(models.Model):
8788
class Meta:
8889
app_label = 'test_app'
8990
constraints = [
90-
CheckConstraint(condition=Q(age__gte=0), name='age_gte_0')
91+
create_check_constraint(Q(age__gte=0), 'age_gte_0')
9192
]
9293

9394
def operation():
@@ -109,7 +110,7 @@ class AddCheckConstraintModel(models.Model):
109110
class Meta:
110111
app_label = 'test_app'
111112

112-
constraint = CheckConstraint(condition=Q(age__gte=0), name='age_gte_0')
113+
constraint = create_check_constraint(Q(age__gte=0), 'age_gte_0')
113114

114115
def operation():
115116
self.schema_editor.add_constraint(AddCheckConstraintModel, constraint)
@@ -129,7 +130,7 @@ class RemoveCheckConstraintModel(models.Model):
129130
class Meta:
130131
app_label = 'test_app'
131132

132-
constraint = CheckConstraint(condition=Q(age__gte=0), name='age_gte_0')
133+
constraint = create_check_constraint(Q(age__gte=0), 'age_gte_0')
133134

134135
def operation():
135136
self.schema_editor.remove_constraint(RemoveCheckConstraintModel, constraint)

aurora_dsql_django/tests/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Shared test utilities for Aurora DSQL Django tests."""
2+
3+
import django
4+
from django.db.models import CheckConstraint
5+
6+
7+
# noinspection PyArgumentList
8+
def create_check_constraint(condition, name):
9+
"""Create CheckConstraint with Django version compatibility"""
10+
if django.VERSION >= (5, 0):
11+
return CheckConstraint(condition=condition, name=name)
12+
else:
13+
return CheckConstraint(check=condition, name=name)

0 commit comments

Comments
 (0)