From e08e7be4648cfd757130d2a443a4ce73ba55524d Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Thu, 31 Oct 2013 11:08:00 +0100 Subject: [PATCH] remove stale threads after all comments have been deleted --- isso/db/__init__.py | 7 +++++++ specs/test_comment.py | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/isso/db/__init__.py b/isso/db/__init__.py index 8ebcecb..56e8f4f 100644 --- a/isso/db/__init__.py +++ b/isso/db/__init__.py @@ -20,6 +20,13 @@ class SQLite3: self.threads = Threads(self) self.comments = Comments(self) + self.execute([ + 'CREATE TRIGGER IF NOT EXISTS remove_stale_threads', + 'AFTER DELETE ON comments', + 'BEGIN', + ' DELETE FROM threads WHERE id NOT IN (SELECT tid FROM comments);', + 'END']) + def execute(self, sql, args=()): if isinstance(sql, (list, tuple)): diff --git a/specs/test_comment.py b/specs/test_comment.py index 09d6e01..07ef3a9 100644 --- a/specs/test_comment.py +++ b/specs/test_comment.py @@ -151,12 +151,14 @@ class TestComments(unittest.TestCase): r = client.delete('/id/1') assert r.status_code == 200 assert loads(r.data)['mode'] == 4 + assert '/path/' in self.app.db.threads assert self.get('/?uri=%2Fpath%2F&id=1').status_code == 200 assert self.get('/?uri=%2Fpath%2F&id=2').status_code == 200 r = client.delete('/id/2') assert self.get('/?uri=%2Fpath%2F').status_code == 404 + assert '/path/' not in self.app.db.threads def testDeleteWithMultipleReferences(self): """ @@ -270,6 +272,13 @@ class TestComments(unittest.TestCase): self.put('/id/1', data=json.dumps({"text": "Typo"})) assert loads(self.get('/id/1').data)["text"] == "

Typo

\n" + def testDeleteCommentRemovesThread(self): + + rv = self.client.post('/new?uri=%2F', data=json.dumps({"text": "..."})) + assert '/' in self.app.db.threads + self.client.delete('/id/1') + assert '/' not in self.app.db.threads + class TestModeratedComments(unittest.TestCase):