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 |
| 19 import MySQLdb |
| 20 from datetime import datetime |
| 21 |
| 22 from sitescripts.filterhits import db |
| 23 |
| 24 class DbTestCase(unittest.TestCase): |
| 25 longMessage = True |
| 26 maxDiff = None |
| 27 |
| 28 def clear_rows(self): |
| 29 if self.db: |
| 30 db.write(self.db, (("DELETE FROM filters",),)) |
| 31 |
| 32 def setUp(self): |
| 33 try: |
| 34 db.testing = True |
| 35 self.db = db.connect() |
| 36 except MySQLdb.Error: |
| 37 self.db = None |
| 38 self.clear_rows() |
| 39 |
| 40 def tearDown(self): |
| 41 if self.db: |
| 42 self.clear_rows() |
| 43 self.db.close() |
| 44 self.db = None |
| 45 |
| 46 def test_query_and_write(self): |
| 47 if not self.db: |
| 48 raise unittest.SkipTest("Not connected to test DB.") |
| 49 |
| 50 insert_sql = """INSERT INTO `filters` (filter, sha1) |
| 51 VALUES (%s, UNHEX(SHA1(filter)))""" |
| 52 select_sql = "SELECT filter FROM filters ORDER BY filter ASC" |
| 53 |
| 54 # Table should be empty to start with |
| 55 self.assertEqual(db.query(self.db, select_sql), ()) |
| 56 # Write some data and query it back |
| 57 db.write(self.db, ((insert_sql, "something"),)) |
| 58 self.assertEqual(db.query(self.db, select_sql), ((u"something",),)) |
| 59 # Write an array of SQL strings |
| 60 db.write(self.db, ((insert_sql, "a"), (insert_sql, "b"), (insert_sql, "c"))) |
| 61 self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",),
(u"something",))) |
| 62 # Write a sequence of SQL but roll back when a problem arrises |
| 63 with self.assertRaises(MySQLdb.ProgrammingError): |
| 64 db.write(self.db, ((insert_sql, "f"), (insert_sql, "g"), (insert_sql, "h")
, |
| 65 ("GFDGks",))) |
| 66 self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",),
(u"something",))) |
| 67 |
| 68 if __name__ == '__main__': |
| 69 unittest.main() |
OLD | NEW |