예제 #1
0
파일: tests.py 프로젝트: ykshatroff/yzcache
    def test_cached_method(self):
        class A(object):
            x = 9

            def __init__(self, x):
                self.x = x

            @cached_function
            def get_x(self):
                return self.x

            @cached_function
            @classmethod
            def get_class_x(cls):
                return cls.x

            def outside(self, a, b=5):
                return self.x + a + b

        class B(A):
            x = 8

        a = A(15)
        self.assertEqual(a.get_x(), 15)
        self.assertEqual(a.get_class_x(), 9)
        self.assertEqual(B.get_class_x(), 8)

        outside = cached_function(a.outside)
        self.assertEqual(outside(1), 21)

        counter = {}
        test_fn = lambda: counter.update({'n': counter.get('n', 0) + 1})
        test_fn_cached = cached_function(test_fn)
        key = test_fn_cached._make_key({})
        self.assertEqual(key, 'yzcache.tests.<lambda>()')

        cache.clear()
        test_fn_cached()
        self.assertEqual(len(cache), 1)
        self.assertIn(key, cache)
        test_fn_cached()
        self.assertEqual(counter['n'], 1)
예제 #2
0
파일: tests.py 프로젝트: ykshatroff/yzcache
    def test_basic(self):
        def f1():
            return 10

        f3 = cached_function(f1)
        self.assertEqual(f3(), f1())

        @cached_function
        def f2():
            return 10

        self.assertEqual(f2.__name__, 'f2')
        self.assertEqual(f2(), 10)
예제 #3
0
파일: tests.py 프로젝트: ykshatroff/yzcache
    def test_callargs(self):
        def f1(a, b=5, c=10):
            return a + b + c
        f3 = cached_function(f1)

        self.assertEqual(f3._make_args(1), {'a': 1, 'b': 5, 'c': 10})