Commits

Thomas Waldmann  committed 54fbe54

sqlite storage: use buffer() for giving BLOBs to it, use str() on buffer BLOB return values, add tests for binary values

  • Participants
  • Parent commits fe20713

Comments (0)

Files changed (2)

File storage/_tests/__init__.py

         with pytest.raises(KeyError):
             self.st[k]
 
+    def test_setitem_getitem_delitem_binary(self):
+        k, v = 'key', '\000\001\002'
+        self.st[k] = StringIO(v)
+        assert v == self.st[k].read()
+        assert len(v) == 3
+        del self.st[k]
+        with pytest.raises(KeyError):
+            self.st[k]
+
     def test_iter(self):
         kvs = set([('1', 'one'), ('2', 'two'), ('3', 'three'), ])
         for k, v in kvs:
         with pytest.raises(KeyError):
             self.st[k]
 
+    def test_setitem_getitem_delitem_binary(self):
+        k, v = 'key', '\000\001\002'
+        self.st[k] = v
+        assert v == self.st[k]
+        assert len(v) == 3
+        del self.st[k]
+        with pytest.raises(KeyError):
+            self.st[k]
+
     def test_iter(self):
         kvs = set([('1', 'one'), ('2', 'two'), ('3', 'three'), ])
         for k, v in kvs:

File storage/sqlite.py

         rows = list(self.conn.execute("select value from %s where key=?" % self.table_name, (key, )))
         if not rows:
             raise KeyError(key)
-        return rows[0]['value']
+        return str(rows[0]['value'])
 
     def __setitem__(self, key, value):
         with self.conn:
-            self.conn.execute('insert into %s values (?, ?)' % self.table_name, (key, value))
+            self.conn.execute('insert into %s values (?, ?)' % self.table_name, (key, buffer(value)))
 
 
 class FileStorage(_Storage, FileMutableStorageBase):
     def __setitem__(self, key, stream):
         value = stream.read()
         with self.conn:
-            self.conn.execute('insert into %s values (?, ?)' % self.table_name, (key, value))
+            self.conn.execute('insert into %s values (?, ?)' % self.table_name, (key, buffer(value)))