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)
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) ]
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
def test_strip_preserve(self): field = StringField() 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_regex_match(self): field = StringField(regex='^h.*$') assert field.validate(self.cfg, 'hello') == 'hello'
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_valid(self): field = StringField(choices=['a', 'b', 'c']) assert field.validate(self.cfg, 'a') == 'a'
def test_invalid_case(self): with pytest.raises(TypeError): field = StringField(transform_case='ASDF')
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_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_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')
def test_strip_whitespace(self): field = StringField(transform_strip=True) assert field.validate(self.cfg, ' hello ') == 'hello'
def test_strip_custom(self): field = StringField(transform_strip='/') assert field.validate(self.cfg, '// hello ///') == ' hello '
def test_case_upper(self): field = StringField(transform_case='upper') assert field.validate(self.cfg, 'hello') == 'HELLO'
def test_choice_invalid(self): field = StringField(choices=['a', 'b', 'c']) with pytest.raises(ValueError): field.validate(self.cfg, 'z')
def test_case_preserve(self): field = StringField() assert field.validate(self.cfg, 'HellO') == 'HellO'
def test_max_length_valid(self): field = StringField(max_len=5) assert field.validate(self.cfg, 'hello') == 'hello'
def test_get_annotation_typestr_field(self): field = StringField(key='hello') assert get_annotation_typestr(field) == 'str'