Commits

Andriy Kornatskyy committed 31ce617

Added unit tests for CacheDependency.

Comments (0)

Files changed (2)

src/wheezy/caching/dependency.py

 
     def next_key(self, namespace=None):
         """ Returns the next unique key for dependency.
-
-            >>> from wheezy.caching.memory import MemoryCache
-            >>> c = MemoryCache()
-            >>> d = CacheDependency(c, 'key')
-            >>> d.next_key()
-            'key1'
-            >>> d.next_key()
-            'key2'
         """
         return self.master_key + str(self.cache.incr(
             self.master_key, 1, namespace or self.namespace, 0))
 
     def next_keys(self, n, namespace=None):
         """ Returns ``n`` number of dependency keys.
-
-            >>> from wheezy.caching.memory import MemoryCache
-            >>> c = MemoryCache()
-            >>> d = CacheDependency(c, 'key')
-            >>> d.next_keys(1)
-            ['key1']
-            >>> d.next_keys(3)
-            ['key2', 'key3', 'key4']
         """
         last_id = self.cache.incr(self.master_key, n,
                                   namespace or self.namespace, 0)
 
     def add(self, key, namespace=None):
         """ Adds a given key to dependency.
-
-            >>> from wheezy.caching.memory import MemoryCache
-            >>> c = MemoryCache()
-            >>> d = CacheDependency(c, 'key')
-            >>> d.add('key-x')
-            True
-            >>> c.get('key1')
-            'key-x'
         """
         namespace = namespace or self.namespace
         return self.cache.add(self.next_key(namespace),
 
     def add_multi(self, keys, key_prefix='', namespace=None):
         """ Adds several keys to dependency.
-
-            >>> from wheezy.caching.memory import MemoryCache
-            >>> c = MemoryCache()
-            >>> d = CacheDependency(c, 'key')
-            >>> d.add_multi(('key-x', 'key-y'))
-            []
-            >>> c.get('key1')
-            'key-x'
-            >>> c.get('key2')
-            'key-y'
-
-            With ``key_prefix``
-
-            >>> d.add_multi(('a', 'b'), key_prefix='key-')
-            []
-            >>> c.get('key3')
-            'key-a'
-            >>> c.get('key4')
-            'key-b'
         """
         namespace = namespace or self.namespace
-        mapping = dict(zip(self.next_keys(len(keys), namespace),
-                           map(lambda k: key_prefix + k, keys)))
+        mapping = dict(zip(self.next_keys(
+            len(keys), namespace),
+            key_prefix and map(lambda k: key_prefix + k, keys) or keys))
         return self.cache.add_multi(mapping, self.time, '', namespace)
 
     def delete(self, namespace=None):
         """ Delete all items wired by this cache dependency.
-
-            >>> from wheezy.caching.memory import MemoryCache
-            >>> c = MemoryCache()
-            >>> d = CacheDependency(c, 'key')
-
-            If there are no dependent items, delete succeed.
-
-            >>> d.delete(c)
-            True
-
-            Clears all dependent items
-
-            >>> mapping = {'key-x': 1, 'key-y': 2}
-            >>> c.set_multi(mapping, 100)
-            []
-            >>> d.add_multi(mapping.keys())
-            []
-            >>> len(c.items)
-            5
-            >>> d.delete()
-            True
-            >>> len(c.items)
-            0
         """
         namespace = namespace or self.namespace
         cache = self.cache

src/wheezy/caching/tests/test_dependency.py

+
+""" Unit tests for ``wheezy.caching.patterns``.
+"""
+
+import unittest
+
+from mock import ANY
+from mock import Mock
+
+
+class CacheDependencyTestCase(unittest.TestCase):
+
+    def setUp(self):
+        from wheezy.caching.dependency import CacheDependency
+        self.mock_cache = Mock()
+        self.d = CacheDependency(self.mock_cache, 'key',
+                                 time=10, namespace='default')
+
+    def test_next_key(self):
+        """ Ensures consistency of key issued.
+        """
+        self.mock_cache.incr.return_value = 1
+        assert 'key1' == self.d.next_key()
+        self.mock_cache.incr.assert_called_once_with('key', 1, 'default', 0)
+
+        self.mock_cache.reset_mock()
+        self.mock_cache.incr.return_value = 2
+        assert 'key2' == self.d.next_key('ns')
+        self.mock_cache.incr.assert_called_once_with('key', 1, 'ns', 0)
+
+    def test_next_keys(self):
+        """ Ensures consistency of keys issued.
+        """
+        self.mock_cache.incr.return_value = 2
+        assert ['key1', 'key2'] == self.d.next_keys(2)
+        self.mock_cache.incr.assert_called_once_with('key', 2, 'default', 0)
+
+        self.mock_cache.reset_mock()
+        self.mock_cache.incr.return_value = 4
+        assert ['key3', 'key4'] == self.d.next_keys(2, 'ns')
+        self.mock_cache.incr.assert_called_once_with('key', 2, 'ns', 0)
+
+    def test_add(self):
+        """ Ensure dependency is added.
+        """
+        self.mock_cache.incr.return_value = 1
+        self.mock_cache.add.return_value = True
+        assert self.d.add('k')
+        self.mock_cache.add.assert_called_once_with(
+            'key1', 'k', 10, 'default')
+
+        self.mock_cache.reset_mock()
+        self.mock_cache.incr.return_value = 1
+        self.mock_cache.add.return_value = True
+        assert self.d.add('k', 'ns')
+        self.mock_cache.add.assert_called_once_with(
+            'key1', 'k', 10, 'ns')
+
+    def test_add_multi(self):
+        """ Ensure multiple dependency keys added.
+        """
+        self.mock_cache.incr.return_value = 1
+        self.mock_cache.add.return_value = True
+        assert self.d.add_multi(['k'])
+        self.mock_cache.add_multi.assert_called_once_with(
+            {'key1': 'k'}, 10, '', 'default')
+
+        self.mock_cache.reset_mock()
+        self.mock_cache.incr.return_value = 1
+        self.mock_cache.add.return_value = True
+        assert self.d.add_multi(['k'], 'prefix-')
+        self.mock_cache.add_multi.assert_called_once_with(
+            {'key1': 'prefix-k'}, 10, '', 'default')
+
+        self.mock_cache.reset_mock()
+        self.mock_cache.incr.return_value = 1
+        self.mock_cache.add.return_value = True
+        assert self.d.add_multi(['k'], namespace='ns')
+        self.mock_cache.add_multi.assert_called_once_with(
+            {'key1': 'k'}, 10, '', 'ns')
+
+    def test_delete(self):
+        """ Ensure related keys are invalidated.
+        """
+        self.mock_cache.get.return_value = None
+        assert self.d.delete()
+        self.mock_cache.get.assert_called_once_with(
+            'key', 'default')
+
+        self.mock_cache.reset_mock()
+        self.mock_cache.get.return_value = None
+        assert self.d.delete('ns')
+        self.mock_cache.get.assert_called_once_with(
+            'key', 'ns')
+
+        def side_effect(*args):
+            assert ['key1', 'key2'] == args[0]
+            return {'key1': 'k1', 'key2': 'k2'}
+        self.mock_cache.reset_mock()
+        self.mock_cache.get.return_value = 2
+        self.mock_cache.get_multi.side_effect = side_effect
+        assert self.d.delete()
+        self.mock_cache.get.assert_called_once_with(
+            'key', 'default')
+        self.mock_cache.get_multi.assert_called_once_with(
+            ANY, '', 'default')
+        self.mock_cache.delete_multi.assert_called_once_with(
+            ANY, 0, '', 'default')
+        assert ['k1', 'k2', 'key', 'key1', 'key2'] == sorted(
+            self.mock_cache.delete_multi.call_args[0][0])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.