def test_list_queues(self): # Setup Expected Response next_page_token = "" queues_element = {} queues = [queues_element] expected_response = { "next_page_token": next_page_token, "queues": queues } expected_response = cloudtasks_pb2.ListQueuesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request parent = client.location_path("[PROJECT]", "[LOCATION]") paged_list_response = client.list_queues(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.queues[0] == resources[0] assert len(channel.requests) == 1 expected_request = cloudtasks_pb2.ListQueuesRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request resource = client.queue_path("[PROJECT]", "[LOCATION]", "[QUEUE]") response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_renew_lease(self): # Setup Expected Response name_2 = 'name2-1052831874' expected_response = {'name': name_2} expected_response = task_pb2.Task(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request name = client.task_path('[PROJECT]', '[LOCATION]', '[QUEUE]', '[TASK]') schedule_time = {} lease_duration = {} response = client.renew_lease(name, schedule_time, lease_duration) assert expected_response == response assert len(channel.requests) == 1 expected_request = cloudtasks_pb2.RenewLeaseRequest( name=name, schedule_time=schedule_time, lease_duration=lease_duration) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create_task(project, queue, location): # [START tasks_create_task] """Create a task for a given queue with an arbitrary payload.""" from google.cloud import tasks_v2beta2 # Create a client. client = tasks_v2beta2.CloudTasksClient() # Prepare the payload of type bytes. payload = 'a message for the recipient'.encode() # Construct the request body. task = { 'pull_message': { 'payload': payload, } } # Construct the fully qualified queue name. parent = client.queue_path(project, location, queue) # Use the client to build and send the task. response = client.create_task(parent, task) print('Created task: {}'.format(response.name)) return response
def test_list_tasks(self): # Setup Expected Response next_page_token = '' tasks_element = {} tasks = [tasks_element] expected_response = { 'next_page_token': next_page_token, 'tasks': tasks } expected_response = cloudtasks_pb2.ListTasksResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request parent = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]') paged_list_response = client.list_tasks(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.tasks[0] == resources[0] assert len(channel.requests) == 1 expected_request = cloudtasks_pb2.ListTasksRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_test_iam_permissions(self): # Setup Expected Response expected_response = {} expected_response = iam_policy_pb2.TestIamPermissionsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request resource = "resource-341064690" permissions = [] response = client.test_iam_permissions(resource, permissions) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.TestIamPermissionsRequest( resource=resource, permissions=permissions) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_cancel_lease(self): # Setup Expected Response name_2 = "name2-1052831874" expected_response = {"name": name_2} expected_response = task_pb2.Task(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request name = client.task_path("[PROJECT]", "[LOCATION]", "[QUEUE]", "[TASK]") schedule_time = {} response = client.cancel_lease(name, schedule_time) assert expected_response == response assert len(channel.requests) == 1 expected_request = cloudtasks_pb2.CancelLeaseRequest( name=name, schedule_time=schedule_time) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_set_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request resource = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]') policy = {} response = client.set_iam_policy(resource, policy) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.SetIamPolicyRequest( resource=resource, policy=policy) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_lease_tasks(self): # Setup Expected Response expected_response = {} expected_response = cloudtasks_pb2.LeaseTasksResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request parent = client.queue_path("[PROJECT]", "[LOCATION]", "[QUEUE]") lease_duration = {} response = client.lease_tasks(parent, lease_duration) assert expected_response == response assert len(channel.requests) == 1 expected_request = cloudtasks_pb2.LeaseTasksRequest( parent=parent, lease_duration=lease_duration) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_queue(self): # Setup Expected Response name = 'name3373707' expected_response = {'name': name} expected_response = queue_pb2.Queue(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request parent = client.location_path('[PROJECT]', '[LOCATION]') queue = {} response = client.create_queue(parent, queue) assert expected_response == response assert len(channel.requests) == 1 expected_request = cloudtasks_pb2.CreateQueueRequest(parent=parent, queue=queue) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_renew_lease(self): # Setup Expected Response name_2 = 'name2-1052831874' expected_response = {'name': name_2} expected_response = task_pb2.Task(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = tasks_v2beta2.CloudTasksClient(channel=channel) # Setup Request name = client.task_path('[PROJECT]', '[LOCATION]', '[QUEUE]', '[TASK]') schedule_time = {} lease_duration = {} response = client.renew_lease(name, schedule_time, lease_duration) assert expected_response == response assert len(channel.requests) == 1 expected_request = cloudtasks_pb2.RenewLeaseRequest( name=name, schedule_time=schedule_time, lease_duration=lease_duration) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_task(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = task_pb2.Task(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request parent = client.queue_path("[PROJECT]", "[LOCATION]", "[QUEUE]") task = {} response = client.create_task(parent, task) assert expected_response == response assert len(channel.requests) == 1 expected_request = cloudtasks_pb2.CreateTaskRequest(parent=parent, task=task) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_set_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup Request resource = "resource-341064690" policy = {} response = client.set_iam_policy(resource, policy) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.SetIamPolicyRequest( resource=resource, policy=policy) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_queues(self): # Setup Expected Response next_page_token = '' queues_element = {} queues = [queues_element] expected_response = { 'next_page_token': next_page_token, 'queues': queues } expected_response = cloudtasks_pb2.ListQueuesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = tasks_v2beta2.CloudTasksClient(channel=channel) # Setup Request parent = client.location_path('[PROJECT]', '[LOCATION]') paged_list_response = client.list_queues(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.queues[0] == resources[0] assert len(channel.requests) == 1 expected_request = cloudtasks_pb2.ListQueuesRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = tasks_v2beta2.CloudTasksClient(channel=channel) # Setup request resource = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]') with pytest.raises(CustomException): client.get_iam_policy(resource)
def test_update_queue_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = tasks_v2beta2.CloudTasksClient(channel=channel) # Setup request queue = {} with pytest.raises(CustomException): client.update_queue(queue)
def test_list_tasks_exception(self): channel = ChannelStub(responses=[CustomException()]) client = tasks_v2beta2.CloudTasksClient(channel=channel) # Setup request parent = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]') paged_list_response = client.list_tasks(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_run_task_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = tasks_v2beta2.CloudTasksClient(channel=channel) # Setup request name = client.task_path('[PROJECT]', '[LOCATION]', '[QUEUE]', '[TASK]') with pytest.raises(CustomException): client.run_task(name)
def test_create_queue_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = tasks_v2beta2.CloudTasksClient(channel=channel) # Setup request parent = client.location_path('[PROJECT]', '[LOCATION]') queue = {} with pytest.raises(CustomException): client.create_queue(parent, queue)
def test_lease_tasks_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = tasks_v2beta2.CloudTasksClient(channel=channel) # Setup request parent = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]') lease_duration = {} with pytest.raises(CustomException): client.lease_tasks(parent, lease_duration)
def test_cancel_lease_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = tasks_v2beta2.CloudTasksClient(channel=channel) # Setup request name = client.task_path('[PROJECT]', '[LOCATION]', '[QUEUE]', '[TASK]') schedule_time = {} with pytest.raises(CustomException): client.cancel_lease(name, schedule_time)
def acknowledge_task(task): """Acknowledge a given task.""" from google.cloud import tasks_v2beta2 # Create a client. client = tasks_v2beta2.CloudTasksClient() # Send request to client to acknowledge task. client.acknowledge_task(task.name, task.schedule_time) print('Acknowledged task {}'.format(task.name))
def test_get_iam_policy_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup request resource = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]') with pytest.raises(CustomException): client.get_iam_policy(resource)
def test_update_queue_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup request queue = {} with pytest.raises(CustomException): client.update_queue(queue)
def test_get_iam_policy_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup request resource = "resource-341064690" with pytest.raises(CustomException): client.get_iam_policy(resource)
def test_list_tasks_exception(self): channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup request parent = client.queue_path("[PROJECT]", "[LOCATION]", "[QUEUE]") paged_list_response = client.list_tasks(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_run_task_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup request name = client.task_path("[PROJECT]", "[LOCATION]", "[QUEUE]", "[TASK]") with pytest.raises(CustomException): client.run_task(name)
def test_delete_task(self): channel = ChannelStub() client = tasks_v2beta2.CloudTasksClient(channel=channel) # Setup Request name = client.task_path('[PROJECT]', '[LOCATION]', '[QUEUE]', '[TASK]') client.delete_task(name) assert len(channel.requests) == 1 expected_request = cloudtasks_pb2.DeleteTaskRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_cancel_lease_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup request name = client.task_path('[PROJECT]', '[LOCATION]', '[QUEUE]', '[TASK]') schedule_time = {} with pytest.raises(CustomException): client.cancel_lease(name, schedule_time)
def test_create_queue_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = tasks_v2beta2.CloudTasksClient() # Setup request parent = client.location_path("[PROJECT]", "[LOCATION]") queue = {} with pytest.raises(CustomException): client.create_queue(parent, queue)