diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2020-03-19 14:00:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2020-03-19 14:00:14 +0000 |
commit | df9615bac55ac6f1c3f516b66279ac0007175030 (patch) | |
tree | 84dd81d1c97835271cea7fbdd67c074742365e07 /gitlint/tests/test_cache.py | |
parent | Initial commit. (diff) | |
download | gitlint-df9615bac55ac6f1c3f516b66279ac0007175030.tar.xz gitlint-df9615bac55ac6f1c3f516b66279ac0007175030.zip |
Adding upstream version 0.13.1.upstream/0.13.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint/tests/test_cache.py')
-rw-r--r-- | gitlint/tests/test_cache.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/gitlint/tests/test_cache.py b/gitlint/tests/test_cache.py new file mode 100644 index 0000000..5d78953 --- /dev/null +++ b/gitlint/tests/test_cache.py @@ -0,0 +1,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"}) |