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), {})
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)
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")
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")
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), {})
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"
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__" )
def test_readonly(self) -> None: class Foo: pass f = Foo() f.readonly = "stuff" d = bcpd.PropertyDescriptor("foo", f) assert d.readonly == "stuff"
def test_serialized(self) -> None: class Foo: pass f = Foo() f.serialized = "stuff" d = bcpd.PropertyDescriptor("foo", f) assert d.serialized == "stuff"
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__
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
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']
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
def test___str__(self): d = bcpd.PropertyDescriptor("foo") assert str(d) == "PropertyDescriptor(foo)"
def test___init__(self): d = bcpd.PropertyDescriptor("foo") assert d.name == "foo"
def test_PropertyDescriptor__init__(): d = pd.PropertyDescriptor("foo") assert d.name == "foo"
def test_PropertyDescriptor__str__(): d = pd.PropertyDescriptor("foo") assert str(d) == "PropertyDescriptor(foo)"
def test___str__(self) -> None: f = Foo() d = bcpd.PropertyDescriptor("foo", f) assert str(d) == str(f)