def test_get_rest(request_type): client = ImageFamilyViewsClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding request_init = { "project": "sample1", "zone": "sample2", "family": "sample3" } request = request_type(request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. return_value = compute.ImageFamilyView() # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 json_return_value = compute.ImageFamilyView.to_json(return_value) 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.ImageFamilyView)
def test_parse_common_billing_account_path(): expected = { "billing_account": "clam", } path = ImageFamilyViewsClient.common_billing_account_path(**expected) # Check that the path construction is reversible. actual = ImageFamilyViewsClient.parse_common_billing_account_path(path) assert expected == actual
def test_parse_common_folder_path(): expected = { "folder": "octopus", } path = ImageFamilyViewsClient.common_folder_path(**expected) # Check that the path construction is reversible. actual = ImageFamilyViewsClient.parse_common_folder_path(path) assert expected == actual
def test_parse_common_organization_path(): expected = { "organization": "nudibranch", } path = ImageFamilyViewsClient.common_organization_path(**expected) # Check that the path construction is reversible. actual = ImageFamilyViewsClient.parse_common_organization_path(path) assert expected == actual
def test_parse_common_project_path(): expected = { "project": "mussel", } path = ImageFamilyViewsClient.common_project_path(**expected) # Check that the path construction is reversible. actual = ImageFamilyViewsClient.parse_common_project_path(path) assert expected == actual
def test_image_family_views_client_get_transport_class(): transport = ImageFamilyViewsClient.get_transport_class() available_transports = [ transports.ImageFamilyViewsRestTransport, ] assert transport in available_transports transport = ImageFamilyViewsClient.get_transport_class("rest") assert transport == transports.ImageFamilyViewsRestTransport
def test_parse_common_location_path(): expected = { "project": "scallop", "location": "abalone", } path = ImageFamilyViewsClient.common_location_path(**expected) # Check that the path construction is reversible. actual = ImageFamilyViewsClient.parse_common_location_path(path) assert expected == actual
def test_get_rest_flattened_error(transport: str = "rest"): client = ImageFamilyViewsClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): client.get( compute.GetImageFamilyViewRequest(), project="project_value", zone="zone_value", family="family_value", )
def test_common_billing_account_path(): billing_account = "squid" expected = "billingAccounts/{billing_account}".format( billing_account=billing_account, ) actual = ImageFamilyViewsClient.common_billing_account_path( billing_account) assert expected == actual
def test_image_family_views_host_with_port(): client = ImageFamilyViewsClient( 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_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.ImageFamilyViewsRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = ImageFamilyViewsClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # It is an error to provide a credentials file and a transport instance. transport = transports.ImageFamilyViewsRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = ImageFamilyViewsClient( client_options={"credentials_file": "credentials.json"}, transport=transport, ) # It is an error to provide an api_key and a transport instance. transport = transports.ImageFamilyViewsRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) options = client_options.ClientOptions() options.api_key = "api_key" with pytest.raises(ValueError): client = ImageFamilyViewsClient( client_options=options, transport=transport, ) # It is an error to provide an api_key and a credential. options = mock.Mock() options.api_key = "api_key" with pytest.raises(ValueError): client = ImageFamilyViewsClient( client_options=options, credentials=ga_credentials.AnonymousCredentials()) # It is an error to provide scopes and a transport instance. transport = transports.ImageFamilyViewsRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = ImageFamilyViewsClient( client_options={"scopes": ["1", "2"]}, transport=transport, )
def test_common_location_path(): project = "winkle" location = "nautilus" expected = "projects/{project}/locations/{location}".format( project=project, location=location, ) actual = ImageFamilyViewsClient.common_location_path(project, location) assert expected == actual
def test_get_rest_flattened(): client = ImageFamilyViewsClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. return_value = compute.ImageFamilyView() # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 json_return_value = compute.ImageFamilyView.to_json(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value # get arguments that satisfy an http rule for this method sample_request = { "project": "sample1", "zone": "sample2", "family": "sample3" } # get truthy value for each flattened field mock_args = dict( project="project_value", zone="zone_value", family="family_value", ) mock_args.update(sample_request) client.get(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( "https://%s/compute/v1/projects/{project}/zones/{zone}/imageFamilyViews/{family}" % client.transport._host, args[1], )
def test_client_with_default_client_info(): client_info = gapic_v1.client_info.ClientInfo() with mock.patch.object(transports.ImageFamilyViewsTransport, "_prep_wrapped_messages") as prep: client = ImageFamilyViewsClient( credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) with mock.patch.object(transports.ImageFamilyViewsTransport, "_prep_wrapped_messages") as prep: transport_class = ImageFamilyViewsClient.get_transport_class() transport = transport_class( credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info)
def test_client_ctx(): transports = [ "rest", ] for transport in transports: client = ImageFamilyViewsClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport) # Test client calls underlying transport. with mock.patch.object(type(client.transport), "close") as close: close.assert_not_called() with client: pass close.assert_called()
def test_transport_close(): transports = { "rest": "_session", } for transport, close_name in transports.items(): client = ImageFamilyViewsClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport) with mock.patch.object(type(getattr(client.transport, close_name)), "close") as close: with client: close.assert_not_called() close.assert_called_once()
def test_image_family_views_auth_adc(): # 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) ImageFamilyViewsClient() adc.assert_called_once_with( scopes=None, default_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_get_rest_bad_request(transport: str = "rest", request_type=compute.GetImageFamilyViewRequest): client = ImageFamilyViewsClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # send a request that will satisfy transcoding request_init = { "project": "sample1", "zone": "sample2", "family": "sample3" } request = request_type(request_init) # Mock the http request call within the method and fake a BadRequest error. with mock.patch.object(Session, "request") as req, pytest.raises( core_exceptions.BadRequest): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 400 response_value.request = Request() req.return_value = response_value client.get(request)
def test__get_default_mtls_endpoint(): api_endpoint = "example.googleapis.com" api_mtls_endpoint = "example.mtls.googleapis.com" sandbox_endpoint = "example.sandbox.googleapis.com" sandbox_mtls_endpoint = "example.mtls.sandbox.googleapis.com" non_googleapi = "api.example.com" assert ImageFamilyViewsClient._get_default_mtls_endpoint(None) is None assert (ImageFamilyViewsClient._get_default_mtls_endpoint(api_endpoint) == api_mtls_endpoint) assert (ImageFamilyViewsClient._get_default_mtls_endpoint( api_mtls_endpoint) == api_mtls_endpoint) assert (ImageFamilyViewsClient._get_default_mtls_endpoint(sandbox_endpoint) == sandbox_mtls_endpoint) assert (ImageFamilyViewsClient._get_default_mtls_endpoint( sandbox_mtls_endpoint) == sandbox_mtls_endpoint) assert (ImageFamilyViewsClient._get_default_mtls_endpoint(non_googleapi) == non_googleapi)
def test_transport_instance(): # A client may be instantiated with a custom transport instance. transport = transports.ImageFamilyViewsRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) client = ImageFamilyViewsClient(transport=transport) assert client.transport is transport
def test_get_rest_required_fields( request_type=compute.GetImageFamilyViewRequest): transport_class = transports.ImageFamilyViewsRestTransport request_init = {} request_init["family"] = "" request_init["project"] = "" request_init["zone"] = "" request = request_type(request_init) jsonified_request = json.loads( request_type.to_json(request, including_default_value_fields=False, use_integers_for_enums=False)) # verify fields with default values are dropped unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials( )).get._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present jsonified_request["family"] = "family_value" jsonified_request["project"] = "project_value" jsonified_request["zone"] = "zone_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials( )).get._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone assert "family" in jsonified_request assert jsonified_request["family"] == "family_value" assert "project" in jsonified_request assert jsonified_request["project"] == "project_value" assert "zone" in jsonified_request assert jsonified_request["zone"] == "zone_value" client = ImageFamilyViewsClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) request = request_type(request_init) # Designate an appropriate value for the returned response. return_value = compute.ImageFamilyView() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values # for required fields will fail the real version if the http_options # expect actual values for those fields. with mock.patch.object(path_template, "transcode") as transcode: # A uri without fields and an empty body will force all the # request fields to show up in the query_params. transcode_result = { "uri": "v1/sample_method", "method": "get", "query_params": request_init, } transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 json_return_value = compute.ImageFamilyView.to_json(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value response = client.get(request) expected_params = [] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params
def test_common_project_path(): project = "cuttlefish" expected = "projects/{project}".format(project=project, ) actual = ImageFamilyViewsClient.common_project_path(project) assert expected == actual
def test_common_organization_path(): organization = "oyster" expected = "organizations/{organization}".format( organization=organization, ) actual = ImageFamilyViewsClient.common_organization_path(organization) assert expected == actual
def test_common_folder_path(): folder = "whelk" expected = "folders/{folder}".format(folder=folder, ) actual = ImageFamilyViewsClient.common_folder_path(folder) assert expected == actual
def test_get_rest_error(): client = ImageFamilyViewsClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest")