diff --git a/.gitattributes b/.gitattributes index a80634fbf4333912e8ad599a6163bb25088f395f..3c783db70dadcb6151b22c3d349af0f78fd28b9a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2109,6 +2109,8 @@ LCS/PyCommon/math.py -text LCS/PyCommon/postgres.py -text LCS/PyCommon/subprocess.py -text LCS/PyCommon/test/python-coverage.sh eol=lf +LCS/PyCommon/test/t_cache.py -text +LCS/PyCommon/test/t_cache.sh -text LCS/PyCommon/test/t_dbcredentials.run eol=lf LCS/PyCommon/test/t_dbcredentials.sh eol=lf LCS/PyCommon/test/t_methodtrigger.sh eol=lf diff --git a/LCS/PyCommon/test/CMakeLists.txt b/LCS/PyCommon/test/CMakeLists.txt index a9d316cf73ad82904eff010202ea2b7adc12c092..00008a3a477283f12949751bfd028c48883c768a 100644 --- a/LCS/PyCommon/test/CMakeLists.txt +++ b/LCS/PyCommon/test/CMakeLists.txt @@ -6,6 +6,7 @@ file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/python-coverage.sh DESTINATION ${CMAKE_BINARY_DIR}/bin) +lofar_add_test(t_cache) lofar_add_test(t_dbcredentials) lofar_add_test(t_methodtrigger) lofar_add_test(t_util) diff --git a/LCS/PyCommon/test/t_cache.py b/LCS/PyCommon/test/t_cache.py new file mode 100644 index 0000000000000000000000000000000000000000..1c75d0088fb544a0fb6317f5c74a1ac41da582a6 --- /dev/null +++ b/LCS/PyCommon/test/t_cache.py @@ -0,0 +1,100 @@ +import unittest +from lofar.common.cache import cache + + +class TestCache(unittest.TestCase): + + @cache + def cached_func(self, arg, kwarg=None): + self.invocations += 1 + return arg, kwarg + + def setUp(self): + self.invocations = 0 + + def test_simple_function(self): + """ Check whether the cache class works with simple functions. """ + + @cache + def cached_func(arg, kwarg=None): + return arg, kwarg + + result = cached_func(1, 2) + + self.assertEqual(result, (1, 2)) + + def test_class_member(self): + """ Check whether the cache class works with class members. """ + + class myclass: + @cache + def cached_func(self, arg, kwarg=None): + return arg, kwarg + + obj = myclass() + result = obj.cached_func(1, 2) + + self.assertEqual(result, (1, 2)) + + def test_class_static_member(self): + """ Check whether the cache class works with static class members. """ + + class myclass: + @staticmethod + @cache + def cached_func(arg, kwarg=None): + return arg, kwarg + + obj = myclass() + result = obj.cached_func(1, 2) + + self.assertEqual(result, (1, 2)) + + def test_class_property(self): + """ Check whether the cache class works with class properties. """ + + class myclass: + @property + @cache + def cached_func(self): + return True + + obj = myclass() + result = obj.cached_func + + self.assertEqual(result, True) + + def test_initial_call(self): + """ Does the cache return the correct result? """ + + result = self.cached_func(1, 2) + + self.assertEqual(result, (1, 2)) + self.assertEqual(self.invocations, 1) + + def test_cached_call(self): + """ Does the cache cache results? """ + + result = self.cached_func(1, 2) + result = self.cached_func(1, 2) + + self.assertEqual(result, (1, 2)) + self.assertEqual(self.invocations, 1) + + def test_different_calls(self): + """ Does the cache NOT cache results if new parameters are provided? """ + + result = self.cached_func(1, 2) + result = self.cached_func(1, 3) + + self.assertEqual(result, (1, 3)) + self.assertEqual(self.invocations, 2) + +def main(argv): + unittest.main() + +if __name__ == "__main__": + # run all tests + import sys + main(sys.argv[1:]) + diff --git a/LCS/PyCommon/test/t_cache.sh b/LCS/PyCommon/test/t_cache.sh new file mode 100755 index 0000000000000000000000000000000000000000..8b7a9ab64fb20592ec98723b5c37b1bac86aa1ae --- /dev/null +++ b/LCS/PyCommon/test/t_cache.sh @@ -0,0 +1,2 @@ +#!/bin/sh +./runctest.sh t_cache