如何测试从API读取,更新Excel表并将文件上传到Dropbox的Python脚本

发布于 2025-02-11 10:34:47 字数 1421 浏览 0 评论 0原文

我有一个非常简单的Python脚本,如下所示。它所做的就是从API读取数据,将数据添加到Excel文件中,然后将Excel文件上传到Dropbox。我知道单位测试应该在结构上非常简单,并且仅测试一件事,而对其他任何东西没有依赖性(Excel,Dropbox)。我可以测试此脚本进行完整集成测试,写入Excel文件并检查它的唯一方法,而对于Dropbox文件夹来说是相同的?

def get_info(url):
    response = session.get(url)
    sold_units = json.loads(response.text)
    return info

def populate_excel_file(info):
    for unit in info:
        last_update_date = parser.parse(unit['lastupdatedate'])
        if now-timedelta(hours=24) <= last_update_date <= now:
            new_used = unit['NewUsed']
            make = unit['Make']
            model = unit['Model']
            model_year = unit['ModelYear']
            dsrp = unit['DSRP']
            row = sheet.max_row + 1
            sheet.cell(column=1, row=row, value=unit['lastupdatedate'])
            sheet.cell(column=2, row=row, value=make)
            sheet.cell(column=3, row=row, value=model)
            sheet.cell(column=4, row=row, value=model_year)
            sheet.cell(column=5, row=row, value=new_used)
            sheet.cell(column=6, row=row, value=dsrp)

    workbook.save(filename='BookNEW.xlsx')

def upload_file(file_from, file_to):
    dbx = dropbox.Dropbox(access_token)
    f = open(file_from, 'rb')
    dbx.files_upload(f.read(), file_to)

info = get_info("https://api.call")
populate_excel_file(info)

file_from = 'BookNEW.xlsx'
file_to = 'Book.xlsx'
upload_file(file_from, file_to)

I have a very simple python script as shown below. All it does is read data from an API, add data to an excel file, and then uploads that excel file to dropbox. I'm aware that unit tests are supposed to be very simple in structure and only test one thing, with no dependencies on anything else (excel, dropbox). Is the only way I can test this script to do full integration tests, writing to an excel file and checking it, and the same for a dropbox folder?

def get_info(url):
    response = session.get(url)
    sold_units = json.loads(response.text)
    return info

def populate_excel_file(info):
    for unit in info:
        last_update_date = parser.parse(unit['lastupdatedate'])
        if now-timedelta(hours=24) <= last_update_date <= now:
            new_used = unit['NewUsed']
            make = unit['Make']
            model = unit['Model']
            model_year = unit['ModelYear']
            dsrp = unit['DSRP']
            row = sheet.max_row + 1
            sheet.cell(column=1, row=row, value=unit['lastupdatedate'])
            sheet.cell(column=2, row=row, value=make)
            sheet.cell(column=3, row=row, value=model)
            sheet.cell(column=4, row=row, value=model_year)
            sheet.cell(column=5, row=row, value=new_used)
            sheet.cell(column=6, row=row, value=dsrp)

    workbook.save(filename='BookNEW.xlsx')

def upload_file(file_from, file_to):
    dbx = dropbox.Dropbox(access_token)
    f = open(file_from, 'rb')
    dbx.files_upload(f.read(), file_to)

info = get_info("https://api.call")
populate_excel_file(info)

file_from = 'BookNEW.xlsx'
file_to = 'Book.xlsx'
upload_file(file_from, file_to)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文