OLD | NEW |
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-2014 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 json, os, shutil, unittest | 18 import json, os, shutil, time, unittest |
19 from datetime import datetime | |
20 import sitescripts.filterhits.common as common | 19 import sitescripts.filterhits.common as common |
21 | 20 |
22 valid_log_data = """{ | |
23 "version": 1, | |
24 "timeSincePush": 12345, | |
25 "addonName": "adblockplus", | |
26 "addonVersion": "2.3.4", | |
27 "application": "firefox", | |
28 "applicationVersion": "31", | |
29 "platform": "gecko", | |
30 "platformVersion": "31", | |
31 "filters": { | |
32 "||example.com^": { | |
33 "firstParty": { | |
34 "example.com": {"hits": 12, "latest": 1413368627000}, | |
35 "example.org": {"hits": 4, "latest": 1413368627000} | |
36 }, | |
37 "thirdParty": { | |
38 "example.com": {"hits": 5, "latest": 123455489000} | |
39 }, | |
40 "subscriptions": ["EasyList", "EasyList Germany+EasyList"] | |
41 } | |
42 } | |
43 }""" | |
44 | |
45 class CommonTestCase(unittest.TestCase): | 21 class CommonTestCase(unittest.TestCase): |
46 longMessage = True | 22 longMessage = True |
47 maxDiff = None | 23 maxDiff = None |
48 | 24 |
49 def setUp(self): | 25 def setUp(self): |
50 self.test_dir = os.path.join(os.path.dirname(__file__), "temp") | 26 self.test_dir = os.path.join(os.path.dirname(__file__), "temp") |
51 shutil.rmtree(self.test_dir, ignore_errors=True) | 27 shutil.rmtree(self.test_dir, ignore_errors=True) |
52 | 28 |
53 def tearDown(self): | 29 def tearDown(self): |
54 shutil.rmtree(self.test_dir, ignore_errors=True) | 30 shutil.rmtree(self.test_dir, ignore_errors=True) |
55 | 31 |
56 def test_datetime_to_timestamp(self): | |
57 examples = [ | |
58 [datetime(2014, 11, 27, 13, 58, 27), 1417096707], | |
59 [datetime(2001, 1, 10, 13, 35, 21), 979133721] | |
60 ] | |
61 for dt, ts in examples: | |
62 self.assertEqual(common.datetime_to_timestamp(dt), ts) | |
63 | |
64 def test_valid_log_data(self): | |
65 data = json.loads(valid_log_data) | |
66 self.assertEqual(common.valid_log_data(data), True) | |
67 data.pop("version") | |
68 self.assertEqual(common.valid_log_data(data), False) | |
69 | |
70 def test_log_filterhits(self): | 32 def test_log_filterhits(self): |
71 def list_files(d): | 33 def list_files(d): |
72 return filter(os.path.isfile, [os.path.join(d, f) for f in os.listdir(d)]) | 34 return filter(os.path.isfile, [os.path.join(d, f) for f in os.listdir(d)]) |
73 | 35 |
74 todays_date = datetime.now().strftime("%Y-%m-%d") | 36 todays_date = time.strftime('%Y-%m-%d', time.gmtime()) |
75 todays_folder = os.path.join(self.test_dir, todays_date) | 37 todays_folder = os.path.join(self.test_dir, todays_date) |
76 | 38 |
77 self.assertEqual(os.path.exists(self.test_dir), False) | 39 self.assertEqual(os.path.exists(self.test_dir), False) |
78 | 40 |
79 log_file = common.log_filterhits({"some": "thing"}, self.test_dir, "a=1") | 41 log_file = common.log_filterhits({"some": "thing"}, self.test_dir, "a=1") |
80 now = datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z') | 42 now = time.strftime('%d/%b/%Y:%H:%M:%S', time.gmtime()) |
81 self.assertEqual(os.path.exists(self.test_dir), True) | 43 self.assertEqual(os.path.exists(self.test_dir), True) |
82 self.assertEqual(os.path.exists(todays_folder), True) | 44 self.assertEqual(os.path.exists(todays_folder), True) |
83 self.assertEqual(len(list_files(todays_folder)), 1) | 45 self.assertEqual(len(list_files(todays_folder)), 1) |
84 self.assertEqual(os.path.exists(log_file), True) | 46 self.assertEqual(os.path.exists(log_file), True) |
85 with open(list_files(todays_folder)[0], 'r') as f: | 47 with open(list_files(todays_folder)[0], 'r') as f: |
86 self.assertEqual(f.read(), '[%s] "a=1" {"some": "thing"}\n' % now) | 48 self.assertEqual(f.read(), '[%s] "a=1" {"some": "thing"}\n' % now) |
87 | 49 |
88 common.log_filterhits({"some": "thing"}, self.test_dir, "") | 50 common.log_filterhits({"some": "thing"}, self.test_dir, "") |
89 self.assertEqual(os.path.exists(self.test_dir), True) | 51 self.assertEqual(os.path.exists(self.test_dir), True) |
90 self.assertEqual(os.path.exists(todays_folder), True) | 52 self.assertEqual(os.path.exists(todays_folder), True) |
91 self.assertEqual(len(list_files(todays_folder)), 2) | 53 self.assertEqual(len(list_files(todays_folder)), 2) |
92 | 54 |
93 if __name__ == '__main__': | 55 if __name__ == '__main__': |
94 unittest.main() | 56 unittest.main() |
OLD | NEW |