Index: sitescripts/filterhits/test/test_helpers.py |
diff --git a/sitescripts/filterhits/test/test_helpers.py b/sitescripts/filterhits/test/test_helpers.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8e56b77d8574e783bad15731966864d5116b8b67 |
--- /dev/null |
+++ b/sitescripts/filterhits/test/test_helpers.py |
@@ -0,0 +1,70 @@ |
+# coding: utf-8 |
+ |
+# This file is part of the Adblock Plus web scripts, |
+# Copyright (C) 2006-2015 Eyeo GmbH |
+# |
+# Adblock Plus is free software: you can redistribute it and/or modify |
+# it under the terms of the GNU General Public License version 3 as |
+# published by the Free Software Foundation. |
+# |
+# Adblock Plus is distributed in the hope that it will be useful, |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+# GNU General Public License for more details. |
+# |
+# You should have received a copy of the GNU General Public License |
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ |
+import tempfile |
+import shutil |
+import unittest |
+ |
+import MySQLdb |
+ |
+from sitescripts.filterhits import db |
+from sitescripts.utils import get_config |
+ |
+class FilterhitsTestCase(unittest.TestCase): |
+ config = get_config() |
+ _live_config = { |
+ "dbuser": config.get("filterhitstats", "dbuser"), |
+ "dbpassword": config.get("filterhitstats", "dbpassword"), |
+ "database": config.get("filterhitstats", "database"), |
+ "log_dir": config.get("filterhitstats", "log_dir") |
+ } |
+ _test_config = { |
+ "dbuser": config.get("filterhitstats", "test_dbuser"), |
+ "dbpassword": config.get("filterhitstats", "test_dbpassword"), |
+ "database": config.get("filterhitstats", "test_database") |
+ } |
+ |
+ def _clear_database(self): |
+ db.write(self.db, (("DELETE FROM frequencies",), ("DELETE FROM filters",))) |
+ |
+ def setUp(self): |
+ # 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
|
+ self.config.set("filterhitstats", "database", self._test_config["database"]) |
+ self.config.set("filterhitstats", "dbuser", self._test_config["dbuser"]) |
+ self.config.set("filterhitstats", "dbpassword", self._test_config["dbpassword"]) |
+ self.test_dir = tempfile.mkdtemp() |
+ self.config.set("filterhitstats", "log_dir", self.test_dir) |
+ # Attempt to set up a clean test database |
+ try: |
+ self.db = db.connect() |
+ self._clear_database() |
+ except MySQLdb.Error: |
+ 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.
|
+ |
+ def tearDown(self): |
+ # Clean the database and close our connection |
+ if self.db: |
+ self._clear_database() |
+ self.db.close() |
+ self.db = None |
+ # Clean any generated logs |
+ shutil.rmtree(self.test_dir, ignore_errors=True) |
+ # Restore the configuration |
+ self.config.set("filterhitstats", "database", self._live_config["database"]) |
+ self.config.set("filterhitstats", "dbuser", self._live_config["dbuser"]) |
+ self.config.set("filterhitstats", "dbpassword", self._live_config["dbpassword"]) |
+ self.config.set("filterhitstats", "log_dir", self._live_config["log_dir"]) |