From f678300b76bbf875f2ce3afa823104fc6a1e7ba1 Mon Sep 17 00:00:00 2001 From: Jan David Mol <mol@astron.nl> Date: Thu, 18 May 2017 08:59:32 +0000 Subject: [PATCH] Task #10801: Added test for lofar.common.cache --- .gitattributes | 2 + LCS/PyCommon/test/CMakeLists.txt | 1 + LCS/PyCommon/test/t_cache.py | 100 +++++++++++++++++++++++++++++++ LCS/PyCommon/test/t_cache.sh | 2 + 4 files changed, 105 insertions(+) create mode 100644 LCS/PyCommon/test/t_cache.py create mode 100755 LCS/PyCommon/test/t_cache.sh diff --git a/.gitattributes b/.gitattributes index a80634fbf43..3c783db70da 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 a9d316cf73a..00008a3a477 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 00000000000..1c75d0088fb --- /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 00000000000..8b7a9ab64fb --- /dev/null +++ b/LCS/PyCommon/test/t_cache.sh @@ -0,0 +1,2 @@ +#!/bin/sh +./runctest.sh t_cache -- GitLab