def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.MachineTypesRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = MachineTypesClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # It is an error to provide a credentials file and a transport instance. transport = transports.MachineTypesRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = MachineTypesClient( client_options={"credentials_file": "credentials.json"}, transport=transport, ) # It is an error to provide scopes and a transport instance. transport = transports.MachineTypesRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = MachineTypesClient( client_options={"scopes": ["1", "2"]}, transport=transport, )
def test_transport_instance(): # A client may be instantiated with a custom transport instance. transport = transports.MachineTypesRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) client = MachineTypesClient(transport=transport) assert client.transport is transport
def test_list_rest_flattened(): client = MachineTypesClient( 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.MachineTypeList() # Wrap the value into a proper Response obj json_return_value = compute.MachineTypeList.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", zone="zone_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 "zone_value" in http_call[1] + str(body) + str(params)
def test_machine_types_host_with_port(): client = MachineTypesClient( credentials=ga_credentials.AnonymousCredentials(), client_options=client_options.ClientOptions( api_endpoint="compute.googleapis.com:8000"), ) assert client.transport._host == "compute.googleapis.com:8000"
def test_get_rest(transport: str = "rest", request_type=compute.GetMachineTypeRequest): client = MachineTypesClient( 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.MachineType( accelerators=[compute.Accelerators(guest_accelerator_count=2452)], creation_timestamp="creation_timestamp_value", deprecated=compute.DeprecationStatus(deleted="deleted_value"), description="description_value", guest_cpus=1090, id="id_value", image_space_gb=1430, is_shared_cpu=True, kind="kind_value", maximum_persistent_disks=2603, maximum_persistent_disks_size_gb="maximum_persistent_disks_size_gb_value", memory_mb=967, name="name_value", scratch_disks=[compute.ScratchDisks(disk_gb=723)], self_link="self_link_value", zone="zone_value", ) # Wrap the value into a proper Response obj json_return_value = compute.MachineType.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.MachineType) assert response.accelerators == [compute.Accelerators(guest_accelerator_count=2452)] assert response.creation_timestamp == "creation_timestamp_value" assert response.deprecated == compute.DeprecationStatus(deleted="deleted_value") assert response.description == "description_value" assert response.guest_cpus == 1090 assert response.id == "id_value" assert response.image_space_gb == 1430 assert response.is_shared_cpu is True assert response.kind == "kind_value" assert response.maximum_persistent_disks == 2603 assert ( response.maximum_persistent_disks_size_gb == "maximum_persistent_disks_size_gb_value" ) assert response.memory_mb == 967 assert response.name == "name_value" assert response.scratch_disks == [compute.ScratchDisks(disk_gb=723)] assert response.self_link == "self_link_value" assert response.zone == "zone_value"
def test_aggregated_list_rest( transport: str = "rest", request_type=compute.AggregatedListMachineTypesRequest ): client = MachineTypesClient( 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.MachineTypeAggregatedList( id="id_value", items={ "key_value": compute.MachineTypesScopedList( machine_types=[ compute.MachineType( accelerators=[ compute.Accelerators(guest_accelerator_count=2452) ] ) ] ) }, kind="kind_value", next_page_token="next_page_token_value", self_link="self_link_value", unreachables=["unreachables_value"], warning=compute.Warning(code=compute.Warning.Code.CLEANUP_FAILED), ) # Wrap the value into a proper Response obj json_return_value = compute.MachineTypeAggregatedList.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.aggregated_list(request) # Establish that the response is the type that we expect. assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.MachineTypesScopedList( machine_types=[ compute.MachineType( accelerators=[compute.Accelerators(guest_accelerator_count=2452)] ) ] ) } assert response.kind == "kind_value" assert response.next_page_token == "next_page_token_value" assert response.self_link == "self_link_value" assert response.unreachables == ["unreachables_value"] assert response.warning == compute.Warning(code=compute.Warning.Code.CLEANUP_FAILED)
def test_aggregated_list_rest_flattened_error(): client = MachineTypesClient(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.aggregated_list( compute.AggregatedListMachineTypesRequest(), project="project_value", )
def test_list_pager(): client = MachineTypesClient( 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.MachineTypeList( items=[ compute.MachineType(), compute.MachineType(), compute.MachineType(), ], next_page_token="abc", ), compute.MachineTypeList( items=[], next_page_token="def", ), compute.MachineTypeList( items=[ compute.MachineType(), ], next_page_token="ghi", ), compute.MachineTypeList(items=[ compute.MachineType(), compute.MachineType(), ], ), ) # Two responses for two calls response = response + response # Wrap the values into proper Response objs response = tuple(compute.MachineTypeList.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.MachineType) 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
def test_get_rest_flattened_error(): client = MachineTypesClient(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.GetMachineTypeRequest(), project="project_value", zone="zone_value", machine_type="machine_type_value", )
def test_machine_types_auth_adc_old_google_auth(): # If no credentials are provided, we should use ADC credentials. with mock.patch.object(google.auth, "default", autospec=True) as adc: adc.return_value = (ga_credentials.AnonymousCredentials(), None) MachineTypesClient() adc.assert_called_once_with( scopes=( "https://www.googleapis.com/auth/compute.readonly", "https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/cloud-platform", ), quota_project_id=None, )
def test_client_withDEFAULT_CLIENT_INFO(): client_info = gapic_v1.client_info.ClientInfo() with mock.patch.object(transports.MachineTypesTransport, "_prep_wrapped_messages") as prep: client = MachineTypesClient( credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) with mock.patch.object(transports.MachineTypesTransport, "_prep_wrapped_messages") as prep: transport_class = MachineTypesClient.get_transport_class() transport = transport_class( credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info)