OLD | NEW |
| (Empty) |
1 # coding: utf-8 | |
2 | |
3 # This file is part of the Adblock Plus web scripts, | |
4 # Copyright (C) 2006-2016 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 unittest | |
19 from datetime import datetime | |
20 | |
21 from sitescripts.filterhits.test import test_helpers | |
22 from sitescripts.filterhits import db, geometrical_mean | |
23 | |
24 test_data = [{ | |
25 "filters": { | |
26 "##.top-box-right-ad": { | |
27 "firstParty": { | |
28 "acxiom-online.com": {"hits": 6, "latest": 1414817340948}, | |
29 "google.com": {"hits": 50, "latest": 1414849084678}, | |
30 "yahoo.com": {"hits": 14, "latest": 1414859271125}, | |
31 "doubleclick.net": {"hits": 26, "latest": 1414823430333} | |
32 }, | |
33 "thirdParty": { | |
34 "demdex.net": {"hits": 36, "latest": 1414838712373} | |
35 } | |
36 } | |
37 } | |
38 }, { | |
39 "filters": { | |
40 "##.top-box-right-ad": { | |
41 "thirdParty": { | |
42 "adsymptotic.com": {"hits": 49, "latest": 1414953943015}, | |
43 "amazon.com": {"hits": 2, "latest": 1414913563746}, | |
44 "live.com": {"hits": 34, "latest": 1414916268769}, | |
45 "google.com": {"hits": 21, "latest": 1414953920364}, | |
46 "yahoo.com": {"hits": 27, "latest": 1414917270343} | |
47 } | |
48 } | |
49 } | |
50 }, { | |
51 "filters": { | |
52 "##.top-box-right-ad": { | |
53 "firstParty": { | |
54 "google.com": {"hits": 14, "latest": 1415008533089}, | |
55 "adsymptotic.com": {"hits": 15, "latest": 1414994112862} | |
56 }, | |
57 "thirdParty": { | |
58 "yahoo.com": {"hits": 43, "latest": 1415045194098} | |
59 } | |
60 }, | |
61 "stevedeace.com##.topAddHolder": { | |
62 "firstParty": { | |
63 "mathtag.com": {"hits": 14, "latest": 1415032601175}, | |
64 "amazonaws.com": {"hits": 18, "latest": 1414977342966} | |
65 } | |
66 } | |
67 } | |
68 }] | |
69 | |
70 | |
71 class GeometricalMeanTestCase(test_helpers.FilterhitsTestCase): | |
72 longMessage = True | |
73 maxDiff = None | |
74 | |
75 def geometrical(self, interval, new, new_timestamp, old, old_timestamp): | |
76 delta_divby_interval = (new_timestamp - old_timestamp) / 1000 / float(in
terval) | |
77 return long(round(old ** (1 - delta_divby_interval) * new ** delta_divby
_interval)) | |
78 | |
79 def test_calculations(self): | |
80 interval = 86400 | |
81 | |
82 # Tables should be empty to start with | |
83 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), ()) | |
84 self.assertEqual(db.query(self.db, "SELECT * FROM frequencies"), ()) | |
85 # First batch | |
86 db.write(self.db, geometrical_mean.update(interval, test_data[0])) | |
87 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), | |
88 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("he
x"), | |
89 u"##.top-box-right-ad"),)) | |
90 self.assertEqual( | |
91 db.query(self.db, "SELECT * FROM frequencies"), | |
92 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom
-online.com", | |
93 6L, datetime.utcfromtimestamp(1414817340948 / 1000)), | |
94 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex
.net", | |
95 36L, datetime.utcfromtimestamp(1414838712373 / 1000)), | |
96 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"dou
bleclick.net", | |
97 26L, datetime.utcfromtimestamp(1414823430333 / 1000)), | |
98 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"goo
gle.com", | |
99 50L, datetime.utcfromtimestamp(1414849084678 / 1000)), | |
100 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yah
oo.com", | |
101 14L, datetime.utcfromtimestamp(1414859271125 / 1000)))) | |
102 # Second batch | |
103 db.write(self.db, geometrical_mean.update(interval, test_data[1])) | |
104 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), | |
105 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("he
x"), | |
106 u"##.top-box-right-ad"),)) | |
107 self.assertEqual( | |
108 db.query(self.db, "SELECT * FROM frequencies"), | |
109 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom
-online.com", | |
110 6L, datetime.utcfromtimestamp(1414817340948 / 1000)), | |
111 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"adsymp
totic.com", | |
112 49L, datetime.utcfromtimestamp(1414953943015 / 1000)), | |
113 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"ama
zon.com", | |
114 2L, datetime.utcfromtimestamp(1414913563746 / 1000)), | |
115 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"dem
dex.net", | |
116 36L, datetime.utcfromtimestamp(1414838712373 / 1000)), | |
117 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"dou
bleclick.net", | |
118 26L, datetime.utcfromtimestamp(1414823430333 / 1000)), | |
119 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"goo
gle.com", | |
120 self.geometrical(interval, 21, 1414953920364, 50, 1414849084678
), | |
121 datetime.utcfromtimestamp(1414953920364 / 1000)), | |
122 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"liv
e.com", | |
123 34L, datetime.utcfromtimestamp(1414916268769 / 1000)), | |
124 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yah
oo.com", | |
125 self.geometrical(interval, 27, 1414917270343, 14, 1414859271125
), | |
126 datetime.utcfromtimestamp(1414917270343 / 1000)))) | |
127 # Third batch | |
128 db.write(self.db, geometrical_mean.update(interval, test_data[2])) | |
129 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), | |
130 (("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("he
x"), | |
131 u"stevedeace.com##.topAddHolder"), | |
132 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("he
x"), | |
133 u"##.top-box-right-ad"))) | |
134 self.assertEqual( | |
135 db.query(self.db, "SELECT * FROM frequencies"), | |
136 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom
-online.com", | |
137 6L, datetime.utcfromtimestamp(1414817340948 / 1000)), | |
138 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"adsymp
totic.com", | |
139 self.geometrical(interval, 15, 1414994112862, 49, 1414953943015)
, | |
140 datetime.utcfromtimestamp(1414994112862 / 1000)), | |
141 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"ama
zon.com", | |
142 2L, datetime.utcfromtimestamp(1414913563746 / 1000)), | |
143 ("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), u"ama
zonaws.com", | |
144 18L, datetime.utcfromtimestamp(1414977342966 / 1000)), | |
145 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"dem
dex.net", | |
146 36L, datetime.utcfromtimestamp(1414838712373 / 1000)), | |
147 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"dou
bleclick.net", | |
148 26L, datetime.utcfromtimestamp(1414823430333 / 1000)), | |
149 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"goo
gle.com", | |
150 self.geometrical(interval, 14, 1415008533089, | |
151 self.geometrical(interval, 21, 1414953920364, | |
152 50, 1414849084678), | |
153 1414953920364), | |
154 datetime.utcfromtimestamp(1415008533089 / 1000)), | |
155 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"liv
e.com", | |
156 34L, datetime.utcfromtimestamp(1414916268769 / 1000)), | |
157 ("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), u"mat
htag.com", | |
158 14L, datetime.utcfromtimestamp(1415032601175 / 1000)), | |
159 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yah
oo.com", | |
160 self.geometrical(interval, 43, 1415045194098, | |
161 self.geometrical(interval, 27, 1414917270343, | |
162 14, 1414859271125), | |
163 1414917270343), | |
164 datetime.utcfromtimestamp(1415045194098 / 1000)))) | |
165 | |
166 if __name__ == "__main__": | |
167 unittest.main() | |
OLD | NEW |