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: Create temporary log directory with tempfile module for tests. Created March 30, 2015, 7 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
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.test import test_helpers
23 from sitescripts.filterhits import db, geometrical_mean
24
25 test_data = [{
26 "filters": {
27 "##.top-box-right-ad": {
28 "firstParty": {
29 "acxiom-online.com": { "hits": 6, "latest": 1414817340948 },
30 "google.com": { "hits": 50, "latest": 1414849084678 },
31 "yahoo.com": { "hits": 14, "latest": 1414859271125 },
32 "doubleclick.net": { "hits": 26, "latest": 1414823430333 }
33 },
34 "thirdParty": {
35 "demdex.net": { "hits": 36, "latest": 1414838712373 }
36 }
37 }
38 }
39 }, {
40 "filters": {
41 "##.top-box-right-ad": {
42 "thirdParty": {
43 "adsymptotic.com": { "hits": 49, "latest": 1414953943015 },
44 "amazon.com": { "hits": 2, "latest": 1414913563746 },
45 "live.com": { "hits": 34, "latest": 1414916268769 },
46 "google.com": { "hits": 21, "latest": 1414953920364 },
47 "yahoo.com": { "hits": 27, "latest": 1414917270343 }
48 }
49 }
50 }
51 }, {
52 "filters": {
53 "##.top-box-right-ad": {
54 "firstParty": {
55 "google.com": { "hits": 14, "latest": 1415008533089 },
56 "adsymptotic.com": { "hits": 15, "latest": 1414994112862 }
57 },
58 "thirdParty": {
59 "yahoo.com": { "hits": 43, "latest": 1415045194098 }
60 }
61 },
62 "stevedeace.com##.topAddHolder": {
63 "firstParty": {
64 "mathtag.com": { "hits": 14, "latest": 1415032601175 },
65 "amazonaws.com": { "hits": 18, "latest": 1414977342966 }
66 }
67 }
68 }
69 }]
70
71 class GeometricalMeanTestCase(unittest.TestCase):
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(interv al)
77 return long(round(old ** (1 - delta_divby_interval) * new ** delta_divby_int erval))
78
79 def clear_rows(self):
80 if self.db:
81 db.write(self.db, (("DELETE FROM frequencies",),
82 ("DELETE FROM filters",)))
83
84 def setUp(self):
85 self.config = test_helpers.setup_config()
86 try:
87 self.db = db.connect()
88 except MySQLdb.Error:
89 self.db = None
90 self.clear_rows()
91
92 def tearDown(self):
93 test_helpers.restore_config()
94 if self.db:
95 self.clear_rows()
96 self.db.close()
97 self.db = None
98
99 def test_calculations(self):
100 if not self.db:
101 raise unittest.SkipTest("Not connected to test DB.")
102
103 interval = 86400
104
105 # Tables should be empty to start with
106 self.assertEqual(db.query(self.db, "SELECT * FROM filters"), ())
107 self.assertEqual(db.query(self.db, "SELECT * FROM frequencies"), ())
108 # First batch
109 db.write(self.db, geometrical_mean.update(interval, test_data[0]))
110 self.assertEqual(db.query(self.db, "SELECT * FROM filters"),
111 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"),
112 u"##.top-box-right-ad"),))
113 self.assertEqual(
114 db.query(self.db, "SELECT * FROM frequencies"),
115 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin e.com",
116 6L, datetime.utcfromtimestamp(1414817340948 / 1000)),
117 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net",
118 36L, datetime.utcfromtimestamp(1414838712373 / 1000)),
119 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick. net",
120 26L, datetime.utcfromtimestamp(1414823430333 / 1000)),
121 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com",
122 50L, datetime.utcfromtimestamp(1414849084678 / 1000)),
123 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com",
124 14L, datetime.utcfromtimestamp(1414859271125 / 1000))))
125 # Second batch
126 db.write(self.db, geometrical_mean.update(interval, test_data[1]))
127 self.assertEqual(db.query(self.db, "SELECT * FROM filters"),
128 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"),
129 u"##.top-box-right-ad"),))
130 self.assertEqual(
131 db.query(self.db, "SELECT * FROM frequencies"),
132 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin e.com",
133 6L, datetime.utcfromtimestamp(1414817340948 / 1000)),
134 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"adsymptotic. com",
135 49L, datetime.utcfromtimestamp(1414953943015 / 1000)),
136 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"amazon.com",
137 2L, datetime.utcfromtimestamp(1414913563746 / 1000)),
138 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net",
139 36L, datetime.utcfromtimestamp(1414838712373 / 1000)),
140 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick. net",
141 26L, datetime.utcfromtimestamp(1414823430333 / 1000)),
142 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com",
143 self.geometrical(interval, 21, 1414953920364, 50, 1414849084678),
144 datetime.utcfromtimestamp(1414953920364 / 1000)),
145 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"live.com",
146 34L, datetime.utcfromtimestamp(1414916268769 / 1000)),
147 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com",
148 self.geometrical(interval, 27, 1414917270343, 14, 1414859271125),
149 datetime.utcfromtimestamp(1414917270343 / 1000))))
150 # Third batch
151 db.write(self.db, geometrical_mean.update(interval, test_data[2]))
152 self.assertEqual(db.query(self.db, "SELECT * FROM filters"),
153 (("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"),
154 u"stevedeace.com##.topAddHolder"),
155 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"),
156 u"##.top-box-right-ad")))
157 self.assertEqual(
158 db.query(self.db, "SELECT * FROM frequencies"),
159 (("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"acxiom-onlin e.com",
160 6L, datetime.utcfromtimestamp(1414817340948 / 1000)),
161 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"adsymptotic. com",
162 self.geometrical(interval, 15, 1414994112862, 49, 1414953943015),
163 datetime.utcfromtimestamp(1414994112862 / 1000)),
164 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"amazon.com",
165 2L, datetime.utcfromtimestamp(1414913563746 / 1000)),
166 ("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), u'amazonaws.co m',
167 18L, datetime.utcfromtimestamp(1414977342966 / 1000)),
168 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"demdex.net",
169 36L, datetime.utcfromtimestamp(1414838712373 / 1000)),
170 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"doubleclick. net",
171 26L, datetime.utcfromtimestamp(1414823430333 / 1000)),
172 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"google.com",
173 self.geometrical(interval, 14, 1415008533089,
174 self.geometrical(interval, 21, 1414953920364,
175 50, 1414849084678),
176 1414953920364),
177 datetime.utcfromtimestamp(1415008533089 / 1000)),
178 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"live.com",
179 34L, datetime.utcfromtimestamp(1414916268769 / 1000)),
180 ("22de8d2ba8429eb170a0ece6ea7a426f7b22e574".decode("hex"), u'mathtag.com' ,
181 14L, datetime.utcfromtimestamp(1415032601175 / 1000)),
182 ("8c5ea548436c61f05536e205a29ada6204f603b0".decode("hex"), u"yahoo.com",
183 self.geometrical(interval, 43, 1415045194098,
184 self.geometrical(interval, 27, 1414917270343,
185 14, 1414859271125),
186 1414917270343),
187 datetime.utcfromtimestamp(1415045194098 / 1000))))
188
189 if __name__ == '__main__':
190 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld