|
25 | 25 | from django.apps import apps |
26 | 26 | from django.contrib.auth.models import User |
27 | 27 | from django.conf import settings |
| 28 | +from django.forms import Form |
28 | 29 | from django.template import Context |
29 | 30 | from django.template import Template # pyflakes:ignore |
30 | 31 | from django.template.defaulttags import URLNode |
|
37 | 38 | from ietf.person.name import name_parts, unidecode_name |
38 | 39 | from ietf.submit.tests import submission_file |
39 | 40 | from ietf.utils.draft import PlaintextDraft, getmeta |
| 41 | +from ietf.utils.fields import SearchableField |
40 | 42 | from ietf.utils.log import unreachable, assertion |
41 | 43 | from ietf.utils.mail import send_mail_preformatted, send_mail_text, send_mail_mime, outbox, get_payload_text |
42 | 44 | from ietf.utils.test_runner import get_template_paths, set_coverage_checking |
@@ -517,3 +519,25 @@ def test_timezone_not_near_midnight(self, mock): |
517 | 519 | ): |
518 | 520 | with self.assertRaises(RuntimeError): |
519 | 521 | timezone_not_near_midnight() |
| 522 | + |
| 523 | + |
| 524 | +class SearchableFieldTests(TestCase): |
| 525 | + def test_has_changed_single_value(self): |
| 526 | + """Should work with initial as a single value or list when max_entries == 1""" |
| 527 | + class TestSearchableField(SearchableField): |
| 528 | + model = "fake model" # needs to be not-None to allow field init |
| 529 | + |
| 530 | + class TestForm(Form): |
| 531 | + test_field = TestSearchableField(max_entries=1) |
| 532 | + |
| 533 | + # single value in initial (e.g., when used as a single-valued field in a formset) |
| 534 | + changed_form = TestForm(initial={'test_field': 1}, data={'test_field': [2]}) |
| 535 | + self.assertTrue(changed_form.has_changed()) |
| 536 | + unchanged_form = TestForm(initial={'test_field': 1}, data={'test_field': [1]}) |
| 537 | + self.assertFalse(unchanged_form.has_changed()) |
| 538 | + |
| 539 | + # list value in initial (usual situation for a MultipleChoiceField subclass like SearchableField) |
| 540 | + changed_form = TestForm(initial={'test_field': [1]}, data={'test_field': [2]}) |
| 541 | + self.assertTrue(changed_form.has_changed()) |
| 542 | + unchanged_form = TestForm(initial={'test_field': [1]}, data={'test_field': [1]}) |
| 543 | + self.assertFalse(unchanged_form.has_changed()) |
0 commit comments