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