|
11 | 11 | NullBlankFormTester,
|
12 | 12 | NullableBlankFormTester,
|
13 | 13 | Bug53Tester,
|
| 14 | + NullableStrFormTester, |
| 15 | +) |
| 16 | +from tests.djenum.enums import ( |
| 17 | + ExternEnum, |
| 18 | + NullableExternEnum, |
| 19 | + StrTestEnum, |
| 20 | + NullableStrEnum, |
14 | 21 | )
|
15 |
| -from tests.djenum.enums import ExternEnum, NullableExternEnum, StrTestEnum |
16 | 22 | from playwright.sync_api import sync_playwright, Page, expect
|
17 | 23 | from django.urls import reverse
|
18 | 24 | from django.contrib.auth import get_user_model
|
@@ -77,6 +83,9 @@ class _GenericAdminFormTest(StaticLiveServerTestCase):
|
77 | 83 |
|
78 | 84 | __test__ = False
|
79 | 85 |
|
| 86 | + def enum(self, field): |
| 87 | + return self.MODEL_CLASS._meta.get_field(field).enum |
| 88 | + |
80 | 89 | @property
|
81 | 90 | def changes(self) -> t.List[t.Dict[str, Enum]]:
|
82 | 91 | # must implement
|
@@ -148,18 +157,23 @@ def setUp(self):
|
148 | 157 | def set_form_value(
|
149 | 158 | self, field_name: str, value: t.Optional[t.Union[Enum, str]], flag=False
|
150 | 159 | ):
|
151 |
| - # should override this if needed |
152 |
| - if value is None and not flag: |
153 |
| - self.page.select_option(f"select[name='{field_name}']", "") |
154 |
| - elif flag: |
155 |
| - self.page.select_option( |
156 |
| - f"select[name='{field_name}']", |
157 |
| - [str(flag.value) for flag in decompose(value)], |
158 |
| - ) |
159 |
| - else: |
160 |
| - self.page.select_option( |
161 |
| - f"select[name='{field_name}']", str(getattr(value, "value", value)) |
162 |
| - ) |
| 160 | + try: |
| 161 | + if value is None and None in self.enum(field_name): |
| 162 | + value = self.enum(field_name)(value) |
| 163 | + # should override this if needed |
| 164 | + if getattr(value, "value", value) is None and not flag: |
| 165 | + self.page.select_option(f"select[name='{field_name}']", "") |
| 166 | + elif flag: |
| 167 | + self.page.select_option( |
| 168 | + f"select[name='{field_name}']", |
| 169 | + [str(flag.value) for flag in decompose(value)], |
| 170 | + ) |
| 171 | + else: |
| 172 | + self.page.select_option( |
| 173 | + f"select[name='{field_name}']", str(getattr(value, "value", value)) |
| 174 | + ) |
| 175 | + except Exception: |
| 176 | + self.page.pause() |
163 | 177 |
|
164 | 178 | def verify_changes(self, obj: Model, expected: t.Dict[str, t.Any]):
|
165 | 179 | count = 0
|
@@ -324,6 +338,7 @@ def changes(self) -> t.Dict[str, Enum]:
|
324 | 338 | class TestNullableBlankAdminBehavior(_GenericAdminFormTest):
|
325 | 339 | MODEL_CLASS = NullableBlankFormTester
|
326 | 340 | __test__ = True
|
| 341 | + HEADLESS = True |
327 | 342 |
|
328 | 343 | @property
|
329 | 344 | def changes(self) -> t.Dict[str, Enum]:
|
@@ -353,6 +368,39 @@ def changes(self) -> t.Dict[str, Enum]:
|
353 | 368 | ]
|
354 | 369 |
|
355 | 370 |
|
| 371 | +class TestNullableStrAdminBehavior(_GenericAdminFormTest): |
| 372 | + MODEL_CLASS = NullableStrFormTester |
| 373 | + __test__ = True |
| 374 | + HEADLESS = True |
| 375 | + |
| 376 | + @property |
| 377 | + def changes(self) -> t.Dict[str, Enum]: |
| 378 | + return [ |
| 379 | + {"required": NullableStrEnum.STR1, "blank": NullableStrEnum.STR2}, |
| 380 | + { |
| 381 | + "required": NullableStrEnum.STR2, |
| 382 | + "required_default": NullableStrEnum.STR1, |
| 383 | + "blank": NullableStrEnum.STR2, |
| 384 | + "blank_nullable": None, |
| 385 | + "blank_nullable_default": None, |
| 386 | + }, |
| 387 | + { |
| 388 | + "required": NullableStrEnum.STR1, |
| 389 | + "required_default": NullableStrEnum.STR2, |
| 390 | + "blank": NullableStrEnum.STR2, |
| 391 | + "blank_nullable": NullableStrEnum.STR1, |
| 392 | + "blank_nullable_default": NullableStrEnum.NONE, |
| 393 | + }, |
| 394 | + { |
| 395 | + "required": NullableStrEnum.STR2, |
| 396 | + "required_default": NullableStrEnum.STR2, |
| 397 | + "blank": None, |
| 398 | + "blank_nullable": "", |
| 399 | + "blank_nullable_default": "", |
| 400 | + }, |
| 401 | + ] |
| 402 | + |
| 403 | + |
356 | 404 | class TestBug53AdminBehavior(_GenericAdminFormTest):
|
357 | 405 | MODEL_CLASS = Bug53Tester
|
358 | 406 | __test__ = True
|
|
0 commit comments