diff options
author | Erik Johnston <erik@matrix.org> | 2017-03-28 11:19:15 +0100 |
---|---|---|
committer | Erik Johnston <erik@matrix.org> | 2017-03-30 14:14:46 +0100 |
commit | eefd9fee81428ecec311999980bb4213b6aac2df (patch) | |
tree | 382b11ef8ca0f896ae73c478fc69d7fe3e17b72f /tests/util/caches | |
parent | Manually calculate cache key as getcallargs is expensive (diff) | |
download | synapse-eefd9fee81428ecec311999980bb4213b6aac2df.tar.xz |
Fix up tests
Diffstat (limited to 'tests/util/caches')
-rw-r--r-- | tests/util/caches/test_descriptors.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/util/caches/test_descriptors.py b/tests/util/caches/test_descriptors.py index 4414e86771..3f14ab503f 100644 --- a/tests/util/caches/test_descriptors.py +++ b/tests/util/caches/test_descriptors.py @@ -175,3 +175,41 @@ class DescriptorTestCase(unittest.TestCase): logcontext.LoggingContext.sentinel) return d1 + + @defer.inlineCallbacks + def test_cache_default_args(self): + class Cls(object): + def __init__(self): + self.mock = mock.Mock() + + @descriptors.cached() + def fn(self, arg1, arg2=2, arg3=3): + return self.mock(arg1, arg2, arg3) + + obj = Cls() + + obj.mock.return_value = 'fish' + r = yield obj.fn(1, 2, 3) + self.assertEqual(r, 'fish') + obj.mock.assert_called_once_with(1, 2, 3) + obj.mock.reset_mock() + + # a call with same params shouldn't call the mock again + r = yield obj.fn(1, 2) + self.assertEqual(r, 'fish') + obj.mock.assert_not_called() + obj.mock.reset_mock() + + # a call with different params should call the mock again + obj.mock.return_value = 'chips' + r = yield obj.fn(2, 3) + self.assertEqual(r, 'chips') + obj.mock.assert_called_once_with(2, 3, 3) + obj.mock.reset_mock() + + # the two values should now be cached + r = yield obj.fn(1, 2) + self.assertEqual(r, 'fish') + r = yield obj.fn(2, 3) + self.assertEqual(r, 'chips') + obj.mock.assert_not_called() |