From 952949156289cb1da8b1a9af59f32687cbdada8e Mon Sep 17 00:00:00 2001 From: Alastair Weakley Date: Thu, 13 Jun 2024 21:12:55 +1000 Subject: [PATCH] Anticipate None for date_display #62 --- edtf/fields.py | 2 +- edtf_django_tests/edtf_integration/tests.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/edtf/fields.py b/edtf/fields.py index 642b6bb..7dba5d4 100644 --- a/edtf/fields.py +++ b/edtf/fields.py @@ -135,7 +135,7 @@ def update_values(self, instance, *args, **kwargs): raise EDTFParseException(direct_input, err) from None # set the natural_text (display) field to the direct_input if it is not provided - if natural_text == "": + if not natural_text: setattr(instance, self.natural_text_field, direct_input) elif natural_text: diff --git a/edtf_django_tests/edtf_integration/tests.py b/edtf_django_tests/edtf_integration/tests.py index 88fdca8..493d0d2 100644 --- a/edtf_django_tests/edtf_integration/tests.py +++ b/edtf_django_tests/edtf_integration/tests.py @@ -74,6 +74,26 @@ def test_date_display(self): self.assertEqual(self.event3.date_display, "2019-11") self.assertEqual(self.event4.date_display, "Approximately August 2018") + def test_date_display_with_none_or_empty_string(self): + """ + Test that the date_display field is correctly populated when the + `natural_date` field is set to empty string (for example, if it + were used with `null=False` in the model definition) or set to + None (if it were used with `null=True`). + """ + event = TestEvent(date_display="") + event.date_edtf_direct = "2020-03-15/2020-04-15" + # Trigger the descriptor to update the date_display field + event.date_edtf = "" + self.assertEqual(event.date_display, "2020-03-15/2020-04-15") + + event = TestEvent(date_display=None) + # Verify date_display is set to None even though the field is `null=False` + self.assertIsNone(event.date_display) + event.date_edtf_direct = "2020-03-15/2020-04-15" + event.date_edtf = "" + self.assertEqual(event.date_display, "2020-03-15/2020-04-15") + def test_comparison(self): # test equality of the same dates self.assertEqual(