Skip to content

Commit 914b828

Browse files
fix linting
1 parent 32dc9a1 commit 914b828

1 file changed

Lines changed: 67 additions & 47 deletions

File tree

tests/test_django5_overrides.py

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,161 @@
1-
21
import pytest
32
from django.db import models
43
from django_enum import EnumField
54
from django_enum.choices import IntegerChoices
65
from django import VERSION as django_version
76

7+
88
class MyEnum(IntegerChoices):
9-
VAL1 = 1, 'Value 1'
10-
VAL2 = 2, 'Value 2'
9+
VAL1 = 1, "Value 1"
10+
VAL2 = 2, "Value 2"
11+
1112

1213
class GroupedEnum(IntegerChoices):
13-
V1 = 1, 'One'
14-
V2 = 2, 'Two'
15-
V3 = 3, 'Three'
14+
V1 = 1, "One"
15+
V2 = 2, "Two"
16+
V3 = 3, "Three"
17+
1618

1719
def get_choices_callable():
18-
return [(1, 'Callable 1'), (2, 'Callable 2')]
20+
return [(1, "Callable 1"), (2, "Callable 2")]
21+
1922

2023
class EnumOverrideModel(models.Model):
2124
# Dict choices (Django 5.0+)
22-
dict_field = EnumField(MyEnum, choices={1: 'Dict 1', 2: 'Dict 2'})
25+
dict_field = EnumField(MyEnum, choices={1: "Dict 1", 2: "Dict 2"})
2326

2427
# Callable choices (Django 5.0+)
2528
callable_field = EnumField(MyEnum, choices=get_choices_callable)
2629

2730
# Grouped choices
28-
grouped_field = EnumField(GroupedEnum, choices=[
29-
('Group A', [(1, 'One'), (2, 'Two')]),
30-
('Group B', [(3, 'Three')]),
31-
])
31+
grouped_field = EnumField(
32+
GroupedEnum,
33+
choices=[
34+
("Group A", [(1, "One"), (2, "Two")]),
35+
("Group B", [(3, "Three")]),
36+
],
37+
)
3238

