import unittest
import sqlite3


class KV(object):

    def __init__(self):
        self._db = sqlite3.connect(':memory:')
        self._execute('CREATE TABLE data (key TEXT PRIMARY KEY, value TEXT)')

    def _execute(self, *args):
        return self._db.cursor().execute(*args)

    def __len__(self):
        return 0

    def __getitem__(self, key):
        for row in self._execute('SELECT value FROM data WHERE key=?', (key,)):
            return row[0]
        else:
            raise KeyError

    def get(self, key, fallback=None):
        try:
            return self[key]
        except KeyError:
            return fallback

    def __setitem__(self, key, value):
        self._execute('INSERT INTO data VALUES (?, ?)', (key, value))


class KVTest(unittest.TestCase):

    def test_new_kv_is_empty(self):
        self.assertEqual(len(KV()), 0)

    def test_get_missing_value_raises_key_error(self):
        with self.assertRaises(KeyError):
            KV()['missing']

    def test_get_missing_value_returns_default(self):
        self.assertIsNone(KV().get('missing'))

    def test_get_missing_value_with_default_returns_argument(self):
        fallback = object()
        self.assertEqual(KV().get('missing', fallback), fallback)

    def test_saved_item_is_retrieved_via_getitem(self):
        kv = KV()
        kv['a'] = 'b'
        self.assertEqual(kv['a'], 'b')

    def test_saved_item_is_retrieved_via_get(self):
        kv = KV()
        kv['a'] = 'b'
        self.assertEqual(kv.get('a'), 'b')

@@ -666,24 +666,41 @@
 ey, value):%0A
+        try:%0A    
         self
@@ -757,16 +757,134 @@
 value))%0A
+        except sqlite3.IntegrityError:%0A            self._execute('UPDATE data SET value=? WHERE key=?', (value, key))%0A
 %0A%0Aclass 
@@ -1645,12 +1645,172 @@
 ('a'), 'b')%0A
+%0A    def test_updated_item_is_retrieved_via_getitem(self):%0A        kv = KV()%0A        kv%5B'a'%5D = 'b'%0A        kv%5B'a'%5D = 'c'%0A        self.assertEqual(kv%5B'a'%5D, 'c')%0A

update existing entries