Skip to content

Commit

Permalink
save to filesystem; use autocommit
Browse files Browse the repository at this point in the history
  • Loading branch information
mgax committed Oct 2, 2012
1 parent bab7a96 commit 4ee7b5f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 5 additions & 3 deletions kv.py
Expand Up @@ -4,9 +4,11 @@

class KV(MutableMapping):

def __init__(self):
self._db = sqlite3.connect(':memory:')
self._execute('CREATE TABLE data (key TEXT PRIMARY KEY, value TEXT)')
def __init__(self, db_uri=':memory:'):
self._db = sqlite3.connect(db_uri)
self._db.isolation_level = None
self._execute('CREATE TABLE IF NOT EXISTS data '
'(key TEXT PRIMARY KEY, value TEXT)')

def _execute(self, *args):
return self._db.cursor().execute(*args)
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
@@ -1 +1,2 @@
nose==1.2.1
path.py==2.4.1
15 changes: 15 additions & 0 deletions tests.py
@@ -1,4 +1,6 @@
import unittest
import tempfile
from path import path
from kv import KV


Expand Down Expand Up @@ -71,3 +73,16 @@ def test_iter_yields_keys(self):
kv['b'] = 'x'
kv['c'] = 'x'
self.assertItemsEqual(kv, ['a', 'b', 'c'])


class KVPersistenceTest(unittest.TestCase):

def setUp(self):
self.tmp = path(tempfile.mkdtemp())
self.addCleanup(self.tmp.rmtree)

def test_value_saved_by_one_kv_client_is_read_by_another(self):
kv1 = KV(self.tmp / 'kv.sqlite')
kv1['a'] = 'b'
kv2 = KV(self.tmp / 'kv.sqlite')
self.assertEqual(kv2['a'], 'b')

0 comments on commit 4ee7b5f

Please sign in to comment.