Esempio n. 1
0
def complete_query(project_id, tenant_id, query):
    """Complete job title given partial text (autocomplete)"""

    client = talent_v4beta1.CompletionClient()

    # project_id = 'Your Google Cloud Project ID'
    # tenant_id = 'Your Tenant ID (using tenancy is optional)'
    # query = '[partially typed job title]'

    if isinstance(project_id, six.binary_type):
        project_id = project_id.decode("utf-8")
    if isinstance(tenant_id, six.binary_type):
        tenant_id = tenant_id.decode("utf-8")
    if isinstance(query, six.binary_type):
        query = query.decode("utf-8")

    parent = f"projects/{project_id}/tenants/{tenant_id}"

    request = talent_v4beta1.CompleteQueryRequest(
        parent=parent,
        query=query,
        page_size=5,  # limit for number of results
        language_codes=["en-US"],  # language code
    )
    response = client.complete_query(request=request)
    for result in response.completion_results:
        print(f"Suggested title: {result.suggestion}")
        # Suggestion type is JOB_TITLE or COMPANY_TITLE
        print(
            f"Suggestion type: {talent_v4beta1.CompleteQueryRequest.CompletionType(result.type_).name}"
        )
    def test_complete_query(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = completion_service_pb2.CompleteQueryResponse(
            **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 = talent_v4beta1.CompletionClient()

        # Setup Request
        parent = client.tenant_path("[PROJECT]", "[TENANT]")
        query = "query107944136"
        page_size = 883849137

        response = client.complete_query(parent, query, page_size)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = completion_service_pb2.CompleteQueryRequest(
            parent=parent, query=query, page_size=page_size)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Esempio n. 3
0
def sample_complete_query(project_id, tenant_id, query, num_results,
                          language_code):
    """Complete job title given partial text (autocomplete)"""
    # [START job_search_autocomplete_job_title_core]

    client = talent_v4beta1.CompletionClient()

    # project_id = 'Your Google Cloud Project ID'
    # tenant_id = 'Your Tenant ID (using tenancy is optional)'
    # query = '[partially typed job title]'
    # num_results = 5
    # language_code = 'en-US'

    if isinstance(project_id, six.binary_type):
        project_id = project_id.decode('utf-8')
    if isinstance(tenant_id, six.binary_type):
        tenant_id = tenant_id.decode('utf-8')
    if isinstance(query, six.binary_type):
        query = query.decode('utf-8')

    if isinstance(language_code, six.binary_type):
        language_code = language_code.decode('utf-8')
    parent = client.tenant_path(project_id, tenant_id)
    language_codes = [language_code]

    response = client.complete_query(parent,
                                     query,
                                     num_results,
                                     language_codes=language_codes)
    for result in response.completion_results:
        print('Suggested title: {}'.format(result.suggestion))
        # Suggestion type is JOB_TITLE or COMPANY_TITLE
        print('Suggestion type: {}'.format(
            enums.CompleteQueryRequest.CompletionType(result.type).name))
    def test_complete_query_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 = talent_v4beta1.CompletionClient()

        # Setup request
        parent = client.tenant_path("[PROJECT]", "[TENANT]")
        query = "query107944136"
        page_size = 883849137

        with pytest.raises(CustomException):
            client.complete_query(parent, query, page_size)
    def test_complete_query_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 = talent_v4beta1.CompletionClient()

        # Setup request
        name = client.project_path('[PROJECT]')
        query = 'query107944136'
        page_size = 883849137

        with pytest.raises(CustomException):
            client.complete_query(name, query, page_size)