예제 #1
0
 def test_set_from_json(self, mock_get: MagicMock,
                        mock_set: MagicMock) -> None:
     f = Foo()
     d = bcpd.PropertyDescriptor("foo", f)
     d.set_from_json(f, "bar", models=10)
     assert mock_get.called_once_with((f, "bar", 10), {})
     assert mock_set.called_once_with((f, "bar", 10), {})
예제 #2
0
    def test_abstract(self):
        d = bcpd.PropertyDescriptor("foo")

        class Foo(object):
            pass

        f = Foo()
        with pytest.raises(NotImplementedError):
            d.__get__(f, f.__class__)

        with pytest.raises(NotImplementedError):
            d.__set__(f, 11)

        with pytest.raises(NotImplementedError):
            d.__delete__(f)

        with pytest.raises(NotImplementedError):
            d.class_default(f)

        with pytest.raises(NotImplementedError):
            d.serialized

        with pytest.raises(NotImplementedError):
            d.readonly

        with pytest.raises(NotImplementedError):
            d.has_ref

        with pytest.raises(NotImplementedError):
            d.trigger_if_changed(f, 11)

        with pytest.raises(NotImplementedError):
            d._internal_set(f, 11)
예제 #3
0
 def test___get__improper(self) -> None:
     f = Foo()
     d = bcpd.PropertyDescriptor("foo", f)
     with pytest.raises(ValueError) as e:
         d.__get__(None, None)
     assert str(e.value).endswith(
         "both 'obj' and 'owner' are None, don't know what to do")
예제 #4
0
 def test_add_prop_descriptor_to_class_dupe_name(self) -> None:
     d = bcpd.PropertyDescriptor("foo")
     new_class_attrs = {'foo': 10}
     with pytest.raises(RuntimeError) as e:
         d.add_prop_descriptor_to_class("bar", new_class_attrs, [], [], {})
     assert str(
         e.value).endswith("Two property generators both created bar.foo")
예제 #5
0
    def test_set_from_json(self, mock_iset):
        class Foo(object):
            pass

        f = Foo()
        d = bcpd.PropertyDescriptor("foo")
        d.set_from_json(f, "bar", 10)
        assert mock_iset.called_once_with((f, "bar", 10), {})
예제 #6
0
    def test_has_ref(self) -> None:
        class Foo:
            pass

        f = Foo()
        f.has_ref = "stuff"
        d = bcpd.PropertyDescriptor("foo", f)
        assert d.has_ref == "stuff"
예제 #7
0
 def test___set__improper(self) -> None:
     f = Foo()
     d = bcpd.PropertyDescriptor("foo", f)
     with pytest.raises(RuntimeError) as e:
         d.__set__("junk", None)
     assert str(e.value).endswith(
         "Cannot set a property value 'foo' on a str instance before HasProps.__init__"
     )
예제 #8
0
    def test_readonly(self) -> None:
        class Foo:
            pass

        f = Foo()
        f.readonly = "stuff"
        d = bcpd.PropertyDescriptor("foo", f)
        assert d.readonly == "stuff"
예제 #9
0
    def test_serialized(self) -> None:
        class Foo:
            pass

        f = Foo()
        f.serialized = "stuff"
        d = bcpd.PropertyDescriptor("foo", f)
        assert d.serialized == "stuff"
예제 #10
0
 def test___init__(self) -> None:
     class Foo:
         '''doc'''
         pass
     f = Foo()
     d = bcpd.PropertyDescriptor("foo", f)
     assert d.name == "foo"
     assert d.property == f
     assert d.__doc__ == f.__doc__
예제 #11
0
    def test__trigger(self, mock_trigger: MagicMock) -> None:
        class Foo:
            _property_values = dict(foo=10, bar=20)

        class Match:
            def matches(*args, **kw): return True

        class NoMatch:
            def matches(*args, **kw): return False
        m = Match()
        nm = NoMatch()
        d1 = bcpd.PropertyDescriptor("foo", m)
        d2 = bcpd.PropertyDescriptor("bar", nm)

        d1.trigger_if_changed(Foo, "junk")
        assert not mock_trigger.called

        d2.trigger_if_changed(Foo, "junk")
        assert mock_trigger.called
예제 #12
0
 def test_class_default(self) -> None:
     result = {}
     class Foo:
         def themed_default(*args, **kw):
             result['called'] = True
     f = Foo()
     f.readonly = "stuff"
     d = bcpd.PropertyDescriptor("foo", f)
     d.class_default(d)
     assert result['called']
예제 #13
0
    def test_erializable_value(self) -> None:
        result = {}
        class Foo:
            def serialize_value(self, val):
                result['foo'] = val
        f = Foo()
        f.foo = 10

        d = bcpd.PropertyDescriptor("foo", f)
        d.property = Foo()

        # simulate the __get__ a subclass would have
        d.__get__ = lambda obj, owner: f.foo

        d.serializable_value(f)
        assert result['foo'] == 10
예제 #14
0
 def test___str__(self):
     d = bcpd.PropertyDescriptor("foo")
     assert str(d) == "PropertyDescriptor(foo)"
예제 #15
0
 def test___init__(self):
     d = bcpd.PropertyDescriptor("foo")
     assert d.name == "foo"
예제 #16
0
def test_PropertyDescriptor__init__():
    d = pd.PropertyDescriptor("foo")
    assert d.name == "foo"
예제 #17
0
def test_PropertyDescriptor__str__():
    d = pd.PropertyDescriptor("foo")
    assert str(d) == "PropertyDescriptor(foo)"
예제 #18
0
 def test___str__(self) -> None:
     f = Foo()
     d = bcpd.PropertyDescriptor("foo", f)
     assert str(d) == str(f)