def test_rename_only_target_dir_no_basename(self, remote_tmpdir: DriveFolder): remote_file = remote_tmpdir.new_file("filename") remote_file.upload_empty() remote_tmpdir.create_path("1/2/3") remote_file.rename("1/2/3") assert remote_tmpdir.child_from_path("1/2/3/filename") == remote_file
def test_children_trashed(self, remote_tmpdir: DriveFolder): remote_file = remote_tmpdir.new_file(random_string()) remote_file.upload_empty() remote_file.trash() assert remote_file not in remote_tmpdir.children() assert remote_file in remote_tmpdir.children(trashed=True)
def test_children_onlyfiles(self, remote_tmpdir: DriveFolder): remote_folder = remote_tmpdir.mkdir(random_string()) remote_file = remote_tmpdir.new_file(random_string()) remote_file.upload_empty() filelist = list(remote_tmpdir.children(folders=False)) assert remote_folder not in filelist assert remote_file in filelist
def test_rename_flat(self, remote_tmpdir: DriveFolder): remote_file = remote_tmpdir.new_file(random_string()) remote_file.upload_empty() new_name = random_string() remote_file.rename(new_name) assert remote_file.name == new_name assert remote_tmpdir.child(new_name) == remote_file
def test_child_all(self, remote_tmpdir: DriveFolder): foldername = random_string() folder = remote_tmpdir.mkdir(foldername) assert folder == remote_tmpdir.child(foldername) filename = random_string() file_ = remote_tmpdir.new_file(filename) file_.upload_empty() assert file_ == remote_tmpdir.child(filename)
def test_upload(self, tmpfile: Path, remote_tmpdir: DriveFolder): local_file = tmpfile(size_bytes=1024) # No chunksize specified remote_file = remote_tmpdir.new_file(str(local_file.parent)) remote_file.upload(str(local_file)) assert md5_file(local_file) == remote_file.md5sum # Chunksize = None remote_file = remote_tmpdir.new_file(str(local_file.parent)) remote_file.upload(str(local_file), chunksize=None) assert md5_file(local_file) == remote_file.md5sum
def test_upload_resume_with_stored_uri(self, tmpfile: Path, remote_tmpdir: DriveFolder): chunksize = chunksize_min local_file = tmpfile(size_bytes=chunksize * 2) remote_file = remote_tmpdir.new_file(local_file.name) progress = ProgressExtractor(abort_at=0.0) with pytest.raises(AbortTransfer): remote_file.upload(str(local_file), chunksize=chunksize, progress_handler=progress.update_status) remote_file = remote_tmpdir.new_file(local_file.name) remote_file.upload(str(local_file), chunksize=chunksize, resumable_uri=progress.status.resumable_uri)
def test_child_from_path(self, remote_tmpdir: DriveFolder): depth = 3 folder = [remote_tmpdir] foldername = [""] for i in range(1, depth + 1): foldername.append(random_string()) folder.append(folder[i - 1].mkdir(foldername[i])) path = "/".join(foldername) assert remote_tmpdir.child_from_path(path) == folder[depth] # test . and .. assert remote_tmpdir.child_from_path(".") == remote_tmpdir path = "/".join((".", foldername[1], foldername[2], "..")) assert remote_tmpdir.child_from_path(path) == folder[1]
def test_upload_remote_file_does_not_match(self, tmpfile: Path, remote_tmpdir: DriveFolder): chunksize = chunksize_min local_file_orig = tmpfile(size_bytes=chunksize * 3) local_file_same_size = tmpfile(size_bytes=chunksize * 3) local_file_different_size = tmpfile(size_bytes=None) shutil.copy(str(local_file_orig), str(local_file_different_size)) with local_file_different_size.open('ba') as fh: fh.write(os.urandom(chunksize)) ## Test size mismatch remote_file = remote_tmpdir.new_file(local_file_orig.name) progress = ProgressExtractor(abort_at=0.0) with pytest.raises(AbortTransfer): remote_file.upload(str(local_file_orig), chunksize=chunksize, progress_handler=progress.update_status) assert progress.status.resumable_progress == chunksize with pytest.raises(HttpError): remote_file.upload(str(local_file_different_size), chunksize=chunksize) ## Test checksum mismatch on intermediate chunk remote_file = remote_tmpdir.new_file(local_file_orig.name) progress = ProgressExtractor(abort_at=0.0) with pytest.raises(AbortTransfer): remote_file.upload(str(local_file_orig), chunksize=chunksize, progress_handler=progress.update_status) assert progress.status.resumable_progress == chunksize with pytest.raises(CheckSumError): remote_file.upload(str(local_file_same_size), chunksize=chunksize) assert remote_file.resumable_uri == None ## Test checksum mismatch on last chunk remote_file = remote_tmpdir.new_file(local_file_orig.name) progress = ProgressExtractor(abort_at=0.6) with pytest.raises(AbortTransfer): remote_file.upload(str(local_file_orig), chunksize=chunksize, progress_handler=progress.update_status) assert progress.status.resumable_progress == 2 * chunksize with pytest.raises(CheckSumError): remote_file.upload(str(local_file_same_size), chunksize=chunksize) assert remote_file.resumable_uri == None
def test_upload_chunksize_bigger_than_filesize(self, tmpfile: Path, remote_tmpdir: DriveFolder): chunksize = 100 local_file = tmpfile(size_bytes=int(chunksize / 2)) remote_file = remote_tmpdir.new_file(local_file.name) remote_file.upload(str(local_file), chunksize=chunksize) assert md5_file(local_file) == remote_file.md5sum
def test_new_file(self, tmpfile: Path, remote_tmpdir: DriveFolder): new_file = remote_tmpdir.new_file(random_string()) assert isinstance(new_file, (DriveFile)) assert new_file.isfolder() == False assert new_file.id == None with pytest.raises(FileNotFoundError): new_file.download(tmpfile)
def test_upload_chunksize_too_small(self, tmpfile: Path, remote_tmpdir: DriveFolder): chunksize = 1 local_file = tmpfile(size_bytes=chunksize * 2) remote_file = remote_tmpdir.new_file(local_file.name) with pytest.raises(HttpError): remote_file.upload(str(local_file), chunksize=chunksize)
def test_children(self, remote_tmp_subdir: DriveFolder, remote_tmpfile): file_count = 5 created_files = set() for _ in range(0, file_count): created_files.add(remote_tmpfile(subdir=remote_tmp_subdir)) listed_files = set(remote_tmp_subdir.children()) assert created_files == listed_files
def test_children_ordered(self, remote_tmp_subdir: DriveFolder, remote_tmpfile): file_count = 5 created_files = [] for _ in range(0, file_count): created_files.append(remote_tmpfile(subdir=remote_tmp_subdir)) created_files.sort(key=lambda x: x.name) listed_files = list(remote_tmp_subdir.children(orderBy="name")) assert created_files == listed_files
def test_upload_invalid_resumable_uri(self, tmpfile: Path, remote_tmpdir: DriveFolder): chunksize = chunksize_min local_file = tmpfile(size_bytes=chunksize * 2) remote_file = remote_tmpdir.new_file(local_file.name) progress = ProgressExtractor(abort_at=0.0) with pytest.raises(AbortTransfer): remote_file.upload(str(local_file), chunksize=chunksize, progress_handler=progress.update_status) assert progress.status.resumable_progress == chunksize remote_file.resumable_uri = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=invalid_id" with pytest.raises(HttpError): remote_file.upload(str(local_file))
def test_upload_progress_resume(self, tmpfile: Path, remote_tmpdir: DriveFolder): chunksize = chunksize_min local_file = tmpfile(size_bytes=chunksize * 5) remote_file = remote_tmpdir.new_file(local_file.name) progress = ProgressExtractor(abort_at=0.4) with pytest.raises(AbortTransfer): remote_file.upload(str(local_file), chunksize=chunksize, progress_handler=progress.update_status) assert progress.status.resumable_progress == 2 * chunksize progress.abort_at = 1 remote_file.upload(str(local_file), chunksize=chunksize, progress_handler=progress.update_status) assert progress.chunks_since_last_abort == 3
def test_create_path(self, remote_tmpdir: DriveFolder): # depends on test_child_from_path depth = 3 path = "/".join((random_string() for _ in range(0, depth))) folder1 = remote_tmpdir.create_path(path) assert folder1 == remote_tmpdir.child_from_path(path) folder2 = remote_tmpdir.create_path(path) assert folder1 == folder2 # test . and .. assert remote_tmpdir.create_path(".") == remote_tmpdir folder = remote_tmpdir.create_path("./1/2a/../2b/") assert folder == remote_tmpdir.child_from_path("1/2b")
def remote_tmp_subdir(remote_tmpdir: DriveFolder) -> DriveFolder: subdir = remote_tmpdir.mkdir(random_string()) yield subdir subdir.remove()
def test_upload_empty_file(self, tmpfile: Path, remote_tmpdir: DriveFolder): local_file = tmpfile(size_bytes=0) remote_file = remote_tmpdir.new_file(str(local_file.parent)) remote_file.upload(str(local_file)) assert md5_file(local_file) == remote_file.md5sum
def test_rename_target_dir_does_not_exist(self, remote_tmpdir: DriveFolder): remote_file = remote_tmpdir.new_file(random_string()) remote_file.upload_empty() with pytest.raises(FileNotFoundError): remote_file.rename("a/b")
def test_mkdir_exists_folder(self, remote_tmpdir: DriveFolder): foldername = random_string() folder1 = remote_tmpdir.mkdir(foldername) folder2 = remote_tmpdir.mkdir(foldername) assert folder1 == folder2
def test_rename_target_exists(self, remote_tmpdir: DriveFolder): remote_file = remote_tmpdir.new_file(random_string()) remote_file.upload_empty() remote_file.parent.new_file("existing_file").upload_empty() remote_file.rename("existing_file") assert remote_tmpdir.child("existing_file") == remote_file
def test_rename_subdir(self, remote_tmpdir: DriveFolder): remote_file = remote_tmpdir.new_file(random_string()) remote_file.upload_empty() remote_tmpdir.create_path("1/2/3") remote_file.rename("1/2/3/b") assert remote_tmpdir.child_from_path("1/2/3/b") == remote_file
def test_rename_target_dir_is_a_file(self, remote_tmpdir: DriveFolder): remote_file = remote_tmpdir.new_file(random_string()) remote_file.upload_empty() remote_tmpdir.new_file("file").upload_empty() with pytest.raises(NotADirectoryError): remote_file.rename("file/b")
def test_isempty(self, remote_tmp_subdir: DriveFolder): assert remote_tmp_subdir.isempty() == True remote_tmp_subdir.new_file(random_string()).upload_empty() assert remote_tmp_subdir.isempty() == False
def test_mkdir_exists_file(self, remote_tmpdir: DriveFolder): foldername = random_string() remote_tmpdir.new_file(foldername).upload_empty() with pytest.raises(FileExistsError): remote_tmpdir.mkdir(foldername)