stages/prompt: fix non-required fields not allowing blank values, add more tests

closes #2544

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-03-22 20:34:00 +01:00
parent 5a07d4ec66
commit 4154b62565
2 changed files with 10 additions and 3 deletions

View File

@ -119,10 +119,12 @@ class Prompt(SerializerModel):
} }
if self.type == FieldTypes.TEXT: if self.type == FieldTypes.TEXT:
kwargs["trim_whitespace"] = False kwargs["trim_whitespace"] = False
kwargs["allow_blank"] = not self.required
if self.type == FieldTypes.TEXT_READ_ONLY: if self.type == FieldTypes.TEXT_READ_ONLY:
field_class = ReadOnlyField field_class = ReadOnlyField
if self.type == FieldTypes.EMAIL: if self.type == FieldTypes.EMAIL:
field_class = EmailField field_class = EmailField
kwargs["allow_blank"] = not self.required
if self.type == FieldTypes.NUMBER: if self.type == FieldTypes.NUMBER:
field_class = IntegerField field_class = IntegerField
if self.type == FieldTypes.CHECKBOX: if self.type == FieldTypes.CHECKBOX:

View File

@ -292,7 +292,7 @@ class TestPromptStage(FlowTestCase):
prompt.save() prompt.save()
def field_type_tester_factory(field_type: FieldTypes): def field_type_tester_factory(field_type: FieldTypes, required: bool):
"""Test field for field_type""" """Test field for field_type"""
def tester(self: TestPromptStage): def tester(self: TestPromptStage):
@ -304,11 +304,16 @@ def field_type_tester_factory(field_type: FieldTypes):
placeholder_expression=False, placeholder_expression=False,
sub_text="test", sub_text="test",
order=123, order=123,
required=required,
) )
self.assertIsNotNone(prompt.field("foo")) self.assertIsNotNone(prompt.field("foo"))
return tester return tester
for _type in FieldTypes: for _required in (True, False):
setattr(TestPromptStage, f"test_field_type_{_type}", field_type_tester_factory(_type)) for _type in FieldTypes:
test_name = f"test_field_type_{_type}"
if _required:
test_name += "_required"
setattr(TestPromptStage, test_name, field_type_tester_factory(_type, _required))