def test_get_rest_flattened(): client = GlobalAddressesClient( 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.Address() # Wrap the value into a proper Response obj json_return_value = compute.Address.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.get( project="project_value", address="address_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") params = http_params.get("params") assert "project_value" in http_call[1] + str(body) + str(params) assert "address_value" in http_call[1] + str(body) + str(params)
def test_get_rest_flattened_error(): client = GlobalAddressesClient( 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.get( compute.GetGlobalAddressRequest(), project="project_value", address="address_value", )
def test_get_rest(transport: str = "rest", request_type=compute.GetGlobalAddressRequest): client = GlobalAddressesClient( 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.Address( address="address_value", address_type=compute.Address.AddressType.EXTERNAL, creation_timestamp="creation_timestamp_value", description="description_value", id=205, ip_version=compute.Address.IpVersion.IPV4, kind="kind_value", name="name_value", network="network_value", network_tier=compute.Address.NetworkTier.PREMIUM, prefix_length=1391, purpose=compute.Address.Purpose.DNS_RESOLVER, region="region_value", self_link="self_link_value", status=compute.Address.Status.IN_USE, subnetwork="subnetwork_value", users=["users_value"], ) # Wrap the value into a proper Response obj json_return_value = compute.Address.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.get(request) # Establish that the response is the type that we expect. assert isinstance(response, compute.Address) assert response.address == "address_value" assert response.address_type == compute.Address.AddressType.EXTERNAL assert response.creation_timestamp == "creation_timestamp_value" assert response.description == "description_value" assert response.id == 205 assert response.ip_version == compute.Address.IpVersion.IPV4 assert response.kind == "kind_value" assert response.name == "name_value" assert response.network == "network_value" assert response.network_tier == compute.Address.NetworkTier.PREMIUM assert response.prefix_length == 1391 assert response.purpose == compute.Address.Purpose.DNS_RESOLVER assert response.region == "region_value" assert response.self_link == "self_link_value" assert response.status == compute.Address.Status.IN_USE assert response.subnetwork == "subnetwork_value" assert response.users == ["users_value"]