gpt4 book ai didi

python - 如何模拟 googleapiclient.discovery.build

转载 作者:行者123 更新时间:2023-12-04 14:19:15 25 4
gpt4 key购买 nike

我正在尝试模拟对计算引擎进行 API 调用以列出 VM 的结果。但不幸的是无法模拟一个确切的函数。

我尝试使用 PATCH 和 MOCK 方法来模拟特定的调用,但仍然不成功

code.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]

我的测试文件看起来像这样

# in-built
from unittest import TestCase
from unittest.mock import patch

# custom
import code


class TestServiceModule(TestCase):
def setUp(self):
self.project_id = "sample-project-id"

@patch('code.googleapiclient.discovery')
def test__validate_event_with_empty_inputs(self, mock_discovery):
mock_discovery.build.zones.list.execute.return_value = {"items": [
{
"name": "eu-west-1"
}
]}

obj = code.Service(event={}, project=self.project_id)

print(obj.zones)



在上面的测试用例中,我希望在打印 obj.zones 时看到“eu-west-1”作为值

最佳答案

你没有 mock googleapiclient.discovery.build方法正确。这是单元测试解决方案:
例如。code.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]
test_code.py :
from unittest import TestCase, main
from unittest.mock import patch
import code


class TestService(TestCase):
def setUp(self):
self.project_id = "sample-project-id"

@patch('code.googleapiclient.discovery')
def test__validate_event_with_empty_inputs(self, mock_discovery):
# Arrange
mock_discovery.build.return_value.zones.return_value.list.return_value.execute.return_value = {
"items": [{"name": "eu-west-1"}]}

# Act
obj = code.Service(event={}, project=self.project_id)

# Assert
mock_discovery.build.assert_called_once_with('compute', 'v1', cache_discovery=False)
mock_discovery.build.return_value.zones.assert_called_once()
mock_discovery.build.return_value.zones.return_value.list.assert_called_once_with(project='sample-project-id')
mock_discovery.build.return_value.zones.return_value.list.return_value.execute.assert_called_once()
self.assertEqual(obj.zones, ["eu-west-1"])


if __name__ == '__main__':
main()
带有覆盖率报告的单元测试结果:
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
Name Stmts Miss Cover Missing
-----------------------------------------------------------------------
src/stackoverflow/56794377/code.py 14 1 93% 16
src/stackoverflow/56794377/test_code.py 16 0 100%
-----------------------------------------------------------------------
TOTAL 30 1 97%
版本:
  • google-api-python-client==1.12.3
  • Python 3.7.5
  • 关于python - 如何模拟 googleapiclient.discovery.build,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56794377/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com