diff --git a/.github/workflows/django-spanner-integration-tests-against-emulator-3.10.yml b/.github/workflows/django-spanner-integration-tests-against-emulator.yml similarity index 85% rename from .github/workflows/django-spanner-integration-tests-against-emulator-3.10.yml rename to .github/workflows/django-spanner-integration-tests-against-emulator.yml index 5b39cb24c34f..1077d7c6db28 100644 --- a/.github/workflows/django-spanner-integration-tests-against-emulator-3.10.yml +++ b/.github/workflows/django-spanner-integration-tests-against-emulator.yml @@ -1,23 +1,27 @@ permissions: contents: read +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + on: pull_request: paths: - 'packages/django-google-spanner/**' - - '.github/workflows/django-spanner-integration-tests-against-emulator-3.10.yml' + - '.github/workflows/django-spanner-integration-tests-against-emulator.yml' push: branches: - main paths: - 'packages/django-google-spanner/**' - - '.github/workflows/django-spanner-integration-tests-against-emulator-3.10.yml' + - '.github/workflows/django-spanner-integration-tests-against-emulator.yml' defaults: run: working-directory: packages/django-google-spanner -name: Run Django Spanner integration tests against emulator 3.10 +name: Run Django Spanner integration tests against emulator jobs: check_changes: runs-on: ubuntu-latest @@ -33,7 +37,7 @@ jobs: filters: | django_spanner: - 'packages/django-google-spanner/**' - - '.github/workflows/django-spanner-integration-tests-against-emulator-3.10.yml' + - '.github/workflows/django-spanner-integration-tests-against-emulator.yml' system-tests: needs: check_changes @@ -42,7 +46,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.10", "3.12"] + python-version: ["3.12", "3.14"] services: emulator: @@ -63,7 +67,7 @@ jobs: - name: Install nox run: python -m pip install nox - name: Run nox - run: nox -s "unit-${{ matrix.python-version }}" + run: nox -s "system-${{ matrix.python-version }}" env: SPANNER_EMULATOR_HOST: localhost:9010 GOOGLE_CLOUD_PROJECT: emulator-test-project diff --git a/packages/django-google-spanner/django_spanner/features.py b/packages/django-google-spanner/django_spanner/features.py index 261599182634..07118a344735 100644 --- a/packages/django-google-spanner/django_spanner/features.py +++ b/packages/django-google-spanner/django_spanner/features.py @@ -10,8 +10,6 @@ from django.db.backends.base.features import BaseDatabaseFeatures from django.db.utils import InterfaceError -from django_spanner import USE_EMULATOR - class DatabaseFeatures(BaseDatabaseFeatures): can_introspect_big_integer_field = False @@ -56,13 +54,8 @@ def introspected_field_types(self): supports_sequence_reset = False supports_timezones = False supports_transactions = True - if USE_EMULATOR: - # Emulator does not support check constraints. - supports_column_check_constraints = False - supports_table_check_constraints = False - else: - supports_column_check_constraints = True - supports_table_check_constraints = True + supports_column_check_constraints = True + supports_table_check_constraints = True has_native_json_field = True supports_json_field = True supports_primitives_in_json_field = True @@ -113,6 +106,15 @@ def introspected_field_types(self): "constraints.tests.UniqueConstraintTests.test_validate_expression_condition", "constraints.tests.UniqueConstraintTests.test_validate_expression_generated_field_stored", "constraints.tests.UniqueConstraintTests.test_validate_fields_generated_field_stored_nulls_distinct", + "constraints.tests.UniqueConstraintTests.test_validate_nullable_textfield_with_isnull_true", + # Schema tests + "schema.tests.SchemaTests.test_check_constraint_exact_jsonfield", + "schema.tests.SchemaTests.test_rename_field_with_check_to_truncated_name", + # Model Fields tests + "model_fields.test_generatedfield.StoredGeneratedFieldTests.test_full_clean_with_check_constraint", + # Validation tests + "validation.test_constraints.PerformConstraintChecksTest.test_full_clean_with_check_constraints", + "validation.test_constraints.PerformConstraintChecksTest.test_full_clean_with_check_constraints_on_child_model", # Custom PK tests "custom_pk.tests.CustomPKTests.test_auto_field_subclass_create", # Datetimes tests @@ -2331,6 +2333,11 @@ def introspected_field_types(self): "model_fields.test_jsonfield.TestQuerying.test_deep_negative_lookup_array", "model_fields.test_jsonfield.TestQuerying.test_deep_negative_lookup_mixed", "model_fields.test_jsonfield.TestQuerying.test_shallow_list_negative_lookup", + # CheckConstraint.validate() uses Q.check() (SELECT 1 WHERE ... without FROM), + # which Spanner rejects and poisons the atomic block. + "foreign_object.tests.ForeignObjectModelValidationTests.test_validate_constraints_with_foreign_object", + "model_forms.tests.ConstraintValidationTests.test_check_constraint_refs_excluded_field", + "model_forms.tests.ConstraintValidationTests.test_unique_constraint_refs_excluded_field", ) if django.VERSION >= (6, 0): diff --git a/packages/django-google-spanner/django_spanner/introspection.py b/packages/django-google-spanner/django_spanner/introspection.py index 867d806e21d0..a03afd0b0f02 100644 --- a/packages/django-google-spanner/django_spanner/introspection.py +++ b/packages/django-google-spanner/django_spanner/introspection.py @@ -241,9 +241,14 @@ def get_constraints(self, cursor, table_name): ) for constraint, constraint_type in constraint_types: already_added = constraint in constraints - if constraint_type == "FOREIGN KEY": - # We don't yet support anything related to FOREIGN KEY. - # See https://github.com/googleapis/python-spanner-django/issues/313. + if constraint_type == "FOREIGN KEY" or ( + constraint_type == "CHECK" and constraint.startswith("CK_IS_NOT_NULL_") + ): + # TODO(https://github.com/googleapis/google-cloud-python/issues/19145): + # Foreign key introspection is not yet implemented in this + # Django backend (can_introspect_foreign_keys = False), and + # CK_IS_NOT_NULL_* entries are Spanner's synthetic NOT NULL + # constraints that cannot be dropped via DROP CONSTRAINT. if already_added: del constraints[constraint] continue diff --git a/packages/django-google-spanner/django_spanner/operations.py b/packages/django-google-spanner/django_spanner/operations.py index 2c21adbff29b..b19a111958ad 100644 --- a/packages/django-google-spanner/django_spanner/operations.py +++ b/packages/django-google-spanner/django_spanner/operations.py @@ -136,10 +136,11 @@ def quote_name(self, name: str) -> str: backticks, backslashes, or ASCII control characters) are escaped and wrapped in backticks as a single identifier. - Spanner does not allow spaces or hyphens in table or index names, even - when quoted. Only when running Django's own backend test suite - (``RUNNING_SPANNER_BACKEND_TESTS=1``) are spaces and hyphens replaced - with underscores so Django test models that use such names can run. + Spanner does not allow spaces, hyphens, or '>' in table, index, or + constraint names, even when quoted. Only when running Django's own + backend test suite (``RUNNING_SPANNER_BACKEND_TESTS=1``) are those + characters replaced with underscores so Django test models that use + such names can run. :type name: str :param name: The table, index, or column name to quote. @@ -154,7 +155,7 @@ def quote_name(self, name: str) -> str: if not name: return name if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1": - name = name.replace(" ", "_").replace("-", "_") + name = name.replace(" ", "_").replace("-", "_").replace(">", "_") if _QUOTED_IDENTIFIER_RE.match(name): return name # TODO(https://github.com/googleapis/google-cloud-python/issues/19072): diff --git a/packages/django-google-spanner/django_spanner/schema.py b/packages/django-google-spanner/django_spanner/schema.py index 146720b60956..3bc94dce77be 100644 --- a/packages/django-google-spanner/django_spanner/schema.py +++ b/packages/django-google-spanner/django_spanner/schema.py @@ -11,7 +11,6 @@ from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.models.fields import NOT_PROVIDED -from django_spanner import USE_EMULATOR from django_spanner._opentelemetry_tracing import trace_call @@ -563,15 +562,6 @@ def _alter_column_type_sql( [], ) - def _check_sql(self, name, check): - # Emulator does not support check constraints yet. - if USE_EMULATOR: - return None - return self.sql_constraint % { - "name": self.quote_name(name), - "constraint": self.sql_check_constraint % {"check": check}, - } - def _unique_sql( self, model, diff --git a/packages/django-google-spanner/noxfile.py b/packages/django-google-spanner/noxfile.py index 1889aa16ef87..a7705ff15bd2 100644 --- a/packages/django-google-spanner/noxfile.py +++ b/packages/django-google-spanner/noxfile.py @@ -136,15 +136,16 @@ def lint_setup_py(session): session.run("python", "setup.py", "check", "--restructuredtext", "--strict") -def default(session, django_version="5.2"): +def _resolve_django_dependency(session, django_version): # Django 6.0 dropped support for Python 3.10 and 3.11 (requires Python >= 3.12) if django_version == "6.0" and session.python in ("3.10", "3.11"): session.skip(f"Django {django_version} requires Python >= 3.12") + return f"django~={django_version}.0" + +def default(session, django_version="5.2"): # Target specific Django version - django_dep = ( - f"django~={django_version}.0" if django_version == "6.0" else "django~=5.2.0" - ) + django_dep = _resolve_django_dependency(session, django_version) # Install all test dependencies, then install this package in-place. session.install( @@ -184,8 +185,10 @@ def mockserver(session, django): default(session, django_version=django) -def system_test(session): +def system_test(session, django_version="5.2"): """Run the system test suite.""" + django_dep = _resolve_django_dependency(session, django_version) + constraints_path = str( CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt" ) @@ -215,7 +218,7 @@ def system_test(session): # Install all test dependencies, then install this package into the # virtualenv's dist-packages. session.install( - "django~=5.2", + django_dep, "mock", "pytest", "google-cloud-testutils", @@ -231,14 +234,17 @@ def system_test(session): @nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS) -def system(session): - # TODO(https://github.com/googleapis/google-cloud-python/issues/16099): - # System tests were disabled in the split repo to speed up the build process. - # See: https://github.com/googleapis/python-spanner-django/pull/929. - session.skip("System tests are temporarily disabled.") - - print("System tests with django 5.2") - system_test(session) +@nox.parametrize("django", DJANGO_VERSIONS) +def system(session, django): + if not os.environ.get("SPANNER_EMULATOR_HOST", ""): + # TODO(https://github.com/googleapis/google-cloud-python/issues/16099): + # System tests were disabled in the split repo to speed up the build + # process against live Spanner. + # See: https://github.com/googleapis/python-spanner-django/pull/929. + session.skip("System tests against live Spanner are temporarily disabled.") + + print(f"System tests with django {django}") + system_test(session, django_version=django) @nox.session(python=DEFAULT_PYTHON_VERSION) diff --git a/packages/django-google-spanner/tests/system/django_spanner/test_check_constraint.py b/packages/django-google-spanner/tests/system/django_spanner/test_check_constraint.py index 2fe4283b393f..3fbc20d225b2 100644 --- a/packages/django-google-spanner/tests/system/django_spanner/test_check_constraint.py +++ b/packages/django-google-spanner/tests/system/django_spanner/test_check_constraint.py @@ -5,14 +5,12 @@ # https://developers.google.com/open-source/licenses/bsd import datetime -import unittest from django.db import connection from django.db.utils import IntegrityError from django.test import TransactionTestCase from django.utils import timezone -from django_spanner import USE_EMULATOR from tests.system.django_spanner.utils import ( setup_database, setup_instance, @@ -23,7 +21,6 @@ from .models import Event -@unittest.skipIf(USE_EMULATOR, "Check Constraint is not implemented in emulator.") class TestCheckConstraint(TransactionTestCase): @classmethod def setUpClass(cls): @@ -61,8 +58,12 @@ def test_insert_invalid_value(self): now = timezone.now() now_minus_1_day = now - timezone.timedelta(days=1) event_invalid = Event(start_date=now, end_date=now_minus_1_day) - with self.assertRaises(IntegrityError): + with self.assertRaisesRegex(IntegrityError, "ConstraintError") as context: event_invalid.save() + cause = context.exception + while cause.__cause__ is not None: + cause = cause.__cause__ + self.assertIn("check_start_date", str(cause)) class TestCheckConstraintValidation(TransactionTestCase): diff --git a/packages/django-google-spanner/tests/system/django_spanner/test_json_field.py b/packages/django-google-spanner/tests/system/django_spanner/test_json_field.py index be3afd02a1ea..520d7e14e3c3 100644 --- a/packages/django-google-spanner/tests/system/django_spanner/test_json_field.py +++ b/packages/django-google-spanner/tests/system/django_spanner/test_json_field.py @@ -4,6 +4,8 @@ # license that can be found in the LICENSE file or at # https://developers.google.com/open-source/licenses/bsd +import unittest + from django.db import connection, models from django.test import TransactionTestCase @@ -17,6 +19,10 @@ from .models import Detail +@unittest.skip( + "TODO(https://github.com/googleapis/google-cloud-python/issues/19088): " + "Re-enable once JSONField key-transform equality lookup is fixed." +) class TestJsonField(TransactionTestCase): @classmethod def setUpClass(cls): diff --git a/packages/django-google-spanner/tests/unit/django_spanner/test_features.py b/packages/django-google-spanner/tests/unit/django_spanner/test_features.py index 75186d0ba3f1..bcad8aab1164 100644 --- a/packages/django-google-spanner/tests/unit/django_spanner/test_features.py +++ b/packages/django-google-spanner/tests/unit/django_spanner/test_features.py @@ -38,6 +38,8 @@ def test_spanner_specific_feature_flags(self): self.assertTrue(features.supports_json_field) self.assertTrue(features.supports_primitives_in_json_field) self.assertFalse(features.supports_json_field_contains) + self.assertTrue(features.supports_column_check_constraints) + self.assertTrue(features.supports_table_check_constraints) self.assertEqual(features.bare_select_suffix, " FROM UNNEST([1])") def test_check_constraint_validate_not_skipped(self): diff --git a/packages/django-google-spanner/tests/unit/django_spanner/test_introspection.py b/packages/django-google-spanner/tests/unit/django_spanner/test_introspection.py index 7091a31b2a46..1db83d64ec5a 100644 --- a/packages/django-google-spanner/tests/unit/django_spanner/test_introspection.py +++ b/packages/django-google-spanner/tests/unit/django_spanner/test_introspection.py @@ -221,3 +221,42 @@ def run_sql_in_snapshot(*args, **kwargs): }, }, ) + + def test_get_constraints_excludes_synthetic_not_null_check(self): + """Tests get_constraints filters synthetic CK_IS_NOT_NULL_* CHECK constraints.""" + db_introspection = DatabaseIntrospection(self.connection) + cursor = mock.MagicMock() + + def run_sql_in_snapshot(sql, *args, **kwargs): + if "CONSTRAINT_NAME, COLUMN_NAME" in sql: + return [ + ["CK_IS_NOT_NULL_Table_1_age", "age"], + ["chk_age_non_negative", "age"], + ] + if "CONSTRAINT_NAME, CONSTRAINT_TYPE" in sql: + return [ + ["CK_IS_NOT_NULL_Table_1_age", "CHECK"], + ["chk_age_non_negative", "CHECK"], + ] + return [] + + cursor.run_sql_in_snapshot = run_sql_in_snapshot + constraints = db_introspection.get_constraints( + cursor=cursor, table_name="Table_1" + ) + + self.assertEqual( + constraints, + { + "chk_age_non_negative": { + "check": True, + "columns": ["age"], + "foreign_key": None, + "index": False, + "orders": [], + "primary_key": False, + "type": None, + "unique": False, + }, + }, + ) diff --git a/packages/django-google-spanner/tests/unit/django_spanner/test_operations.py b/packages/django-google-spanner/tests/unit/django_spanner/test_operations.py index d78fb49c12eb..ee71c56b8ad2 100644 --- a/packages/django-google-spanner/tests/unit/django_spanner/test_operations.py +++ b/packages/django-google-spanner/tests/unit/django_spanner/test_operations.py @@ -282,6 +282,10 @@ def test_quote_name_running_spanner_backend_tests(self): self.assertEqual(self.db_operations.quote_name("`already`"), "`already`") self.assertEqual(self.db_operations.quote_name(""), "") self.assertEqual(self.db_operations.quote_name(" "), "___") + self.assertEqual( + self.db_operations.quote_name("Generated model check constraint a > 0"), + "Generated_model_check_constraint_a___0", + ) for sample in ("my col-name", "SELECT", "`a b-c`", "a b\nc"): once = self.db_operations.quote_name(sample) self.assertEqual(self.db_operations.quote_name(once), once) @@ -293,6 +297,10 @@ def test_quote_name_running_spanner_backend_tests(self): self.assertEqual( self.db_operations.quote_name("my col-name"), "`my col-name`" ) + self.assertEqual( + self.db_operations.quote_name("a > 0"), + "`a > 0`", + ) with mock.patch.dict("django_spanner.operations.os.environ", {}, clear=True): self.assertEqual( diff --git a/packages/django-google-spanner/tests/unit/django_spanner/test_schema.py b/packages/django-google-spanner/tests/unit/django_spanner/test_schema.py index 95b240031e19..27ffacb1032e 100644 --- a/packages/django-google-spanner/tests/unit/django_spanner/test_schema.py +++ b/packages/django-google-spanner/tests/unit/django_spanner/test_schema.py @@ -8,8 +8,8 @@ from unittest import mock from django.db import NotSupportedError, connection, connections, models -from django.db.models import Index -from django.db.models.fields import AutoField, IntegerField +from django.db.models import CheckConstraint, Index, Q +from django.db.models.fields import AutoField, IntegerField, PositiveIntegerField from django.test.utils import isolate_apps from google.api_core.exceptions import InvalidArgument @@ -520,6 +520,68 @@ def test_skip_default_generated_and_db_default(self): normal_field = mock.MagicMock(generated=False, db_default=None) self.assertTrue(schema_editor.skip_default(normal_field)) + def test_check_sql(self): + """Tests _check_sql generates CHECK constraint SQL.""" + schema_editor = DatabaseSchemaEditor(self.connection) + self.assertEqual( + schema_editor._check_sql("check_age", "age >= 0"), + "CONSTRAINT check_age CHECK (age >= 0)", + ) + + # Keep the inline CheckedModel out of Django's global app registry. + @isolate_apps("tests") + def test_create_model_with_check_constraint(self): + """Tests create_model includes table-level CHECK constraints.""" + + class CheckedModel(models.Model): + num = models.IntegerField() + + class Meta: + app_label = "tests" + db_table = "tests_checked_model" + constraints = [ + CheckConstraint( + condition=Q(num__gte=0), + name="num_gte_0", + ), + ] + + with DatabaseSchemaEditor(self.connection) as schema_editor: + schema_editor.execute = mock.MagicMock() + schema_editor.create_model(CheckedModel) + + schema_editor.execute.assert_called_once_with( + "CREATE TABLE tests_checked_model (id INT64 NOT NULL, " + "num INT64 NOT NULL, CONSTRAINT num_gte_0 CHECK (num >= 0)) " + "PRIMARY KEY(id)", + None, + ) + + def test_alter_field_add_check_constraint(self): + """Tests altering IntegerField to PositiveIntegerField emits ADD CONSTRAINT CHECK.""" + with DatabaseSchemaEditor(self.connection) as schema_editor: + schema_editor.execute = mock.MagicMock() + old_field = IntegerField() + old_field.set_attributes_from_name("num") + old_field.model = Author + new_field = PositiveIntegerField() + new_field.set_attributes_from_name("num") + new_field.model = Author + + schema_editor.alter_field(Author, old_field, new_field) + + schema_editor.execute.assert_called_once() + positional_args, keyword_args = schema_editor.execute.call_args + self.assertEqual(len(positional_args), 1) + self.assertEqual(keyword_args, {}) + statement = positional_args[0] + self.assertIsNotNone(statement) + self.assertEqual( + str(statement), + "ALTER TABLE tests_author ADD CONSTRAINT " + "tests_author_num_f8282a0e_check CHECK (num >= 0)", + ) + def _assert_recorded_spans( self, expected_spans: list[tuple[str, StatusCode, dict[str, str]]],