diff --git a/isso/db.py b/isso/db.py index 93dbd8e..42b1337 100644 --- a/isso/db.py +++ b/isso/db.py @@ -81,23 +81,39 @@ class SQLite(Abstract): with sqlite3.connect(self.dbpath) as con: keys = ','.join(self.fields) values = ','.join('?'*len(self.fields)) - con.execute('INSERT INTO comments (%s) VALUES (%s);' % (keys, values), ( + x = con.execute('INSERT INTO comments (%s) VALUES (%s);' % (keys, values), ( 0, path, c.created, c.modified, c.text, c.author, c.email, c.website, c.parent, self.mode) ) + with sqlite3.connect(self.dbpath) as con: + return con.execute('SELECT path, MAX(id) FROM comments;').fetchone() + def update(self, path, id, comment): with sqlite3.connect(self.dbpath) as con: for field, value in comment.iteritems(): - con.execute('UPDATE comments SET ?=? WHERE id=?;', (field, value, id)) + con.execute('UPDATE comments SET %s=? WHERE path=? AND id=?;' % field, + (value, id, path)) + return path, id + + def get(self, path, id): + with sqlite3.connect(self.dbpath) as con: + return self.query2comment(con.execute( + 'SELECT * FROM comments WHERE path=? AND id=?;', (path, id)).fetchone()) def delete(self, path, id): - return + with sqlite3.connect(self.dbpath) as con: + con.execute('UPDATE comments SET text=? WHERE path=? AND id=?', ('', path, id)) + for field in Comment.fields: + if field == 'text': continue + con.execute('UPDATE comments SET %s=? WHERE path=? AND id=?' % field, + (None, path, id)) + return path, id def retrieve(self, path, limit=20): with sqlite3.connect(self.dbpath) as con: rv = con.execute("SELECT * FROM comments WHERE path = ?" \ - + " ORDER BY id DESC;", (path, )).fetchall() + + " ORDER BY id DESC LIMIT ?;", (path, limit)).fetchall() for item in rv: yield self.query2comment(item) diff --git a/specs/test_db.py b/specs/test_db.py index 6adf0c7..852315a 100644 --- a/specs/test_db.py +++ b/specs/test_db.py @@ -20,9 +20,17 @@ class TestSQLite(unittest.TestCase): self.db = SQLite() self.db.initialize({'SQLITE': self.path}) + def test_get(self): + + rv = self.db.add('/', comment(text='Spam')) + c = self.db.get(*rv) + + assert c.id == 1 + assert c.text == 'Spam' + def test_add(self): - self.db.add('/', comment(text='Foo')) + x = self.db.add('/', comment(text='Foo')) self.db.add('/', comment(text='Bar')) self.db.add('/path/', comment(text='Baz')) @@ -37,5 +45,27 @@ class TestSQLite(unittest.TestCase): assert rv[0].id == 1 assert rv[0].text == 'Baz' + def test_update(self): + + path, id = self.db.add('/', comment(text='Foo')) + path, id = self.db.update(path, id, comment(text='Bla')) + c = self.db.get(path, id) + + assert c.id == 1 + assert c.text == 'Foo' + + def test_delete(self): + + path, id = self.db.add('/', comment( + text='F**CK', author='P*NIS', website='http://somebadhost.org/')) + + self.db.delete(path, id) + c = self.db.get(path, id) + + assert c.id == 1 + assert c.text == '' + assert c.author is None + assert c.website is None + def tearDown(self): os.unlink(self.path)