|
| 1 | +import pytest |
| 2 | +from django.forms import modelform_factory |
| 3 | +from django.utils.translation import override |
| 4 | + |
| 5 | +from testapp.field_types_models import CustomFieldModel |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.django_db |
| 9 | +def test_custom_field_choices_actual_behavior(): |
| 10 | + """ |
| 11 | + Test to demonstrate the current behavior with custom field deconstruct. |
| 12 | +
|
| 13 | + This test confirms what actually happens (not what we want to happen). |
| 14 | + """ |
| 15 | + # Get the runtime field instances |
| 16 | + custom_en = CustomFieldModel._meta.get_field("custom_choices_en") |
| 17 | + custom_de = CustomFieldModel._meta.get_field("custom_choices_de") |
| 18 | + |
| 19 | + # The actual current behavior - choices are taken from deconstruct() [("", "")] |
| 20 | + # rather than the original [("a", "Option A"), ("b", "Option B"), ("c", "Option C")] |
| 21 | + actual_choices = [("", "")] |
| 22 | + |
| 23 | + # These assertions show what actually happens (would pass) |
| 24 | + assert custom_en.choices == actual_choices |
| 25 | + assert custom_de.choices == actual_choices |
| 26 | + |
| 27 | + # Get the deconstruct() values to confirm this behavior |
| 28 | + name_en, path_en, args_en, kwargs_en = custom_en.deconstruct() |
| 29 | + name_de, path_de, args_de, kwargs_de = custom_de.deconstruct() |
| 30 | + |
| 31 | + # Confirm that deconstruct() returns the same hardcoded choices |
| 32 | + assert kwargs_en["choices"] == actual_choices |
| 33 | + assert kwargs_de["choices"] == actual_choices |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.xfail( |
| 37 | + reason="TranslatedField doesn't handle custom deconstruct correctly yet" |
| 38 | +) |
| 39 | +@pytest.mark.django_db |
| 40 | +def test_custom_field_choices_preserved(): |
| 41 | + """ |
| 42 | + Test that choices from custom fields with custom deconstruct methods are preserved. |
| 43 | +
|
| 44 | + This is currently an expected failure because TranslatedField doesn't properly |
| 45 | + handle fields with custom deconstruct methods that modify field parameters. |
| 46 | +
|
| 47 | + The issue is in translated_fields/fields.py line 82: |
| 48 | + _n, _p, args, kwargs = self._field.deconstruct() |
| 49 | +
|
| 50 | + When using a field with a custom deconstruct() method that modifies parameters like 'choices', |
| 51 | + those modifications affect the translated fields that are created. |
| 52 | +
|
| 53 | + A possible fix would be to store the original parameters before deconstruct() is called, |
| 54 | + or to copy the field's __dict__ attributes directly instead of using deconstruct(). |
| 55 | + """ |
| 56 | + # Get the runtime field instances |
| 57 | + custom_en = CustomFieldModel._meta.get_field("custom_choices_en") |
| 58 | + custom_de = CustomFieldModel._meta.get_field("custom_choices_de") |
| 59 | + |
| 60 | + # The runtime choices should be the ones we specified at field creation time, |
| 61 | + # not the hardcoded ones from deconstruct() |
| 62 | + expected_choices = [("a", "Option A"), ("b", "Option B"), ("c", "Option C")] |
| 63 | + |
| 64 | + # Check the field choices match what we defined, not what deconstruct() returns |
| 65 | + assert custom_en.choices == expected_choices |
| 66 | + assert custom_de.choices == expected_choices |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.xfail( |
| 70 | + reason="TranslatedField doesn't handle custom deconstruct correctly yet" |
| 71 | +) |
| 72 | +@pytest.mark.django_db |
| 73 | +def test_custom_field_form_generation(): |
| 74 | + """ |
| 75 | + Test that forms generated from custom fields have the correct choices. |
| 76 | +
|
| 77 | + This fails for the same reason as test_custom_field_choices_preserved: |
| 78 | + the choices from the original field definition are not preserved when |
| 79 | + deconstruct() replaces them with hardcoded values. |
| 80 | + """ |
| 81 | + form_class = modelform_factory(CustomFieldModel, fields="__all__") |
| 82 | + form = form_class() |
| 83 | + |
| 84 | + # Form field choices should include the default empty choice plus our defined choices |
| 85 | + expected_form_choices = [ |
| 86 | + ("", "---------"), |
| 87 | + ("a", "Option A"), |
| 88 | + ("b", "Option B"), |
| 89 | + ("c", "Option C"), |
| 90 | + ] |
| 91 | + |
| 92 | + # Check that form fields have the expected choices |
| 93 | + assert form.fields["custom_choices_en"].choices == expected_form_choices |
| 94 | + assert form.fields["custom_choices_de"].choices == expected_form_choices |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.xfail( |
| 98 | + reason="TranslatedField doesn't handle custom deconstruct correctly yet" |
| 99 | +) |
| 100 | +@pytest.mark.django_db |
| 101 | +def test_custom_field_model_usage(): |
| 102 | + """ |
| 103 | + Test using the custom field with a model instance. |
| 104 | +
|
| 105 | + This fails because the get_FOO_display() method relies on the correct choices |
| 106 | + being set on the field. Since the choices are replaced with [("", "")] during |
| 107 | + field creation, the display values don't match what we expect. |
| 108 | + """ |
| 109 | + # Create a model instance with values for the custom field |
| 110 | + model = CustomFieldModel.objects.create( |
| 111 | + custom_choices_en="a", |
| 112 | + custom_choices_de="b", |
| 113 | + ) |
| 114 | + |
| 115 | + # Check that the values are correctly stored and the choices behavior works |
| 116 | + with override("en"): |
| 117 | + assert model.custom_choices == "a" |
| 118 | + assert model.get_custom_choices_display() == "Option A" |
| 119 | + |
| 120 | + with override("de"): |
| 121 | + assert model.custom_choices == "b" |
| 122 | + assert model.get_custom_choices_display() == "Option B" |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.parametrize("option", ["a", "b", "c"]) |
| 126 | +@pytest.mark.django_db |
| 127 | +def test_custom_field_valid_options(option): |
| 128 | + """Test that valid choice options can be saved.""" |
| 129 | + # This test should pass even if the choices aren't preserved correctly |
| 130 | + # as long as the field validation isn't overly strict |
| 131 | + model = CustomFieldModel() |
| 132 | + model.custom_choices_en = option |
| 133 | + model.custom_choices_de = option |
| 134 | + model.save() |
| 135 | + assert model.custom_choices_en == option |
| 136 | + assert model.custom_choices_de == option |
0 commit comments