From 1e5878bec35e84c206dde7cbd983bf7b7d52b5bb Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Mon, 21 Apr 2014 10:23:45 +0200 Subject: [PATCH] auto-correct wrong/invalid parent to max nesting level of 1 --- isso/db/comments.py | 8 ++++---- isso/tests/test_comments.py | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/isso/db/comments.py b/isso/db/comments.py index ef97eeb..8c97121 100644 --- a/isso/db/comments.py +++ b/isso/db/comments.py @@ -38,10 +38,10 @@ class Comments: database values. """ - if "parent" in c: - rv = self.db.execute("SELECT parent FROM comments WHERE id=?", (c.get('parent'), )).fetchone() - if rv and rv[0] is not None: - c["parent"] = None + if c.get("parent") is not None: + ref = self.get(c["parent"]) + if ref.get("parent") is not None: + c["parent"] = ref["parent"] self.db.execute([ 'INSERT INTO comments (', diff --git a/isso/tests/test_comments.py b/isso/tests/test_comments.py index fa46d92..5124743 100644 --- a/isso/tests/test_comments.py +++ b/isso/tests/test_comments.py @@ -105,6 +105,14 @@ class TestComments(unittest.TestCase): rv = loads(r.data) self.assertEqual(len(rv), 20) + def testCreateInvalidParent(self): + + self.post('/new?uri=test', data=json.dumps({'text': '...'})) + self.post('/new?uri=test', data=json.dumps({'text': '...', 'parent': 1})) + invalid = self.post('/new?uri=test', data=json.dumps({'text': '...', 'parent': 2})) + + self.assertEqual(loads(invalid.data)["parent"], 1) + def testVerifyFields(self): verify = lambda comment: comments.API.verify(comment)[0] @@ -176,19 +184,16 @@ class TestComments(unittest.TestCase): [ comment 1 ] | --- [ comment 2, ref 1 ] - | - --- [ comment 3, ref 2 ] - | - --- [ comment 4, ref 2 ] - [ comment 5 ] + | + --- [ comment 3, ref 1 ] + [ comment 4 ] """ client = JSONClient(self.app, Response) client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First'})) client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Second', 'parent': 1})) - client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Third 1', 'parent': 2})) - client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Third 2', 'parent': 2})) - client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': '...'})) + client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Third', 'parent': 1})) + client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Last'})) client.delete('/id/1') self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200) @@ -197,8 +202,6 @@ class TestComments(unittest.TestCase): client.delete('/id/3') self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200) client.delete('/id/4') - self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200) - client.delete('/id/5') self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 404) def testPathVariations(self):