예제 #1
0
    def test_delete_entity_type_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2beta1.EntityTypesClient(channel=channel)

        # Setup request
        name = client.entity_type_path('[PROJECT]', '[ENTITY_TYPE]')

        with pytest.raises(CustomException):
            client.delete_entity_type(name)
예제 #2
0
    def test_update_entity_type_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2beta1.EntityTypesClient(channel=channel)

        # Setup request
        entity_type = {}

        with pytest.raises(CustomException):
            client.update_entity_type(entity_type)
예제 #3
0
    def test_create_entity_type_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2beta1.EntityTypesClient(channel=channel)

        # Setup request
        parent = client.project_agent_path('[PROJECT]')
        entity_type = {}

        with pytest.raises(CustomException):
            client.create_entity_type(parent, entity_type)
    def test_update_entity_type_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 = dialogflow_v2beta1.EntityTypesClient()

        # Setup request
        entity_type = {}

        with pytest.raises(CustomException):
            client.update_entity_type(entity_type)
예제 #5
0
    def test_delete_entity_type(self):
        channel = ChannelStub()
        client = dialogflow_v2beta1.EntityTypesClient(channel=channel)

        # Setup Request
        name = client.entity_type_path('[PROJECT]', '[ENTITY_TYPE]')

        client.delete_entity_type(name)

        assert len(channel.requests) == 1
        expected_request = entity_type_pb2.DeleteEntityTypeRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_delete_entity_type_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 = dialogflow_v2beta1.EntityTypesClient()

        # Setup request
        name = client.entity_type_path("[PROJECT]", "[ENTITY_TYPE]")

        with pytest.raises(CustomException):
            client.delete_entity_type(name)
    def test_list_entity_types_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 = dialogflow_v2beta1.EntityTypesClient()

        # Setup request
        parent = client.project_agent_path("[PROJECT]")

        paged_list_response = client.list_entity_types(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_create_entity_type_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 = dialogflow_v2beta1.EntityTypesClient()

        # Setup request
        parent = client.project_agent_path('[PROJECT]')
        entity_type = {}

        with pytest.raises(CustomException):
            client.create_entity_type(parent, entity_type)
    def test_delete_entity_type(self):
        channel = ChannelStub()
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.EntityTypesClient()

        # Setup Request
        name = client.entity_type_path("[PROJECT]", "[ENTITY_TYPE]")

        client.delete_entity_type(name)

        assert len(channel.requests) == 1
        expected_request = entity_type_pb2.DeleteEntityTypeRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
예제 #10
0
def batch_delete_entities(entity_ids):
    """ Batch deletes multiple entities
    :param entity_ids: list of ids to delete
    :return: response which is an operation/ future object. """

    client = dialogflow_v2beta1.EntityTypesClient()
    parent = client.project_agent_path(PROJECT_ID)

    entity_ids_fixed_path = []

    # This may seems weird and it seems like it would not be necessary but
    # there is a bug with the api and therefore we have to append the
    # beginning of the path to every entity_id in order to get this to work.
    for entity_id in entity_ids:
        entity_ids_fixed_path.append(parent + "/entityTypes/" + entity_id)

    return client.batch_delete_entity_types(parent, entity_ids_fixed_path)
예제 #11
0
    def test_batch_delete_entities_exception(self):
        # Setup Response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name='operations/test_batch_delete_entities_exception', done=True)
        operation.error.CopyFrom(error)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        client = dialogflow_v2beta1.EntityTypesClient(channel=channel)

        # Setup Request
        parent = client.entity_type_path('[PROJECT]', '[ENTITY_TYPE]')
        entity_values = []

        response = client.batch_delete_entities(parent, entity_values)
        exception = response.exception()
        assert exception.errors[0] == error
예제 #12
0
    def test_batch_update_entity_types_exception(self):
        # Setup Response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name='operations/test_batch_update_entity_types_exception',
            done=True)
        operation.error.CopyFrom(error)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        client = dialogflow_v2beta1.EntityTypesClient(channel=channel)

        # Setup Request
        parent = client.project_agent_path('[PROJECT]')

        response = client.batch_update_entity_types(parent)
        exception = response.exception()
        assert exception.errors[0] == error
예제 #13
0
def get_entities():
    """This method is used for fetching every entity and caching it."""
    global entities_loaded

    global entities
    entities = {}
    client = dialogflow_v2beta1.EntityTypesClient()
    parent = client.project_agent_path(PROJECT_ID)

    # For every entity_type element
    for element in client.list_entity_types(parent):
        entity_type = element.display_name
        for entity in element.entities:
            # Get every synonym.
            for synonym in entity.synonyms:
                # Insert them into the big dictionary.
                entities[synonym] = entity_type

    entities_loaded = True
