Beispiel #1
0
    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")
Beispiel #2
0
    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"
Beispiel #3
0
    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")
Beispiel #4
0
    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
Beispiel #5
0
    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