Permalink
Browse files

Updated unit tests to use mock. All except sqla.

  • Loading branch information...
mmerickel committed Mar 16, 2012
1 parent c1bbc59 commit d7a5d67e42d10dd953e009ce839f470dd777e35a
@@ -1,43 +1,85 @@
-import time
-
import unittest2 as unittest
+from mock import MagicMock
-def setUpModule():
- try: # pragma: no cover
- import memcache
- except ImportError: # pragma: no cover
- raise unittest.SkipTest(
- 'must install python-memcached to run memcached tests')
-
-class TestRedisStore(unittest.TestCase):
+class TestMemcachedStore(unittest.TestCase):
- def _makeOne(self):
+ def _makeOne(self,
+ servers='myhost:8080',
+ key_prefix='key.',
+ backend_api=None):
from anykeystore.backends.memcached import MemcachedStore
store = MemcachedStore(
- servers='localhost:11211', key_prefix='test_anykey.')
+ servers=servers, key_prefix=key_prefix, backend_api=backend_api)
return store
- def test_it(self):
+ def _makeClient(self):
+ client = MagicMock(spec=['get', 'set', 'delete'])
+ return client
+
+ def test_init_with_server_string(self):
+ store = self._makeOne(servers='myhost:8080\n yourhost:6790')
+ self.assertEqual(store.servers, ['myhost:8080', 'yourhost:6790'])
+
+ def test_init_with_server_list(self):
+ store = self._makeOne(servers=['myhost:8080', 'yourhost:6790'])
+ self.assertEqual(store.servers, ['myhost:8080', 'yourhost:6790'])
+
+ def test__get_conn(self):
+ api = MagicMock(spec=['Client'])
+ api.Client.return_value = 'myconn'
+
+ store = self._makeOne(backend_api=api)
+ conn = store._get_conn()
+
+ self.assertEqual(conn, 'myconn')
+ api.Client.assert_called_with(['myhost:8080'])
+
+ def test_store(self):
store = self._makeOne()
+ client = self._makeClient()
+ store._get_conn = MagicMock(return_value=client)
+
store.store('foo', 'bar')
- value = store.retrieve('foo')
- self.assertEqual(value, 'bar')
- def test_it_delete(self):
+ client.set.assert_called_with('key.foo', 'bar', 0)
+
+ def test_store_with_expires(self):
store = self._makeOne()
- store.store('foo', 'bar')
+ client = self._makeClient()
+ store._get_conn = MagicMock(return_value=client)
+
+ store.store('foo', 'bar', expires=5)
+
+ client.set.assert_called_with('key.foo', 'bar', 5)
+
+ def test_delete(self):
+ store = self._makeOne()
+ client = self._makeClient()
+ store._get_conn = MagicMock(return_value=client)
+
store.delete('foo')
- self.assertRaises(KeyError, store.retrieve, 'foo')
- def test_it_old(self):
+ client.delete.assert_called_with('key.foo')
+
+ def test_retrieve(self):
store = self._makeOne()
- store.store('foo', 'bar', expires=1)
- time.sleep(1.01)
+ client = self._makeClient()
+ client.get.return_value = 'bar'
+ store._get_conn = MagicMock(return_value=client)
+
+ result = store.retrieve('foo')
+
+ self.assertEqual(result, 'bar')
+ client.get.assert_called_with('key.foo')
+
+ def test_retrieve_expired(self):
+ store = self._makeOne()
+ client = self._makeClient()
+ client.get.return_value = None
+ store._get_conn = MagicMock(return_value=client)
self.assertRaises(KeyError, store.retrieve, 'foo')
+ client.get.assert_called_with('key.foo')
- def test_it_purge(self):
+ def test_purge(self):
store = self._makeOne()
- store.store('foo', 'bar', expires=1)
- time.sleep(1.01)
store.purge_expired()
- self.assertRaises(KeyError, store.retrieve, 'foo')
@@ -1,5 +1,3 @@
-import time
-
import unittest2 as unittest
class TestMemoryStore(unittest.TestCase):
@@ -27,7 +25,6 @@ def test_it_old(self):
def test_it_purge(self):
store = self._makeOne()
- store.store('foo', 'bar', expires=0.01)
- time.sleep(0.02)
+ store.store('foo', 'bar', expires=-1)
store.purge_expired()
self.assertRaises(KeyError, store.retrieve, 'foo')
@@ -1,40 +1,152 @@
-import time
+from datetime import datetime, timedelta
import unittest2 as unittest
-
-def setUpModule():
- try: # pragma: no cover
- import pymongo
- except ImportError: # pragma: no cover
- raise unittest.SkipTest('must install pymongo to run mongodb tests')
+from mock import MagicMock, patch
class TestMongoStore(unittest.TestCase):
- def _makeOne(self):
+ def _makeOne(self,
+ db='testdb',
+ collection='keystore',
+ host='localhost',
+ port=8080,
+ backend_api=None):
from anykeystore.backends.mongodb import MongoDBStore
- store = MongoDBStore(db='test', collection='test_store')
+ store = MongoDBStore(db=db, collection=collection, host=host,
+ port=port, backend_api=backend_api)
return store
- def test_it(self):
- store = self._makeOne()
+ def _makeConnection(self):
+ conn = MagicMock(spec=[
+ 'create_collection', 'collection_names', '__getitem__'])
+ return conn
+
+ def test__get_conn(self):
+ conn = self._makeConnection()
+ conn.collection_names.return_value = ['keystore']
+
+ db_conn = MagicMock()
+ db_conn.__getitem__.return_value = conn
+
+ api = MagicMock(spec=['Connection', 'binary'])
+ api.Connection.return_value = db_conn
+
+ store = self._makeOne(backend_api=api)
+ result = store._get_conn()
+
+ self.assertEqual(result, conn)
+ api.Connection.assert_called_with('localhost', 8080, slave_okay=False)
+ self.assertFalse(conn.create_collection.called)
+
+ def test__get_conn_and_create_collection(self):
+ conn = self._makeConnection()
+ conn.collection_names.return_value = []
+
+ db_conn = MagicMock()
+ db_conn.__getitem__.return_value = conn
+
+ api = MagicMock(spec=['Connection', 'binary'])
+ api.Connection.return_value = db_conn
+
+ store = self._makeOne(backend_api=api)
+ result = store._get_conn()
+
+ self.assertEqual(result, conn)
+ api.Connection.assert_called_with('localhost', 8080, slave_okay=False)
+ self.assertTrue(conn.create_collection.called)
+
+ def test_store(self):
+ api = MagicMock(spec=['Connection', 'binary'])
+ store = self._makeOne(backend_api=api)
+ collection = MagicMock(spec=['update', 'remove', 'find_one'])
+ conn = self._makeConnection()
+ conn.__getitem__.return_value = collection
+ store._get_conn = MagicMock(return_value=conn)
+
store.store('foo', 'bar')
- value = store.retrieve('foo')
- self.assertEqual(value, 'bar')
- def test_it_delete(self):
+ conn.__getitem__.assert_called_with('keystore')
+ args = collection.update.call_args[0]
+ self.assertEqual(args[0], {'key': 'foo'})
+ self.assertEqual(args[1]['$set']['expires'], None)
+
+ def test_store_with_expires(self):
+ api = MagicMock(spec=['Connection', 'binary'])
+ store = self._makeOne(backend_api=api)
+ collection = MagicMock(spec=['update', 'remove', 'find_one'])
+ conn = self._makeConnection()
+ conn.__getitem__.return_value = collection
+ store._get_conn = MagicMock(return_value=conn)
+
+ now = datetime.utcnow()
+ with patch('anykeystore.backends.mongodb.datetime') as dt:
+ dt.utcnow.return_value = now
+ store.store('foo', 'bar', expires=5)
+
+ conn.__getitem__.assert_called_with('keystore')
+ args = collection.update.call_args[0]
+ self.assertEqual(args[0], {'key': 'foo'})
+ self.assertEqual(args[1]['$set']['expires'],
+ now + timedelta(seconds=5))
+
+ def test_delete(self):
store = self._makeOne()
- store.store('foo', 'bar')
+ collection = MagicMock(spec=['update', 'remove', 'find_one'])
+ conn = self._makeConnection()
+ conn.__getitem__.return_value = collection
+ store._get_conn = MagicMock(return_value=conn)
+
store.delete('foo')
- self.assertRaises(KeyError, store.retrieve, 'foo')
- def test_it_old(self):
+ conn.__getitem__.assert_called_with('keystore')
+ args = collection.remove.call_args[0]
+ self.assertEqual(args[0], {'key': 'foo'})
+
+ def test_retrieve(self):
store = self._makeOne()
- store.store('foo', 'bar', expires=-1)
- self.assertRaises(KeyError, store.retrieve, 'foo')
+ collection = MagicMock(spec=['update', 'remove', 'find_one'])
+ conn = self._makeConnection()
+ conn.__getitem__.return_value = collection
+ store._get_conn = MagicMock(return_value=conn)
+ collection.find_one.return_value = {'expires': None, 'value': 'bar'}
+
+ with patch('anykeystore.backends.mongodb.pickle') as pickle:
+ pickle.loads = lambda value: value
+ result = store.retrieve('foo')
- def test_it_purge(self):
+ self.assertEqual(result, 'bar')
+ conn.__getitem__.assert_called_with('keystore')
+ args = collection.find_one.call_args[0]
+ self.assertEqual(args[0], {'key': 'foo'})
+
+ def test_retrieve_expired(self):
store = self._makeOne()
- store.store('foo', 'bar', expires=0.01)
- time.sleep(0.1)
- store.purge_expired()
+ collection = MagicMock(spec=['update', 'remove', 'find_one'])
+ conn = self._makeConnection()
+ conn.__getitem__.return_value = collection
+ store._get_conn = MagicMock(return_value=conn)
+
+ old = datetime.utcnow() - timedelta(seconds=1)
+ collection.find_one.return_value = {'expires': old, 'value': 'bar'}
+
self.assertRaises(KeyError, store.retrieve, 'foo')
+
+ conn.__getitem__.assert_called_with('keystore')
+ args = collection.find_one.call_args[0]
+ self.assertEqual(args[0], {'key': 'foo'})
+
+ def test_purge(self):
+ store = self._makeOne()
+ collection = MagicMock(spec=['update', 'remove', 'find_one'])
+ conn = self._makeConnection()
+ conn.__getitem__.return_value = collection
+ store._get_conn = MagicMock(return_value=conn)
+
+ now = datetime.utcnow()
+ with patch('anykeystore.backends.mongodb.datetime') as dt:
+ dt.utcnow.return_value = now
+ store.purge_expired()
+
+ conn.__getitem__.assert_called_with('keystore')
+ args = collection.remove.call_args[0]
+ self.assertEqual(args[0], {'expires': {'$lte': now}})
Oops, something went wrong.

0 comments on commit d7a5d67

Please sign in to comment.