Ejemplo n.º 1
0
    def test_generate_stub_no_class_name(self):
        schema = Schema()
        schema.x = VirtualField(lambda x: None)
        schema.y = StringField()

        with pytest.raises(TypeError):
            generate_stub(schema)
Ejemplo n.º 2
0
    def test_generate_argparse_parser(self, mock_argparse):
        parser = MagicMock()
        mock_argparse.return_value = parser
        schema = Schema()
        schema.long_name = StringField(help='asdf')
        schema.sub.short_name = IntField()
        schema.enable = BoolField()
        schema.runtime = FloatField()
        schema.ignore_me2 = Field()  # no storage_type

        retval = generate_argparse_parser(schema, x=1, y=2)
        assert retval is parser
        mock_argparse.assert_called_once_with(x=1, y=2)
        parser.add_argument.call_args_list == [
            call('--long-name',
                 action='store',
                 dest='long_name',
                 help='asdf',
                 metavar='LONG_NAME'),
            call('--sub-short-name',
                 action='store',
                 dest='sub.short_name',
                 help=None,
                 metavar='SUB_SHORT_NAME'),
            call('--enable', action='store_true', help=None, dest='enable'),
            call('--no-enable', action='store_false', help=None,
                 dest='enable'),
            call('--runtime', action='store', dest='runtime', help=None)
        ]
Ejemplo n.º 3
0
    def test_generate_stub_schema(self):
        schema = Schema()
        schema.x = VirtualField(lambda x: None)
        schema.y = StringField()

        stub = generate_stub(schema, 'Thing').split('\n')
        assert 'class Thing(cincoconfig.config.ConfigType):' in stub
        assert '    x: typing.Any' in stub
        assert '    y: str' in stub
        assert '    def __init__(self, y: str): ...' in stub
Ejemplo n.º 4
0
 def test_strip_preserve(self):
     field = StringField()
     assert field.validate(self.cfg, ' hello ') == ' hello '
Ejemplo n.º 5
0
 def test_regex_no_match(self):
     field = StringField(regex='^H.*$')
     with pytest.raises(ValueError):
         field.validate(self.cfg, 'hello')
Ejemplo n.º 6
0
 def test_regex_match(self):
     field = StringField(regex='^h.*$')
     assert field.validate(self.cfg, 'hello') == 'hello'
Ejemplo n.º 7
0
 def test_max_length_invalid(self):
     field = StringField(max_len=4)
     with pytest.raises(ValueError):
         field.validate(self.cfg, 'hello')
Ejemplo n.º 8
0
 def test_choice_lower_valid(self):
     field = StringField(choices=['a', 'b', 'c'], transform_case='lower')
     assert field.validate(self.cfg, 'A') == 'a'
Ejemplo n.º 9
0
 def test_choice_valid(self):
     field = StringField(choices=['a', 'b', 'c'])
     assert field.validate(self.cfg, 'a') == 'a'
Ejemplo n.º 10
0
 def test_invalid_case(self):
     with pytest.raises(TypeError):
         field = StringField(transform_case='ASDF')
Ejemplo n.º 11
0
 def test_empty_string_not_required(self):
     field = StringField(required=False)
     assert field.validate(self.cfg, '') == ''
Ejemplo n.º 12
0
 def test_empty_string_requied(self):
     field = StringField(required=True)
     with pytest.raises(ValueError):
         field.validate(self.cfg, '')
Ejemplo n.º 13
0
 def test_non_string(self):
     field = StringField()
     with pytest.raises(ValueError):
         field.validate(self.cfg, 100)
Ejemplo n.º 14
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')
Ejemplo n.º 15
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')
Ejemplo n.º 16
0
 def test_strip_whitespace(self):
     field = StringField(transform_strip=True)
     assert field.validate(self.cfg, '  hello  ') == 'hello'
Ejemplo n.º 17
0
 def test_strip_custom(self):
     field = StringField(transform_strip='/')
     assert field.validate(self.cfg, '// hello  ///') == ' hello  '
Ejemplo n.º 18
0
 def test_case_upper(self):
     field = StringField(transform_case='upper')
     assert field.validate(self.cfg, 'hello') == 'HELLO'
Ejemplo n.º 19
0
 def test_choice_invalid(self):
     field = StringField(choices=['a', 'b', 'c'])
     with pytest.raises(ValueError):
         field.validate(self.cfg, 'z')
Ejemplo n.º 20
0
 def test_case_preserve(self):
     field = StringField()
     assert field.validate(self.cfg, 'HellO') == 'HellO'
Ejemplo n.º 21
0
 def test_max_length_valid(self):
     field = StringField(max_len=5)
     assert field.validate(self.cfg, 'hello') == 'hello'
Ejemplo n.º 22
0
 def test_get_annotation_typestr_field(self):
     field = StringField(key='hello')
     assert get_annotation_typestr(field) == 'str'