如何嘲笑GoogleAppClient.discovery.build

2024-09-28 01:25:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图模拟对计算引擎进行API调用以列出vm的结果。但不幸的是不能模仿一个精确的函数。在

我尝试过使用补丁和MOCK方法来模拟特定的调用,但仍然失败

在代码.py文件看起来像这样

import googleapiclient.discovery
import logging

class Service:
    def __init__(self, project, event):
        self.project_id = project
        self.compute = googleapiclient.discovery.build('compute', 'v1',
                                                       cache_discovery=False)
        self.event = event
        self.zones = self._validate_event()

    def _validate_event(self):
        if "jsonPayload" not in self.event:
            zones = self.compute.zones().list(
                project=self.project_id).execute()['items']

        else:
            zones = self.compute.zones().get(project=self.project_id,
                                             zone=self.event["jsonPayload"]
                                             ["resource"]["zone"]).execute()

        logging.debug(f"Identified Zones are {zones}")
        return [zone["name"] for zone in zones]

我的测试文件如下所示

^{pr2}$

在上面的测试用例中,我希望在打印时看到“eu-west-1”作为值目标区域在


Tags: 文件importselfprojecteventidzonelogging

热门问题