3339
# Nested dict choices (Django 5.0+)
34-
nested_dict_field = EnumField(MyEnum, choices={
35-
"Audio": {
36-
1: "Vinyl",
37-
2: "CD",
40+
nested_dict_field = EnumField(
41+
MyEnum,
42+
choices={
43+
"Audio": {
44+
1: "Vinyl",
45+
2: "CD",
46+
},
47+
"unknown": "Unknown",
3848
},
39-
"unknown": "Unknown",
40-
})
49+
)
4150

4251
class Meta:
4352
abstract = True
44-
app_label = 'tests'
53+
app_label = "tests"
54+
4555

4656
def test_deconstruct_preserves_overrides():
4757
"""Verify that deconstruct() preserves dictionary and callable overrides."""
48-
field = EnumOverrideModel._meta.get_field('dict_field')
58+
field = EnumOverrideModel._meta.get_field("dict_field")
4959
name, path, args, kwargs = field.deconstruct()
50-
assert kwargs['choices'] == {1: 'Dict 1', 2: 'Dict 2'}
60+
assert kwargs["choices"] == {1: "Dict 1", 2: "Dict 2"}
5161

52-
field = EnumOverrideModel._meta.get_field('callable_field')
62+
field = EnumOverrideModel._meta.get_field("callable_field")
5363
name, path, args, kwargs = field.deconstruct()
54-
assert kwargs['choices'] == get_choices_callable
64+
assert kwargs["choices"] == get_choices_callable
5565

56-
field = EnumOverrideModel._meta.get_field('nested_dict_field')
66+
field = EnumOverrideModel._meta.get_field("nested_dict_field")
5767
name, path, args, kwargs = field.deconstruct()
58-
assert kwargs['choices'] == {
68+
assert kwargs["choices"] == {
5969
"Audio": {1: "Vinyl", 2: "CD"},
6070
"unknown": "Unknown",
6171
}
6272

73+
6374
def test_get_choices_handles_dict():
6475
"""Verify that get_choices() handles dictionary choices correctly."""
65-
field = EnumOverrideModel._meta.get_field('dict_field')
76+
field = EnumOverrideModel._meta.get_field("dict_field")
6677
# Django converts dict to list of tuples internally in super().get_choices()
6778
choices = field.get_choices(include_blank=False)
68-
assert choices == [(1, 'Dict 1'), (2, 'Dict 2')]
79+
assert choices == [(1, "Dict 1"), (2, "Dict 2")]
80+
6981

7082
def test_get_choices_handles_nested_dict():
7183
"""Verify that get_choices() handles nested dictionary choices correctly."""
72-
field = EnumOverrideModel._meta.get_field('nested_dict_field')
84+
field = EnumOverrideModel._meta.get_field("nested_dict_field")
7385
choices = field.get_choices(include_blank=False)
7486
# Normalized by Django + Coerced by EnumField
7587
assert choices == [
7688
("Audio", [(1, "Vinyl"), (2, "CD")]),
7789
("unknown", "Unknown"),
7890
]
7991

92+
8093
def test_get_choices_handles_recursion():
8194
"""Verify that get_choices() handles grouped choices correctly."""
82-
field = EnumOverrideModel._meta.get_field('grouped_field')
95+
field = EnumOverrideModel._meta.get_field("grouped_field")
8396
choices = field.get_choices(include_blank=False)
8497
assert choices == [
85-
('Group A', [(1, 'One'), (2, 'Two')]),
86-
('Group B', [(3, 'Three')]),
98+
("Group A", [(1, "One"), (2, "Two")]),
99+
("Group B", [(3, "Three")]),
87100
]
88101

102+
89103
def test_validation_with_overridden_choices():
90104
"""Verify that validation uses the overridden labels or values."""
91-
field = EnumOverrideModel._meta.get_field('dict_field')
105+
field = EnumOverrideModel._meta.get_field("dict_field")
92106
# Validation in Django usually checks if value is in choices
93107
# EnumField also tries to coerce.
94108
# 1 is a valid value for MyEnum and also in the dict
95109
field.validate(1, None)
96-
110+
97111
# 3 is NOT in MyEnum and NOT in the dict
98-
with pytest.raises(Exception): # Django raises ValidationError
112+
with pytest.raises(Exception): # Django raises ValidationError
99113
field.validate(3, None)
100114

115+
101116
from django import forms as django_forms
102117
from django_enum.forms import EnumChoiceField
103118

119+
104120
def test_form_field_with_nested_dict():
105121
"""Verify that EnumChoiceField handles nested dictionary choices (non-strict)."""
122+
106123
class NonStrictForm(django_forms.Form):
107124
field = EnumChoiceField(
108-
MyEnum,
125+
MyEnum,
109126
choices={
110127
"Audio": {1: "Vinyl", 2: "CD"},
111128
"unknown": "Unknown",
112129
},
113-
strict=False
130+
strict=False,
114131
)
115132

116-
form = NonStrictForm(data={'field': 1})
133+
form = NonStrictForm(data={"field": 1})
117134
assert form.is_valid()
118-
135+
119136
# 3 is not in Enum and not in dict
120-
form = NonStrictForm(data={'field': 3})
121-
assert form.is_valid() # Non-strict allows it
122-
137+
form = NonStrictForm(data={"field": 3})
138+
assert form.is_valid() # Non-strict allows it
139+
123140
# Rendering should include the added choice if it's not in choices
124141
# This verifies NonStrictMixin.render()
125-
widget = form.fields['field'].widget
142+
widget = form.fields["field"].widget
126143
# Simulate a value not in choices
127-
rendered = widget.render('field', 3, attrs={'id': 'id_field'})
144+
rendered = widget.render("field", 3, attrs={"id": "id_field"})
128145
assert 'value="3"' in rendered
129-
assert '>3<' in rendered
146+
assert ">3<" in rendered
147+
130148

131149
def test_default_choices_still_work():
132150
"""Verify that if no choices are provided, defaults from enum are used."""
151+
133152
class DefaultModel(models.Model):
134153
field = EnumField(MyEnum)
154+
135155
class Meta:
136156
abstract = True
137-
app_label = 'tests'
157+
app_label = "tests"
138158

139-
field = DefaultModel._meta.get_field('field')
159+
field = DefaultModel._meta.get_field("field")
140160
name, path, args, kwargs = field.deconstruct()
141-
assert kwargs['choices'] == [(1, 'Value 1'), (2, 'Value 2')]
161+
assert kwargs["choices"] == [(1, "Value 1"), (2, "Value 2")]

0 commit comments

Comments
 (0)