Exemple #1
0
def test_list_rest_flattened():
    client = InterconnectLocationsClient(
        credentials=ga_credentials.AnonymousCredentials(), )

    # Mock the http request call within the method and fake a response.
    with mock.patch.object(Session, "request") as req:
        # Designate an appropriate value for the returned response.
        return_value = compute.InterconnectLocationList()

        # Wrap the value into a proper Response obj
        json_return_value = compute.InterconnectLocationList.to_json(
            return_value)
        response_value = Response()
        response_value.status_code = 200
        response_value._content = json_return_value.encode("UTF-8")
        req.return_value = response_value

        # Call the method with a truthy value for each flattened field,
        # using the keyword arguments to the method.
        client.list(project="project_value", )

        # Establish that the underlying call was made with the expected
        # request object values.
        assert len(req.mock_calls) == 1
        _, http_call, http_params = req.mock_calls[0]
        body = http_params.get("data")
        assert "project_value" in http_call[1] + str(body)
Exemple #2
0
def test_list_pager():
    client = InterconnectLocationsClient(
        credentials=ga_credentials.AnonymousCredentials(), )

    # Mock the http request call within the method and fake a response.
    with mock.patch.object(Session, "request") as req:
        # Set the response as a series of pages
        response = (
            compute.InterconnectLocationList(
                items=[
                    compute.InterconnectLocation(),
                    compute.InterconnectLocation(),
                    compute.InterconnectLocation(),
                ],
                next_page_token="abc",
            ),
            compute.InterconnectLocationList(
                items=[],
                next_page_token="def",
            ),
            compute.InterconnectLocationList(
                items=[
                    compute.InterconnectLocation(),
                ],
                next_page_token="ghi",
            ),
            compute.InterconnectLocationList(items=[
                compute.InterconnectLocation(),
                compute.InterconnectLocation(),
            ], ),
        )
        # Two responses for two calls
        response = response + response

        # Wrap the values into proper Response objs
        response = tuple(
            compute.InterconnectLocationList.to_json(x) for x in response)
        return_values = tuple(Response() for i in response)
        for return_val, response_val in zip(return_values, response):
            return_val._content = response_val.encode("UTF-8")
            return_val.status_code = 200
        req.side_effect = return_values

        metadata = ()
        pager = client.list(request={})

        assert pager._metadata == metadata

        results = list(pager)
        assert len(results) == 6
        assert all(
            isinstance(i, compute.InterconnectLocation) for i in results)

        pages = list(client.list(request={}).pages)
        for page_, token in zip(pages, ["abc", "def", "ghi", ""]):
            assert page_.raw_page.next_page_token == token
Exemple #3
0
def test_list_rest_flattened_error():
    client = InterconnectLocationsClient(
        credentials=ga_credentials.AnonymousCredentials(), )

    # Attempting to call a method with both a request object and flattened
    # fields is an error.
    with pytest.raises(ValueError):
        client.list(
            compute.ListInterconnectLocationsRequest(),
            project="project_value",
        )
Exemple #4
0
def test_list_rest(transport: str = "rest",
                   request_type=compute.ListInterconnectLocationsRequest):
    client = InterconnectLocationsClient(
        credentials=ga_credentials.AnonymousCredentials(),
        transport=transport,
    )

    # Everything is optional in proto3 as far as the runtime is concerned,
    # and we are mocking out the actual API, so just send an empty request.
    request = request_type()

    # Mock the http request call within the method and fake a response.
    with mock.patch.object(Session, "request") as req:
        # Designate an appropriate value for the returned response.
        return_value = compute.InterconnectLocationList(
            id="id_value",
            items=[compute.InterconnectLocation(address="address_value")],
            kind="kind_value",
            next_page_token="next_page_token_value",
            self_link="self_link_value",
            warning=compute.Warning(code=compute.Warning.Code.CLEANUP_FAILED),
        )

        # Wrap the value into a proper Response obj
        json_return_value = compute.InterconnectLocationList.to_json(
            return_value)
        response_value = Response()
        response_value.status_code = 200
        response_value._content = json_return_value.encode("UTF-8")
        req.return_value = response_value
        response = client.list(request)

    # Establish that the response is the type that we expect.
    assert isinstance(response, pagers.ListPager)
    assert response.id == "id_value"
    assert response.items == [
        compute.InterconnectLocation(address="address_value")
    ]
    assert response.kind == "kind_value"
    assert response.next_page_token == "next_page_token_value"
    assert response.self_link == "self_link_value"
    assert response.warning == compute.Warning(
        code=compute.Warning.Code.CLEANUP_FAILED)