如何模拟google.cloud.storage.blob.blob对象列表?

cgh8pdjw  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(182)

我有一个函数,它接收了一组google.cloud.storage.blob.blob对象列表,以按我要进行单元测试的创建日期进行解析。我该怎么嘲笑它呢?我曾考虑过修补,但我意识到这更多是为了测试函数中的调用。如何模拟这些blob的输入?这是我要测试的功能:

def bucket_parser(objects,start_time,end_time):
    file_count = 0
    latest_timestamp = datetime.datetime.strptime('0000','%H%M')
    for file in objects:
        if file.time_created.time().replace(tzinfo=None) <= end_time and file.time_created.time().replace(tzinfo=None) >= start_time:
            file_count += 1
        if file.time_created.replace(tzinfo=None) > latest_timestamp.replace(tzinfo=None):
            latest_timestamp = file.time_created
    return file_count , latest_timestamp.time()

其中对象为<class'google.cloud.storage.blob.blob'>且开始和结束时间为<class'datetime.time'>。我如何使用mock对这个函数进行单元测试?我觉得被卡住了,因为我不知道该怎么做。非常感谢。
编辑:这是从bucket中获取blob的代码,并将它们传递到上述函数中:

def bucket_scraper(bucket_name,bucket_prefix):
    '''
    Input: Bucket , Bucket Prefix
    Output: All objects/files in specified bucket prefix
    '''
    client = storage.Client()
    prefix_files = list(client.list_blobs(bucket_name,max_results = 700,page_token=None,prefix=bucket_prefix,delimiter='/'))
    return prefix_files

返回的前缀_文件是进入bucket_解析器函数的文件。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题