示例#1
0
    def test_create_context(self):
        # Setup Expected Response
        name = 'name3373707'
        lifespan_count = 1178775510
        expected_response = {'name': name, 'lifespan_count': lifespan_count}
        expected_response = context_pb2.Context(**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_v2.ContextsClient()

        # Setup Request
        parent = client.session_path('[PROJECT]', '[SESSION]')
        context = {}

        response = client.create_context(parent, context)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = context_pb2.CreateContextRequest(parent=parent,
                                                            context=context)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_create_context(self):
        # Setup Expected Response
        name = "name3373707"
        lifespan_count = 1178775510
        expected_response = {"name": name, "lifespan_count": lifespan_count}
        expected_response = context_pb2.Context(**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_v2.ContextsClient()

        # Setup Request
        parent = "parent-995424086"
        context = {}

        response = client.create_context(parent, context)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = context_pb2.CreateContextRequest(
            parent=parent, context=context
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_contexts(self):
        # Setup Expected Response
        next_page_token = ''
        contexts_element = {}
        contexts = [contexts_element]
        expected_response = {
            'next_page_token': next_page_token,
            'contexts': contexts
        }
        expected_response = context_pb2.ListContextsResponse(
            **expected_response)

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

        # Setup Request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        paged_list_response = client.list_contexts(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.contexts[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = context_pb2.ListContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#4
0
    def test_list_contexts(self):
        # Setup Expected Response
        next_page_token = ''
        contexts_element = {}
        contexts = [contexts_element]
        expected_response = {
            'next_page_token': next_page_token,
            'contexts': contexts
        }
        expected_response = context_pb2.ListContextsResponse(
            **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_v2.ContextsClient()

        # Setup Request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        paged_list_response = client.list_contexts(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.contexts[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = context_pb2.ListContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#5
0
def delete_context(project_id, session_id, context_id):
    contexts_client = dialogflow.ContextsClient()

    context_name = contexts_client.context_path(project_id, session_id,
                                                context_id)

    contexts_client.delete_context(context_name)
    def test_list_contexts(self):
        # Setup Expected Response
        next_page_token = ""
        contexts_element = {}
        contexts = [contexts_element]
        expected_response = {"next_page_token": next_page_token, "contexts": contexts}
        expected_response = context_pb2.ListContextsResponse(**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_v2.ContextsClient()

        # Setup Request
        parent = "parent-995424086"

        paged_list_response = client.list_contexts(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.contexts[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = context_pb2.ListContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#7
0
 def __init__(self, project_id, session_id):
     self.project_id = project_id
     self.session_id = session_id
     self.intents_client = dialogflow.IntentsClient()
     self.contexts_client = dialogflow.ContextsClient()
     self.parent = self.intents_client.project_agent_path(project_id)
     self.session_path = self.contexts_client.session_path(
         project_id, session_id)
    def test_delete_all_contexts_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2.ContextsClient(channel=channel)

        # Setup request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        with pytest.raises(CustomException):
            client.delete_all_contexts(parent)
    def test_list_contexts_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2.ContextsClient(channel=channel)

        # Setup request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        paged_list_response = client.list_contexts(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_update_context_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2.ContextsClient(channel=channel)

        # Setup request
        context = {}

        with pytest.raises(CustomException):
            client.update_context(context)
    def test_delete_context_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2.ContextsClient(channel=channel)

        # Setup request
        name = client.context_path('[PROJECT]', '[SESSION]', '[CONTEXT]')

        with pytest.raises(CustomException):
            client.delete_context(name)
示例#12
0
def create_context(project_id, session_id, context_id, lifespan_count):
    contexts_client = dialogflow.ContextsClient()

    session_path = contexts_client.session_path(project_id, session_id)
    context_name = contexts_client.context_path(project_id, session_id,
                                                context_id)

    context = dialogflow.types.Context(name=context_name,
                                       lifespan_count=lifespan_count)

    response = contexts_client.create_context(session_path, context)

    print('Context created: \n{}'.format(response))
    def test_delete_context_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_v2.ContextsClient()

        # Setup request
        name = "name3373707"

        with pytest.raises(CustomException):
            client.delete_context(name)
示例#14
0
    def test_delete_context_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_v2.ContextsClient()

        # Setup request
        name = client.context_path('[PROJECT]', '[SESSION]', '[CONTEXT]')

        with pytest.raises(CustomException):
            client.delete_context(name)
示例#15
0
    def test_update_context_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_v2.ContextsClient()

        # Setup request
        context = {}

        with pytest.raises(CustomException):
            client.update_context(context)
示例#16
0
    def test_delete_all_contexts_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_v2.ContextsClient()

        # Setup request
        parent = client.session_path("[PROJECT]", "[SESSION]")

        with pytest.raises(CustomException):
            client.delete_all_contexts(parent)
    def test_list_contexts_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_v2.ContextsClient()

        # Setup request
        parent = "parent-995424086"

        paged_list_response = client.list_contexts(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_delete_all_contexts(self):
        channel = ChannelStub()
        client = dialogflow_v2.ContextsClient(channel=channel)

        # Setup Request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        client.delete_all_contexts(parent)

        assert len(channel.requests) == 1
        expected_request = context_pb2.DeleteAllContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#19
0
    def test_list_contexts_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_v2.ContextsClient()

        # Setup request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        paged_list_response = client.list_contexts(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_delete_context(self):
        channel = ChannelStub()
        client = dialogflow_v2.ContextsClient(channel=channel)

        # Setup Request
        name = client.context_path('[PROJECT]', '[SESSION]', '[CONTEXT]')

        client.delete_context(name)

        assert len(channel.requests) == 1
        expected_request = context_pb2.DeleteContextRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#21
0
def create_context(project_id, session_id, context_id, lifespan_count,
                   parameters):
    contexts_client = dialogflow_v2.ContextsClient(
        credentials=(service_account.Credentials.from_service_account_info(
            config.DIALOG_FLOW_JSON)))

    session_path = contexts_client.session_path(project_id, session_id)
    context_name = contexts_client.context_path(project_id, session_id,
                                                context_id)

    context = dialogflow_v2.types.Context(name=context_name,
                                          lifespan_count=lifespan_count,
                                          parameters=parameters)

    response = contexts_client.create_context(session_path, context)
示例#22
0
def list_contexts(project_id, session_id):
    contexts_client = dialogflow.ContextsClient()

    session_path = contexts_client.session_path(project_id, session_id)

    contexts = contexts_client.list_contexts(session_path)

    print('Contexts for session {}:\n'.format(session_path))
    for context in contexts:
        print('Context name: {}'.format(context.name))
        print('Lifespan count: {}'.format(context.lifespan_count))
        print('Fields:')
        for field, value in context.parameters.fields.items():
            if value.string_value:
                print('\t{}: {}'.format(field, value))
    def test_delete_all_contexts(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_v2.ContextsClient()

        # Setup Request
        parent = "parent-995424086"

        client.delete_all_contexts(parent)

        assert len(channel.requests) == 1
        expected_request = context_pb2.DeleteAllContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#24
0
    def __init__(self, project_id, session_id, language_code):

        self.project_id = project_id
        self.session_id = session_id

        self.intents_client = dialogflow.IntentsClient()
        self.parent = self.intents_client.project_agent_path(project_id)
        self.intents = self.intents_client.list_intents(self.parent)

        self.contexts_client = dialogflow.ContextsClient()
        self.context_parent = self.contexts_client.session_path(
            project_id, session_id)

        self.session_client = dialogflow.SessionsClient()
        self.session = self.session_client.session_path(project_id, session_id)
        self.lang_code = "es"
示例#25
0
    def test_delete_context(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_v2.ContextsClient()

        # Setup Request
        name = client.context_path("[PROJECT]", "[SESSION]", "[CONTEXT]")

        client.delete_context(name)

        assert len(channel.requests) == 1
        expected_request = context_pb2.DeleteContextRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#26
0
    def test_delete_all_contexts(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_v2.ContextsClient()

        # Setup Request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        client.delete_all_contexts(parent)

        assert len(channel.requests) == 1
        expected_request = context_pb2.DeleteAllContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
def set_variables(proj_id, google_auth_fname_path):
    """
    Sets global variables for use by functions in this module.
    : param proj_id : Google project id
    : param google_auth_fname_path : path to json google authentication file
    : return : void
    """
    global parent
    global intents_client
    global contexts_client
    global project_id
    global path

    project_id = proj_id
    intents_client = dialogflow.IntentsClient()
    contexts_client = dialogflow.ContextsClient()
    parent = intents_client.project_agent_path(project_id)
    path = google_auth_fname_path
示例#28
0
 def __init__(self):
     self.session_client = dialogflow.SessionsClient()
     self.session_context = dialogflow.ContextsClient()
     self.neutral_words = [
         "bot",
         "chatbot",
         "iris",
         "chat bot",
         "hotel",
         "no",
     ]
     self.spanish_words = [
         "oy",
         "oye",
         "ey",
         "si"
     ]
     self.translate = YandexTranslate('trnsl.1.1.20200215T104617Z.e985952a7'
                                      'c20d3fc.45cea67a739d4bbe0d98177bb452'
                                      '7b84b0857455')
    def test_get_context(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        lifespan_count = 1178775510
        expected_response = {'name': name_2, 'lifespan_count': lifespan_count}
        expected_response = context_pb2.Context(**expected_response)

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

        # Setup Request
        name = client.context_path('[PROJECT]', '[SESSION]', '[CONTEXT]')

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

        assert len(channel.requests) == 1
        expected_request = context_pb2.GetContextRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_update_context(self):
        # Setup Expected Response
        name = 'name3373707'
        lifespan_count = 1178775510
        expected_response = {'name': name, 'lifespan_count': lifespan_count}
        expected_response = context_pb2.Context(**expected_response)

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

        # Setup Request
        context = {}

        response = client.update_context(context)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = context_pb2.UpdateContextRequest(context=context)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request