| 5 | |
| 6 | class TestDbQuery: |
| 7 | def testParse(self): |
| 8 | query_text = """ |
| 9 | SELECT |
| 10 | 'comment' AS type, |
| 11 | date_added, post.title AS title, |
| 12 | keyvalue.value || ': ' || comment.body AS body, |
| 13 | '?Post:' || comment.post_id || '#Comments' AS url |
| 14 | FROM |
| 15 | comment |
| 16 | LEFT JOIN json USING (json_id) |
| 17 | LEFT JOIN json AS json_content ON (json_content.directory = json.directory AND json_content.file_name='content.json') |
| 18 | LEFT JOIN keyvalue ON (keyvalue.json_id = json_content.json_id AND key = 'cert_user_id') |
| 19 | LEFT JOIN post ON (comment.post_id = post.post_id) |
| 20 | WHERE |
| 21 | post.date_added > 123 |
| 22 | ORDER BY |
| 23 | date_added DESC |
| 24 | LIMIT 20 |
| 25 | """ |
| 26 | query = DbQuery(query_text) |
| 27 | assert query.parts["LIMIT"] == "20" |
| 28 | assert query.fields["body"] == "keyvalue.value || ': ' || comment.body" |
| 29 | assert re.sub("[ \r\n]", "", str(query)) == re.sub("[ \r\n]", "", query_text) |
| 30 | query.wheres.append("body LIKE '%hello%'") |
| 31 | assert "body LIKE '%hello%'" in str(query) |