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

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

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Left Patch Set: Created base class for tests and reverted earlier content-type change. Created April 1, 2015, 7:01 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/log_tests.py ('k') | sitescripts/filterhits/web/__init__.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 tempfile 18 import tempfile
19 import shutil 19 import shutil
20 import unittest 20 import unittest
21 21
22 import MySQLdb 22 import MySQLdb
23 23
24 from sitescripts.filterhits import db 24 from sitescripts.filterhits import db
25 from sitescripts.utils import get_config 25 from sitescripts.utils import get_config
26 26
27 class FilterhitsTestCase(unittest.TestCase): 27 class FilterhitsTestCase(unittest.TestCase):
28 config = get_config() 28 config = get_config()
29 _db = None
29 _live_config = { 30 _live_config = {
30 "dbuser": config.get("filterhitstats", "dbuser"), 31 "dbuser": config.get("filterhitstats", "dbuser"),
31 "dbpassword": config.get("filterhitstats", "dbpassword"), 32 "dbpassword": config.get("filterhitstats", "dbpassword"),
32 "database": config.get("filterhitstats", "database"), 33 "database": config.get("filterhitstats", "database"),
33 "log_dir": config.get("filterhitstats", "log_dir") 34 "log_dir": config.get("filterhitstats", "log_dir")
34 } 35 }
35 _test_config = { 36 _test_config = {
36 "dbuser": config.get("filterhitstats", "test_dbuser"), 37 "dbuser": config.get("filterhitstats", "test_dbuser"),
37 "dbpassword": config.get("filterhitstats", "test_dbpassword"), 38 "dbpassword": config.get("filterhitstats", "test_dbpassword"),
38 "database": config.get("filterhitstats", "test_database") 39 "database": config.get("filterhitstats", "test_database")
39 } 40 }
40 41
41 def _clear_database(self): 42 def _clear_database(self):
42 db.write(self.db, (("DELETE FROM frequencies",), ("DELETE FROM filters",))) 43 db.write(self._db, (("DELETE FROM frequencies",), ("DELETE FROM filters",)))
44
45 @property
46 def db(self):
47 if (not self._db):
48 self._db = db.connect()
49 self._clear_database()
50 return self._db
43 51
44 def setUp(self): 52 def setUp(self):
53 # Set up a temporary log directory for testing
54 self.test_dir = tempfile.mkdtemp()
45 # Set up test config 55 # Set up test config
Sebastian Noack 2015/04/02 07:37:04 There is a lot of boilerplate and duplication in t
kzar 2015/04/02 07:47:47 I prefer it as it is, although it doesn't matter m
Sebastian Noack 2015/04/02 08:11:44 We don't have to care about performance here. More
kzar 2015/04/02 10:16:54 Fair enough, well how about this? I've reduced dup
46 self.config.set("filterhitstats", "database", self._test_config["database"]) 56 for k, v in self._test_config.items():
47 self.config.set("filterhitstats", "dbuser", self._test_config["dbuser"]) 57 self.config.set("filterhitstats", k, v)
48 self.config.set("filterhitstats", "dbpassword", self._test_config["dbpasswor d"])
49 self.test_dir = tempfile.mkdtemp()
50 self.config.set("filterhitstats", "log_dir", self.test_dir) 58 self.config.set("filterhitstats", "log_dir", self.test_dir)
51 # Attempt to set up a clean test database
52 try:
53 self.db = db.connect()
54 self._clear_database()
55 except MySQLdb.Error:
56 self.db = None
Sebastian Noack 2015/04/02 07:37:04 Wouldn't it be better to fail here with the actual
kzar 2015/04/02 07:47:47 The tests that require database access first check
Sebastian Noack 2015/04/02 08:11:44 How about using a getter for the db property? @pr
kzar 2015/04/02 10:16:54 Done.
57 59
58 def tearDown(self): 60 def tearDown(self):
59 # Clean the database and close our connection 61 # Clean the database and close our connection
60 if self.db: 62 if self._db:
61 self._clear_database() 63 self._clear_database()
62 self.db.close() 64 self._db.close()
63 self.db = None 65 self._db = None
64 # Clean any generated logs 66 # Clean any generated logs
65 shutil.rmtree(self.test_dir, ignore_errors=True) 67 shutil.rmtree(self.test_dir, ignore_errors=True)
66 # Restore the configuration 68 # Restore the configuration
67 self.config.set("filterhitstats", "database", self._live_config["database"]) 69 for k, v in self._live_config.items():
68 self.config.set("filterhitstats", "dbuser", self._live_config["dbuser"]) 70 self.config.set("filterhitstats", k, v)
69 self.config.set("filterhitstats", "dbpassword", self._live_config["dbpasswor d"])
70 self.config.set("filterhitstats", "log_dir", self._live_config["log_dir"])
LEFTRIGHT

Powered by Google App Engine
This is Rietveld