Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/filterhits/test/common_tests.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Addressed more comments. Created March 2, 2015, 10:37 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/filterhits/test/api_tests.py ('k') | sitescripts/filterhits/test/db_tests.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 19 from sitescripts.filterhits import common
20 import sitescripts.filterhits.common as common
21 20
22 valid_log_data = """{ 21 class CommonTestCase(unittest.TestCase):
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": 1413368627},
35 "example.org": {"hits": 4, "latest": 1413368627}
36 },
37 "thirdParty": {
38 "example.com": {"hits": 5, "latest": 123455489}
39 },
40 "subscriptions": ["EasyList", "EasyList Germany+EasyList"]
41 }
42 }
43 }"""
44
45 class Test(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 common.log_filterhits({"some": "thing"}, self.test_dir) 41 log_file = common.log_filterhits({"some": "thing"}, self.test_dir, "a=1")
42 now = time.strftime('%d/%b/%Y:%H:%M:%S', time.gmtime())
80 self.assertEqual(os.path.exists(self.test_dir), True) 43 self.assertEqual(os.path.exists(self.test_dir), True)
81 self.assertEqual(os.path.exists(todays_folder), True) 44 self.assertEqual(os.path.exists(todays_folder), True)
82 self.assertEqual(len(list_files(todays_folder)), 1) 45 self.assertEqual(len(list_files(todays_folder)), 1)
46 self.assertEqual(os.path.exists(log_file), True)
83 with open(list_files(todays_folder)[0], 'r') as f: 47 with open(list_files(todays_folder)[0], 'r') as f:
84 self.assertEqual(f.read(), '{"some": "thing"}\n') 48 self.assertEqual(f.read(), '[%s] "a=1" {"some": "thing"}\n' % now)
85 49
86 common.log_filterhits({"some": "thing"}, self.test_dir) 50 common.log_filterhits({"some": "thing"}, self.test_dir, "")
87 self.assertEqual(os.path.exists(self.test_dir), True) 51 self.assertEqual(os.path.exists(self.test_dir), True)
88 self.assertEqual(os.path.exists(todays_folder), True) 52 self.assertEqual(os.path.exists(todays_folder), True)
89 self.assertEqual(len(list_files(todays_folder)), 2) 53 self.assertEqual(len(list_files(todays_folder)), 2)
90 54
91 if __name__ == '__main__': 55 if __name__ == '__main__':
92 unittest.main() 56 unittest.main()
OLDNEW
« no previous file with comments | « sitescripts/filterhits/test/api_tests.py ('k') | sitescripts/filterhits/test/db_tests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld