def test__set_name__calling_twice_with_same_name_is_ok(self): class Owner: pass prop = MagicProperty(abstract=MyCls, default_value=AnnotationHint.NO_DEFAULT_VALUE) prop.__set_name__(Owner, "myprop") prop.__set_name__(Owner, "myprop")
def test__set_name__sets_name(self): class Owner: pass prop = MagicProperty(abstract=MyCls, default_value=AnnotationHint.NO_DEFAULT_VALUE) prop.__set_name__(Owner, "myprop") assert prop.name == "myprop"
def test__get__result_is_not_cached_per_type(self, mock_get_container): class Owner: pass prop = MagicProperty(abstract=MyCls, default_value=AnnotationHint.NO_DEFAULT_VALUE) prop.__set_name__(Owner, "prop") Owner.prop = prop mock_get_container.return_value = Container() owner_1_prop = Owner().prop owner_2_prop = Owner().prop assert owner_1_prop is not owner_2_prop
def test__get__uses_container_to_make(self, mock_get_container): class Owner: pass prop = MagicProperty(abstract=MyCls, default_value=AnnotationHint.NO_DEFAULT_VALUE) prop.__set_name__(Owner, "prop") Owner.prop = prop singleton_obj = MyCls() container = Container() mock_get_container.return_value = container container.bind_instance(MyCls, singleton_obj) owners_prop = Owner().prop assert owners_prop is singleton_obj
def test__get__raises_if_not_assigned_name(self): class Owner: pass Owner.prop = MagicProperty( abstract=MyCls, default_value=AnnotationHint.NO_DEFAULT_VALUE) with pytest.raises(TypeError, match="has not been assigned a name"): Owner().prop
def test__set_name__raises_if_name_mismatch(self): class Owner: pass prop = MagicProperty(abstract=MyCls, default_value=AnnotationHint.NO_DEFAULT_VALUE) prop.__set_name__(Owner, "myprop") with pytest.raises(TypeError, match="two different names"): prop.__set_name__(Owner, "otherprop")
class Owner: obj = MagicProperty(abstract=MyCls, default_value=AnnotationHint.NO_DEFAULT_VALUE)