@@ -35,7 +35,7 @@ def dst(self, dt):
3535from roundup .rest import RestfulInstance , calculate_etag
3636from roundup .backends import list_backends
3737from roundup .cgi import client
38- from roundup .anypy .strings import b2s , s2b
38+ from roundup .anypy .strings import b2s , s2b , us2u
3939import random
4040
4141from roundup .backends .sessions_dbm import OneTimeKeys
@@ -1319,6 +1319,107 @@ def testDispatchPost(self):
13191319 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/2" )
13201320
13211321
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+
13221423 def testDispatch (self ):
13231424 """
13241425 run changes through rest dispatch(). This also tests
0 commit comments