Commits

Marcos Araujo Sobrinho  committed ca4036d

get_multi, set_multi and del_multi tests, fix for python-binary-memcached that does not implements delete_multi

  • Participants
  • Parent commits 6a1318d

Comments (0)

Files changed (5)

File dogpile/cache/__init__.py

-__version__ = '0.4.3.1'
+__version__ = '0.4.5'
 
 from .region import CacheRegion, register_backend, make_region

File dogpile/cache/backends/memcached.py

             username=self.username,
             password=self.password
         )
+
+    def delete_multi(self, keys):
+        """python-binary-memcached api does not implements delete_multi"""
+        for key in keys:
+            self.delete(key)
       install_requires=['dogpile.core>=0.4.1'],
       test_suite='nose.collector',
       tests_require=['nose', 'mock'],
-)
+)

File tests/cache/_fixtures.py

         reg.set("some key", "some value")
         eq_(reg.get("some key"), "some value")
 
+    def test_region_set_multiple_values(self):
+        reg = self._region()
+        values = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
+        reg.set_multi(values)
+        eq_(values['key1'], reg.get('key1'))
+        eq_(values['key2'], reg.get('key2'))
+        eq_(values['key3'], reg.get('key3'))
+
+    def test_region_get_multiple_values(self):
+        reg = self._region()
+        key1 = 'value1'
+        key2 = 'value2'
+        key3 = 'value3'
+        reg.set('key1', key1)
+        reg.set('key2', key2)
+        reg.set('key3', key3)
+        values = reg.get_multi(['key1', 'key2', 'key3'])
+        eq_(key1, values['key1'])
+        eq_(key2, values['key2'])
+        eq_(key3, values['key3'])
+
+    def test_region_get_nothing_multiple(self):
+        reg = self._region()
+        values = {'key1': 'value1', 'key3': 'value3', 'key5': 'value5'}
+        reg.set_multi(values)
+        reg_values = reg.get_multi(['key1', 'key2', 'key3', 'key4', 'key5', 'key6'])
+        eq_(reg_values['key1'], values['key1'])
+        eq_(reg_values['key2'], NO_VALUE)
+        eq_(reg_values['key3'], values['key3'])
+        eq_(reg_values['key4'], NO_VALUE)
+        eq_(reg_values['key5'], values['key5'])
+        eq_(reg_values['key6'], NO_VALUE)
+
+    def test_region_delete_multiple(self):
+        reg = self._region()
+        values = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
+        reg.set_multi(values)
+        reg.delete_multi(['key2', 'key10'])
+        eq_(values['key1'], reg.get('key1'))
+        eq_(NO_VALUE, reg.get('key2'))
+        eq_(values['key3'], reg.get('key3'))
+        eq_(NO_VALUE, reg.get('key10'))
+
     def test_region_set_get_nothing(self):
         reg = self._region()
         eq_(reg.get("some key"), NO_VALUE)

File tests/cache/test_region.py

             return self._cache[key]
         except KeyError:
             return NO_VALUE
+    def get_multi(self, keys):
+        values = {}
+        for key in keys:
+            values[key] = self.get(key)
+        return values
     def set(self, key, value):
         self._cache[key] = value
+    def set_multi(self, mapping):
+        for key,value in mapping.items():
+            self.set(key, value)
     def delete(self, key):
         self._cache.pop(key, None)
+    def delete_multi(self, keys):
+        for key in keys:
+            self.delete(key)
 register_backend("mock", __name__, "MockBackend")
 
 def key_mangler(key):
         eq_(ret, 3)
         eq_(reg.backend._cache['some key'][0], 3)
 
+    def test_should_set_multiple_values(self):
+        reg = self._region()
+        values = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
+        reg.set_multi(values)
+        eq_(values['key1'], reg.get('key1'))
+        eq_(values['key2'], reg.get('key2'))
+        eq_(values['key3'], reg.get('key3'))
+
+    def test_should_get_multiple_values(self):
+        reg = self._region()
+        values = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
+        reg.set_multi(values)
+        reg_values = reg.get_multi(['key1', 'key2', 'key3'])
+        eq_(values['key1'], reg_values['key1'])
+        eq_(values['key2'], reg_values['key2'])
+        eq_(values['key3'], reg_values['key3'])
+
+    def test_should_delete_multiple_values(self):
+        reg = self._region()
+        values = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
+        reg.set_multi(values)
+        reg.delete_multi(['key2', 'key1000'])
+        eq_(values['key1'], reg.get('key1'))
+        eq_(NO_VALUE, reg.get('key2'))
+        eq_(values['key3'], reg.get('key3'))
+
+
 class CacheDecoratorTest(TestCase):
     def _region(self, init_args={}, config_args={}, backend="mock"):
         reg = CacheRegion(**init_args)