Esempio n. 1
0
 def test_strip_custom(self):
     field = StringField(transform_strip='/')
     assert field.validate(self.cfg, '// hello  ///') == ' hello  '
Esempio n. 2
0
 def test_choice_valid(self):
     field = StringField(choices=['a', 'b', 'c'])
     assert field.validate(self.cfg, 'a') == 'a'
Esempio n. 3
0
 def test_strip_preserve(self):
     field = StringField()
     assert field.validate(self.cfg, ' hello ') == ' hello '
Esempio n. 4
0
 def test_strip_whitespace(self):
     field = StringField(transform_strip=True)
     assert field.validate(self.cfg, '  hello  ') == 'hello'
Esempio n. 5
0
 def test_regex_match(self):
     field = StringField(regex='^h.*$')
     assert field.validate(self.cfg, 'hello') == 'hello'
Esempio n. 6
0
 def test_regex_no_match(self):
     field = StringField(regex='^H.*$')
     with pytest.raises(ValueError):
         field.validate(self.cfg, 'hello')
Esempio n. 7
0
 def test_max_length_valid(self):
     field = StringField(max_len=5)
     assert field.validate(self.cfg, 'hello') == 'hello'
Esempio n. 8
0
    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')
Esempio n. 9
0
 def test_case_upper(self):
     field = StringField(transform_case='upper')
     assert field.validate(self.cfg, 'hello') == 'HELLO'
Esempio n. 10
0
 def test_case_preserve(self):
     field = StringField()
     assert field.validate(self.cfg, 'HellO') == 'HellO'
Esempio n. 11
0
 def test_empty_string_not_required(self):
     field = StringField(required=False)
     assert field.validate(self.cfg, '') == ''
Esempio n. 12
0
 def test_empty_string_requied(self):
     field = StringField(required=True)
     with pytest.raises(ValueError):
         field.validate(self.cfg, '')
Esempio n. 13
0
 def test_non_string(self):
     field = StringField()
     with pytest.raises(ValueError):
         field.validate(self.cfg, 100)
Esempio n. 14
0
 def test_choice_invalid(self):
     field = StringField(choices=['a', 'b', 'c'])
     with pytest.raises(ValueError):
         field.validate(self.cfg, 'z')
Esempio n. 15
0
 def test_max_length_invalid(self):
     field = StringField(max_len=4)
     with pytest.raises(ValueError):
         field.validate(self.cfg, 'hello')
Esempio n. 16
0
 def test_choice_lower_valid(self):
     field = StringField(choices=['a', 'b', 'c'], transform_case='lower')
     assert field.validate(self.cfg, 'A') == 'a'
Esempio n. 17
0
    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')