コード例 #1
0
def test_find_file_by_search_terms(
        gdrive_service: DriveService,
        gdrive_folder: DriveObject,
        subdir="test_find_file_by_search_terms",
        search_terms=("hello", "world", "who"),
        filenames=("hello____world-who.txt", "hello____world----who.txt"),
):
    """Search for files by search terms."""

    subdir = mkdir(gdrive_service, gdrive_folder.id, subdir)

    put_request = put_file(gdrive_service, subdir.id, filenames[0])
    with put_request as fh:
        fh.write("this is random text 1")

    # find the file and retrieve the contents.
    result = find_file_by_search_terms(gdrive_service, subdir.id, search_terms)
    assert result.id == put_request.id

    # find the file and retrieve the contents.  this should fail because we
    # have an extra term that is not satisfied.
    with pytest.raises(RuntimeError):
        result = find_file_by_search_terms(gdrive_service, subdir.id,
                                           search_terms + ("chicken", ))

    put_request = put_file(gdrive_service, subdir.id, filenames[1])
    with put_request as fh:
        fh.write("this is random text 2")

    # find the file and retrieve the contents.  this should fail because we
    # now have multiple matching files.
    with pytest.raises(RuntimeError):
        result = find_file_by_search_terms(gdrive_service, subdir.id,
                                           search_terms)
コード例 #2
0
def test_find_file_by_name_not_dir(
    gdrive_service,
    gdrive_folder: DriveObject,
    new_filename="find_file_by_name_not_dir.txt",
):
    """Tests that we can search for a file and not get matched to a directory that is present."""
    mkdir(gdrive_service, gdrive_folder.id, new_filename)
    with pytest.raises(RuntimeError):
        find_file_by_name(gdrive_service, gdrive_folder.id, new_filename)

    # now we put a file with the same name (yeah, google drive is weird in that this is permitted)
    put_request = put_file(gdrive_service, gdrive_folder.id, new_filename)
    with put_request as fh:
        fh.write("this is random text 1")

    found_file_id = find_file_by_name(gdrive_service, gdrive_folder.id,
                                      new_filename).id
    assert put_request.id == found_file_id
コード例 #3
0
def gdrive_folder(gdrive_service: DriveService) -> DriveObject:
    """This fixture sets up a folder with a unique name on gdrive and removes it and its contents at
    the end of the test.  The folder will always be at the top level of the account, i.e., the
    folder's parent is `root`.  A DriveObject object is returned."""
    name = f"TEST-PLEASE-DELETE-ME-{uuid.uuid4()}"
    drive_folder = mkdir(gdrive_service, "root", name)
    yield drive_folder

    gdrive_service.files().delete(fileId=drive_folder.id).execute(
        num_retries=NUM_RETRIES)
コード例 #4
0
def test_mkdir(
    gdrive_service: DriveService,
    gdrive_folder: DriveObject,
    new_folder_name="mkdir-test",
):
    subdir = mkdir(gdrive_service, gdrive_folder.id, new_folder_name)
    traversed_folder_id = get_folder_id_of_path(
        gdrive_service, [gdrive_folder.name, new_folder_name]
    )
    assert subdir.id == traversed_folder_id
コード例 #5
0
def test_get_contents_by_folder_id_types(
    gdrive_service: DriveService,
    gdrive_folder: DriveObject,
    new_folder_name="test_get_contents_by_folder_id_types",
):
    """Verify that get_contents_by_folder_id can filter by only files correctly."""
    subdir = mkdir(gdrive_service, gdrive_folder.id, new_folder_name)

    # put a file and subdirectory there.
    with put_file(gdrive_service, subdir.id, "file") as fh:
        fh.write(b"this is a file")

    mkdir(gdrive_service, subdir.id, "another-subdir")

    results = get_contents_by_folder_id(gdrive_service, subdir.id, only_files=True)
    assert len(results) == 1
    assert results[0].name == "file"

    results = get_contents_by_folder_id(gdrive_service, subdir.id, only_files=False)
    assert len(results) == 2
    assert any(result.name == "file" for result in results)
    assert any(result.name == "another-subdir" for result in results)
コード例 #6
0
def test_get_contents_by_folder_id_paging(
    gdrive_service: DriveService,
    gdrive_folder: DriveObject,
    new_folder_name="test_get_contents_by_folder_id_paging",
):
    """Verify that get_contents_by_folder_id can iterate through pages of results correctly."""
    subdir = mkdir(gdrive_service, gdrive_folder.id, new_folder_name)

    # put two files there.
    with put_file(gdrive_service, subdir.id, "file0") as fh:
        fh.write(b"this is a file")
    with put_file(gdrive_service, subdir.id, "file1") as fh:
        fh.write(b"this is a file")

    # even with page_size=1, we should be able to retrieve all the results.
    results = get_contents_by_folder_id(
        gdrive_service, subdir.id, only_files=True, page_size=1
    )
    assert len(results) == 2
コード例 #7
0
def test_find_file_by_search_terms_exclude_contents(
        gdrive_service: DriveService,
        gdrive_folder: DriveObject,
        subdir="test_find_file_by_search_terms_exclude_contents",
        search_terms=("hello", "world", "who"),
        filenames=("keywords_not_in_filename.txt",
                   "hello____world----who.txt"),
):
    """Search for files by search terms."""

    subdir = mkdir(gdrive_service, gdrive_folder.id, subdir)

    put_request = put_file(gdrive_service, subdir.id, filenames[0])
    with put_request as fh:
        fh.write(" ".join(search_terms))

    put_request = put_file(gdrive_service, subdir.id, filenames[1])
    with put_request as fh:
        fh.write("this is random text 2")

    # find the file and retrieve the contents.
    result = find_file_by_search_terms(gdrive_service, subdir.id, search_terms)
    assert result.id == put_request.id