Skip to content
Browse files

Added tests for sqlalchemy.

  • Loading branch information...
1 parent 17e195a commit b0563f80d8f670bc95ba1b165d7fda5431511650 @mmerickel mmerickel committed Mar 12, 2012
Showing with 42 additions and 8 deletions.
  1. +7 −8 anykeystore/backends/sqla.py
  2. +32 −0 anykeystore/tests/test_backends/test_sqla.py
  3. +3 −0 tox.ini
View
15 anykeystore/backends/sqla.py
@@ -2,10 +2,9 @@
from sqlalchemy import engine_from_config
from sqlalchemy import Column, MetaData
-from sqlalchemy import String, Text, DateTime, Table
+from sqlalchemy import String, PickleType, DateTime, Table
from sqlalchemy.sql import select, delete
-from anykeystore.compat import pickle
from anykeystore.interfaces import KeyValueStore
from anykeystore.utils import coerce_timedelta
@@ -43,8 +42,8 @@ def __init__(self, url, **kw):
table_name = kw.pop('table_name', 'key_storage')
meta = kw.pop('metadata', MetaData())
self.table = Table(table_name, meta,
- Column('key', String(200), primary_key=True, nullable=False),
- Column('value', Text(), nullable=False),
+ Column('key', String(256), primary_key=True, nullable=False),
+ Column('value', PickleType(), nullable=False),
Column('expires', DateTime()),
)
kw['url'] = url
@@ -54,14 +53,15 @@ def create(self):
self.table.create(checkfirst=True, bind=self.engine)
def retrieve(self, key):
- s = self.table
c = self.engine.connect()
try:
- data = c.execute(select([s.c.value, s.c.expires], s.c.key == key))
+ data = c.execute(select(
+ [self.table.c.value, self.table.c.expires],
+ self.table.c.key == key)).fetchone()
if data:
value, expires = data
if expires is None or datetime.utcnow() < expires:
- return pickle.loads(data[0])
+ return value
finally:
c.close()
raise KeyError
@@ -70,7 +70,6 @@ def store(self, key, value, expires=None):
expiration = None
if expires:
expiration = datetime.utcnow() + coerce_timedelta(expires)
- value = pickle.dumps(value, protocol=pickle.HIGHEST_PROTOCOL)
c = self.engine.connect()
try:
c.execute(
View
32 anykeystore/tests/test_backends/test_sqla.py
@@ -0,0 +1,32 @@
+import unittest
+
+class TestSQLStore(unittest.TestCase):
+
+ def _makeOne(self, url='sqlite://', **kw):
+ from anykeystore.backends.sqla import SQLStore
+ store = SQLStore(url, **kw)
+ store.create()
+ return store
+
+ def test_it(self):
+ store = self._makeOne()
+ store.store('foo', 'bar')
+ value = store.retrieve('foo')
+ self.assertEqual(value, 'bar')
+
+ def test_it_delete(self):
+ store = self._makeOne()
+ store.store('foo', 'bar')
+ store.delete('foo')
+ self.assertRaises(KeyError, store.retrieve, 'foo')
+
+ def test_it_old(self):
+ store = self._makeOne()
+ store.store('foo', 'bar', expires=-1)
+ self.assertRaises(KeyError, store.retrieve, 'foo')
+
+ def test_it_purge(self):
+ store = self._makeOne()
+ store.store('foo', 'bar', expires=-1)
+ store.purge_expired()
+ self.assertRaises(KeyError, store.retrieve, 'foo')
View
3 tox.ini
@@ -5,6 +5,8 @@ envlist =
[testenv]
commands =
python setup.py test -q
+deps =
+ sqlalchemy
[testenv:cover]
basepython =
@@ -15,6 +17,7 @@ deps =
nose
coverage
nosexcover
+ sqlalchemy
# we separate coverage into its own testenv because a) "last run wins" wrt
# cobertura jenkins reporting and b) pypy and jython can't handle any

0 comments on commit b0563f8

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