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 unittest | 18 import unittest |
19 from datetime import datetime | 19 from datetime import datetime |
20 | |
21 import MySQLdb | |
22 | 20 |
23 from sitescripts.filterhits.test import test_helpers | 21 from sitescripts.filterhits.test import test_helpers |
24 from sitescripts.filterhits import db, geometrical_mean | 22 from sitescripts.filterhits import db, geometrical_mean |
25 | 23 |
26 test_data = [{ | 24 test_data = [{ |
27 "filters": { | 25 "filters": { |
28 "##.top-box-right-ad": { | 26 "##.top-box-right-ad": { |
29 "firstParty": { | 27 "firstParty": { |
30 "acxiom-online.com": { "hits": 6, "latest": 1414817340948 }, | 28 "acxiom-online.com": { "hits": 6, "latest": 1414817340948 }, |
31 "google.com": { "hits": 50, "latest": 1414849084678 }, | 29 "google.com": { "hits": 50, "latest": 1414849084678 }, |
(...skipping 30 matching lines...) Expand all Loading... |
62 }, | 60 }, |
63 "stevedeace.com##.topAddHolder": { | 61 "stevedeace.com##.topAddHolder": { |
64 "firstParty": { | 62 "firstParty": { |
65 "mathtag.com": { "hits": 14, "latest": 1415032601175 }, | 63 "mathtag.com": { "hits": 14, "latest": 1415032601175 }, |
66 "amazonaws.com": { "hits": 18, "latest": 1414977342966 } | 64 "amazonaws.com": { "hits": 18, "latest": 1414977342966 } |
67 } | 65 } |
68 } | 66 } |
69 } | 67 } |
70 }] | 68 }] |
71 | 69 |
72 class GeometricalMeanTestCase(unittest.TestCase): | 70 class GeometricalMeanTestCase(test_helpers.FilterhitsTestCase): |
73 longMessage = True | 71 longMessage = True |
74 maxDiff = None | 72 maxDiff = None |
75 | 73 |
76 def geometrical(self, interval, new, new_timestamp, old, old_timestamp): | 74 def geometrical(self, interval, new, new_timestamp, old, old_timestamp): |
77 delta_divby_interval = (new_timestamp - old_timestamp) / 1000 / float(interv
al) | 75 delta_divby_interval = (new_timestamp - old_timestamp) / 1000 / float(interv
al) |
78 return long(round(old ** (1 - delta_divby_interval) * new ** delta_divby_int
erval)) | 76 return long(round(old ** (1 - delta_divby_interval) * new ** delta_divby_int
erval)) |
79 | 77 |
80 def clear_rows(self): | |
81 if self.db: | |
82 db.write(self.db, (("DELETE FROM frequencies",), | |
83 ("DELETE FROM filters",))) | |
84 | |
85 def setUp(self): | |
86 self.config = test_helpers.setup_config() | |
87 try: | |
88 self.db = db.connect() | |
89 except MySQLdb.Error: | |
90 self.db = None | |
91 self.clear_rows() | |
92 | |
93 def tearDown(self): | |
94 test_helpers.restore_config() | |
95 if self.db: | |
96 self.clear_rows() | |
97 self.db.close() | |
98 self.db = None | |
99 | |
100 def test_calculations(self): | 78 def test_calculations(self): |
101 if not self.db: | |
102 raise unittest.SkipTest("Not connected to test DB.") | |
103 | |
104 interval = 86400 | 79 interval = 86400 |
105 | 80 |
106 # Tables should be empty to start with | 81 # Tables should be empty to start with |
107 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), ()) | 82 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), ()) |
108 self.assertEqual(db.query(self.db, "SELECT * FROM frequencies"), ()) | 83 self.assertEqual(db.query(self.db, "SELECT * FROM frequencies"), ()) |
109 # First batch | 84 # First batch |
110 db.write(self.db, geometrical_mean.update(interval, test_data[0])) | 85 db.write(self.db, geometrical_mean.update(interval, test_data[0])) |
111 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), | 86 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), |
112 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), | 87 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), |
113 u"##.top-box-right-ad"),)) | 88 u"##.top-box-right-ad"),)) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 14L, datetime.utcfromtimestamp(1415032601175 / 1000)), | 157 14L, datetime.utcfromtimestamp(1415032601175 / 1000)), |
183 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com", | 158 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com", |
184 self.geometrical(interval, 43, 1415045194098, | 159 self.geometrical(interval, 43, 1415045194098, |
185 self.geometrical(interval, 27, 1414917270343, | 160 self.geometrical(interval, 27, 1414917270343, |
186 14, 1414859271125), | 161 14, 1414859271125), |
187 1414917270343), | 162 1414917270343), |
188 datetime.utcfromtimestamp(1415045194098 / 1000)))) | 163 datetime.utcfromtimestamp(1415045194098 / 1000)))) |
189 | 164 |
190 if __name__ == "__main__": | 165 if __name__ == "__main__": |
191 unittest.main() | 166 unittest.main() |
LEFT | RIGHT |