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

Delta Between Two Patch Sets: sitescripts/filterhits/test/api_tests.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Left Patch Set: Created base class for tests and reverted earlier content-type change. Created April 1, 2015, 7:01 p.m.
Right Patch Set: Addressed further comments from Sebastian. Created April 2, 2015, 10:13 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/filterhits/test/__init__.py ('k') | sitescripts/filterhits/test/db_tests.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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,
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 self.assertResponse(submit_handler, "400 Processing Error", 77 self.assertResponse(submit_handler, "400 Processing Error",
78 REQUEST_METHOD="POST", body="") 78 REQUEST_METHOD="POST", body="")
79 self.assertResponse(submit_handler, "400 Processing Error", 79 self.assertResponse(submit_handler, "400 Processing Error",
80 REQUEST_METHOD="POST", body="Oops...") 80 REQUEST_METHOD="POST", body="Oops...")
81 self.assertResponse(submit_handler, "400 Processing Error", 81 self.assertResponse(submit_handler, "400 Processing Error",
82 REQUEST_METHOD="POST", body="{123:]") 82 REQUEST_METHOD="POST", body="{123:]")
83 self.assertResponse(submit_handler, "400 Processing Error", 83 self.assertResponse(submit_handler, "400 Processing Error",
84 REQUEST_METHOD="POST", body="1") 84 REQUEST_METHOD="POST", body="1")
85 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 0) 85 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 0)
86 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 0) 86 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 0)
87 # Ensure even an empty object, or one with the wrong fields returns OK 87 # Ensure even an empty object, or one with the wrong fields returns successf ully
88 self.assertResponse(submit_handler, "200 OK", 88 self.assertResponse(submit_handler, "204 No Content",
89 REQUEST_METHOD="POST", body="{}") 89 REQUEST_METHOD="POST", body="{}")
90 self.assertResponse(submit_handler, "200 OK", 90 self.assertResponse(submit_handler, "204 No Content",
91 REQUEST_METHOD="POST", body="{\"hello\": \"world\"}") 91 REQUEST_METHOD="POST", body="{\"hello\": \"world\"}")
92 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 0) 92 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 0)
93 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 0) 93 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 0)
94 # Now some actually valid data 94 # Now some actually valid data
95 self.assertResponse(submit_handler, "200 OK", 95 self.assertResponse(submit_handler, "204 No Content",
96 REQUEST_METHOD="POST", body=valid_data) 96 REQUEST_METHOD="POST", body=valid_data)
97 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 2) 97 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 2)
98 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 1) 98 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 1)
99 # Now make sure apparently valid data with timestamps that cause geometrical 99 # Now make sure apparently valid data with timestamps that cause geometrical
100 # mean calculations to fail with MySQL errors return OK but don't change DB 100 # mean calculations to fail with MySQL errors return OK but don't change DB
101 invalid_data = json.loads(valid_data) 101 invalid_data = json.loads(valid_data)
102 invalid_data["filters"]["||example.com^"]["firstParty"]["example.com"]["late st"] = 3 102 invalid_data["filters"]["||example.com^"]["firstParty"]["example.com"]["late st"] = 3
103 invalid_data = json.dumps(invalid_data) 103 invalid_data = json.dumps(invalid_data)
104 self.assertResponse(submit_handler, "200 OK", 104 self.assertResponse(submit_handler, "204 No Content",
105 REQUEST_METHOD="POST", body=invalid_data) 105 REQUEST_METHOD="POST", body=invalid_data)
106 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 2) 106 self.assertEqual(len(db.query(self.db, "SELECT * FROM frequencies")), 2)
107 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 1) 107 self.assertEqual(len(db.query(self.db, "SELECT * FROM filters")), 1)
108 108
109 def test_query(self): 109 def test_query(self):
110 # Basic query with no data, should return OK 110 # Basic query with no data, should return successfully
111 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "resul ts": [], "echo": 0}) 111 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "resul ts": [], "echo": 0})
112 # If echo parameter is passed and is integer it should be returned 112 # If echo parameter is passed and is integer it should be returned
113 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "resul ts": [], "echo": 1337}, 113 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "resul ts": [], "echo": 1337},
114 get_params={ "echo": 1337 }) 114 get_params={ "echo": 1337 })
115 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "resul ts": [], "echo": 0}, 115 self.assertResponse(query_handler, "200 OK", {"count": 0, "total": 0, "resul ts": [], "echo": 0},
116 get_params={ "echo": "naughty" }) 116 get_params={ "echo": "naughty" })
117 # Now let's submit some data so we can query it back out 117 # Now let's submit some data so we can query it back out
118 test_data = [json.loads(valid_data), json.loads(valid_data), json.loads(vali d_data)] 118 test_data = [json.loads(valid_data), json.loads(valid_data), json.loads(vali d_data)]
119 test_data[1]["filters"]["##Second-Filter|"] = test_data[1]["filters"].pop("| |example.com^") 119 test_data[1]["filters"]["##Second-Filter|"] = test_data[1]["filters"].pop("| |example.com^")
120 test_data[2]["filters"]["##Third-Filter|"] = test_data[2]["filters"].pop("|| example.com^") 120 test_data[2]["filters"]["##Third-Filter|"] = test_data[2]["filters"].pop("|| example.com^")
121 for data in test_data: 121 for data in test_data:
122 self.assertResponse(submit_handler, "200 OK", 122 self.assertResponse(submit_handler, "204 No Content",
123 REQUEST_METHOD="POST", body=json.dumps(data)) 123 REQUEST_METHOD="POST", body=json.dumps(data))
124 # Ordering parameters should be respected 124 # Ordering parameters should be respected
125 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 6, 125 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 6,
126 "results": [{"domain": "exampl e.com", 126 "results": [{"domain": "exampl e.com",
127 "filter": "||exam ple.com^", 127 "filter": "||exam ple.com^",
128 "frequency": 0}], "echo": 0}, 128 "frequency": 0}], "echo": 0},
129 get_params={ "order_by": "filter", "order": "desc", "tak e": "1" }) 129 get_params={ "order_by": "filter", "order": "desc", "tak e": "1" })
130 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 6, 130 self.assertResponse(query_handler, "200 OK", {"count": 1, "total": 6,
131 "results": [{"domain": "exampl e.com", 131 "results": [{"domain": "exampl e.com",
132 "filter": "##Seco nd-Filter|", 132 "filter": "##Seco nd-Filter|",
(...skipping 29 matching lines...) Expand all
162 "results": [{"domain": "exampl e.org", 162 "results": [{"domain": "exampl e.org",
163 "filter": "##Seco nd-Filter|", 163 "filter": "##Seco nd-Filter|",
164 "frequency": 4}, 164 "frequency": 4},
165 {"domain": "exampl e.com", 165 {"domain": "exampl e.com",
166 "filter": "##Thir d-Filter|", 166 "filter": "##Thir d-Filter|",
167 "frequency": 0}], "echo": 0}, 167 "frequency": 0}], "echo": 0},
168 get_params={ "skip": "2", "take": "2" }) 168 get_params={ "skip": "2", "take": "2" })
169 169
170 if __name__ == "__main__": 170 if __name__ == "__main__":
171 unittest.main() 171 unittest.main()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld