Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions pydrive2/test/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from googleapiclient.http import HttpRequest

from pydrive2.drive import GoogleDrive
from pydrive2.files import FOLDER_MIME_TYPE
from pydrive2.fs import GDriveFileSystem
from pydrive2.test.test_util import (
GDRIVE_USER_CREDENTIALS_DATA,
Expand Down Expand Up @@ -171,6 +172,43 @@ def test_invalidating_parent_reloads_replaced_mount(fs, remote_dir):
assert reader.ls(mount) == [mount + "/after"]


def create_on_drive(service_auth, parent_id, name, content=None):
"""Create a file, or a folder without content, as another client would."""
file = GoogleDrive(service_auth).CreateFile(
{"name": name, "parents": [parent_id]}
| ({"mimeType": FOLDER_MIME_TYPE} if content is None else {})
)
if content is not None:
file.SetContentString(content)
pydrive_retry(file.Upload)
return file


@pytest.fixture
def remote_dir_id(service_auth, fs_factory, remote_dir):
"""Create remote_dir through the core API and return its Drive ID."""
base_id = fs_factory()[1]["id"]
name = posixpath.basename(remote_dir)
return create_on_drive(service_auth, base_id, name)["id"]


def test_folder_renamed_by_another_client_is_missing_after_invalidate_cache(
service_auth, fs, remote_dir, remote_dir_id
):
folder = create_on_drive(service_auth, remote_dir_id, "before")
create_on_drive(service_auth, folder["id"], "file.txt", "data")
assert fs.ls(f"{remote_dir}/before") == [f"{remote_dir}/before/file.txt"]

folder["name"] = "after"
pydrive_retry(folder.Upload)

fs.invalidate_cache(f"{remote_dir}/before")
assert not fs.exists(f"{remote_dir}/before")
assert not fs.exists(f"{remote_dir}/before/file.txt")
assert fs.ls(remote_dir) == [f"{remote_dir}/after/"]
assert fs.cat_file(f"{remote_dir}/after/file.txt") == b"data"


def test_rmdir_only_removes_empty_folders(fs, fs_factory, remote_dir):
path = remote_dir + "/empty"
fs.makedirs(path)
Expand Down Expand Up @@ -294,6 +332,43 @@ def test_ops_work_with_duplicate_names(fs_factory, remote_dir):
assert set(result) == {dir_path + file for file in ["a.txt", "b.txt"]}


@pytest.fixture
def same_name_files(service_auth, remote_dir_id):
"""Two files named same.txt from another client, content by Drive ID."""
texts = ["first", "second file"]
ids = [
create_on_drive(service_auth, remote_dir_id, "same.txt", text)["id"]
for text in texts
]
return dict(zip(ids, texts))


def test_name_two_files_share_reads_as_the_file_with_the_lowest_id(
fs_factory, remote_dir, same_name_files
):
lowest = same_name_files[min(same_name_files)]
path = f"{remote_dir}/same.txt"

fs, _ = fs_factory()
assert fs.ls(remote_dir) == [path, path]
assert fs.cat_file(path) == lowest.encode()
assert fs.info(path)["size"] == len(lowest)
assert fs.find(remote_dir, detail=True)[path]["size"] == len(lowest)


def test_rm_file_of_a_name_two_files_share_leaves_the_other_file(
fs_factory, remote_dir, same_name_files
):
other = same_name_files[max(same_name_files)]
path = f"{remote_dir}/same.txt"

fs_factory()[0].rm_file(path)

reader, _ = fs_factory()
assert reader.ls(remote_dir) == [path]
assert reader.cat_file(path) == other.encode()


def test_ls_non_existing_dir(fs, remote_dir):
with pytest.raises(FileNotFoundError):
fs.ls(remote_dir + "dir/")
Expand Down
Loading