|
2 | 2 |
|
3 | 3 | pytest.importorskip("enum_properties")
|
4 | 4 | from tests.test_forms import FormTests, TestFormField
|
5 |
| -from tests.enum_prop.models import EnumTester |
| 5 | +from tests.enum_prop.models import EnumTester, BitFieldModel |
6 | 6 | from tests.enum_prop.forms import EnumTesterForm
|
| 7 | +from tests.examples.models import FlagExample |
| 8 | +from django_enum.forms import EnumFlagField, FlagSelectMultiple |
| 9 | +from django.forms import ModelForm |
7 | 10 |
|
8 | 11 |
|
9 | 12 | class EnumPropertiesFormTests(FormTests):
|
@@ -34,6 +37,99 @@ def model_params(self):
|
34 | 37 | "no_coerce": "Value 1",
|
35 | 38 | }
|
36 | 39 |
|
| 40 | + def test_flag_choices_admin_form(self): |
| 41 | + from django.contrib import admin |
| 42 | + |
| 43 | + admin_class = admin.site._registry.get(BitFieldModel) |
| 44 | + self.assertIsInstance( |
| 45 | + admin_class.get_form(None).base_fields.get("bit_field_small"), EnumFlagField |
| 46 | + ) |
| 47 | + |
| 48 | + def test_flag_choices_model_form(self): |
| 49 | + from tests.examples.models.flag import Permissions |
| 50 | + from tests.enum_prop.enums import GNSSConstellation |
| 51 | + |
| 52 | + class FlagChoicesModelForm(ModelForm): |
| 53 | + class Meta(EnumTesterForm.Meta): |
| 54 | + model = BitFieldModel |
| 55 | + |
| 56 | + form = FlagChoicesModelForm( |
| 57 | + data={"bit_field_small": [GNSSConstellation.GPS, GNSSConstellation.GLONASS]} |
| 58 | + ) |
| 59 | + |
| 60 | + form.full_clean() |
| 61 | + self.assertTrue(form.is_valid()) |
| 62 | + self.assertEqual( |
| 63 | + form.cleaned_data["bit_field_small"], |
| 64 | + GNSSConstellation.GPS | GNSSConstellation.GLONASS, |
| 65 | + ) |
| 66 | + self.assertIsInstance(form.base_fields["bit_field_small"], EnumFlagField) |
| 67 | + |
| 68 | + def test_extern_flag_admin_form(self): |
| 69 | + from django.contrib import admin |
| 70 | + |
| 71 | + admin_class = admin.site._registry.get(FlagExample) |
| 72 | + self.assertIsInstance( |
| 73 | + admin_class.get_form(None).base_fields.get("permissions"), EnumFlagField |
| 74 | + ) |
| 75 | + |
| 76 | + def test_extern_flag_model_form(self): |
| 77 | + from tests.examples.models.flag import Permissions |
| 78 | + |
| 79 | + class FlagModelForm(ModelForm): |
| 80 | + class Meta(EnumTesterForm.Meta): |
| 81 | + model = FlagExample |
| 82 | + |
| 83 | + form = FlagModelForm( |
| 84 | + data={"permissions": [Permissions.READ, Permissions.WRITE]} |
| 85 | + ) |
| 86 | + |
| 87 | + form.full_clean() |
| 88 | + self.assertTrue(form.is_valid()) |
| 89 | + self.assertEqual( |
| 90 | + form.cleaned_data["permissions"], Permissions.READ | Permissions.WRITE |
| 91 | + ) |
| 92 | + self.assertIsInstance(form.base_fields["permissions"], EnumFlagField) |
| 93 | + |
| 94 | + def test_flag_select_multiple_format(self): |
| 95 | + from tests.examples.models.flag import Permissions |
| 96 | + |
| 97 | + widget = FlagSelectMultiple() # no enum |
| 98 | + self.assertEqual( |
| 99 | + widget.format_value(Permissions.READ | Permissions.WRITE), |
| 100 | + [str(Permissions.READ.value), str(Permissions.WRITE.value)], |
| 101 | + ) |
| 102 | + self.assertEqual( |
| 103 | + widget.format_value(Permissions.READ | Permissions.EXECUTE), |
| 104 | + [str(Permissions.READ.value), str(Permissions.EXECUTE.value)], |
| 105 | + ) |
| 106 | + self.assertEqual( |
| 107 | + widget.format_value(Permissions.EXECUTE | Permissions.WRITE), |
| 108 | + [str(Permissions.WRITE.value), str(Permissions.EXECUTE.value)], |
| 109 | + ) |
| 110 | + |
| 111 | + widget = FlagSelectMultiple(enum=Permissions) # no enum |
| 112 | + self.assertEqual( |
| 113 | + widget.format_value(Permissions.READ | Permissions.WRITE), |
| 114 | + [str(Permissions.READ.value), str(Permissions.WRITE.value)], |
| 115 | + ) |
| 116 | + self.assertEqual( |
| 117 | + widget.format_value(Permissions.READ | Permissions.EXECUTE), |
| 118 | + [str(Permissions.READ.value), str(Permissions.EXECUTE.value)], |
| 119 | + ) |
| 120 | + self.assertEqual( |
| 121 | + widget.format_value(Permissions.EXECUTE | Permissions.WRITE), |
| 122 | + [str(Permissions.WRITE.value), str(Permissions.EXECUTE.value)], |
| 123 | + ) |
| 124 | + |
| 125 | + # check pass through |
| 126 | + self.assertEqual( |
| 127 | + widget.format_value( |
| 128 | + [str(Permissions.WRITE.value), str(Permissions.EXECUTE.value)] |
| 129 | + ), |
| 130 | + [str(Permissions.WRITE.value), str(Permissions.EXECUTE.value)], |
| 131 | + ) |
| 132 | + |
37 | 133 |
|
38 | 134 | FormTests = None
|
39 | 135 | TestFormField = None
|
0 commit comments