Макет дает TypeError («Объект« MachineInfo »не является итератором») - PullRequest
0 голосов
/ 10 февраля 2020

Я пытаюсь смоделировать функцию, которая возвращает объект azure для возврата объекта python. Цель состоит в том, чтобы проверить эту функцию:

@retry(stop=stop_after_attempt(3))
@cache.memoize(60)
def get_azure_machine_info(rg_name, machine_name, expand="instanceView"):

    try:
        tmp = {}
        compute_client = get_azure_compute_client()
        machines = (compute_client.virtual_machines.get(rg_name, machine_name, expand=expand))
        tmp['name'] = machines.name
        tmp['location'] = machines.location
        tmp["storage_profile"] = machines.storage_profile
        tmp["network_profile"] = machines.network_profile
        tmp["instance_view"] = machines.instance_view
        return tmp
    except CloudError:
        return None

Мой тест:

def _get_azure_machine_info1(resourcegroup, machine_name):
    class MachineInfo(object):
        pass
    data = MachineInfo()
    data.name = "machine1"
    data.location = "Swiss"
    data.storage_profile = "storage"
    data.network_profile = "network_profile"
    data.instance_view = "instance_view"
    return data


@patch("dev_maintenance.machines.get_azure_compute_client", side_effect =(_get_azure_machine_info1("rg1","m1")))
def test_get_azure_machine_info (get_azure_compute_client):

    expected = {
     "name": "machine1",
     "location": "Swiss",
     "storage_profile": "storage",
     "network_profile": "network_profile",
     "instance_view": "instance_view",
     }

    with app.test_client():
        ret = get_azure_machine_info("rg1","m1")
        assert expected == ret

Макет позволил бы get_azure_machine_info вернуть side_effect, и поэтому я мог проверить возвращаемое значение get_azure_machine_info опять не ожидаемый диктат, который я создаю там.

Но я получаю эту ошибку:

get_azure_compute_client = <MagicMock name='get_azure_compute_client' id='4537942352'>

    @patch("dev_maintenance.machines.get_azure_compute_client", side_effect =(_get_azure_machine_info1("rg1","m1")))
    def test_get_azure_machine_info (get_azure_compute_client):

        expected = {
         "name": "machine1",
         "location": "Swiss",
         "storage_profile": "storage",
         "network_profile": "network_profile",
         "instance_view": "instance_view",
         }

        with app.test_client():
>           ret = get_azure_machine_info("rg1","m1")

dev_maintenance/tests/test_machines.py:134:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../venv/lib/python3.7/site-packages/tenacity/__init__.py:292: in wrapped_f
    return self.call(f, *args, **kw)
../../venv/lib/python3.7/site-packages/tenacity/__init__.py:358: in call
    do = self.iter(retry_state=retry_state)
../../venv/lib/python3.7/site-packages/tenacity/__init__.py:332: in iter
    six.raise_from(retry_exc, fut.exception())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

value = None, from_value = TypeError("'MachineInfo' object is not an iterator")

>   ???
E   tenacity.RetryError: RetryError[<Future at 0x10e7bfbd0 state=finished raised TypeError>]

<string>:3: RetryError
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...