auto-correct wrong/invalid parent to max nesting level of 1

This commit is contained in:
Martin Zimmermann 2014-04-21 10:23:45 +02:00
parent ce14726f2f
commit 1e5878bec3
2 changed files with 17 additions and 14 deletions

View File

@ -38,10 +38,10 @@ class Comments:
database values. database values.
""" """
if "parent" in c: if c.get("parent") is not None:
rv = self.db.execute("SELECT parent FROM comments WHERE id=?", (c.get('parent'), )).fetchone() ref = self.get(c["parent"])
if rv and rv[0] is not None: if ref.get("parent") is not None:
c["parent"] = None c["parent"] = ref["parent"]
self.db.execute([ self.db.execute([
'INSERT INTO comments (', 'INSERT INTO comments (',

View File

@ -105,6 +105,14 @@ class TestComments(unittest.TestCase):
rv = loads(r.data) rv = loads(r.data)
self.assertEqual(len(rv), 20) 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): def testVerifyFields(self):
verify = lambda comment: comments.API.verify(comment)[0] verify = lambda comment: comments.API.verify(comment)[0]
@ -176,19 +184,16 @@ class TestComments(unittest.TestCase):
[ comment 1 ] [ comment 1 ]
| |
--- [ comment 2, ref 1 ] --- [ comment 2, ref 1 ]
| |
--- [ comment 3, ref 2 ] --- [ comment 3, ref 1 ]
| [ comment 4 ]
--- [ comment 4, ref 2 ]
[ comment 5 ]
""" """
client = JSONClient(self.app, Response) 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': 'First'}))
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Second', 'parent': 1})) 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', 'parent': 1}))
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Third 2', 'parent': 2})) client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Last'}))
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': '...'}))
client.delete('/id/1') client.delete('/id/1')
self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200) self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200)
@ -197,8 +202,6 @@ class TestComments(unittest.TestCase):
client.delete('/id/3') client.delete('/id/3')
self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200) self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200)
client.delete('/id/4') 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) self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 404)
def testPathVariations(self): def testPathVariations(self):