@@ -369,5 +369,71 @@ def test_upgrade_by_asset_name_fetch_error(self, mock_retrieve):
369369 self .assertIn ("Failed to fetch record" , response .json ()["error" ])
370370
371371
372+ class TestRetrieveRecordsEndpoint (unittest .TestCase ):
373+
374+ @patch ("aind_metadata_viz.endpoints.retrieve_records" )
375+ def test_filter_query (self , mock_retrieve ):
376+ mock_result = Mock ()
377+ mock_result .backend = "cache"
378+ mock_result .elapsed_seconds = 0.1
379+ mock_result .asset_names = ["asset-1" ]
380+ mock_result .records = [{"name" : "asset-1" }]
381+ mock_retrieve .return_value = mock_result
382+
383+ response = client .post ("/retrieve-records" , json = {"subject.subject_id" : "123456" })
384+ self .assertEqual (response .status_code , 200 )
385+ body = response .json ()
386+ self .assertEqual (body ["asset_names" ], ["asset-1" ])
387+ self .assertEqual (body ["records" ], [{"name" : "asset-1" }])
388+ mock_retrieve .assert_called_once ()
389+
390+ @patch ("aind_metadata_viz.endpoints.retrieve_aggregation" )
391+ def test_aggregation_pipeline (self , mock_aggregate ):
392+ mock_result = Mock ()
393+ mock_result .backend = "docdb"
394+ mock_result .elapsed_seconds = 0.2
395+ mock_result .asset_names = ["asset-2" ]
396+ mock_result .records = [{"name" : "asset-2" , "count" : 5 }]
397+ mock_aggregate .return_value = mock_result
398+
399+ pipeline = [{"$match" : {"subject.subject_id" : "123456" }}, {"$limit" : 5 }]
400+ response = client .post ("/retrieve-records" , json = pipeline )
401+ self .assertEqual (response .status_code , 200 )
402+ body = response .json ()
403+ self .assertEqual (body ["backend" ], "docdb" )
404+ self .assertEqual (body ["asset_names" ], ["asset-2" ])
405+ self .assertEqual (body ["records" ], [{"name" : "asset-2" , "count" : 5 }])
406+ mock_aggregate .assert_called_once_with (pipeline )
407+
408+ @patch ("aind_metadata_viz.endpoints.retrieve_aggregation" )
409+ def test_aggregation_pipeline_error (self , mock_aggregate ):
410+ mock_aggregate .side_effect = Exception ("pipeline error" )
411+
412+ response = client .post ("/retrieve-records" , json = [{"$match" : {}}])
413+ self .assertEqual (response .status_code , 500 )
414+ self .assertIn ("Aggregation execution failed" , response .json ()["error" ])
415+
416+ @patch ("aind_metadata_viz.endpoints.retrieve_records" )
417+ def test_filter_query_error (self , mock_retrieve ):
418+ mock_retrieve .side_effect = Exception ("connection error" )
419+
420+ response = client .post ("/retrieve-records" , json = {"name" : "test" })
421+ self .assertEqual (response .status_code , 500 )
422+ self .assertIn ("Query execution failed" , response .json ()["error" ])
423+
424+ def test_invalid_json (self ):
425+ response = client .post (
426+ "/retrieve-records" ,
427+ content = b"not json" ,
428+ headers = {"Content-Type" : "application/json" },
429+ )
430+ self .assertEqual (response .status_code , 400 )
431+ self .assertIn ("Invalid JSON format" , response .json ()["error" ])
432+
433+ def test_invalid_body_type (self ):
434+ response = client .post ("/retrieve-records" , json = "a string" )
435+ self .assertEqual (response .status_code , 400 )
436+
437+
372438if __name__ == "__main__" :
373439 unittest .main ()
0 commit comments