@@ -410,6 +410,63 @@ def _checked_write(path, content):
410410 with pytest .raises (ValueError , match = "malformed" ):
411411 collection .download (file_link = bad_file , local_path = target_dir )
412412
413+ def test_read (collection : FileCollection , session ):
414+ """
415+ Test that reading a file works as expected.
416+
417+ """
418+ # Given
419+ filename = 'diagram.pdf'
420+ url = f"projects/{ collection .project_id } /datasets/{ collection .dataset_id } /files/{ uuid4 ()} /versions/{ uuid4 ()} "
421+ file = FileLink .build (FileLinkDataFactory (url = url , filename = filename ))
422+ pre_signed_url = "http://files.citrine.io/secret-codes/jiifema987pjfsda" # arbitrary
423+ session .set_response ({
424+ 'pre_signed_read_link' : pre_signed_url ,
425+ })
426+
427+ with requests_mock .mock () as mock_get :
428+ mock_get .get (pre_signed_url , text = "lorem ipsum" )
429+ # When
430+ io = collection .read (file_link = file )
431+ assert io .decode ('UTF-8' ) == 'lorem ipsum'
432+ # When
433+ assert mock_get .call_count == 1
434+ expected_call = FakeCall (
435+ method = 'GET' ,
436+ path = url + '/content-link'
437+ )
438+ assert expected_call == session .last_call
439+
440+
441+
442+ bad_url = f"bin/uuid3/versions/uuid4"
443+ bad_file = FileLink .build (FileLinkDataFactory (url = bad_url , filename = filename ))
444+ with pytest .raises (ValueError , match = "malformed" ):
445+ collection .read (file_link = bad_file )
446+
447+
448+ def test_external_file_read (collection : FileCollection , session ):
449+ """
450+ Test that reading a file works as expected for external files.
451+
452+ """
453+ # Given
454+ filename = 'spreadsheet.xlsx'
455+ url = "http://customer.com/data-lake/files/123/versions/456"
456+ file = FileLink .build (FileLinkDataFactory (url = url , filename = filename ))
457+
458+
459+ with requests_mock .mock () as mock_get :
460+ mock_get .get (url , text = '010111011' )
461+
462+ # When
463+ io = collection .read (file_link = file )
464+ assert io .decode ('UTF-8' ) == '010111011'
465+
466+ # When
467+ assert mock_get .call_count == 1
468+
469+ # assert local_path.read_text() == '010111011'
413470
414471def test_external_file_download (collection : FileCollection , session , tmpdir ):
415472 """
0 commit comments