예제 #14
0
    def test_get_entity_type(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        display_name = 'displayName1615086568'
        expected_response = {'name': name_2, 'display_name': display_name}
        expected_response = entity_type_pb2.EntityType(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dialogflow_v2beta1.EntityTypesClient(channel=channel)

        # Setup Request
        name = client.entity_type_path('[PROJECT]', '[ENTITY_TYPE]')

        response = client.get_entity_type(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = entity_type_pb2.GetEntityTypeRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_batch_update_entity_types_exception(self):
        # Setup Response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name='operations/test_batch_update_entity_types_exception',
            done=True)
        operation.error.CopyFrom(error)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.EntityTypesClient()

        # Setup Request
        parent = client.project_agent_path('[PROJECT]')

        response = client.batch_update_entity_types(parent)
        exception = response.exception()
        assert exception.errors[0] == error
    def test_batch_delete_entities_exception(self):
        # Setup Response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name="operations/test_batch_delete_entities_exception", done=True)
        operation.error.CopyFrom(error)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.EntityTypesClient()

        # Setup Request
        parent = client.entity_type_path("[PROJECT]", "[ENTITY_TYPE]")
        entity_values = []

        response = client.batch_delete_entities(parent, entity_values)
        exception = response.exception()
        assert exception.errors[0] == error
예제 #17
0
    def test_update_entity_type(self):
        # Setup Expected Response
        name = 'name3373707'
        display_name = 'displayName1615086568'
        expected_response = {'name': name, 'display_name': display_name}
        expected_response = entity_type_pb2.EntityType(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dialogflow_v2beta1.EntityTypesClient(channel=channel)

        # Setup Request
        entity_type = {}

        response = client.update_entity_type(entity_type)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = entity_type_pb2.UpdateEntityTypeRequest(
            entity_type=entity_type)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
예제 #18
0
def batch_create_entities(entity_types):
    """ Creates entites and adds them into a global entity dictionary.
    :param entity_types: A list of entity types.
    :return: a list of the entity ID's created. """
    client = dialogflow_v2beta1.EntityTypesClient()
    parent = client.project_agent_path(PROJECT_ID)

    # A list of the newly created entities ID's.
    ID_list = []

    for entity_type in entity_types:
        entity_type_out = {
            "display_name": entity_type["entity_type_name"],
            "kind": "KIND_MAP",
            "entities": entity_type["entities"]
        }
        response = client.create_entity_type(parent, entity_type_out)

        ID_list.append(response.name.split("/")[-1])
    # After we have inserted all the new entities we get_entities() again.
    get_entities()

    return ID_list
예제 #19
0
    def test_create_entity_type(self):
        # Setup Expected Response
        name = 'name3373707'
        display_name = 'displayName1615086568'
        expected_response = {'name': name, 'display_name': display_name}
        expected_response = entity_type_pb2.EntityType(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dialogflow_v2beta1.EntityTypesClient(channel=channel)

        # Setup Request
        parent = client.project_agent_path('[PROJECT]')
        entity_type = {}

        response = client.create_entity_type(parent, entity_type)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = entity_type_pb2.CreateEntityTypeRequest(
            parent=parent, entity_type=entity_type)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_get_entity_type(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        display_name = 'displayName1615086568'
        expected_response = {'name': name_2, 'display_name': display_name}
        expected_response = entity_type_pb2.EntityType(**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 = dialogflow_v2beta1.EntityTypesClient()

        # Setup Request
        name = client.entity_type_path('[PROJECT]', '[ENTITY_TYPE]')

        response = client.get_entity_type(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = entity_type_pb2.GetEntityTypeRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
예제 #21
0
import dialogflow_v2beta1
from dialogflow_v2beta1.types import EntityType

client = dialogflow_v2beta1.EntityTypesClient()

parent = client.project_agent_path('shoppingmalldemo')
entity_type = EntityType(display_name='entity-creation-demo',
                         kind=EntityType.Kind.Value('KIND_MAP'))
# entity_type = EntityType(name='entity_demo', display_name='Entity creation demo', kind=1)
print entity_type

# response = client.create_entity_type(parent, entity_type)
# print response

# parent = response.name
parent = 'projects/shoppingmalldemo/agent/entityTypes/5b2c3eae-9f8e-46d8-8b6c-16073f685cb4'
entities = [
    EntityType.Entity(value='aa3', synonyms=['bb', 'cc']),
    EntityType.Entity(value='aa4', synonyms=['bb', 'cc'])
]

response = client.batch_create_entities(parent, entities)
print response


def callback(operation_future):
    # Handle result.
    result = operation_future.result()


response.add_done_callback(callback)