def test_gs_file(self): """Tests accessing a Google Cloud Storage file. """ # Test the blob property # File exists gs_file_exists = StorageFile("gs://aries_test/file_in_root.txt") self.assertFalse(gs_file_exists.is_gz()) self.assertTrue(gs_file_exists.blob.exists()) self.assertEqual(gs_file_exists.size, 34) # File does not exists gs_file_null = StorageFile("gs://aries_test/abc.txt") self.assertFalse(gs_file_null.blob.exists()) # Test the read() method self.assertEqual(gs_file_exists.read(), b'This is a file in the bucket root.') with self.assertRaises(Exception): gs_file_null.read() # Test write into a new file with gs_file_null('w+b') as f: f.write(b"abc") f.seek(0) self.assertEqual(f.read(), b"abc") # File will be uploaded to bucket after closed. # Test reading from the bucket self.assertEqual(gs_file_null.read(), b"abc") gs_file_null.delete()
def test_upload_from_file(self): gs_file = StorageFile("gs://aries_test/local_upload.txt") # Try to upload a file that does not exist. local_file_non_exist = os.path.join(os.path.dirname(__file__), "abc.txt") with self.assertRaises(FileNotFoundError): gs_file.upload_from_file(local_file_non_exist) # Upload a file and check the content. local_file = os.path.join(os.path.dirname(__file__), "fixtures", "test_file.txt") gs_file.upload_from_file(local_file) self.assertEqual(gs_file.read(), b'This is a local test file.\n') gs_file.delete()
def test_binary_read_write(self): # File does not exist, a new one will be created file_uri = os.path.join(self.TEST_ROOT, "test.txt") storage_file = StorageFile(file_uri).open("wb") self.assertEqual(storage_file.scheme, self.SCHEME) self.assertTrue(storage_file.seekable()) self.assertFalse(storage_file.readable()) self.assertEqual(storage_file.write(b"abc"), 3) self.assertEqual(storage_file.tell(), 3) self.assertEqual(storage_file.write(b"def"), 3) self.assertEqual(storage_file.tell(), 6) storage_file.close() self.assertTrue(storage_file.exists()) storage_file.open('rb') self.assertEqual(storage_file.read(), b"abcdef") storage_file.close() storage_file.delete() self.assertFalse(storage_file.exists())
def test_create_copy_and_delete_file(self): new_folder_uri = os.path.join(self.TEST_ROOT, "new_folder") with TempFolder(new_folder_uri) as folder: self.assertTrue(folder.is_empty()) # Create a sub folder inside the new folder sub_folder_uri = os.path.join(new_folder_uri, "sub_folder") logger.debug(sub_folder_uri) sub_folder = StorageFolder(sub_folder_uri).create() self.assertTrue(sub_folder.exists()) # Copy an empty file src_file_path = os.path.join(self.TEST_ROOT, "test_folder_0", "empty_file") dst_file_path = os.path.join(new_folder_uri, "copied_file") f = StorageFile(src_file_path) logger.debug(f.exists()) time.sleep(2) f.copy(dst_file_path) self.assertTrue(StorageFile(dst_file_path).exists()) # Copy a file with content and replace the empty file src_file_path = os.path.join(self.TEST_ROOT, "test_folder_0", "abc.txt") dst_file_path = os.path.join(new_folder_uri, "copied_file") f = StorageFile(src_file_path) f.copy(dst_file_path) dst_file = StorageFile(dst_file_path) self.assertTrue(dst_file.exists()) # Use the shortcut to read file, the content will be binary. self.assertEqual(dst_file.read(), b"abc\ncba\n") # Empty the folder. This should delete file and sub folder only folder.empty() self.assertTrue(folder.exists()) self.assertTrue(folder.is_empty()) self.assertFalse(sub_folder.exists()) self.assertFalse(dst_file.exists())