Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/filterhits/test/db_tests.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Improvements regarding comments Created Feb. 17, 2015, 10:50 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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-2014 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, MySQLdb
19 from datetime import datetime 19 from datetime import datetime
20 from ConfigParser import NoOptionError 20 from ConfigParser import NoOptionError
21 21
22 from sitescripts.utils import get_config 22 from sitescripts.utils import get_config
23 import sitescripts.filterhits.db as db 23 import sitescripts.filterhits.db as db
24 24
25 class DbTestCase(unittest.TestCase): 25 class DbTestCase(unittest.TestCase):
26 longMessage = True 26 longMessage = True
27 maxDiff = None 27 maxDiff = None
28 28
29 def clear_rows(self): 29 def clear_rows(self):
30 if self.db: 30 if self.db:
31 db.write("DELETE FROM filters;") 31 db.write(self.db, "DELETE FROM filters")
32 32
33 def setUp(self): 33 def setUp(self):
34 db.disconnect()
35 try: 34 try:
36 self.config = get_config() 35 self.config = get_config()
37 self.db = db.connect( 36 self.db = db.connect(
38 self.config.get("filterhitstats", "dbuser"), 37 self.config.get("filterhitstats", "dbuser"),
39 self.config.get("filterhitstats", "dbpassword"), 38 self.config.get("filterhitstats", "dbpassword"),
40 self.config.get("filterhitstats", "test_database")) 39 self.config.get("filterhitstats", "test_database"))
41 except (MySQLdb.Error, NoOptionError): 40 except (MySQLdb.Error, NoOptionError):
42 self.db = None 41 self.db = None
43 self.clear_rows() 42 self.clear_rows()
44 43
45 def tearDown(self): 44 def tearDown(self):
46 self.clear_rows() 45 if self.db:
47 db.disconnect() 46 self.clear_rows()
47 self.db.close()
48 self.db = None
48 49
49 def test_query_and_write(self): 50 def test_query_and_write(self):
50 if not self.db: 51 if not self.db:
51 raise unittest.SkipTest("Not connected to test DB.") 52 raise unittest.SkipTest("Not connected to test DB.")
52 53
53 insert_sql = """INSERT INTO `filters` (filter, md5) 54 insert_sql = """INSERT INTO `filters` (filter, sha1)
54 VALUES ('%s', UNHEX(MD5(filter)));""" 55 VALUES (%s, UNHEX(SHA1(filter)))"""
55 select_sql = "SELECT filter FROM filters ORDER BY filter ASC;" 56 select_sql = "SELECT filter FROM filters ORDER BY filter ASC"
56 57
57 # Table should be empty to start with 58 # Table should be empty to start with
58 self.assertEqual(db.query(select_sql), ()) 59 self.assertEqual(db.query(self.db, select_sql), ())
59 # Write some data and query it back 60 # Write some data and query it back
60 db.write(insert_sql % "something") 61 db.write(self.db, ((insert_sql, "something"),))
61 self.assertEqual(db.query(select_sql), ((u"something",),)) 62 self.assertEqual(db.query(self.db, select_sql), ((u"something",),))
62 # Write an array of SQL strings 63 # Write an array of SQL strings
63 db.write([insert_sql % "a", insert_sql % "b", insert_sql % "c"]) 64 db.write(self.db, ((insert_sql, "a"), (insert_sql, "b"), (insert_sql, "c")))
64 self.assertEqual(db.query(select_sql), ((u"a",), (u"b",), (u"c",), (u"someth ing",))) 65 self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",), (u"something",)))
65 # Write an array of concatinated SQL strings
66 db.write([insert_sql % "d" + insert_sql % "e"])
67 self.assertEqual(db.query(select_sql), ((u"a",), (u"b",), (u"c",), (u"d",), (u"e",),
68 (u"something",)))
69 # Write a sequence of SQL but roll back when a problem arrises 66 # Write a sequence of SQL but roll back when a problem arrises
70 with self.assertRaises(MySQLdb.ProgrammingError): 67 with self.assertRaises(MySQLdb.ProgrammingError):
71 db.write([insert_sql % "f" + insert_sql % "g", insert_sql % "h", "GFDGks"] ) 68 db.write(self.db, ((insert_sql, "f"), (insert_sql, "g"), (insert_sql, "h") ,
72 self.assertEqual(db.query(select_sql), ((u"a",), (u"b",), (u"c",), (u"d",), (u"e",), 69 ("GFDGks",)))
73 (u"something",))) 70 self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",), (u"something",)))
74 71
75 if __name__ == '__main__': 72 if __name__ == '__main__':
76 unittest.main() 73 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld