def test_validate_validator_invalid(self): def inner(cfg, value): raise KeyError() field = Field(key='key', validator=inner) with pytest.raises(KeyError): field.validate(self.cfg, 'hello')
def test_get_annotation_typestr_type_module(self): custom_type = type('CustomType', tuple(), {}) custom_type.__module__ = 'a.b.c' field = Field(key='helo') field.storage_type = custom_type assert get_annotation_typestr(field) == 'a.b.c.CustomType'
def test_getitem(self): schema = Schema() schema.x = Field() schema.y.z = Field() assert schema['x'] is schema.x assert schema['y'] is schema.y assert schema['y.z'] is schema.y.z
def test_setattr_field(self): field = Field() field.__setkey__ = MagicMock() schema = Schema() schema.field = field assert field.__setkey__.called_once_with(schema, 'field') assert schema._fields['field'] is field
def test_iter(self): schema = Schema() subfield = Field() topfield = Field() schema.sub.field = subfield schema.field = topfield items = sorted(list(schema.__iter__()), key=lambda x: x[0]) assert items == [('field', topfield), ('sub', schema.sub)]
def test_make_type(self): schema = Schema() schema.x = Field(default=2) schema.y = Field() CustomConfig = schema.make_type('CustomConfig') a = CustomConfig(y=10) assert isinstance(a, Config) assert a.x == 2 assert a.y == 10
def test_get_all_fields(self): schema = Schema() schema.x = Field() schema.sub1.y = Field() schema.sub1.sub2.z = Field() schema.sub1.a = Field() schema.sub3.b = Field() check = schema.get_all_fields() assert check == [('x', schema, schema.x), ('sub1', schema, schema.sub1), ('sub1.y', schema.sub1, schema.sub1.y), ('sub1.sub2', schema.sub1, schema.sub1.sub2), ('sub1.sub2.z', schema.sub1.sub2, schema.sub1.sub2.z), ('sub1.a', schema.sub1, schema.sub1.a), ('sub3', schema, schema.sub3), ('sub3.b', schema.sub3, schema.sub3.b)]
def test_getattr(self): schema = Schema() field = Field() schema.field = field assert schema.field is field
def test_getitem_keyerror_not_schema(self): schema = Schema() schema.x = Field() with pytest.raises(KeyError): y = schema['x.y']
def test_name(self): field = Field(name='name', key='key') assert field.name == 'name'
def test_validate_validator_valid(self): field = Field(key='key', validator=lambda cfg, value: 'HELLO') assert field.validate(self.cfg, 'asdf') == 'HELLO'
def test_setval(self): field = Field(key='key') field.__setval__(self.cfg, 'hello') assert self.cfg._data == {'key': 'hello'}
def test_add_field_field(self): field = Field() field.__setkey__ = MagicMock() schema = BaseSchema() schema._add_field('hello', field) field.__setkey__.assert_called_once_with(schema, 'hello')
def test__validate(self): field = Field() x = 'hello' assert field._validate(self.cfg, x) is x
def test_to_basic(self): field = Field() x = 'hello' assert field.to_basic(self.cfg, x) is x
def test_to_python(self): field = Field() x = 'hello' assert field.to_python(self.cfg, x) is x
def test_setdefault(self): field = Field(key='key', default='hello') field.__setdefault__(self.cfg) assert self.cfg._data['key'] == 'hello'
def test_setkey(self): field = Field() field.__setkey__(self.cfg, 'key') assert field.key == 'key'
def test_getval(self): field = Field(key='key') self.cfg._data['key'] = 'hello' assert field.__getval__(self.cfg) == 'hello'
def test_get_annotation_typestr_str(self): field = Field(key='hello') field.storage_type = 'asdf' assert get_annotation_typestr(field) == 'asdf'
def test_default_callable(self): field = Field(default=list) assert field.default == []
def test_required(self): field = Field(required=True, key='key') with pytest.raises(ValueError): field.validate(self.cfg, None)
def test_key_name(self): field = Field(key='key') assert field.name == 'key'
def test_not_required(self): field = Field(key='key') assert field.validate(self.cfg, None) is None
def test_validate_value(self): field = Field(key='key') x = 'hello' assert field.validate(self.cfg, x) is x
def test_get_annotation_type_any(self): field = Field(key='hello') field.storage_type = '' assert get_annotation_typestr(field) == 'typing.Any'
def test_default_value(self): field = Field(default=2) assert field.default == 2