OLD | NEW |
| (Empty) |
1 # coding: utf-8 | |
2 | |
3 # This file is part of the Adblock Plus web scripts, | |
4 # Copyright (C) 2006-2016 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 tempfile | |
19 import shutil | |
20 import unittest | |
21 | |
22 import MySQLdb | |
23 | |
24 from sitescripts.filterhits import db | |
25 from sitescripts.utils import get_config | |
26 | |
27 | |
28 class FilterhitsTestCase(unittest.TestCase): | |
29 config = get_config() | |
30 _db = None | |
31 _live_config = { | |
32 "dbuser": config.get("filterhitstats", "dbuser"), | |
33 "dbpassword": config.get("filterhitstats", "dbpassword"), | |
34 "database": config.get("filterhitstats", "database"), | |
35 "log_dir": config.get("filterhitstats", "log_dir") | |
36 } | |
37 _test_config = { | |
38 "dbuser": config.get("filterhitstats", "test_dbuser"), | |
39 "dbpassword": config.get("filterhitstats", "test_dbpassword"), | |
40 "database": config.get("filterhitstats", "test_database") | |
41 } | |
42 | |
43 def _clear_database(self): | |
44 db.write(self._db, (("DELETE FROM frequencies",), ("DELETE FROM filters"
,))) | |
45 | |
46 @property | |
47 def db(self): | |
48 if (not self._db): | |
49 self._db = db.connect() | |
50 self._clear_database() | |
51 return self._db | |
52 | |
53 def setUp(self): | |
54 # Set up a temporary log directory for testing | |
55 self.test_dir = tempfile.mkdtemp() | |
56 # Set up test config | |
57 for k, v in self._test_config.items(): | |
58 self.config.set("filterhitstats", k, v) | |
59 self.config.set("filterhitstats", "log_dir", self.test_dir) | |
60 | |
61 def tearDown(self): | |
62 # Clean the database and close our connection | |
63 if self._db: | |
64 self._clear_database() | |
65 self._db.close() | |
66 self._db = None | |
67 # Clean any generated logs | |
68 shutil.rmtree(self.test_dir, ignore_errors=True) | |
69 # Restore the configuration | |
70 for k, v in self._live_config.items(): | |
71 self.config.set("filterhitstats", k, v) | |
OLD | NEW |