diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-11-22 13:41:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-22 13:41:32 +0100 |
| commit | 101a85a5a06585ba16ecb25860146d034a8a55ec (patch) | |
| tree | d26504b13f703450b2d750de519c9ff189d240d9 /tests/model_fields/test_generatedfield.py | |
| parent | 828082dad954e87d09a99b53424e6faa1860ccc7 (diff) | |
Fixed #34985 -- Fixed GeneratedFields.contribute_to_class() crash when apps are not populated.
Thanks Paolo Melchiorre for the report.
Regression in f333e3513e8bdf5ffeb6eeb63021c230082e6f95.
Diffstat (limited to 'tests/model_fields/test_generatedfield.py')
| -rw-r--r-- | tests/model_fields/test_generatedfield.py | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/tests/model_fields/test_generatedfield.py b/tests/model_fields/test_generatedfield.py index 04d52f6799..9e5d9d87c3 100644 --- a/tests/model_fields/test_generatedfield.py +++ b/tests/model_fields/test_generatedfield.py @@ -1,3 +1,4 @@ +from django.apps import apps from django.db import IntegrityError, connection from django.db.models import ( CharField, @@ -33,6 +34,25 @@ class BaseGeneratedFieldTests(SimpleTestCase): db_persist=False, ) + @isolate_apps("model_fields") + def test_contribute_to_class(self): + class BareModel(Model): + pass + + new_field = GeneratedField( + expression=Lower("nonexistent"), + output_field=IntegerField(), + db_persist=True, + ) + apps.models_ready = False + try: + # GeneratedField can be added to the model even when apps are not + # fully loaded. + new_field.contribute_to_class(BareModel, "name") + self.assertEqual(BareModel._meta.get_field("name"), new_field) + finally: + apps.models_ready = True + def test_blank_unsupported(self): with self.assertRaisesMessage(ValueError, "GeneratedField must be blank."): GeneratedField( @@ -217,10 +237,6 @@ class GeneratedFieldTestMixin: db_parameters = field.db_parameters(connection) self.assertEqual(db_parameters["collation"], collation) self.assertEqual(db_parameters["type"], field.output_field.db_type(connection)) - self.assertNotEqual( - db_parameters["type"], - field._resolved_expression.output_field.db_type(connection), - ) def test_db_type_parameters(self): db_type_parameters = self.output_field_db_collation_model._meta.get_field( |
