Beispiel #1
0
    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')
Beispiel #2
0
    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'
Beispiel #3
0
    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
Beispiel #4
0
    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
Beispiel #5
0
 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)]
Beispiel #6
0
    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
Beispiel #7
0
    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)]
Beispiel #8
0
    def test_getattr(self):
        schema = Schema()
        field = Field()
        schema.field = field

        assert schema.field is field
Beispiel #9
0
 def test_getitem_keyerror_not_schema(self):
     schema = Schema()
     schema.x = Field()
     with pytest.raises(KeyError):
         y = schema['x.y']
Beispiel #10
0
 def test_name(self):
     field = Field(name='name', key='key')
     assert field.name == 'name'
Beispiel #11
0
 def test_validate_validator_valid(self):
     field = Field(key='key', validator=lambda cfg, value: 'HELLO')
     assert field.validate(self.cfg, 'asdf') == 'HELLO'
Beispiel #12
0
 def test_setval(self):
     field = Field(key='key')
     field.__setval__(self.cfg, 'hello')
     assert self.cfg._data == {'key': 'hello'}
Beispiel #13
0
 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')
Beispiel #14
0
 def test__validate(self):
     field = Field()
     x = 'hello'
     assert field._validate(self.cfg, x) is x
Beispiel #15
0
 def test_to_basic(self):
     field = Field()
     x = 'hello'
     assert field.to_basic(self.cfg, x) is x
Beispiel #16
0
 def test_to_python(self):
     field = Field()
     x = 'hello'
     assert field.to_python(self.cfg, x) is x
Beispiel #17
0
 def test_setdefault(self):
     field = Field(key='key', default='hello')
     field.__setdefault__(self.cfg)
     assert self.cfg._data['key'] == 'hello'
Beispiel #18
0
 def test_setkey(self):
     field = Field()
     field.__setkey__(self.cfg, 'key')
     assert field.key == 'key'
Beispiel #19
0
 def test_getval(self):
     field = Field(key='key')
     self.cfg._data['key'] = 'hello'
     assert field.__getval__(self.cfg) == 'hello'
Beispiel #20
0
 def test_get_annotation_typestr_str(self):
     field = Field(key='hello')
     field.storage_type = 'asdf'
     assert get_annotation_typestr(field) == 'asdf'
Beispiel #21
0
 def test_default_callable(self):
     field = Field(default=list)
     assert field.default == []
Beispiel #22
0
 def test_required(self):
     field = Field(required=True, key='key')
     with pytest.raises(ValueError):
         field.validate(self.cfg, None)
Beispiel #23
0
 def test_key_name(self):
     field = Field(key='key')
     assert field.name == 'key'
Beispiel #24
0
 def test_not_required(self):
     field = Field(key='key')
     assert field.validate(self.cfg, None) is None
Beispiel #25
0
 def test_validate_value(self):
     field = Field(key='key')
     x = 'hello'
     assert field.validate(self.cfg, x) is x
Beispiel #26
0
 def test_get_annotation_type_any(self):
     field = Field(key='hello')
     field.storage_type = ''
     assert get_annotation_typestr(field) == 'typing.Any'
Beispiel #27
0
 def test_default_value(self):
     field = Field(default=2)
     assert field.default == 2