| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # 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.
| |
| 2 | |
| 3 # This file is part of the Adblock Plus web scripts, | |
| 4 # Copyright (C) 2006-2014 Eyeo GmbH | |
| 5 # | |
| 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 | |
| 8 # published by the Free Software Foundation. | |
| 9 # | |
| 10 # Adblock Plus is distributed in the hope that it will be useful, | |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 # GNU General Public License for more details. | |
| 14 # | |
| 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/>. | |
| 17 | |
| 18 import json, os, shutil, unittest | |
| 19 from datetime import datetime | |
| 20 import sitescripts.filterhits.common as common | |
| 21 | |
| 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): | |
| 46 longMessage = True | |
| 47 maxDiff = None | |
| 48 | |
| 49 def setUp(self): | |
| 50 self.test_dir = os.path.join(os.path.dirname(__file__), "temp") | |
| 51 shutil.rmtree(self.test_dir, ignore_errors=True) | |
| 52 | |
| 53 def tearDown(self): | |
| 54 shutil.rmtree(self.test_dir, ignore_errors=True) | |
| 55 | |
| 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): | |
| 71 def list_files(d): | |
| 72 return filter(os.path.isfile, [os.path.join(d, f) for f in os.listdir(d)]) | |
| 73 | |
| 74 todays_date = datetime.now().strftime("%Y-%m-%d") | |
| 75 todays_folder = os.path.join(self.test_dir, todays_date) | |
| 76 | |
| 77 self.assertEqual(os.path.exists(self.test_dir), False) | |
| 78 | |
| 79 log_file = common.log_filterhits({"some": "thing"}, self.test_dir, "a=1") | |
| 80 now = datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z') | |
| 81 self.assertEqual(os.path.exists(self.test_dir), True) | |
| 82 self.assertEqual(os.path.exists(todays_folder), True) | |
| 83 self.assertEqual(len(list_files(todays_folder)), 1) | |
| 84 self.assertEqual(os.path.exists(log_file), True) | |
| 85 with open(list_files(todays_folder)[0], 'r') as f: | |
| 86 self.assertEqual(f.read(), '[%s] "a=1" {"some": "thing"}\n' % now) | |
| 87 | |
| 88 common.log_filterhits({"some": "thing"}, self.test_dir, "") | |
| 89 self.assertEqual(os.path.exists(self.test_dir), True) | |
| 90 self.assertEqual(os.path.exists(todays_folder), True) | |
| 91 self.assertEqual(len(list_files(todays_folder)), 2) | |
| 92 | |
| 93 if __name__ == '__main__': | |
| 94 unittest.main() | |
| OLD | NEW |