@@ -351,3 +351,39 @@ async def test_get_download_node(array_data_node, async_client):
351351 response = await async_client .get (f'/nodes/{ array_data_node .pk } /download' )
352352 assert response .status_code == 422 , response .json ()
353353 assert 'Please specify the download format' in response .json ()['detail' ]
354+
355+
356+ @pytest .mark .anyio
357+ async def test_get_statistics (default_nodes , async_client ):
358+ """Test get statistics for nodes."""
359+
360+ from datetime import datetime
361+
362+ default_user_reference_json = {
363+ 'total' : 4 ,
364+ 'types' : {
365+ 'data.core.float.Float.' : 1 ,
366+ 'data.core.str.Str.' : 1 ,
367+ 'data.core.bool.Bool.' : 1 ,
368+ 'data.core.int.Int.' : 1 ,
369+ },
370+ 'ctime_by_day' : {datetime .today ().strftime ('%Y-%m-%d' ): 4 },
371+ }
372+
373+ # Test without specifiying user, should use default user
374+ response = await async_client .get ('/nodes/statistics' )
375+ assert response .status_code == 200 , response .json ()
376+ assert response .json () == default_user_reference_json
377+
378+ # Test that the output is the same when we use the pk of the default user
379+ from aiida import orm
380+
381+ default_user_pk = orm .User (email = '' ).collection .get_default ().pk
382+ response = await async_client .get (f'/nodes/statistics?user={ default_user_pk } ' )
383+ assert response .status_code == 200 , response .json ()
384+ assert response .json () == default_user_reference_json
385+
386+ # Test empty response for nonexisting user
387+ response = await async_client .get ('/nodes/statistics?user=99999' )
388+ assert response .status_code == 200 , response .json ()
389+ assert response .json () == {'total' : 0 , 'types' : {}, 'ctime_by_day' : {}}
0 commit comments