| LEFT | RIGHT | 
|---|
| 1 # coding: utf-8 | 1 # coding: utf-8 | 
| 2 | 2 | 
| 3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, | 
| 4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH | 
| 5 # | 5 # | 
| 6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify | 
| 7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as | 
| 8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. | 
| 9 # | 9 # | 
| 10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, | 
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. | 
| 14 # | 14 # | 
| 15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License | 
| 16 # along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. | 
| 17 | 17 | 
| 18 import unittest, MySQLdb | 18 import unittest | 
| 19 from datetime import datetime |  | 
| 20 | 19 | 
|  | 20 import MySQLdb | 
|  | 21 | 
|  | 22 from sitescripts.filterhits.test import test_helpers | 
| 21 from sitescripts.filterhits import db | 23 from sitescripts.filterhits import db | 
| 22 | 24 | 
| 23 class DbTestCase(unittest.TestCase): | 25 class DbTestCase(test_helpers.FilterhitsTestCase): | 
| 24   longMessage = True | 26   longMessage = True | 
| 25   maxDiff = None | 27   maxDiff = None | 
| 26 | 28 | 
| 27   def clear_rows(self): |  | 
| 28     if self.db: |  | 
| 29       db.write(self.db, (("DELETE FROM filters",),)) |  | 
| 30 |  | 
| 31   def setUp(self): |  | 
| 32     try: |  | 
| 33       db.testing = True |  | 
| 34       self.db = db.connect() |  | 
| 35     except MySQLdb.Error: |  | 
| 36       self.db = None |  | 
| 37     self.clear_rows() |  | 
| 38 |  | 
| 39   def tearDown(self): |  | 
| 40     if self.db: |  | 
| 41       self.clear_rows() |  | 
| 42       self.db.close() |  | 
| 43       self.db = None |  | 
| 44 |  | 
| 45   def test_query_and_write(self): | 29   def test_query_and_write(self): | 
| 46     if not self.db: |  | 
| 47       raise unittest.SkipTest("Not connected to test DB.") |  | 
| 48 |  | 
| 49     insert_sql = """INSERT INTO `filters` (filter, sha1) | 30     insert_sql = """INSERT INTO `filters` (filter, sha1) | 
| 50                     VALUES (%s, UNHEX(SHA1(filter)))""" | 31                     VALUES (%s, UNHEX(SHA1(filter)))""" | 
| 51     select_sql = "SELECT filter FROM filters ORDER BY filter ASC" | 32     select_sql = "SELECT filter FROM filters ORDER BY filter ASC" | 
| 52 | 33 | 
| 53     # Table should be empty to start with | 34     # Table should be empty to start with | 
| 54     self.assertEqual(db.query(self.db, select_sql), ()) | 35     self.assertEqual(db.query(self.db, select_sql), ()) | 
| 55     # Write some data and query it back | 36     # Write some data and query it back | 
| 56     db.write(self.db, ((insert_sql, "something"),)) | 37     db.write(self.db, ((insert_sql, "something"),)) | 
| 57     self.assertEqual(db.query(self.db, select_sql), ((u"something",),)) | 38     self.assertEqual(db.query(self.db, select_sql), ((u"something",),)) | 
| 58     # Write an array of SQL strings | 39     # Write an array of SQL strings | 
| 59     db.write(self.db, ((insert_sql, "a"), (insert_sql, "b"), (insert_sql, "c"))) | 40     db.write(self.db, ((insert_sql, "a"), (insert_sql, "b"), (insert_sql, "c"))) | 
| 60     self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",), 
    (u"something",))) | 41     self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",), 
    (u"something",))) | 
| 61     # Write a sequence of SQL but roll back when a problem arrises | 42     # Write a sequence of SQL but roll back when a problem arrises | 
| 62     with self.assertRaises(MySQLdb.ProgrammingError): | 43     with self.assertRaises(MySQLdb.ProgrammingError): | 
| 63       db.write(self.db, ((insert_sql, "f"), (insert_sql, "g"), (insert_sql, "h")
    , | 44       db.write(self.db, ((insert_sql, "f"), (insert_sql, "g"), (insert_sql, "h")
    , | 
| 64                          ("GFDGks",))) | 45                          ("GFDGks",))) | 
| 65     self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",), 
    (u"something",))) | 46     self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",), 
    (u"something",))) | 
| 66 | 47 | 
| 67 if __name__ == '__main__': | 48 if __name__ == "__main__": | 
| 68   unittest.main() | 49   unittest.main() | 
| LEFT | RIGHT | 
|---|