Skip to content

Commit 2bb3a20

Browse files
committed
test has_changed on model form, test against 5.2rc1
1 parent 5789ad5 commit 2bb3a20

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- '3.2' # LTS April 2024
3737
- '4.2' # LTS April 2026
3838
- '5.1' # December 2025
39-
- '5.2b1'
39+
- '5.2rc1'
4040
exclude:
4141
- django-version: '4.2'
4242
postgres-version: '9.6'
@@ -58,15 +58,15 @@ jobs:
5858
django-version: '5.1'
5959

6060
- postgres-version: '9.6'
61-
django-version: '5.2b1'
61+
django-version: '5.2rc1'
6262

6363
- postgres-version: '12'
64-
django-version: '5.2b1'
64+
django-version: '5.2rc1'
6565

6666
- python-version: '3.9'
6767
django-version: '5.1'
6868
- python-version: '3.9'
69-
django-version: '5.2b1'
69+
django-version: '5.2rc1'
7070
- python-version: '3.13'
7171
django-version: '3.2'
7272
- python-version: '3.13'
@@ -122,7 +122,7 @@ jobs:
122122
- name: Install Release Dependencies
123123
run: |
124124
just setup ${{ steps.sp.outputs.python-path }}
125-
if [ "${{ matrix.django-version }}" = "5.2b1" ]; then
125+
if [ "${{ matrix.django-version }}" = "5.2rc1" ]; then
126126
just test-lock "Django==${{ matrix.django-version }}"
127127
else
128128
just test-lock "Django~=${{ matrix.django-version }}.0"

src/django_enum/forms.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,15 @@ class EnumMultipleChoiceField( # type: ignore
424424
non_strict_widget = NonStrictSelectMultiple
425425

426426
def has_changed(self, initial, data):
427-
# TODO
428-
return super().has_changed(initial, data)
427+
return super().has_changed(
428+
*(
429+
[
430+
(str(en.value) if isinstance(en, Enum) else en)
431+
for en in initial or []
432+
],
433+
[(str(en.value) if isinstance(en, Enum) else en) for en in data or []],
434+
)
435+
)
429436

430437

431438
class EnumFlagField(ChoiceFieldMixin, TypedMultipleChoiceField): # type: ignore

tests/test_forms.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,59 @@ def test_non_strict_field(self):
645645
self.enum_primitive("non_strict_int"),
646646
)
647647
self.assertEqual(form["non_strict_int"].value(), [200, 203])
648+
649+
def test_has_changed(self):
650+
{
651+
"small_pos_int": [0],
652+
"small_int": [self.SmallIntEnum.VAL2, self.SmallIntEnum.VALn1],
653+
"pos_int": [2147483647, self.PosIntEnum.VAL3],
654+
"int": [self.IntEnum.VALn1],
655+
"big_pos_int": [2, self.BigPosIntEnum.VAL3],
656+
"big_int": [self.BigIntEnum.VAL0],
657+
"constant": [2.71828, self.Constants.GOLDEN_RATIO],
658+
"text": [self.TextEnum.VALUE3, self.TextEnum.VALUE2],
659+
"extern": [self.ExternEnum.THREE],
660+
"date_enum": [self.DateEnum.BRIAN, date(1989, 7, 27)],
661+
"datetime_enum": [self.DateTimeEnum.ST_HELENS, self.DateTimeEnum.ST_HELENS],
662+
"duration_enum": [self.DurationEnum.FORTNIGHT],
663+
"time_enum": [self.TimeEnum.COB, self.TimeEnum.LUNCH],
664+
"decimal_enum": [self.DecimalEnum.ONE],
665+
"non_strict_int": [self.SmallPosIntEnum.VAL2],
666+
"non_strict_text": ["arbitrary", "A" * 13],
667+
"no_coerce": [self.SmallPosIntEnum.VAL1],
668+
}
669+
form = self.FORM_CLASS(
670+
data={"small_pos_int": [self.SmallPosIntEnum.VAL1]},
671+
initial={"small_pos_int": [0]},
672+
)
673+
self.assertFalse(form.has_changed())
674+
675+
form = self.FORM_CLASS(
676+
data={"small_pos_int": [self.SmallPosIntEnum.VAL2]},
677+
initial={"small_pos_int": [0]},
678+
)
679+
self.assertTrue(form.has_changed())
680+
681+
form = self.FORM_CLASS(
682+
data={"small_pos_int": [self.SmallPosIntEnum.VAL1.value]},
683+
initial={"small_pos_int": [self.SmallPosIntEnum.VAL1]},
684+
)
685+
self.assertFalse(form.has_changed())
686+
687+
form = self.FORM_CLASS(
688+
data={"small_pos_int": [self.SmallPosIntEnum.VAL2.value]},
689+
initial={"small_pos_int": [self.SmallPosIntEnum.VAL1]},
690+
)
691+
self.assertTrue(form.has_changed())
692+
693+
form = self.FORM_CLASS(
694+
data={"small_pos_int": [str(self.SmallPosIntEnum.VAL1.value)]},
695+
initial={"small_pos_int": [self.SmallPosIntEnum.VAL1]},
696+
)
697+
self.assertFalse(form.has_changed())
698+
699+
form = self.FORM_CLASS(
700+
data={"small_pos_int": [str(self.SmallPosIntEnum.VAL2.value)]},
701+
initial={"small_pos_int": [self.SmallPosIntEnum.VAL1]},
702+
)
703+
self.assertTrue(form.has_changed())

0 commit comments

Comments
 (0)