|
1 | 1 | import unittest |
2 | 2 | from unittest.mock import patch, MagicMock |
3 | | -from aurora_dsql_django.schema import DatabaseSchemaEditor |
| 3 | + |
4 | 4 | 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 |
5 | 10 |
|
6 | 11 |
|
7 | 12 | class TestDatabaseSchemaEditor(unittest.TestCase): |
@@ -110,6 +115,68 @@ def test_check_sql_feature_enabled(self, mock_super_check_sql): |
110 | 115 | mock_super_check_sql.assert_called_once_with("test_check", "age >= 0") |
111 | 116 | self.assertEqual(result, "CHECK (age >= 0)") |
112 | 117 |
|
| 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 | + |
113 | 180 |
|
114 | 181 | if __name__ == '__main__': |
115 | 182 | unittest.main() |
0 commit comments