def test_transport_instance(): # A client may be instantiated with a custom transport instance. transport = transports.LicenseCodesRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) client = LicenseCodesClient(transport=transport) assert client.transport is transport
def test_common_billing_account_path(): billing_account = "squid" expected = "billingAccounts/{billing_account}".format( billing_account=billing_account, ) actual = LicenseCodesClient.common_billing_account_path(billing_account) assert expected == actual
def test_test_iam_permissions_rest_flattened_error(transport: str = "rest"): client = LicenseCodesClient( 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.test_iam_permissions( compute.TestIamPermissionsLicenseCodeRequest(), project="project_value", resource="resource_value", test_permissions_request_resource=compute.TestPermissionsRequest( permissions=["permissions_value"]), )
def test_license_codes_host_with_port(): client = LicenseCodesClient( 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.GetLicenseCodeRequest): client = LicenseCodesClient( 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.LicenseCode( creation_timestamp="creation_timestamp_value", description="description_value", id="id_value", kind="kind_value", license_alias=[ compute.LicenseCodeLicenseAlias(description="description_value") ], name="name_value", self_link="self_link_value", state=compute.LicenseCode.State.DISABLED, transferable=True, ) # Wrap the value into a proper Response obj json_return_value = compute.LicenseCode.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.LicenseCode) assert response.creation_timestamp == "creation_timestamp_value" assert response.description == "description_value" assert response.id == "id_value" assert response.kind == "kind_value" assert response.license_alias == [ compute.LicenseCodeLicenseAlias(description="description_value") ] assert response.name == "name_value" assert response.self_link == "self_link_value" assert response.state == compute.LicenseCode.State.DISABLED assert response.transferable is True
def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.LicenseCodesRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = LicenseCodesClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # It is an error to provide a credentials file and a transport instance. transport = transports.LicenseCodesRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = LicenseCodesClient( client_options={"credentials_file": "credentials.json"}, transport=transport, ) # It is an error to provide an api_key and a transport instance. transport = transports.LicenseCodesRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) options = client_options.ClientOptions() options.api_key = "api_key" with pytest.raises(ValueError): client = LicenseCodesClient( 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 = LicenseCodesClient( client_options=options, credentials=ga_credentials.AnonymousCredentials()) # It is an error to provide scopes and a transport instance. transport = transports.LicenseCodesRestTransport( credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = LicenseCodesClient( 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 = LicenseCodesClient.common_location_path(project, location) assert expected == actual
def test_client_withDEFAULT_CLIENT_INFO(): client_info = gapic_v1.client_info.ClientInfo() with mock.patch.object(transports.LicenseCodesTransport, "_prep_wrapped_messages") as prep: client = LicenseCodesClient( credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) with mock.patch.object(transports.LicenseCodesTransport, "_prep_wrapped_messages") as prep: transport_class = LicenseCodesClient.get_transport_class() transport = transport_class( credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info)
def test_get_rest(request_type): client = LicenseCodesClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding request_init = {"project": "sample1", "license_code": "sample2"} 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.LicenseCode( creation_timestamp="creation_timestamp_value", description="description_value", id=205, kind="kind_value", name="name_value", self_link="self_link_value", state="state_value", transferable=True, ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 json_return_value = compute.LicenseCode.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.LicenseCode) assert response.creation_timestamp == "creation_timestamp_value" assert response.description == "description_value" assert response.id == 205 assert response.kind == "kind_value" assert response.name == "name_value" assert response.self_link == "self_link_value" assert response.state == "state_value" assert response.transferable is True
def test_test_iam_permissions_rest_flattened(): client = LicenseCodesClient( 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.TestPermissionsResponse() # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 json_return_value = compute.TestPermissionsResponse.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", "resource": "sample2"} # get truthy value for each flattened field mock_args = dict( project="project_value", resource="resource_value", test_permissions_request_resource=compute.TestPermissionsRequest( permissions=["permissions_value"]), ) mock_args.update(sample_request) client.test_iam_permissions(**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}/global/licenseCodes/{resource}/testIamPermissions" % client.transport._host, args[1], )
def test_get_rest_bad_request(transport: str = "rest", request_type=compute.GetLicenseCodeRequest): client = LicenseCodesClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # send a request that will satisfy transcoding request_init = {"project": "sample1", "license_code": "sample2"} 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_test_iam_permissions_rest_flattened(): client = LicenseCodesClient( 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.TestPermissionsResponse() # Wrap the value into a proper Response obj json_return_value = compute.TestPermissionsResponse.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. test_permissions_request_resource = compute.TestPermissionsRequest( permissions=["permissions_value"]) client.test_iam_permissions( project="project_value", resource="resource_value", test_permissions_request_resource=test_permissions_request_resource, ) # 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 "resource_value" in http_call[1] + str(body) + str(params) assert compute.TestPermissionsRequest.to_json( test_permissions_request_resource, including_default_value_fields=False, use_integers_for_enums=False, ) in http_call[1] + str(body) + str(params)
def test_license_codes_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) LicenseCodesClient() 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_transport_close(): transports = { "rest": "_session", } for transport, close_name in transports.items(): client = LicenseCodesClient( 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_client_ctx(): transports = [ "rest", ] for transport in transports: client = LicenseCodesClient( 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__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 LicenseCodesClient._get_default_mtls_endpoint(None) is None assert (LicenseCodesClient._get_default_mtls_endpoint(api_endpoint) == api_mtls_endpoint) assert (LicenseCodesClient._get_default_mtls_endpoint(api_mtls_endpoint) == api_mtls_endpoint) assert (LicenseCodesClient._get_default_mtls_endpoint(sandbox_endpoint) == sandbox_mtls_endpoint) assert (LicenseCodesClient._get_default_mtls_endpoint( sandbox_mtls_endpoint) == sandbox_mtls_endpoint) assert LicenseCodesClient._get_default_mtls_endpoint( non_googleapi) == non_googleapi
def test_common_project_path(): project = "cuttlefish" expected = "projects/{project}".format(project=project, ) actual = LicenseCodesClient.common_project_path(project) assert expected == actual
def test_common_organization_path(): organization = "oyster" expected = "organizations/{organization}".format( organization=organization, ) actual = LicenseCodesClient.common_organization_path(organization) assert expected == actual
def test_common_folder_path(): folder = "whelk" expected = "folders/{folder}".format(folder=folder, ) actual = LicenseCodesClient.common_folder_path(folder) assert expected == actual
def test_get_rest_error(): client = LicenseCodesClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest")
def test_test_iam_permissions_rest_required_fields( request_type=compute.TestIamPermissionsLicenseCodeRequest, ): transport_class = transports.LicenseCodesRestTransport request_init = {} request_init["project"] = "" request_init["resource"] = "" 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( )).test_iam_permissions._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present jsonified_request["project"] = "project_value" jsonified_request["resource"] = "resource_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials( )).test_iam_permissions._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone assert "project" in jsonified_request assert jsonified_request["project"] == "project_value" assert "resource" in jsonified_request assert jsonified_request["resource"] == "resource_value" client = LicenseCodesClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) request = request_type(request_init) # Designate an appropriate value for the returned response. return_value = compute.TestPermissionsResponse() # 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": "post", "query_params": request_init, } transcode_result["body"] = {} transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 json_return_value = compute.TestPermissionsResponse.to_json( return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value response = client.test_iam_permissions(request) expected_params = [] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params
def test_test_iam_permissions_rest_error(): client = LicenseCodesClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest")