Beispiel #1
0
 def test_no_setdefault(self):
     getter = MagicMock()
     getter.return_value = 'hello'
     cfg = MockConfig()
     field = VirtualField(getter)
     field.__setdefault__(cfg)
     assert cfg._data == {}
Beispiel #2
0
 def test__getval(self):
     getter = MagicMock()
     getter.return_value = 'hello'
     cfg = MockConfig()
     field = VirtualField(getter)
     assert field.__getval__(cfg) == 'hello'
     getter.assert_called_once_with(cfg)
Beispiel #3
0
    def test__setval_no_setter(self):
        getter = MagicMock()
        getter.return_value = 'hello'

        field = VirtualField(getter)
        with pytest.raises(TypeError):
            field.__setval__(MockConfig(), 'goodbye')
        getter.assert_not_called()
Beispiel #4
0
    def test__setval_setter(self):
        cfg = MockConfig()
        getter = MagicMock()
        getter.return_value = 'hello'

        setter = MagicMock()
        field = VirtualField(getter, setter)
        field.__setval__(cfg, 'hello')
        setter.assert_called_once_with(cfg, 'hello')
Beispiel #5
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)
Beispiel #6
0
 def test_asdict(self):
     schema = Schema(dynamic=True)
     schema.x = Field()
     schema.sub1.y = Field()
     schema.a = VirtualField(lambda cfg: 100)
     config = schema()
     config.x = 1
     config.sub1.y = 2
     config.z = 3
     assert asdict(config) == {'x': 1, 'z': 3, 'sub1': {'y': 2}}
Beispiel #7
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
Beispiel #8
0
 def test_asdict_virtual(self):
     schema = Schema()
     schema.x = Field()
     schema.sub1.y = Field()
     schema.a = VirtualField(lambda cfg: 100)
     config = schema()
     config.x = 1
     config.sub1.y = 2
     assert asdict(config, virtual=True) == {
         'x': 1,
         'sub1': {
             'y': 2
         },
         'a': 100
     }
Beispiel #9
0
 def test_default_error(self):
     with pytest.raises(TypeError):
         field = VirtualField(self.getter, default=1)
Beispiel #10
0
 def test_to_tree_exclude_virtual(self):
     sentinel = object()
     schema = Schema()
     schema.v = VirtualField(getter=lambda config: sentinel)
     config = schema()
     assert config.to_tree(virtual=False) == {}