Beispiel #1
0
    def test_with_func_only(self, mock_property):
        def func():
            pass

        result = cacheprop.cached_property(func)

        self.assertTrue(isinstance(result, cacheprop.CacheProperty))
        mock_property.assert_called_once_with(result)
        self.assertEqual(result.func, func)
        self.assertEqual(result.prop, 'func')
        self.assertEqual(result.cache_attr, '_func')
        self.assertEqual(result.attrs, {})
        self.assertEqual(result.base('spam'), 'spam')
Beispiel #2
0
    def test_with_func_attrs(self, mock_property):
        def func():
            pass

        result = cacheprop.cached_property(func, 'a', 'b', 'c')

        self.assertTrue(isinstance(result, cacheprop.CacheProperty))
        mock_property.assert_called_once_with(result)
        self.assertEqual(result.func, func)
        self.assertEqual(result.prop, 'func')
        self.assertEqual(result.cache_attr, '_func')
        self.assertEqual(len(result.attrs), 3)
        for attr, getter in result.attrs.items():
            self.assertTrue(attr in 'abc')
            self.assertEqual(getter(mock.Mock(**{attr: 'spam'})), 'spam')
        self.assertEqual(result.base('spam'), 'spam')
Beispiel #3
0
    def test_with_base(self, mock_property):
        def func():
            pass

        decorator = cacheprop.cached_property(base='foo')

        self.assertTrue(callable(decorator))
        self.assertFalse(isinstance(decorator, cacheprop.CacheProperty))
        self.assertFalse(mock_property.called)

        result = decorator(func)

        self.assertTrue(isinstance(result, cacheprop.CacheProperty))
        mock_property.assert_called_once_with(result)
        self.assertEqual(result.func, func)
        self.assertEqual(result.prop, 'func')
        self.assertEqual(result.cache_attr, '_func')
        self.assertEqual(result.attrs, {})
        self.assertEqual(result.base(mock.Mock(foo='spam')), 'spam')