From af8fcdab197551c76d27700aedef5effb2270672 Mon Sep 17 00:00:00 2001 From: posativ Date: Wed, 17 Oct 2012 12:00:11 +0200 Subject: [PATCH] most database operations now return Comment object --- isso/comment.py | 4 ++-- isso/db.py | 15 +++++++++++---- specs/test_comment.py | 13 +++++++++---- specs/test_db.py | 16 ++++++++-------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/isso/comment.py b/isso/comment.py index 9e8be56..a3cf0f5 100644 --- a/isso/comment.py +++ b/isso/comment.py @@ -12,7 +12,7 @@ def create(app, environ, request, path): except ValueError as e: return Response(unicode(e), 400) - return Response(json.dumps(app.db.get(*rv)), 201, content_type='application/json') + return Response(json.dumps(rv), 201, content_type='application/json') def get(app, environ, request, path, id=None): @@ -32,4 +32,4 @@ def modify(app, environ, request, path, id): return Response(unicode(e), 400) else: rv = app.db.delete(path, id) - return Response(json.dumps(app.db.get(*rv)), 200, content_type='application/json') + return Response(json.dumps(rv), 200, content_type='application/json') diff --git a/isso/db.py b/isso/db.py index ba0a6d9..bc6cb94 100644 --- a/isso/db.py +++ b/isso/db.py @@ -97,7 +97,8 @@ class SQLite(Abstract): ) with sqlite3.connect(self.dbpath) as con: - return con.execute('SELECT path, MAX(id) FROM comments;').fetchone() + return self.query2comment( + con.execute('SELECT *, MAX(id) FROM comments;').fetchone()) def update(self, path, id, comment): with sqlite3.connect(self.dbpath) as con: @@ -108,7 +109,7 @@ class SQLite(Abstract): with sqlite3.connect(self.dbpath) as con: con.execute('UPDATE comments SET modified=? WHERE path=? AND id=?', (time.time(), path, id)) - return path, id + return self.get(path, id) def get(self, path, id): with sqlite3.connect(self.dbpath) as con: @@ -117,14 +118,20 @@ class SQLite(Abstract): def delete(self, path, id): with sqlite3.connect(self.dbpath) as con: + refs = con.execute('SELECT id FROM comments WHERE id=?', (id, )).fetchone() + + if len(refs) == 0: + con.execute('DELETE FROM comments WHERE path=? AND id=?', (path, id)) + return None + con.execute('UPDATE comments SET text=? WHERE path=? AND id=?', ('', path, id)) con.execute('UPDATE comments SET mode=? WHERE path=? AND id=?', (2, path, id)) for field in set(Comment.fields) - set(['text', 'parent']): con.execute('UPDATE comments SET %s=? WHERE path=? AND id=?' % field, (None, path, id)) - return path, id + return self.get(path, id) - def retrieve(self, path, limit=20): + def retrieve(self, path, limit=20, mode=None): with sqlite3.connect(self.dbpath) as con: rv = con.execute("SELECT * FROM comments WHERE path = ?" \ + " ORDER BY id DESC LIMIT ?;", (path, limit)).fetchall() diff --git a/specs/test_comment.py b/specs/test_comment.py index 49df0d5..7c47249 100644 --- a/specs/test_comment.py +++ b/specs/test_comment.py @@ -84,9 +84,14 @@ class TestComments(unittest.TestCase): def testDelete(self): self.post('/comment/path/new', data=json.dumps(comment(text='Lorem ipsum ...'))) + assert self.delete('/comment/path/1').status_code == 200 + assert self.get('/comment/path/1').status_code == 404 - r = self.delete('/comment/path/1') + def testDeleteWithReference(self): + + self.post('/comment/path/new', data=json.dumps(comment(text='First'))) + self.post('/comment/path/new', data=json.dumps(comment(text='Second', parent=1))) + + r = self.delete('/comment/path/2') assert r.status_code == 200 - - c = Comment(**json.loads(r.data)) - assert c.deleted + assert Comment(**json.loads(r.data)).deleted diff --git a/specs/test_db.py b/specs/test_db.py index 52351a2..9e4a46d 100644 --- a/specs/test_db.py +++ b/specs/test_db.py @@ -23,14 +23,14 @@ class TestSQLite(unittest.TestCase): def test_get(self): rv = self.db.add('/', comment(text='Spam')) - c = self.db.get(*rv) + c = self.db.get('/', rv.id) assert c.id == 1 assert c.text == 'Spam' def test_add(self): - x = self.db.add('/', comment(text='Foo')) + self.db.add('/', comment(text='Foo')) self.db.add('/', comment(text='Bar')) self.db.add('/path/', comment(text='Baz')) @@ -47,10 +47,10 @@ class TestSQLite(unittest.TestCase): def test_update(self): - path, id = self.db.add('/', comment(text='Foo')) + rv = self.db.add('/', comment(text='Foo')) time.sleep(0.1) - path, id = self.db.update(path, id, comment(text='Bla')) - c = self.db.get(path, id) + rv = self.db.update('/', rv.id, comment(text='Bla')) + c = self.db.get('/', rv.id) assert c.id == 1 assert c.text == 'Bla' @@ -58,11 +58,11 @@ class TestSQLite(unittest.TestCase): def test_delete(self): - path, id = self.db.add('/', comment( + rv = 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) + self.db.delete('/', rv.id) + c = self.db.get('/', rv.id) assert c.id == 1 assert c.text == ''