Skip to content
Browse files

Supporting Memcached driver in sessions

  • Loading branch information...
1 parent edbe109 commit 973c1de5ce26f34e1f278b7593717e8e6b14db41 @diogobaeder committed Oct 28, 2011
Showing with 42 additions and 12 deletions.
  1. +1 −2 pycket/session.py
  2. +18 −0 tests/test_driver.py
  3. +23 −10 tests/test_session.py
View
3 pycket/session.py
@@ -47,7 +47,6 @@ class SessionManager(object):
'''
SESSION_ID_NAME = 'PYCKET_ID'
- EXPIRE_SECONDS = 24 * 60 * 60
STORAGE_CATEGORY = 'db_sessions'
driver = None
@@ -65,7 +64,7 @@ def __setup_driver(self):
self.__setup_settings()
storage_settings = self.settings.get('storage', {})
factory = DriverFactory()
- self.driver = factory.create('redis', storage_settings, self.STORAGE_CATEGORY)
+ self.driver = factory.create(self.settings.get('engine'), storage_settings, self.STORAGE_CATEGORY)
def __setup_settings(self):
pycket_settings = self.handler.settings.get('pycket')
View
18 tests/test_driver.py
@@ -60,6 +60,14 @@ def expire(self, session_id, expiration):
driver.set('session-id', foo)
+ @istest
+ def starts_with_1_day_to_expire_in_database(self):
+ driver = RedisDriver(dict(db=0))
+
+ one_day = 24 * 60 * 60
+
+ self.assertEqual(driver.EXPIRE_SECONDS, one_day)
+
class MemcachedTestCase(TestCase):
client = None
@@ -127,6 +135,16 @@ def fails_to_load_if_storage_settings_contain_wrong_host(self):
driver.set('session-id', 'foo')
+ @istest
+ def starts_with_1_day_to_expire_in_database(self):
+ driver = MemcachedDriver({
+ 'servers': ('localhost:11211',)
+ })
+
+ one_day = 24 * 60 * 60
+
+ self.assertEqual(driver.EXPIRE_SECONDS, one_day)
+
class DriverFactoryTest(TestCase):
@istest
View
33 tests/test_session.py
@@ -5,7 +5,7 @@
from nose.tools import istest, raises
import redis
-from pycket.driver import RedisDriver
+from pycket.driver import MemcachedDriver, RedisDriver
from pycket.session import ConfigurationError, SessionManager, SessionMixin
@@ -44,6 +44,28 @@ class StubHandler(SessionMixin):
StubHandler().session.get('something')
+ @istest
+ def creates_session_for_redis(self):
+ class StubHandler(SessionMixin):
+ settings = {
+ 'pycket': {
+ 'engine': 'redis',
+ }
+ }
+
+ self.assertIsInstance(StubHandler().session.driver, RedisDriver)
+
+ @istest
+ def creates_session_for_memcached(self):
+ class StubHandler(SessionMixin):
+ settings = {
+ 'pycket': {
+ 'engine': 'memcached',
+ }
+ }
+
+ self.assertIsInstance(StubHandler().session.driver, MemcachedDriver)
+
class RedisTestCase(TestCase):
client = None
@@ -157,15 +179,6 @@ def deletes_objects_from_session(self):
self.assertEqual(session.keys(), ['some-object2'])
@istest
- def starts_with_1_day_to_expire_in_database(self):
- handler = StubHandler()
- manager = SessionManager(handler)
-
- one_day = 24 * 60 * 60
-
- self.assertEqual(manager.EXPIRE_SECONDS, one_day)
-
- @istest
@skipIf(skip_slow_tests, 'This test is too slow')
def still_retrieves_object_if_not_passed_from_expiration(self):
handler = StubHandler()

0 comments on commit 973c1de

Please sign in to comment.
Something went wrong with that request. Please try again.