def test_strip_custom(self): field = StringField(transform_strip='/') assert field.validate(self.cfg, '// hello ///') == ' hello '
def test_choice_valid(self): field = StringField(choices=['a', 'b', 'c']) assert field.validate(self.cfg, 'a') == 'a'
def test_strip_preserve(self): field = StringField() assert field.validate(self.cfg, ' hello ') == ' hello '
def test_strip_whitespace(self): field = StringField(transform_strip=True) assert field.validate(self.cfg, ' hello ') == 'hello'
def test_regex_match(self): field = StringField(regex='^h.*$') assert field.validate(self.cfg, 'hello') == 'hello'
def test_regex_no_match(self): field = StringField(regex='^H.*$') with pytest.raises(ValueError): field.validate(self.cfg, 'hello')
def test_max_length_valid(self): field = StringField(max_len=5) assert field.validate(self.cfg, 'hello') == 'hello'
def test_choice_error_message_too_many(self): field = StringField(choices=['a', 'b', 'c', 'd', 'e', 'f', 'g']) with pytest.raises(ValueError) as excinfo: field.validate(self.cfg, 'qwer') assert not str(excinfo.value).endswith(' a, b, c, d, e, f, g')
def test_case_upper(self): field = StringField(transform_case='upper') assert field.validate(self.cfg, 'hello') == 'HELLO'
def test_case_preserve(self): field = StringField() assert field.validate(self.cfg, 'HellO') == 'HellO'
def test_empty_string_not_required(self): field = StringField(required=False) assert field.validate(self.cfg, '') == ''
def test_empty_string_requied(self): field = StringField(required=True) with pytest.raises(ValueError): field.validate(self.cfg, '')
def test_non_string(self): field = StringField() with pytest.raises(ValueError): field.validate(self.cfg, 100)
def test_choice_invalid(self): field = StringField(choices=['a', 'b', 'c']) with pytest.raises(ValueError): field.validate(self.cfg, 'z')
def test_max_length_invalid(self): field = StringField(max_len=4) with pytest.raises(ValueError): field.validate(self.cfg, 'hello')
def test_choice_lower_valid(self): field = StringField(choices=['a', 'b', 'c'], transform_case='lower') assert field.validate(self.cfg, 'A') == 'a'
def test_choice_error_message_list(self): field = StringField(choices=['a', 'b', 'c']) with pytest.raises(ValueError) as excinfo: field.validate(self.cfg, 'qwer') assert str(excinfo.value).endswith(' a, b, c')