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, MySQLdb | 18 import unittest |
19 from datetime import datetime | 19 from datetime import datetime |
20 from ConfigParser import NoOptionError | |
21 | 20 |
22 from sitescripts.utils import get_config | 21 from sitescripts.filterhits.test import test_helpers |
23 import sitescripts.filterhits.db as db | 22 from sitescripts.filterhits import db, geometrical_mean |
24 import sitescripts.filterhits.geometrical_mean as 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 }, |
32 "yahoo.com": { "hits": 14, "latest": 1414859271125 }, | 30 "yahoo.com": { "hits": 14, "latest": 1414859271125 }, |
33 "doubleclick.net": { "hits": 26, "latest": 1414823430333 } | 31 "doubleclick.net": { "hits": 26, "latest": 1414823430333 } |
34 }, | 32 }, |
(...skipping 27 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 geometrical_mean",), | |
83 ("DELETE FROM filters",))) | |
84 | |
85 def setUp(self): | |
86 try: | |
87 self.config = get_config() | |
88 self.db = db.connect( | |
89 self.config.get("filterhitstats", "dbuser"), | |
90 self.config.get("filterhitstats", "dbpassword"), | |
91 self.config.get("filterhitstats", "test_database")) | |
92 except (MySQLdb.Error, NoOptionError): | |
93 self.db = None | |
94 self.clear_rows() | |
95 | |
96 def tearDown(self): | |
97 if self.db: | |
98 self.clear_rows() | |
99 self.db.close() | |
100 self.db = None | |
101 | |
102 def test_calculations(self): | 78 def test_calculations(self): |
103 if not self.db: | |
104 raise unittest.SkipTest("Not connected to test DB.") | |
105 | |
106 interval = 86400 | 79 interval = 86400 |
107 | 80 |
108 # Tables should be empty to start with | 81 # Tables should be empty to start with |
109 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), ()) | 82 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), ()) |
110 self.assertEqual(db.query(self.db, "SELECT * FROM geometrical_mean"), ()) | 83 self.assertEqual(db.query(self.db, "SELECT * FROM frequencies"), ()) |
111 # First batch | 84 # First batch |
112 db.write(self.db, geometrical_mean.update(interval, test_data[0])) | 85 db.write(self.db, geometrical_mean.update(interval, test_data[0])) |
113 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), | 86 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), |
114 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), | 87 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), |
115 u"##.top-box-right-ad"),)) | 88 u"##.top-box-right-ad"),)) |
116 self.assertEqual( | 89 self.assertEqual( |
117 db.query(self.db, "SELECT * FROM geometrical_mean"), | 90 db.query(self.db, "SELECT * FROM frequencies"), |
118 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin
e.com", | 91 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin
e.com", |
119 6L, datetime.utcfromtimestamp(1414817340948 / 1000)), | 92 6L, datetime.utcfromtimestamp(1414817340948 / 1000)), |
120 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net", | 93 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net", |
121 36L, datetime.utcfromtimestamp(1414838712373 / 1000)), | 94 36L, datetime.utcfromtimestamp(1414838712373 / 1000)), |
122 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick.
net", | 95 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick.
net", |
123 26L, datetime.utcfromtimestamp(1414823430333 / 1000)), | 96 26L, datetime.utcfromtimestamp(1414823430333 / 1000)), |
124 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com", | 97 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com", |
125 50L, datetime.utcfromtimestamp(1414849084678 / 1000)), | 98 50L, datetime.utcfromtimestamp(1414849084678 / 1000)), |
126 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com", | 99 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com", |
127 14L, datetime.utcfromtimestamp(1414859271125 / 1000)))) | 100 14L, datetime.utcfromtimestamp(1414859271125 / 1000)))) |
128 # Second batch | 101 # Second batch |
129 db.write(self.db, geometrical_mean.update(interval, test_data[1])) | 102 db.write(self.db, geometrical_mean.update(interval, test_data[1])) |
130 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), | 103 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), |
131 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), | 104 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), |
132 u"##.top-box-right-ad"),)) | 105 u"##.top-box-right-ad"),)) |
133 self.assertEqual( | 106 self.assertEqual( |
134 db.query(self.db, "SELECT * FROM geometrical_mean"), | 107 db.query(self.db, "SELECT * FROM frequencies"), |
135 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin
e.com", | 108 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin
e.com", |
136 6L, datetime.utcfromtimestamp(1414817340948 / 1000)), | 109 6L, datetime.utcfromtimestamp(1414817340948 / 1000)), |
137 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"adsymptotic.
com", | 110 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"adsymptotic.
com", |
138 49L, datetime.utcfromtimestamp(1414953943015 / 1000)), | 111 49L, datetime.utcfromtimestamp(1414953943015 / 1000)), |
139 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"amazon.com", | 112 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"amazon.com", |
140 2L, datetime.utcfromtimestamp(1414913563746 / 1000)), | 113 2L, datetime.utcfromtimestamp(1414913563746 / 1000)), |
141 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net", | 114 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net", |
142 36L, datetime.utcfromtimestamp(1414838712373 / 1000)), | 115 36L, datetime.utcfromtimestamp(1414838712373 / 1000)), |
143 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick.
net", | 116 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick.
net", |
144 26L, datetime.utcfromtimestamp(1414823430333 / 1000)), | 117 26L, datetime.utcfromtimestamp(1414823430333 / 1000)), |
145 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com", | 118 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com", |
146 self.geometrical(interval, 21, 1414953920364, 50, 1414849084678), | 119 self.geometrical(interval, 21, 1414953920364, 50, 1414849084678), |
147 datetime.utcfromtimestamp(1414953920364 / 1000)), | 120 datetime.utcfromtimestamp(1414953920364 / 1000)), |
148 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"live.com", | 121 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"live.com", |
149 34L, datetime.utcfromtimestamp(1414916268769 / 1000)), | 122 34L, datetime.utcfromtimestamp(1414916268769 / 1000)), |
150 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com", | 123 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com", |
151 self.geometrical(interval, 27, 1414917270343, 14, 1414859271125), | 124 self.geometrical(interval, 27, 1414917270343, 14, 1414859271125), |
152 datetime.utcfromtimestamp(1414917270343 / 1000)))) | 125 datetime.utcfromtimestamp(1414917270343 / 1000)))) |
153 # Third batch | 126 # Third batch |
154 db.write(self.db, geometrical_mean.update(interval, test_data[2])) | 127 db.write(self.db, geometrical_mean.update(interval, test_data[2])) |
155 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), | 128 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), |
156 (("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), | 129 (("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), |
157 u"stevedeace.com##.topAddHolder"), | 130 u"stevedeace.com##.topAddHolder"), |
158 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), | 131 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), |
159 u"##.top-box-right-ad"))) | 132 u"##.top-box-right-ad"))) |
160 self.assertEqual( | 133 self.assertEqual( |
161 db.query(self.db, "SELECT * FROM geometrical_mean"), | 134 db.query(self.db, "SELECT * FROM frequencies"), |
162 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin
e.com", | 135 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin
e.com", |
163 6L, datetime.utcfromtimestamp(1414817340948 / 1000)), | 136 6L, datetime.utcfromtimestamp(1414817340948 / 1000)), |
164 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"adsymptotic.
com", | 137 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"adsymptotic.
com", |
165 self.geometrical(interval, 15, 1414994112862, 49, 1414953943015), | 138 self.geometrical(interval, 15, 1414994112862, 49, 1414953943015), |
166 datetime.utcfromtimestamp(1414994112862 / 1000)), | 139 datetime.utcfromtimestamp(1414994112862 / 1000)), |
167 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"amazon.com", | 140 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"amazon.com", |
168 2L, datetime.utcfromtimestamp(1414913563746 / 1000)), | 141 2L, datetime.utcfromtimestamp(1414913563746 / 1000)), |
169 ("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), u'amazonaws.co
m', | 142 ("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), u"amazonaws.co
m", |
170 18L, datetime.utcfromtimestamp(1414977342966 / 1000)), | 143 18L, datetime.utcfromtimestamp(1414977342966 / 1000)), |
171 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net", | 144 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net", |
172 36L, datetime.utcfromtimestamp(1414838712373 / 1000)), | 145 36L, datetime.utcfromtimestamp(1414838712373 / 1000)), |
173 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick.
net", | 146 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick.
net", |
174 26L, datetime.utcfromtimestamp(1414823430333 / 1000)), | 147 26L, datetime.utcfromtimestamp(1414823430333 / 1000)), |
175 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com", | 148 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com", |
176 self.geometrical(interval, 14, 1415008533089, | 149 self.geometrical(interval, 14, 1415008533089, |
177 self.geometrical(interval, 21, 1414953920364, | 150 self.geometrical(interval, 21, 1414953920364, |
178 50, 1414849084678), | 151 50, 1414849084678), |
179 1414953920364), | 152 1414953920364), |
180 datetime.utcfromtimestamp(1415008533089 / 1000)), | 153 datetime.utcfromtimestamp(1415008533089 / 1000)), |
181 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"live.com", | 154 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"live.com", |
182 34L, datetime.utcfromtimestamp(1414916268769 / 1000)), | 155 34L, datetime.utcfromtimestamp(1414916268769 / 1000)), |
183 ("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), u'mathtag.com'
, | 156 ("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), u"mathtag.com"
, |
184 14L, datetime.utcfromtimestamp(1415032601175 / 1000)), | 157 14L, datetime.utcfromtimestamp(1415032601175 / 1000)), |
185 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com", | 158 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com", |
186 self.geometrical(interval, 43, 1415045194098, | 159 self.geometrical(interval, 43, 1415045194098, |
187 self.geometrical(interval, 27, 1414917270343, | 160 self.geometrical(interval, 27, 1414917270343, |
188 14, 1414859271125), | 161 14, 1414859271125), |
189 1414917270343), | 162 1414917270343), |
190 datetime.utcfromtimestamp(1415045194098 / 1000)))) | 163 datetime.utcfromtimestamp(1415045194098 / 1000)))) |
191 | 164 |
192 if __name__ == '__main__': | 165 if __name__ == "__main__": |
193 unittest.main() | 166 unittest.main() |
LEFT | RIGHT |