@@ -777,8 +777,8 @@ def post_side_effect(*args, **kwargs):
777777
778778 svc = TdmsUploadService (rest_config )
779779
780- # Should raise if run_id is provided
781- with pytest .raises (ValueError , match = "Metadata can only be included in new runs " ):
780+ # Should raise if run_id and run_name is provided
781+ with pytest .raises (ValueError , match = "Must specify either run_name or run_id, not both " ):
782782 svc .upload (
783783 "some_tdms.tdms" ,
784784 "asset_name" ,
@@ -788,7 +788,7 @@ def post_side_effect(*args, **kwargs):
788788 )
789789
790790 # Should raise if run_name is not provided
791- with pytest .raises (ValueError , match = "Must provide a run_name to include metadata " ):
791+ with pytest .raises (ValueError , match = "Metadata can only be included in Runs " ):
792792 svc .upload (
793793 "some_tdms.tdms" ,
794794 "asset_name" ,
@@ -845,3 +845,45 @@ def post_side_effect(*args, **kwargs):
845845 == MetadataKeyType .METADATA_KEY_TYPE_STRING
846846 )
847847 assert create_run_post_data ["metadata" ][4 ]["string_value" ].startswith ("2024-01-01T12:00:00" )
848+
849+
850+ def test_tdms_upload_service_upload_with_metadata_run_id (
851+ mocker : MockFixture , mock_waveform_tdms_file : MockTdmsFile
852+ ):
853+ mock_path_is_file = mocker .patch ("sift_py.data_import.tdms.Path.is_file" )
854+ mock_path_is_file .return_value = True
855+
856+ mock_path_getsize = mocker .patch ("sift_py.data_import.csv.os.path.getsize" )
857+ mock_path_getsize .return_value = 10
858+
859+ # Patch TdmsFile to return our mock file
860+ mocker .patch ("sift_py.data_import.tdms.TdmsFile" , return_value = mock_waveform_tdms_file )
861+
862+ # Patch requests.Session.post and patch requests.Session.patch for metadata update
863+ mock_requests_post = mocker .patch ("sift_py.rest.requests.Session.post" )
864+ mock_requests_post .return_value = MockResponse ()
865+ mock_requests_patch = mocker .patch ("sift_py.rest.requests.Session.patch" )
866+ mock_requests_patch .return_value = MockResponse (
867+ status_code = 200 ,
868+ text = json .dumps ({"run" : {"runId" : "existing_run_id" }}),
869+ )
870+
871+ svc = TdmsUploadService (rest_config )
872+
873+ # Should succeed and call _add_metadata_to_run via PATCH with metadata if only run_id is provided
874+ svc .upload (
875+ "some_tdms.tdms" ,
876+ "asset_name" ,
877+ include_metadata = True ,
878+ run_id = "existing_run_id" ,
879+ )
880+
881+ # Check that PATCH was called for metadata update
882+ patch_call = mock_requests_patch .call_args_list [0 ]
883+ patch_data = json .loads (patch_call .kwargs ["data" ])
884+ assert patch_data ["run" ]["runId" ] == "existing_run_id"
885+ assert "metadata" in patch_data ["run" ]
886+ assert patch_data ["updateMask" ] == "metadata"
887+ # Metadata keys should match those in the mock_tdms_file properties
888+ keys = [md ["key" ]["name" ] for md in patch_data ["run" ]["metadata" ]]
889+ assert set (keys ) == set (mock_waveform_tdms_file .properties .keys ())
0 commit comments