OLD | NEW |
(Empty) | |
| 1 # coding: utf-8 |
| 2 |
| 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH |
| 5 # |
| 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 |
| 8 # published by the Free Software Foundation. |
| 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. |
| 14 # |
| 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/>. |
| 17 |
| 18 import unittest, MySQLdb |
| 19 from datetime import datetime |
| 20 |
| 21 import sitescripts.filterhits.db as db |
| 22 |
| 23 class DbTestCase(unittest.TestCase): |
| 24 longMessage = True |
| 25 maxDiff = None |
| 26 |
| 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): |
| 46 if not self.db: |
| 47 raise unittest.SkipTest("Not connected to test DB.") |
| 48 |
| 49 insert_sql = """INSERT INTO `filters` (filter, sha1) |
| 50 VALUES (%s, UNHEX(SHA1(filter)))""" |
| 51 select_sql = "SELECT filter FROM filters ORDER BY filter ASC" |
| 52 |
| 53 # Table should be empty to start with |
| 54 self.assertEqual(db.query(self.db, select_sql), ()) |
| 55 # Write some data and query it back |
| 56 db.write(self.db, ((insert_sql, "something"),)) |
| 57 self.assertEqual(db.query(self.db, select_sql), ((u"something",),)) |
| 58 # Write an array of SQL strings |
| 59 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",))) |
| 61 # Write a sequence of SQL but roll back when a problem arrises |
| 62 with self.assertRaises(MySQLdb.ProgrammingError): |
| 63 db.write(self.db, ((insert_sql, "f"), (insert_sql, "g"), (insert_sql, "h")
, |
| 64 ("GFDGks",))) |
| 65 self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",),
(u"something",))) |
| 66 |
| 67 if __name__ == '__main__': |
| 68 unittest.main() |
OLD | NEW |