def test_get_blurb_field_headers(): client = MessagingClient( credentials=credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as # a field header. Set these to a non-empty value. request = messaging.GetBlurbRequest( name='name/value', ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( type(client._transport.get_blurb), '__call__') as call: call.return_value = messaging.Blurb() response = client.get_blurb(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] assert args[0] == request # Establish that the field header was sent. _, _, kw = call.mock_calls[0] assert ( 'x-goog-request-params', 'name=name/value', ) in kw['metadata']
def test_create_blurb_flattened(): client = MessagingClient( credentials=credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( type(client._transport.create_blurb), '__call__') as call: # Designate an appropriate return value for the call. call.return_value = messaging.Blurb() # Call the method with a truthy value for each flattened field, # using the keyword arguments to the method. response = client.create_blurb( parent='parent_value', user='******', text='text_value', image=b'image_blob', ) # Establish that the underlying call was made with the expected # request object values. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] assert args[0].parent == 'parent_value' assert args[0].blurb.user == 'user_value' assert args[0].blurb.text == 'text_value' assert args[0].blurb.image == b'image_blob'
def test_update_blurb(transport: str = 'grpc'): client = MessagingClient( credentials=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 = messaging.UpdateBlurbRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.update_blurb), '__call__') as call: # Designate an appropriate return value for the call. call.return_value = messaging.Blurb( name='name_value', user='******', text='text_value', image=b'image_blob', ) response = client.update_blurb(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] assert args[0] == request # Establish that the response is the type that we expect. assert isinstance(response, messaging.Blurb) assert response.name == 'name_value' assert response.user == 'user_value' assert response.text == 'text_value' assert response.image == b'image_blob'
def test_list_blurbs_pages(): client = MessagingClient(credentials=credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.list_blurbs), '__call__') as call: # Set the response to a series of pages. call.side_effect = ( messaging.ListBlurbsResponse( blurbs=[ messaging.Blurb(), messaging.Blurb(), messaging.Blurb(), ], next_page_token='abc', ), messaging.ListBlurbsResponse( blurbs=[], next_page_token='def', ), messaging.ListBlurbsResponse( blurbs=[ messaging.Blurb(), ], next_page_token='ghi', ), messaging.ListBlurbsResponse(blurbs=[ messaging.Blurb(), messaging.Blurb(), ], ), RuntimeError, ) pages = list(client.list_blurbs(request={}).pages) for page, token in zip(pages, ['abc', 'def', 'ghi', '']): assert page.raw_page.next_page_token == token
def test_list_blurbs_pager(): client = MessagingClient(credentials=credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.list_blurbs), '__call__') as call: # Set the response to a series of pages. call.side_effect = ( messaging.ListBlurbsResponse( blurbs=[ messaging.Blurb(), messaging.Blurb(), messaging.Blurb(), ], next_page_token='abc', ), messaging.ListBlurbsResponse( blurbs=[], next_page_token='def', ), messaging.ListBlurbsResponse( blurbs=[ messaging.Blurb(), ], next_page_token='ghi', ), messaging.ListBlurbsResponse(blurbs=[ messaging.Blurb(), messaging.Blurb(), ], ), RuntimeError, ) results = [i for i in client.list_blurbs(request={}, )] assert len(results) == 6 assert all([isinstance(i, messaging.Blurb) for i in results])