summaryrefslogtreecommitdiffstats
path: root/gitlint-core/gitlint/tests/test_cache.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-12-04 03:31:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-12-04 03:31:41 +0000
commit72b8c35be4293bd21de123854491c658c53af100 (patch)
tree735464cc081879561927a37650b1102beaa1f4f9 /gitlint-core/gitlint/tests/test_cache.py
parentAdding upstream version 0.16.0. (diff)
downloadgitlint-72b8c35be4293bd21de123854491c658c53af100.tar.xz
gitlint-72b8c35be4293bd21de123854491c658c53af100.zip
Adding upstream version 0.17.0.upstream/0.17.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint-core/gitlint/tests/test_cache.py')
-rw-r--r--gitlint-core/gitlint/tests/test_cache.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/gitlint-core/gitlint/tests/test_cache.py b/gitlint-core/gitlint/tests/test_cache.py
new file mode 100644
index 0000000..4b1d47a
--- /dev/null
+++ b/gitlint-core/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 "bår"
+
+ @property
+ @cache(cachekey="hür")
+ def bar(self):
+ self.counter += 1
+ return "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, "bår")
+ self.assertEqual(myclass.counter, 1)
+ self.assertDictEqual(myclass._cache, {"foo": "bår"})
+
+ # After function is not called on subsequent access, cache is still set
+ self.assertEqual(myclass.foo, "bår")
+ self.assertEqual(myclass.counter, 1)
+ self.assertDictEqual(myclass._cache, {"foo": "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, "fōo")
+ self.assertEqual(myclass.counter, 1)
+ self.assertDictEqual(myclass._cache, {"hür": "fōo"})
+
+ # After function is not called on subsequent access, cache is still set
+ self.assertEqual(myclass.bar, "fōo")
+ self.assertEqual(myclass.counter, 1)
+ self.assertDictEqual(myclass._cache, {"hür": "fōo"})