Index: sitescripts/filterhits/test/common_tests.py |
diff --git a/sitescripts/filterhits/test/common_tests.py b/sitescripts/filterhits/test/common_tests.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fe408d5c01bb4a19845138b1d79bd69821033680 |
--- /dev/null |
+++ b/sitescripts/filterhits/test/common_tests.py |
@@ -0,0 +1,94 @@ |
+# coding: utf-8 |
Sebastian Noack
2015/02/11 16:00:12
I wonder whether we should also have a test runnin
Wladimir Palant
2015/02/17 15:12:21
We don't have to run a real server, calling the WS
kzar
2015/02/24 18:05:11
Yea, I agree a few tests for the API would be nice
kzar
2015/02/28 19:39:56
OK, I've written some now.
|
+ |
+# This file is part of the Adblock Plus web scripts, |
+# Copyright (C) 2006-2014 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 json, os, shutil, unittest |
+from datetime import datetime |
+import sitescripts.filterhits.common as common |
+ |
+valid_log_data = """{ |
+ "version": 1, |
+ "timeSincePush": 12345, |
+ "addonName": "adblockplus", |
+ "addonVersion": "2.3.4", |
+ "application": "firefox", |
+ "applicationVersion": "31", |
+ "platform": "gecko", |
+ "platformVersion": "31", |
+ "filters": { |
+ "||example.com^": { |
+ "firstParty": { |
+ "example.com": {"hits": 12, "latest": 1413368627000}, |
+ "example.org": {"hits": 4, "latest": 1413368627000} |
+ }, |
+ "thirdParty": { |
+ "example.com": {"hits": 5, "latest": 123455489000} |
+ }, |
+ "subscriptions": ["EasyList", "EasyList Germany+EasyList"] |
+ } |
+ } |
+}""" |
+ |
+class CommonTestCase(unittest.TestCase): |
+ longMessage = True |
+ maxDiff = None |
+ |
+ def setUp(self): |
+ self.test_dir = os.path.join(os.path.dirname(__file__), "temp") |
+ shutil.rmtree(self.test_dir, ignore_errors=True) |
+ |
+ def tearDown(self): |
+ shutil.rmtree(self.test_dir, ignore_errors=True) |
+ |
+ def test_datetime_to_timestamp(self): |
+ examples = [ |
+ [datetime(2014, 11, 27, 13, 58, 27), 1417096707], |
+ [datetime(2001, 1, 10, 13, 35, 21), 979133721] |
+ ] |
+ for dt, ts in examples: |
+ self.assertEqual(common.datetime_to_timestamp(dt), ts) |
+ |
+ def test_valid_log_data(self): |
+ data = json.loads(valid_log_data) |
+ self.assertEqual(common.valid_log_data(data), True) |
+ data.pop("version") |
+ self.assertEqual(common.valid_log_data(data), False) |
+ |
+ def test_log_filterhits(self): |
+ def list_files(d): |
+ return filter(os.path.isfile, [os.path.join(d, f) for f in os.listdir(d)]) |
+ |
+ todays_date = datetime.now().strftime("%Y-%m-%d") |
+ todays_folder = os.path.join(self.test_dir, todays_date) |
+ |
+ self.assertEqual(os.path.exists(self.test_dir), False) |
+ |
+ log_file = common.log_filterhits({"some": "thing"}, self.test_dir, "a=1") |
+ now = datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z') |
+ self.assertEqual(os.path.exists(self.test_dir), True) |
+ self.assertEqual(os.path.exists(todays_folder), True) |
+ self.assertEqual(len(list_files(todays_folder)), 1) |
+ self.assertEqual(os.path.exists(log_file), True) |
+ with open(list_files(todays_folder)[0], 'r') as f: |
+ self.assertEqual(f.read(), '[%s] "a=1" {"some": "thing"}\n' % now) |
+ |
+ common.log_filterhits({"some": "thing"}, self.test_dir, "") |
+ self.assertEqual(os.path.exists(self.test_dir), True) |
+ self.assertEqual(os.path.exists(todays_folder), True) |
+ self.assertEqual(len(list_files(todays_folder)), 2) |
+ |
+if __name__ == '__main__': |
+ unittest.main() |