def test_find_file_by_name_most_recent(
    gdrive_service,
    gdrive_folder: DriveObject,
    filename="test_find_file_by_name_most_recent.txt",
):
    """Puts a file, and then put it again with overwrite_if_present=False.  Finding by
    filename, using require single result mode, should fail.  Finding by the filename,
    using the most recent mode, should find the second file."""
    put_request = put_file(gdrive_service, gdrive_folder.id, filename)
    with put_request as fh:
        fh.write("first")

    put_request = put_file(gdrive_service,
                           gdrive_folder.id,
                           filename,
                           overwrite_if_present=False)
    with put_request as fh:
        fh.write("second")

    with pytest.raises(MultipleMatchesError):
        find_file_by_name(gdrive_service, gdrive_folder.id, filename)

    id = find_file_by_name(gdrive_service, gdrive_folder.id, filename,
                           FindMode.MOST_RECENTLY_MODIFIED).id
    assert id == put_request.id

    with pytest.raises(NoMatchesError):
        find_file_by_name(
            gdrive_service,
            gdrive_folder.id,
            "this_file_does_not_exist",
            FindMode.MOST_RECENTLY_MODIFIED,
        )
def test_find_file_by_name(
    gdrive_service,
    gdrive_folder: DriveObject,
    new_filename="find_file_by_name.txt",
):
    """Tests that we can search for a file by name successfully and unsuccessfully."""
    with pytest.raises(RuntimeError):
        find_file_by_name(gdrive_service, gdrive_folder.id, new_filename)
    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
Exemple #3
0
        def retrieve_file(filename: str) -> ChecksummedFileInfo:
            http_client = getattr(tls, "http", None)
            if http_client is None:
                http_client = new_http_client_from_service(self.drive_service)
                setattr(tls, "http", http_client)

            drive_obj = drive.find_file_by_name(
                self.drive_service,
                self.folder_id,
                filename,
                drive.FindMode.MOST_RECENTLY_MODIFIED,
                http=http_client,
            )

            with drive.get_file(self.drive_service,
                                drive_obj.id,
                                http=http_client) as fh:
                data = fh.read()
                if isinstance(data, str):
                    data_fh = StringIO(data)
                elif isinstance(data, bytes):
                    data_fh = BytesIO(data)

                data_fh.name = filename  # needed for readers that expect a name attr
                return ChecksummedFileInfo(filename, data_fh,
                                           drive_obj.md5Checksum)
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
Exemple #5
0
 def set_qPCR_processed_timestamp(self):
     """Extract the processed time from the results file timestamp"""
     if self.qPCR_processing_complete:
         file = drive.find_file_by_name(
             self.drive_service,
             self.results_folder_id,
             self.results_filename,
             drive.FindMode.MOST_RECENTLY_MODIFIED,
         )
         time_str = file.modifiedTime
         # gdrive saving these files as UTC need to convert
         date_utc = datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S.%f%z")
         pst_date = date_utc.astimezone(timezone("US/Pacific"))
         pst_date.strftime("%m/%d/%Y, %H:%M:%S")
         self.qPCR_processed_timestamp = pst_date.strftime("%m/%d/%Y, %H:%M:%S")