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