Skip to content
Browse files

Adding coverage for RedisDriver and DriverFactory

  • Loading branch information...
1 parent 5613e21 commit 1e1b9e49bd0a7315f09a74b51e32b0edb917fbba @diogobaeder committed Oct 27, 2011
Showing with 80 additions and 8 deletions.
  1. +2 −5 pycket/driver.py
  2. +75 −0 tests/test_driver.py
  3. +3 −3 tests/test_session.py
View
7 pycket/driver.py
@@ -16,9 +16,6 @@ class RedisDriver(object):
def __init__(self, settings):
self.settings = settings
- def load(self, dataset):
- self.dataset = dataset
-
def set(self, session_id, session):
pickled_session = pickle.dumps(session)
self.__setup_dataset()
@@ -46,9 +43,9 @@ class DriverFactory(object):
STORAGE_CATEGORIES = ('db_sessions', 'db_notifications')
def create(self, name, storage_settings, storage_category):
- return self.create_redis(storage_settings, storage_category)
+ return self.__create_redis(storage_settings, storage_category)
- def create_redis(self, storage_settings, storage_category):
+ def __create_redis(self, storage_settings, storage_category):
storage_settings = copy(storage_settings)
default_storage_identifier = RedisDriver.DEFAULT_STORAGE_IDENTIFIERS[storage_category]
storage_settings['db'] = storage_settings.get(storage_category, default_storage_identifier)
View
75 tests/test_driver.py
@@ -0,0 +1,75 @@
+import pickle
+from unittest import TestCase
+
+from nose.tools import istest, raises
+import redis
+
+from pycket.driver import DriverFactory, RedisDriver
+
+
+class RedisTestCase(TestCase):
+ dataset = None
+
+ def setUp(self):
+ if self.dataset is None:
+ self.dataset = redis.Redis(db=0)
+ self.dataset.flushall()
+
+
+class RedisDriverTest(RedisTestCase):
+ @istest
+ def inserts_pickable_object_into_session(self):
+ driver = RedisDriver(dict(db=0))
+
+ foo = dict(foo='bar')
+
+ driver.set('session-id', foo)
+
+ result = self.dataset.get('session-id')
+
+ self.assertEqual(pickle.loads(result), foo)
+
+ @istest
+ def retrieves_a_pickled_object_from_session(self):
+ driver = RedisDriver(dict(db=0))
+
+ foo = dict(foo='bar')
+
+ self.dataset.set('session-id', pickle.dumps(foo))
+
+ result = driver.get('session-id')
+
+ self.assertEqual(result, foo)
+
+ @istest
+ def makes_session_expire_in_one_day_in_the_dataset(self):
+ driver = RedisDriver(dict(db=0))
+
+ foo = dict(foo='bar')
+
+ test_case = self
+
+ class StubClient(object):
+ def set(self, session_id, pickled_session):
+ pass
+
+ def expire(self, session_id, expiration):
+ test_case.assertEqual(expiration, RedisDriver.EXPIRE_SECONDS)
+
+ driver.dataset = StubClient()
+
+ driver.set('session-id', foo)
+
+
+class DriverFactoryTest(TestCase):
+ @istest
+ def creates_instance_for_redis_session(self):
+ factory = DriverFactory()
+
+ instance = factory.create('redis', storage_settings={}, storage_category='db_sessions')
+
+ self.assertIsInstance(instance, RedisDriver)
+
+ instance.get('dataset-is-lazy-loaded')
+
+ self.assertEqual(instance.dataset.connection_pool._available_connections[0].db, 0)
View
6 tests/test_session.py
@@ -9,7 +9,7 @@
from pycket.session import SessionManager, SessionMixin
-skip_slow_tests = True
+skip_slow_tests = False
class SessionMixinTest(TestCase):
@@ -150,11 +150,11 @@ def still_retrieves_object_if_not_passed_from_expiration(self):
def cannot_retrieve_object_if_passed_from_expiration(self):
handler = StubHandler()
manager = SessionManager(handler)
- manager.EXPIRE_SECONDS = 1
+ manager.driver.EXPIRE_SECONDS = 1
manager.set('foo', 'bar')
- time.sleep(manager.EXPIRE_SECONDS + 1)
+ time.sleep(manager.driver.EXPIRE_SECONDS + 1)
self.assertIsNone(manager.get('foo'))

0 comments on commit 1e1b9e4

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