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

Delta Between Two Patch Sets: sitescripts/filterhits/test/db_tests.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Left Patch Set: Addressed comments. Created Feb. 24, 2015, 5:59 p.m.
Right Patch Set: Addressed further comments from Sebastian. Created April 2, 2015, 10:13 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/filterhits/test/api_tests.py ('k') | sitescripts/filterhits/test/geometrical_mean_tests.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 import unittest, MySQLdb 18 import unittest
19 from datetime import datetime
20 from ConfigParser import NoOptionError
21 19
22 from sitescripts.utils import get_config 20 import MySQLdb
23 import sitescripts.filterhits.db as db
24 21
25 class DbTestCase(unittest.TestCase): 22 from sitescripts.filterhits.test import test_helpers
23 from sitescripts.filterhits import db
24
25 class DbTestCase(test_helpers.FilterhitsTestCase):
26 longMessage = True 26 longMessage = True
27 maxDiff = None 27 maxDiff = None
28 28
29 def clear_rows(self):
30 if self.db:
31 db.write(self.db, "DELETE FROM filters")
32
33 def setUp(self):
34 try:
35 self.config = get_config()
36 self.db = db.connect(
37 self.config.get("filterhitstats", "dbuser"),
38 self.config.get("filterhitstats", "dbpassword"),
39 self.config.get("filterhitstats", "test_database"))
40 except (MySQLdb.Error, NoOptionError):
41 self.db = None
42 self.clear_rows()
43
44 def tearDown(self):
45 if self.db:
46 self.clear_rows()
47 self.db.close()
48 self.db = None
49
50 def test_query_and_write(self): 29 def test_query_and_write(self):
51 if not self.db:
52 raise unittest.SkipTest("Not connected to test DB.")
53
54 insert_sql = """INSERT INTO `filters` (filter, sha1) 30 insert_sql = """INSERT INTO `filters` (filter, sha1)
55 VALUES (%s, UNHEX(SHA1(filter)))""" 31 VALUES (%s, UNHEX(SHA1(filter)))"""
56 select_sql = "SELECT filter FROM filters ORDER BY filter ASC" 32 select_sql = "SELECT filter FROM filters ORDER BY filter ASC"
57 33
58 # Table should be empty to start with 34 # Table should be empty to start with
59 self.assertEqual(db.query(self.db, select_sql), ()) 35 self.assertEqual(db.query(self.db, select_sql), ())
60 # Write some data and query it back 36 # Write some data and query it back
61 db.write(self.db, ((insert_sql, "something"),)) 37 db.write(self.db, ((insert_sql, "something"),))
62 self.assertEqual(db.query(self.db, select_sql), ((u"something",),)) 38 self.assertEqual(db.query(self.db, select_sql), ((u"something",),))
63 # Write an array of SQL strings 39 # Write an array of SQL strings
64 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")))
65 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",)))
66 # 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
67 with self.assertRaises(MySQLdb.ProgrammingError): 43 with self.assertRaises(MySQLdb.ProgrammingError):
68 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") ,
69 ("GFDGks",))) 45 ("GFDGks",)))
70 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",)))
71 47
72 if __name__ == '__main__': 48 if __name__ == "__main__":
73 unittest.main() 49 unittest.main()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld