|
| 1 | +import unittest |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | +import django |
| 4 | +from django.conf import settings |
| 5 | +from django.db import models |
| 6 | +from aurora_dsql_django.base import DatabaseWrapper |
| 7 | +from aurora_dsql_django.features import DatabaseFeatures |
| 8 | +from aurora_dsql_django.schema import DatabaseSchemaEditor |
| 9 | + |
| 10 | +if not settings.configured: |
| 11 | + settings.configure( |
| 12 | + INSTALLED_APPS=['django.contrib.contenttypes'], |
| 13 | + DATABASES={'default': {'ENGINE': 'aurora_dsql_django'}}, |
| 14 | + USE_TZ=True, |
| 15 | + ) |
| 16 | + django.setup() |
| 17 | + |
| 18 | + |
| 19 | +class TestWrapper(unittest.TestCase): |
| 20 | + """Test Aurora DSQL wrapper behavior when all parts are working together""" |
| 21 | + |
| 22 | + def setUp(self): |
| 23 | + self.connection = DatabaseWrapper({}) |
| 24 | + self.connection.connection = MagicMock() |
| 25 | + self.connection.connection.encoding = 'utf8' |
| 26 | + |
| 27 | + # Configure mock to use real components. |
| 28 | + self.connection.features = DatabaseFeatures(self.connection) |
| 29 | + self.schema_editor = DatabaseSchemaEditor(self.connection) |
| 30 | + |
| 31 | + def test_foreign_key_sql_generation(self): |
| 32 | + """Ensure foreign key SQL is not generated when the feature is disabled""" |
| 33 | + |
| 34 | + class ParentModel(models.Model): |
| 35 | + class Meta: |
| 36 | + app_label = 'test_app' |
| 37 | + |
| 38 | + class ChildModel(models.Model): |
| 39 | + parent = models.ForeignKey(ParentModel, on_delete=models.CASCADE) |
| 40 | + |
| 41 | + class Meta: |
| 42 | + app_label = 'test_app' |
| 43 | + |
| 44 | + # Mock execute to capture SQL without actually running it. |
| 45 | + with patch.object(self.schema_editor, 'execute'): |
| 46 | + with self.schema_editor: |
| 47 | + self.schema_editor.create_model(ChildModel) |
| 48 | + |
| 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") |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == '__main__': |
| 55 | + unittest.main() |
0 commit comments