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