Skip to content

Commit

Permalink
Talent v4beta1 samples [Restoring deleted branch samples] [(#3273)](#…
Browse files Browse the repository at this point in the history
…3273)

* restored deleted samples

* fixed lint issues

* added tests and formatted code

* corrected test req.txt file

* made requested changes and added return val for samples

* added secrets.txt to bash, script

* fixed the lint

* moved to requirements-test.txt

* delete resources in teardown just in case

* restored deleted samples

fixed lint issues

added tests and formatted code

corrected test req.txt file

made requested changes and added return val for samples

added secrets.txt to bash, script

fixed the lint

* added some spacing

* restored deleted samples

fixed lint issues

added tests and formatted code

corrected test req.txt file

made requested changes and added return val for samples

added secrets.txt to bash, script

fixed the lint

restored deleted samples

fixed lint issues

added tests and formatted code

corrected test req.txt file

made requested changes and added return val for samples

added secrets.txt to bash, script

fixed the lint

moved to requirements-test.txt

delete resources in teardown just in case

added some spacing

* fixed merge conflicts

* added conftest.py and refactored samples with loop

* removed talent secret.txt from bash script

* fixed the lint issue

* removed unnecessary env vars

* deleted global random ids, deleted unnecessary setup code from delete tests

* removed unused imports

* removed pytest

* removed unused IDs

* deleted unused uuid imports

Co-authored-by: Takashi Matsuo <[email protected]>
  • Loading branch information
2 people authored and busunkim96 committed Sep 25, 2020
0 parents commit 1654c75
Show file tree
Hide file tree
Showing 40 changed files with 1,836 additions and 0 deletions.
87 changes: 87 additions & 0 deletions talent/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import uuid

from google.api_core.exceptions import NotFound

import pytest

import job_search_create_company
import job_search_create_job
import job_search_create_tenant
import job_search_delete_company
import job_search_delete_job
import job_search_delete_tenant

PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]


@pytest.fixture(scope="module")
def tenant():
tenant_ext_unique_id = "TEST_TENANT_{}".format(uuid.uuid4())
# create a temporary tenant
tenant_name = job_search_create_tenant.create_tenant(
PROJECT_ID, tenant_ext_unique_id
)

# extract company id
tenant_id = tenant_name.split("/")[-1]

yield tenant_id

try:
job_search_delete_tenant.delete_tenant(PROJECT_ID, tenant_id)
except NotFound as e:
print("Ignoring NotFound upon cleanup, details: {}".format(e))


@pytest.fixture(scope="module")
def company(tenant):
company_ext_id = "COMPANY_EXT_ID_{}".format(uuid.uuid4())

# create a temporary company
company_name = job_search_create_company.create_company(
PROJECT_ID, tenant, "Test Company Name", company_ext_id
)

# extract company id
company_id = company_name.split("/")[-1]

yield company_id

try:
job_search_delete_company.delete_company(PROJECT_ID, tenant, company_id)
except NotFound as e:
print("Ignoring NotFound upon cleanup, details: {}".format(e))


@pytest.fixture(scope="module")
def job(tenant, company):
post_unique_id = "TEST_POST_{}".format(uuid.uuid4().hex)[:20]
# create a temporary job
job_name = job_search_create_job.create_job(
PROJECT_ID, tenant, company, post_unique_id, "www.jobUrl.com"
)

# extract company id
job_id = job_name.split("/")[-1]

yield job_id

try:
job_search_delete_job.delete_job(PROJECT_ID, tenant, job_id)
except NotFound as e:
print("Ignoring NotFound upon cleanup, details: {}".format(e))
56 changes: 56 additions & 0 deletions talent/job_search_autocomplete_job_title.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START job_search_autocomplete_job_title]

from google.cloud import talent_v4beta1
from google.cloud.talent import enums
import six


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 = client.tenant_path(project_id, tenant_id)

response = client.complete_query(
parent,
query,
page_size=5, # limit for number of results
language_codes=["en-US"], # language code
)
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
)
)


# [END job_search_autocomplete_job_title]
25 changes: 25 additions & 0 deletions talent/job_search_autocomplete_job_title_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import job_search_autocomplete_job_title

PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]


