Mercurial > p > roundup > code
comparison test/rest_common.py @ 6185:1cb2375015f0
Enable timing stats reporting in REST interface.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Wed, 03 Jun 2020 00:52:32 -0400 |
| parents | e097ff5064b8 |
| children | 29c6dc8ed004 |
comparison
equal
deleted
inserted
replaced
| 6184:c757a6a14c8d | 6185:1cb2375015f0 |
|---|---|
| 33 from roundup.exceptions import * | 33 from roundup.exceptions import * |
| 34 from roundup import password, hyperdb | 34 from roundup import password, hyperdb |
| 35 from roundup.rest import RestfulInstance, calculate_etag | 35 from roundup.rest import RestfulInstance, calculate_etag |
| 36 from roundup.backends import list_backends | 36 from roundup.backends import list_backends |
| 37 from roundup.cgi import client | 37 from roundup.cgi import client |
| 38 from roundup.anypy.strings import b2s, s2b | 38 from roundup.anypy.strings import b2s, s2b, us2u |
| 39 import random | 39 import random |
| 40 | 40 |
| 41 from roundup.backends.sessions_dbm import OneTimeKeys | 41 from roundup.backends.sessions_dbm import OneTimeKeys |
| 42 from roundup.anypy.dbm_ import anydbm, whichdb | 42 from roundup.anypy.dbm_ import anydbm, whichdb |
| 43 | 43 |
| 1317 self.assertEqual(json_dict['data']['attributes']\ | 1317 self.assertEqual(json_dict['data']['attributes']\ |
| 1318 ['assignedto']['link'], | 1318 ['assignedto']['link'], |
| 1319 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/2") | 1319 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/2") |
| 1320 | 1320 |
| 1321 | 1321 |
| 1322 def testStatsGen(self): | |
| 1323 # check stats being returned by put and get ops | |
| 1324 # using dispatch which parses the @stats query param | |
| 1325 | |
| 1326 # find correct py2/py3 list comparison ignoring order | |
| 1327 try: | |
| 1328 list_test = self.assertCountEqual # py3 | |
| 1329 except AttributeError: | |
| 1330 list_test = self.assertItemsEqual # py2.7+ | |
| 1331 | |
| 1332 # get stats | |
| 1333 form = cgi.FieldStorage() | |
| 1334 form.list = [ | |
| 1335 cgi.MiniFieldStorage('@stats', 'True'), | |
| 1336 ] | |
| 1337 results = self.server.dispatch('GET', | |
| 1338 "/rest/data/user/1/realname", | |
| 1339 form) | |
| 1340 self.assertEqual(self.dummy_client.response_code, 200) | |
| 1341 json_dict = json.loads(b2s(results)) | |
| 1342 | |
| 1343 # check that @stats are defined | |
| 1344 self.assertTrue( '@stats' in json_dict['data'] ) | |
| 1345 # check that the keys are present | |
| 1346 # not validating values as that changes | |
| 1347 valid_fields= [ us2u('elapsed'), | |
| 1348 us2u('cache_hits'), | |
| 1349 us2u('cache_misses'), | |
| 1350 us2u('get_items'), | |
| 1351 us2u('filtering') ] | |
| 1352 list_test(valid_fields,json_dict['data']['@stats'].keys()) | |
| 1353 | |
| 1354 # Make sure false value works to suppress @stats | |
| 1355 form = cgi.FieldStorage() | |
| 1356 form.list = [ | |
| 1357 cgi.MiniFieldStorage('@stats', 'False'), | |
| 1358 ] | |
| 1359 results = self.server.dispatch('GET', | |
| 1360 "/rest/data/user/1/realname", | |
| 1361 form) | |
| 1362 self.assertEqual(self.dummy_client.response_code, 200) | |
| 1363 json_dict = json.loads(b2s(results)) | |
| 1364 print(results) | |
| 1365 # check that @stats are not defined | |
| 1366 self.assertTrue( '@stats' not in json_dict['data'] ) | |
| 1367 | |
| 1368 # Make sure non-true value works to suppress @stats | |
| 1369 # false will always work | |
| 1370 form = cgi.FieldStorage() | |
| 1371 form.list = [ | |
| 1372 cgi.MiniFieldStorage('@stats', 'random'), | |
| 1373 ] | |
| 1374 results = self.server.dispatch('GET', | |
| 1375 "/rest/data/user/1/realname", | |
| 1376 form) | |
| 1377 self.assertEqual(self.dummy_client.response_code, 200) | |
| 1378 json_dict = json.loads(b2s(results)) | |
| 1379 print(results) | |
| 1380 # check that @stats are not defined | |
| 1381 self.assertTrue( '@stats' not in json_dict['data'] ) | |
| 1382 | |
| 1383 # if @stats is not defined there should be no stats | |
| 1384 results = self.server.dispatch('GET', | |
| 1385 "/rest/data/user/1/realname", | |
| 1386 self.empty_form) | |
| 1387 self.assertEqual(self.dummy_client.response_code, 200) | |
| 1388 json_dict = json.loads(b2s(results)) | |
| 1389 | |
| 1390 # check that @stats are not defined | |
| 1391 self.assertTrue( '@stats' not in json_dict['data'] ) | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 # change admin's realname via a normal web form | |
| 1396 # This generates a FieldStorage that looks like: | |
| 1397 # FieldStorage(None, None, []) | |
| 1398 # use etag from header | |
| 1399 # | |
| 1400 # Also use GET on the uri via the dispatch to retrieve | |
| 1401 # the results from the db. | |
| 1402 etag = calculate_etag(self.db.user.getnode('1'), | |
| 1403 self.db.config['WEB_SECRET_KEY']) | |
| 1404 headers={"if-match": etag, | |
| 1405 "accept": "application/vnd.json.test-v1+json", | |
| 1406 } | |
| 1407 form = cgi.FieldStorage() | |
| 1408 form.list = [ | |
| 1409 cgi.MiniFieldStorage('data', 'Joe Doe'), | |
| 1410 cgi.MiniFieldStorage('@apiver', '1'), | |
| 1411 cgi.MiniFieldStorage('@stats', 'true'), | |
| 1412 ] | |
| 1413 self.headers = headers | |
| 1414 self.server.client.request.headers.get = self.get_header | |
| 1415 self.db.setCurrentUser('admin') # must be admin to change user | |
| 1416 results = self.server.dispatch('PUT', | |
| 1417 "/rest/data/user/1/realname", | |
| 1418 form) | |
| 1419 self.assertEqual(self.dummy_client.response_code, 200) | |
| 1420 json_dict = json.loads(b2s(results)) | |
| 1421 list_test(valid_fields,json_dict['data']['@stats'].keys()) | |
| 1422 | |
| 1322 def testDispatch(self): | 1423 def testDispatch(self): |
| 1323 """ | 1424 """ |
| 1324 run changes through rest dispatch(). This also tests | 1425 run changes through rest dispatch(). This also tests |
| 1325 sending json payload through code as dispatch is the | 1426 sending json payload through code as dispatch is the |
| 1326 code that changes json payload into something we can | 1427 code that changes json payload into something we can |
