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

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

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Rebased. Created March 28, 2015, 2:10 p.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/db_tests.py ('k') | sitescripts/filterhits/test/log_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
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 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 import MySQLdb
20 from datetime import datetime
21
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 class GeometricalMeanTestCase(unittest.TestCase):
71 longMessage = True
72 maxDiff = None
73
74 def geometrical(self, interval, new, new_timestamp, old, old_timestamp):
75 delta_divby_interval = (new_timestamp - old_timestamp) / 1000 / float(interv al)
76 return long(round(old ** (1 - delta_divby_interval) * new ** delta_divby_int erval))
77
78 def clear_rows(self):
79 if self.db:
80 db.write(self.db, (("DELETE FROM frequencies",),
81 ("DELETE FROM filters",)))
82
83 def setUp(self):
84 try:
85 db.testing = True
86 self.db = db.connect()
87 except MySQLdb.Error:
88 self.db = None
89 self.clear_rows()
90
91 def tearDown(self):
92 if self.db:
93 self.clear_rows()
94 self.db.close()
95 self.db = None
96
97 def test_calculations(self):
98 if not self.db:
99 raise unittest.SkipTest("Not connected to test DB.")
100
101 interval = 86400
102
103 # Tables should be empty to start with
104 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), ())
105 self.assertEqual(db.query(self.db, "SELECT * FROM frequencies"), ())
106 # First batch
107 db.write(self.db, geometrical_mean.update(interval, test_data[0]))
108 self.assertEqual(db.query(self.db, "SELECT * FROM filters"),
109 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"),
110 u"##.top-box-right-ad"),))
111 self.assertEqual(
112 db.query(self.db, "SELECT * FROM frequencies"),
113 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin e.com",
114 6L, datetime.utcfromtimestamp(1414817340948 / 1000)),
115 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net",
116 36L, datetime.utcfromtimestamp(1414838712373 / 1000)),
117 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick. net",
118 26L, datetime.utcfromtimestamp(1414823430333 / 1000)),
119 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com",
120 50L, datetime.utcfromtimestamp(1414849084678 / 1000)),
121 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com",
122 14L, datetime.utcfromtimestamp(1414859271125 / 1000))))
123 # Second batch
124 db.write(self.db, geometrical_mean.update(interval, test_data[1]))
125 self.assertEqual(db.query(self.db, "SELECT * FROM filters"),
126 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"),
127 u"##.top-box-right-ad"),))
128 self.assertEqual(
129 db.query(self.db, "SELECT * FROM frequencies"),
130 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin e.com",
131 6L, datetime.utcfromtimestamp(1414817340948 / 1000)),
132 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"adsymptotic. com",
133 49L, datetime.utcfromtimestamp(1414953943015 / 1000)),
134 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"amazon.com",
135 2L, datetime.utcfromtimestamp(1414913563746 / 1000)),
136 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net",
137 36L, datetime.utcfromtimestamp(1414838712373 / 1000)),
138 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick. net",
139 26L, datetime.utcfromtimestamp(1414823430333 / 1000)),
140 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com",
141 self.geometrical(interval, 21, 1414953920364, 50, 1414849084678),
142 datetime.utcfromtimestamp(1414953920364 / 1000)),
143 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"live.com",
144 34L, datetime.utcfromtimestamp(1414916268769 / 1000)),
145 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com",
146 self.geometrical(interval, 27, 1414917270343, 14, 1414859271125),
147 datetime.utcfromtimestamp(1414917270343 / 1000))))
148 # Third batch
149 db.write(self.db, geometrical_mean.update(interval, test_data[2]))
150 self.assertEqual(db.query(self.db, "SELECT * FROM filters"),
151 (("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"),
152 u"stevedeace.com##.topAddHolder"),
153 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"),
154 u"##.top-box-right-ad")))
155 self.assertEqual(
156 db.query(self.db, "SELECT * FROM frequencies"),
157 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin e.com",
158 6L, datetime.utcfromtimestamp(1414817340948 / 1000)),
159 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"adsymptotic. com",
160 self.geometrical(interval, 15, 1414994112862, 49, 1414953943015),
161 datetime.utcfromtimestamp(1414994112862 / 1000)),
162 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"amazon.com",
163 2L, datetime.utcfromtimestamp(1414913563746 / 1000)),
164 ("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), u'amazonaws.co m',
165 18L, datetime.utcfromtimestamp(1414977342966 / 1000)),
166 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net",
167 36L, datetime.utcfromtimestamp(1414838712373 / 1000)),
168 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick. net",
169 26L, datetime.utcfromtimestamp(1414823430333 / 1000)),
170 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com",
171 self.geometrical(interval, 14, 1415008533089,
172 self.geometrical(interval, 21, 1414953920364,
173 50, 1414849084678),
174 1414953920364),
175 datetime.utcfromtimestamp(1415008533089 / 1000)),
176 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"live.com",
177 34L, datetime.utcfromtimestamp(1414916268769 / 1000)),
178 ("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), u'mathtag.com' ,
179 14L, datetime.utcfromtimestamp(1415032601175 / 1000)),
180 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com",
181 self.geometrical(interval, 43, 1415045194098,
182 self.geometrical(interval, 27, 1414917270343,
183 14, 1414859271125),
184 1414917270343),
185 datetime.utcfromtimestamp(1415045194098 / 1000))))
186
187 if __name__ == '__main__':
188 unittest.main()
OLDNEW
« no previous file with comments | « sitescripts/filterhits/test/db_tests.py ('k') | sitescripts/filterhits/test/log_tests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld