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 json | |
19 import os | 18 import os |
20 import shutil | 19 import shutil |
21 import time | 20 import time |
22 import unittest | 21 import unittest |
| 22 |
| 23 from sitescripts.filterhits.test import test_helpers |
23 from sitescripts.filterhits.web import submit | 24 from sitescripts.filterhits.web import submit |
24 | 25 |
25 class LogTestCase(unittest.TestCase): | 26 class LogTestCase(test_helpers.FilterhitsTestCase): |
26 longMessage = True | |
27 maxDiff = None | |
28 | |
29 def setUp(self): | |
30 self.test_dir = os.path.join(os.path.dirname(__file__), "temp") | |
31 shutil.rmtree(self.test_dir, ignore_errors=True) | |
32 | |
33 def tearDown(self): | |
34 shutil.rmtree(self.test_dir, ignore_errors=True) | |
35 | |
36 def test_log_filterhits(self): | 27 def test_log_filterhits(self): |
37 def list_files(d): | 28 def list_files(d): |
38 return filter(os.path.isfile, [os.path.join(d, f) for f in os.listdir(d)]) | 29 return filter(os.path.isfile, [os.path.join(d, f) for f in os.listdir(d)]) |
39 | 30 |
40 todays_date = time.strftime('%Y-%m-%d', time.gmtime()) | 31 todays_date = time.strftime("%Y-%m-%d", time.gmtime()) |
41 todays_folder = os.path.join(self.test_dir, todays_date) | 32 todays_folder = os.path.join(self.test_dir, todays_date) |
42 | 33 |
| 34 # The temporary logging directory is created at the start of all tests but |
| 35 # we want to test that the directory is created if it doesn't already exist. |
| 36 # So we'll delete the directory here and make sure it's re-created later on. |
| 37 shutil.rmtree(self.test_dir) |
43 self.assertEqual(os.path.exists(self.test_dir), False) | 38 self.assertEqual(os.path.exists(self.test_dir), False) |
44 | 39 |
45 log_file = submit.log_filterhits({"some": "thing"}, self.test_dir, "a=1") | 40 log_file = submit.log_filterhits({"some": "thing"}, self.test_dir, "a=1") |
46 now = time.strftime('%d/%b/%Y:%H:%M:%S', time.gmtime()) | 41 now = time.strftime("%d/%b/%Y:%H:%M:%S", time.gmtime()) |
47 self.assertEqual(os.path.exists(self.test_dir), True) | 42 self.assertEqual(os.path.exists(self.test_dir), True) |
48 self.assertEqual(os.path.exists(todays_folder), True) | 43 self.assertEqual(os.path.exists(todays_folder), True) |
49 self.assertEqual(len(list_files(todays_folder)), 1) | 44 self.assertEqual(len(list_files(todays_folder)), 1) |
50 self.assertEqual(os.path.exists(log_file), True) | 45 self.assertEqual(os.path.exists(log_file), True) |
51 with open(list_files(todays_folder)[0], 'r') as f: | 46 with open(list_files(todays_folder)[0], "r") as f: |
52 self.assertEqual(f.read(), '[%s] "a=1" {"some": "thing"}\n' % now) | 47 self.assertEqual(f.read(), '[%s] a=1\n{"some": "thing"}' % now) |
53 | 48 |
54 submit.log_filterhits({"some": "thing"}, self.test_dir, "") | 49 submit.log_filterhits({"some": "thing"}, self.test_dir, "") |
55 self.assertEqual(os.path.exists(self.test_dir), True) | 50 self.assertEqual(os.path.exists(self.test_dir), True) |
56 self.assertEqual(os.path.exists(todays_folder), True) | 51 self.assertEqual(os.path.exists(todays_folder), True) |
57 self.assertEqual(len(list_files(todays_folder)), 2) | 52 self.assertEqual(len(list_files(todays_folder)), 2) |
58 | 53 |
59 if __name__ == '__main__': | 54 if __name__ == "__main__": |
60 unittest.main() | 55 unittest.main() |
LEFT | RIGHT |