summaryrefslogtreecommitdiffstats
path: root/gitlint/tests/test_cache.py
blob: 5d78953b94d86156d3b4090eebb953866393d97f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
from gitlint.tests.base import BaseTestCase
from gitlint.cache import PropertyCache, cache


class CacheTests(BaseTestCase):

    class MyClass(PropertyCache):
        """ Simple class that has cached properties, used for testing. """

        def __init__(self):
            PropertyCache.__init__(self)
            self.counter = 0

        @property
        @cache
        def foo(self):
            self.counter += 1
            return u"bår"

        @property
        @cache(cachekey=u"hür")
        def bar(self):
            self.counter += 1
            return u"fōo"

    def test_cache(self):
        # Init new class with cached properties
        myclass = self.MyClass()
        self.assertEqual(myclass.counter, 0)
        self.assertDictEqual(myclass._cache, {})

        # Assert that function is called on first access, cache is set
        self.assertEqual(myclass.foo, u"bår")
        self.assertEqual(myclass.counter, 1)
        self.assertDictEqual(myclass._cache, {"foo": u"bår"})

        # After function is not called on subsequent access, cache is still set
        self.assertEqual(myclass.foo, u"bår")
        self.assertEqual(myclass.counter, 1)
        self.assertDictEqual(myclass._cache, {"foo": u"bår"})

    def test_cache_custom_key(self):
        # Init new class with cached properties
        myclass = self.MyClass()
        self.assertEqual(myclass.counter, 0)
        self.assertDictEqual(myclass._cache, {})

        # Assert that function is called on first access, cache is set with custom key
        self.assertEqual(myclass.bar, u"fōo")
        self.assertEqual(myclass.counter, 1)
        self.assertDictEqual(myclass._cache, {u"hür": u"fōo"})

        # After function is not called on subsequent access, cache is still set
        self.assertEqual(myclass.bar, u"fōo")
        self.assertEqual(myclass.counter, 1)
        self.assertDictEqual(myclass._cache, {u"hür": u"fōo"})