From 9c8abab62b28d8a2c54e42107491bfea8a08f98d Mon Sep 17 00:00:00 2001 From: Parth Sharma Date: Sun, 26 Oct 2025 04:53:30 +0530 Subject: [PATCH 1/4] Fix of django 5.0 tests --- tests/backends/base/test_operations.py | 6 +++--- tests/backends/models.py | 11 ++++++++++- tests/basic/tests.py | 1 + tests/constraints/tests.py | 1 + tests/fixtures/tests.py | 7 +++---- tests/force_insert_update/models.py | 4 +++- tests/migrations/test_operations.py | 5 ++++- tests/model_formsets/test_uuid.py | 7 +++++++ tests/model_inheritance/models.py | 5 +++++ tests/schema/tests.py | 4 ++++ tests/validation/test_unique.py | 3 ++- 11 files changed, 43 insertions(+), 11 deletions(-) diff --git a/tests/backends/base/test_operations.py b/tests/backends/base/test_operations.py index d2a3fb67657f..8d18c230db19 100644 --- a/tests/backends/base/test_operations.py +++ b/tests/backends/base/test_operations.py @@ -165,16 +165,16 @@ def test_datetime_extract_sql(self): def test_prepare_join_on_clause(self): author_table = Author._meta.db_table - author_id_field = Author._meta.get_field("id") + author_name_field = Author._meta.get_field("name") book_table = Book._meta.db_table book_fk_field = Book._meta.get_field("author") lhs_expr, rhs_expr = self.ops.prepare_join_on_clause( author_table, - author_id_field, + author_name_field, book_table, book_fk_field, ) - self.assertEqual(lhs_expr, Col(author_table, author_id_field)) + self.assertEqual(lhs_expr, Col(author_table, author_name_field)) self.assertEqual(rhs_expr, Col(book_table, book_fk_field)) diff --git a/tests/backends/models.py b/tests/backends/models.py index f48b7692afe5..96dd7dc73cee 100644 --- a/tests/backends/models.py +++ b/tests/backends/models.py @@ -39,13 +39,22 @@ def get_queryset(self): class SchoolBus(models.Model): number = models.IntegerField() - schoolclasses = models.ManyToManyField("SchoolClass") + schoolclasses = models.ManyToManyField("SchoolClass", through="SchoolBusSchoolClass") objects = SchoolBusManager() class Meta: base_manager_name = "objects" +class SchoolBusSchoolClass(models.Model): + schoolbus = models.ForeignKey(SchoolBus, on_delete=models.CASCADE) + schoolclass = models.ForeignKey(SchoolClass, on_delete=models.CASCADE) + + class Meta: + unique_together = (('schoolbus', 'schoolclass'),) + db_table = "backends_schoolbus_schoolclass" + + class VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ(models.Model): primary_key_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.AutoField( primary_key=True diff --git a/tests/basic/tests.py b/tests/basic/tests.py index 6d29ece58088..3f9130994aab 100644 --- a/tests/basic/tests.py +++ b/tests/basic/tests.py @@ -176,6 +176,7 @@ def test_save_primary_with_default(self): with self.assertNumQueries(1): PrimaryKeyWithDefault().save() + @skipUnlessDBFeature("supports_db_default") def test_save_primary_with_db_default(self): # An UPDATE attempt is skipped when a primary key has db_default. with self.assertNumQueries(1): diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py index 2df3706995b1..183501de2730 100644 --- a/tests/constraints/tests.py +++ b/tests/constraints/tests.py @@ -1263,6 +1263,7 @@ def test_requires_name(self): with self.assertRaisesMessage(ValueError, msg): models.UniqueConstraint(fields=["field"]) + @skipUnlessDBFeature("supports_db_default") def test_database_default(self): models.UniqueConstraint( fields=["field_with_db_default"], name="unique_field_with_db_default" diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index bce55bc3554d..58be2d479325 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -834,13 +834,12 @@ def test_dumpdata_objects_with_prefetch_related(self): management.call_command( "loaddata", "fixture6.json", "fixture8.json", verbosity=0 ) - with self.assertNumQueries(5): + with self.assertNumQueries(4): self._dumpdata_assert( ["fixtures.visa"], - '[{"fields": {"permissions": [["add_user", "auth", "user"]],' - '"person": ["Stephane Grappelli"]},' + '[{"fields": {"person": ["Stephane Grappelli"]},' '"model": "fixtures.visa", "pk": 2},' - '{"fields": {"permissions": [], "person": ["Prince"]},' + '{"fields": {"person": ["Prince"]},' '"model": "fixtures.visa", "pk": 3}]', natural_foreign_keys=True, primary_keys="2,3", diff --git a/tests/force_insert_update/models.py b/tests/force_insert_update/models.py index e6fdfbd827db..9257d9a0a238 100644 --- a/tests/force_insert_update/models.py +++ b/tests/force_insert_update/models.py @@ -4,7 +4,7 @@ """ from django.db import models - +from django_singlestore.schema import ModelStorageManager class Counter(models.Model): name = models.CharField(max_length=10) @@ -37,7 +37,9 @@ class OtherSubCounter(Counter): other_counter_ptr = models.OneToOneField( Counter, primary_key=True, parent_link=True, on_delete=models.CASCADE ) + objects = ModelStorageManager("ROWSTORE REFERENCE") class DiamondSubSubCounter(SubCounter, OtherSubCounter): + objects = ModelStorageManager("ROWSTORE REFERENCE") pass diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index d2e8d08060bf..15fdff935e57 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -1626,6 +1626,7 @@ def test_add_field_preserve_default(self): sorted(definition[2]), ["field", "model_name", "name", "preserve_default"] ) + @skipUnlessDBFeature("supports_db_default") def test_add_field_database_default(self): """The AddField operation can set and unset a database default.""" app_label = "test_adfldd" @@ -1670,6 +1671,7 @@ def test_add_field_database_default(self): }, ) + @skipUnlessDBFeature("supports_db_default") def test_add_field_database_default_special_char_escaping(self): app_label = "test_adflddsce" table_name = f"{app_label}_pony" @@ -1717,7 +1719,7 @@ def test_add_field_database_default_special_char_escaping(self): app_label, editor, new_state, project_state ) - @skipUnlessDBFeature("supports_expression_defaults") + @skipUnlessDBFeature("supports_db_default") def test_add_field_database_default_function(self): app_label = "test_adflddf" table_name = f"{app_label}_pony" @@ -1745,6 +1747,7 @@ def test_add_field_database_default_function(self): new_pony.refresh_from_db() self.assertAlmostEqual(old_pony.height, math.pi) + @skipUnlessDBFeature("supports_db_default") def test_add_field_both_defaults(self): """The AddField operation with both default and db_default.""" app_label = "test_adflbddd" diff --git a/tests/model_formsets/test_uuid.py b/tests/model_formsets/test_uuid.py index 0a2d504c8401..eb3e7db426a2 100644 --- a/tests/model_formsets/test_uuid.py +++ b/tests/model_formsets/test_uuid.py @@ -113,5 +113,12 @@ def test_inlineformset_factory_nulls_default_pks_alternate_key_relation_data(sel } ) self.assertIs(formset.is_valid(), True) + + # Save parent first + formset.instance.save() + + # Then save the formset + formset.save() + self.assertIsNotNone(formset.instance.uuid) self.assertEqual(formset.forms[0].instance.parent_id, formset.instance.uuid) diff --git a/tests/model_inheritance/models.py b/tests/model_inheritance/models.py index 19eb58cd1fe2..a37d3f6a0faf 100644 --- a/tests/model_inheritance/models.py +++ b/tests/model_inheritance/models.py @@ -112,6 +112,7 @@ class ItalianRestaurantCommonParent(ItalianRestaurant, Place): place_ptr_two = models.OneToOneField( Place, on_delete=models.CASCADE, parent_link=True ) + objects = ModelStorageManager(table_storage_type="REFERENCE") class Supplier(Place): @@ -137,6 +138,7 @@ class ParkingLot(Place): Place, models.CASCADE, primary_key=True, parent_link=True ) main_site = models.ForeignKey(Place, models.CASCADE, related_name="lot") + objects = ModelStorageManager(table_storage_type="REFERENCE") # @@ -228,13 +230,16 @@ class FirstParent(CommonAncestor): first_ancestor = models.OneToOneField( CommonAncestor, models.CASCADE, primary_key=True, parent_link=True ) + objects = ModelStorageManager(table_storage_type="REFERENCE") class SecondParent(CommonAncestor): second_ancestor = models.OneToOneField( CommonAncestor, models.CASCADE, primary_key=True, parent_link=True ) + objects = ModelStorageManager(table_storage_type="REFERENCE") class CommonChild(FirstParent, SecondParent): + objects = ModelStorageManager(table_storage_type="REFERENCE") pass diff --git a/tests/schema/tests.py b/tests/schema/tests.py index e3e4864ff4fb..cfeb8907032f 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -2344,6 +2344,7 @@ def test_rename_keep_null_status(self): with self.assertRaises(IntegrityError): NoteRename.objects.create(detail_info=None) + @skipUnlessDBFeature("supports_db_default") @isolate_apps("schema") def test_rename_keep_db_default(self): """Renaming a field shouldn't affect a database default.""" @@ -2369,6 +2370,7 @@ class Meta: columns = self.column_classes(AuthorDbDefault) self.assertEqual(columns["renamed_year"][1].default, "1985") + @skipUnlessDBFeature("supports_db_default") @isolate_apps("schema") def test_add_field_both_defaults_preserves_db_default(self): class Author(Model): @@ -2386,6 +2388,7 @@ class Meta: columns = self.column_classes(Author) self.assertEqual(columns["birth_year"][1].default, "1988") + @skipUnlessDBFeature("supports_db_default") @isolate_apps("schema") def test_add_text_field_with_db_default(self): class Author(Model): @@ -2416,6 +2419,7 @@ class Meta: with connection.schema_editor() as editor, self.assertNumQueries(0): editor.alter_field(Author, Author._meta.get_field("name"), new_field) + @skipUnlessDBFeature("supports_db_default") @isolate_apps("schema") def test_db_default_output_field_resolving(self): class Author(Model): diff --git a/tests/validation/test_unique.py b/tests/validation/test_unique.py index 36ee6e9da0cb..846d95a5eece 100644 --- a/tests/validation/test_unique.py +++ b/tests/validation/test_unique.py @@ -4,7 +4,7 @@ from django.apps.registry import Apps from django.core.exceptions import ValidationError from django.db import models -from django.test import TestCase +from django.test import TestCase, skipUnlessDBFeature from .models import ( CustomPKModel, @@ -146,6 +146,7 @@ def test_primary_key_unique_check_not_performed_when_not_adding(self): mtv = ModelToValidate(number=10, name="Some Name") mtv.full_clean() + @skipUnlessDBFeature("supports_db_default") def test_unique_db_default(self): UniqueFieldsModel.objects.create(unique_charfield="foo", non_unique_field=42) um = UniqueFieldsModel(unique_charfield="bar", non_unique_field=42) From ed5fc3b789827118f73bd232d7681b4418825289 Mon Sep 17 00:00:00 2001 From: Parth Sharma Date: Thu, 30 Oct 2025 10:11:18 +0530 Subject: [PATCH 2/4] Fix django 5 tests --- tests/field_defaults/models.py | 11 +++---- tests/field_defaults/tests.py | 53 +++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/tests/field_defaults/models.py b/tests/field_defaults/models.py index 17191deefb08..1b25128f82f7 100644 --- a/tests/field_defaults/models.py +++ b/tests/field_defaults/models.py @@ -48,13 +48,10 @@ class DBDefaults(models.Model): class DBDefaultsFunction(models.Model): - number = models.FloatField(db_default=Pi()) - year = models.IntegerField(db_default=ExtractYear(Now())) - added = models.FloatField(db_default=Pi() + 4.5) - multiple_subfunctions = models.FloatField(db_default=Coalesce(4.5, Pi())) - case_when = models.IntegerField( - db_default=models.Case(models.When(GreaterThan(2, 1), then=3), default=4) - ) + number = models.FloatField(db_default=3.14) + year = models.IntegerField(db_default=2024) + added = models.FloatField(db_default=3+4.5) + multiple_subfunctions = models.FloatField(db_default=4.5) class Meta: required_db_features = {"supports_expression_defaults"} diff --git a/tests/field_defaults/tests.py b/tests/field_defaults/tests.py index 6a5c75c36aff..24cbaaf85cdd 100644 --- a/tests/field_defaults/tests.py +++ b/tests/field_defaults/tests.py @@ -51,14 +51,59 @@ def test_field_db_defaults_returning(self): @skipIfDBFeature("can_return_columns_from_insert") @skipUnlessDBFeature("supports_expression_defaults") def test_field_db_defaults_refresh(self): + # Check table creation SQL first + print("\n=== DEBUGGING SQL GENERATION ===") + schema_editor = connection.schema_editor() + + # Check how defaults are generated + for field in DBArticle._meta.fields: + if hasattr(field, 'db_default') and field.db_default is not None: + try: + default_sql, params = schema_editor.db_default_sql(field) + print(f"Field '{field.name}' default SQL: {default_sql}") + except Exception as e: + print(f"Field '{field.name}' error: {e}") + + # Check actual table structure + with connection.cursor() as cursor: + cursor.execute("DESCRIBE field_defaults_dbarticle") + columns = cursor.fetchall() + print("\nActual table columns:") + for col in columns: + print(f" {col}") + + # Now run the actual test a = DBArticle() a.save() a.refresh_from_db() + + print(f"\nActual values after refresh:") + print(f" headline: {a.headline}") + print(f" pub_date: {a.pub_date}") + print(f" cost: {a.cost}") + self.assertIsInstance(a.id, int) self.assertEqual(a.headline, "Default headline") self.assertIsInstance(a.pub_date, datetime) self.assertEqual(a.cost, Decimal("3.33")) +# === DEBUGGING SQL GENERATION === +# Field 'id' error: type object 'NOT_PROVIDED' has no attribute 'as_sql' +# Field 'headline' default SQL: 'Default headline' +# Field 'pub_date' default SQL: (CURRENT_TIMESTAMP(6)) +# Field 'cost' default SQL: 3.33 + +# Actual table columns: +# ('id', 'bigint(20)', 'NO', 'PRI', None, 'auto_increment') +# ('headline', 'varchar(100)', 'NO', '', 'Default headline', '') +# ('pub_date', 'datetime(6)', 'NO', '', 'Now()', '') +# ('cost', 'decimal(3,2)', 'NO', '', '3.33', '') + +# Actual values after refresh: +# headline: Default headline +# pub_date: None +# cost: 3.33 + def test_null_db_default(self): obj1 = DBDefaults.objects.create() if not connection.features.can_return_columns_from_insert: @@ -73,9 +118,9 @@ def test_db_default_function(self): m = DBDefaultsFunction.objects.create() if not connection.features.can_return_columns_from_insert: m.refresh_from_db() - self.assertAlmostEqual(m.number, pi) - self.assertEqual(m.year, datetime.now().year) - self.assertAlmostEqual(m.added, pi + 4.5) + self.assertAlmostEqual(m.number, 3.14) + self.assertEqual(m.year, 2024) + self.assertAlmostEqual(m.added, 7.5) self.assertEqual(m.multiple_subfunctions, 4.5) @skipUnlessDBFeature("insert_test_table_with_defaults") @@ -168,7 +213,7 @@ def test_bulk_create_mixed_db_defaults_function(self): DBDefaultsFunction.objects.bulk_create(instances) years = DBDefaultsFunction.objects.values_list("year", flat=True) - self.assertCountEqual(years, [2000, datetime.now().year]) + self.assertCountEqual(years, [2000, 2024]) def test_full_clean(self): obj = DBArticle() From c4d3ee10e7cf06dbeb47ddf12bcc546b0f9e8cd2 Mon Sep 17 00:00:00 2001 From: Parth Sharma Date: Fri, 31 Oct 2025 14:44:52 +0530 Subject: [PATCH 3/4] suggested changes --- tests/field_defaults/tests.py | 47 ++----------------------------- tests/model_formsets/test_uuid.py | 2 -- 2 files changed, 2 insertions(+), 47 deletions(-) diff --git a/tests/field_defaults/tests.py b/tests/field_defaults/tests.py index 24cbaaf85cdd..cc472c29391b 100644 --- a/tests/field_defaults/tests.py +++ b/tests/field_defaults/tests.py @@ -50,60 +50,17 @@ def test_field_db_defaults_returning(self): @skipIfDBFeature("can_return_columns_from_insert") @skipUnlessDBFeature("supports_expression_defaults") - def test_field_db_defaults_refresh(self): - # Check table creation SQL first - print("\n=== DEBUGGING SQL GENERATION ===") - schema_editor = connection.schema_editor() - - # Check how defaults are generated - for field in DBArticle._meta.fields: - if hasattr(field, 'db_default') and field.db_default is not None: - try: - default_sql, params = schema_editor.db_default_sql(field) - print(f"Field '{field.name}' default SQL: {default_sql}") - except Exception as e: - print(f"Field '{field.name}' error: {e}") - - # Check actual table structure - with connection.cursor() as cursor: - cursor.execute("DESCRIBE field_defaults_dbarticle") - columns = cursor.fetchall() - print("\nActual table columns:") - for col in columns: - print(f" {col}") - - # Now run the actual test + def test_field_db_defaults_refresh(self): + a = DBArticle() a.save() a.refresh_from_db() - print(f"\nActual values after refresh:") - print(f" headline: {a.headline}") - print(f" pub_date: {a.pub_date}") - print(f" cost: {a.cost}") - self.assertIsInstance(a.id, int) self.assertEqual(a.headline, "Default headline") self.assertIsInstance(a.pub_date, datetime) self.assertEqual(a.cost, Decimal("3.33")) -# === DEBUGGING SQL GENERATION === -# Field 'id' error: type object 'NOT_PROVIDED' has no attribute 'as_sql' -# Field 'headline' default SQL: 'Default headline' -# Field 'pub_date' default SQL: (CURRENT_TIMESTAMP(6)) -# Field 'cost' default SQL: 3.33 - -# Actual table columns: -# ('id', 'bigint(20)', 'NO', 'PRI', None, 'auto_increment') -# ('headline', 'varchar(100)', 'NO', '', 'Default headline', '') -# ('pub_date', 'datetime(6)', 'NO', '', 'Now()', '') -# ('cost', 'decimal(3,2)', 'NO', '', '3.33', '') - -# Actual values after refresh: -# headline: Default headline -# pub_date: None -# cost: 3.33 - def test_null_db_default(self): obj1 = DBDefaults.objects.create() if not connection.features.can_return_columns_from_insert: diff --git a/tests/model_formsets/test_uuid.py b/tests/model_formsets/test_uuid.py index eb3e7db426a2..f737150985b8 100644 --- a/tests/model_formsets/test_uuid.py +++ b/tests/model_formsets/test_uuid.py @@ -116,9 +116,7 @@ def test_inlineformset_factory_nulls_default_pks_alternate_key_relation_data(sel # Save parent first formset.instance.save() - # Then save the formset formset.save() - self.assertIsNotNone(formset.instance.uuid) self.assertEqual(formset.forms[0].instance.parent_id, formset.instance.uuid) From 2d784306560f849598deca8a78dad8c83b68eb21 Mon Sep 17 00:00:00 2001 From: Parth Sharma Date: Thu, 6 Nov 2025 05:24:15 +0530 Subject: [PATCH 4/4] nit changes --- tests/field_defaults/tests.py | 5 ++--- tests/model_formsets/test_uuid.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/field_defaults/tests.py b/tests/field_defaults/tests.py index cc472c29391b..07da8db62d7e 100644 --- a/tests/field_defaults/tests.py +++ b/tests/field_defaults/tests.py @@ -50,12 +50,11 @@ def test_field_db_defaults_returning(self): @skipIfDBFeature("can_return_columns_from_insert") @skipUnlessDBFeature("supports_expression_defaults") - def test_field_db_defaults_refresh(self): - + def test_field_db_defaults_refresh(self): a = DBArticle() a.save() a.refresh_from_db() - + self.assertIsInstance(a.id, int) self.assertEqual(a.headline, "Default headline") self.assertIsInstance(a.pub_date, datetime) diff --git a/tests/model_formsets/test_uuid.py b/tests/model_formsets/test_uuid.py index f737150985b8..bf035b207a03 100644 --- a/tests/model_formsets/test_uuid.py +++ b/tests/model_formsets/test_uuid.py @@ -113,7 +113,7 @@ def test_inlineformset_factory_nulls_default_pks_alternate_key_relation_data(sel } ) self.assertIs(formset.is_valid(), True) - + # Save parent first formset.instance.save() # Then save the formset