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