def test_autocomplete_job_title(capsys, tenant):
job_search_autocomplete_job_title.complete_query(PROJECT_ID, tenant, "Software")
out, _ = capsys.readouterr()
assert "Suggested title:" in out
133 changes: 133 additions & 0 deletions talent/job_search_batch_create_jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START job_search_batch_create_jobs]

from google.cloud import talent
import six


def batch_create_jobs(
project_id,
tenant_id,
company_name_one,
requisition_id_one,
title_one,
description_one,
job_application_url_one,
address_one,
language_code_one,
company_name_two,
requisition_id_two,
title_two,
description_two,
job_application_url_two,
address_two,
language_code_two,
):
"""
Batch Create Jobs
Args:
project_id Your Google Cloud Project ID
tenant_id Identifier of the Tenant
"""

client = talent.JobServiceClient()

# project_id = 'Your Google Cloud Project ID'
# tenant_id = 'Your Tenant ID (using tenancy is optional)'
# company_name_one = 'Company name, e.g. projects/your-project/companies/company-id'
# requisition_id_one = 'Job requisition ID, aka Posting ID. Unique per job.'
# title_one = 'Software Engineer'
# description_one = 'This is a description of this <i>wonderful</i> job!'
# job_application_url_one = 'https://www.example.org/job-posting/123'
# address_one = '1600 Amphitheatre Parkway, Mountain View, CA 94043'
# language_code_one = 'en-US'
# company_name_two = 'Company name, e.g. projects/your-project/companies/company-id'
# requisition_id_two = 'Job requisition ID, aka Posting ID. Unique per job.'
# title_two = 'Quality Assurance'
# description_two = 'This is a description of this <i>wonderful</i> job!'
# job_application_url_two = 'https://www.example.org/job-posting/123'
# address_two = '111 8th Avenue, New York, NY 10011'
# language_code_two = '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(company_name_one, six.binary_type):
company_name_one = company_name_one.decode("utf-8")
if isinstance(requisition_id_one, six.binary_type):
requisition_id_one = requisition_id_one.decode("utf-8")
if isinstance(title_one, six.binary_type):
title_one = title_one.decode("utf-8")
if isinstance(description_one, six.binary_type):
description_one = description_one.decode("utf-8")
if isinstance(job_application_url_one, six.binary_type):
job_application_url_one = job_application_url_one.decode("utf-8")
if isinstance(address_one, six.binary_type):
address_one = address_one.decode("utf-8")
if isinstance(language_code_one, six.binary_type):
language_code_one = language_code_one.decode("utf-8")
if isinstance(company_name_two, six.binary_type):
company_name_two = company_name_two.decode("utf-8")
if isinstance(requisition_id_two, six.binary_type):
requisition_id_two = requisition_id_two.decode("utf-8")
if isinstance(title_two, six.binary_type):
title_two = title_two.decode("utf-8")
if isinstance(description_two, six.binary_type):
description_two = description_two.decode("utf-8")
if isinstance(job_application_url_two, six.binary_type):
job_application_url_two = job_application_url_two.decode("utf-8")
if isinstance(address_two, six.binary_type):
address_two = address_two.decode("utf-8")
if isinstance(language_code_two, six.binary_type):
language_code_two = language_code_two.decode("utf-8")
parent = client.tenant_path(project_id, tenant_id)
uris = [job_application_url_one]
application_info = {"uris": uris}
addresses = [address_one]
jobs_element = {
"company": company_name_one,
"requisition_id": requisition_id_one,
"title": title_one,
"description": description_one,
"application_info": application_info,
"addresses": addresses,
"language_code": language_code_one,
}
uris_2 = [job_application_url_two]
application_info_2 = {"uris": uris_2}
addresses_2 = [address_two]
jobs_element_2 = {
"company": company_name_two,
"requisition_id": requisition_id_two,
"title": title_two,
"description": description_two,
"application_info": application_info_2,
"addresses": addresses_2,
"language_code": language_code_two,
}
jobs = [jobs_element, jobs_element_2]

operation = client.batch_create_jobs(parent, jobs)

print("Waiting for operation to complete...")
response = operation.result(90)

print("Batch response: {}".format(response))


# [END job_search_batch_create_jobs]
Loading

0 comments on commit 1654c75

Please sign in to comment.