Skip to content

Commit bb1d01a

Browse files
munkhuushmglTakashi Matsuo
authored andcommitted
Talent v4beta1 samples [Restoring deleted branch samples] [(#3273)](#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]>
1 parent 88cf2fe commit bb1d01a

40 files changed

+1836
-0
lines changed

talent/conftest.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
18+
from google.api_core.exceptions import NotFound
19+
20+
import pytest
21+
22+
import job_search_create_company
23+
import job_search_create_job
24+
import job_search_create_tenant
25+
import job_search_delete_company
26+
import job_search_delete_job
27+
import job_search_delete_tenant
28+
29+
PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
30+
31+
32+
@pytest.fixture(scope="module")
33+
def tenant():
34+
tenant_ext_unique_id = "TEST_TENANT_{}".format(uuid.uuid4())
35+
# create a temporary tenant
36+
tenant_name = job_search_create_tenant.create_tenant(
37+
PROJECT_ID, tenant_ext_unique_id
38+
)
39+
40+
# extract company id
41+
tenant_id = tenant_name.split("/")[-1]
42+
43+
yield tenant_id
44+
45+
try:
46+
job_search_delete_tenant.delete_tenant(PROJECT_ID, tenant_id)
47+
except NotFound as e:
48+
print("Ignoring NotFound upon cleanup, details: {}".format(e))
49+
50+
51+
@pytest.fixture(scope="module")
52+
def company(tenant):
53+
company_ext_id = "COMPANY_EXT_ID_{}".format(uuid.uuid4())
54+
55+
# create a temporary company
56+
company_name = job_search_create_company.create_company(
57+
PROJECT_ID, tenant, "Test Company Name", company_ext_id
58+
)
59+
60+
# extract company id
61+
company_id = company_name.split("/")[-1]
62+
63+
yield company_id
64+
65+
try:
66+
job_search_delete_company.delete_company(PROJECT_ID, tenant, company_id)
67+
except NotFound as e:
68+
print("Ignoring NotFound upon cleanup, details: {}".format(e))
69+
70+
71+
@pytest.fixture(scope="module")
72+
def job(tenant, company):
73+
post_unique_id = "TEST_POST_{}".format(uuid.uuid4().hex)[:20]
74+
# create a temporary job
75+
job_name = job_search_create_job.create_job(
76+
PROJECT_ID, tenant, company, post_unique_id, "www.jobUrl.com"
77+
)
78+
79+
# extract company id
80+
job_id = job_name.split("/")[-1]
81+
82+
yield job_id
83+
84+
try:
85+
job_search_delete_job.delete_job(PROJECT_ID, tenant, job_id)
86+
except NotFound as e:
87+
print("Ignoring NotFound upon cleanup, details: {}".format(e))
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START job_search_autocomplete_job_title]
16+
17+
from google.cloud import talent_v4beta1
18+
from google.cloud.talent import enums
19+
import six
20+
21+
22+
def complete_query(project_id, tenant_id, query):
23+
"""Complete job title given partial text (autocomplete)"""
24+
25+
client = talent_v4beta1.CompletionClient()
26+
27+
# project_id = 'Your Google Cloud Project ID'
28+
# tenant_id = 'Your Tenant ID (using tenancy is optional)'
29+
# query = '[partially typed job title]'
30+
31+
if isinstance(project_id, six.binary_type):
32+
project_id = project_id.decode("utf-8")
33+
if isinstance(tenant_id, six.binary_type):
34+
tenant_id = tenant_id.decode("utf-8")
35+
if isinstance(query, six.binary_type):
36+
query = query.decode("utf-8")
37+
38+
parent = client.tenant_path(project_id, tenant_id)
39+
40+
response = client.complete_query(
41+
parent,
42+
query,
43+
page_size=5, # limit for number of results
44+
language_codes=["en-US"], # language code
45+
)
46+
for result in response.completion_results:
47+
print("Suggested title: {}".format(result.suggestion))
48+
# Suggestion type is JOB_TITLE or COMPANY_TITLE
49+
print(
50+
"Suggestion type: {}".format(
51+
enums.CompleteQueryRequest.CompletionType(result.type).name
52+
)
53+
)
54+
55+
56+
# [END job_search_autocomplete_job_title]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import job_search_autocomplete_job_title
18+
19+
PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
20+
21+
22+
def test_autocomplete_job_title(capsys, tenant):
23+
job_search_autocomplete_job_title.complete_query(PROJECT_ID, tenant, "Software")
24+
out, _ = capsys.readouterr()
25+
assert "Suggested title:" in out
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START job_search_batch_create_jobs]
16+
17+
from google.cloud import talent
18+
import six
19+
20+
21+
def batch_create_jobs(
22+
project_id,
23+
tenant_id,
24+
company_name_one,
25+
requisition_id_one,
26+
title_one,
27+
description_one,
28+
job_application_url_one,
29+
address_one,
30+
language_code_one,
31+
company_name_two,
32+
requisition_id_two,
33+
title_two,
34+
description_two,
35+
job_application_url_two,
36+
address_two,
37+
language_code_two,
38+
):
39+
"""
40+
Batch Create Jobs
41+
42+
Args:
43+
project_id Your Google Cloud Project ID
44+
tenant_id Identifier of the Tenant
45+
"""
46+
47+
client = talent.JobServiceClient()
48+
49+
# project_id = 'Your Google Cloud Project ID'
50+
# tenant_id = 'Your Tenant ID (using tenancy is optional)'
51+
# company_name_one = 'Company name, e.g. projects/your-project/companies/company-id'
52+
# requisition_id_one = 'Job requisition ID, aka Posting ID. Unique per job.'
53+
# title_one = 'Software Engineer'
54+
# description_one = 'This is a description of this <i>wonderful</i> job!'
55+
# job_application_url_one = 'https://www.example.org/job-posting/123'
56+
# address_one = '1600 Amphitheatre Parkway, Mountain View, CA 94043'
57+
# language_code_one = 'en-US'
58+
# company_name_two = 'Company name, e.g. projects/your-project/companies/company-id'
59+
# requisition_id_two = 'Job requisition ID, aka Posting ID. Unique per job.'
60+
# title_two = 'Quality Assurance'
61+
# description_two = 'This is a description of this <i>wonderful</i> job!'
62+
# job_application_url_two = 'https://www.example.org/job-posting/123'
63+
# address_two = '111 8th Avenue, New York, NY 10011'
64+
# language_code_two = 'en-US'
65+
66+
if isinstance(project_id, six.binary_type):
67+
project_id = project_id.decode("utf-8")
68+
if isinstance(tenant_id, six.binary_type):
69+
tenant_id = tenant_id.decode("utf-8")
70+
if isinstance(company_name_one, six.binary_type):
71+
company_name_one = company_name_one.decode("utf-8")
72+
if isinstance(requisition_id_one, six.binary_type):
73+
requisition_id_one = requisition_id_one.decode("utf-8")
74+
if isinstance(title_one, six.binary_type):
75+
title_one = title_one.decode("utf-8")
76+
if isinstance(description_one, six.binary_type):
77+
description_one = description_one.decode("utf-8")
78+
if isinstance(job_application_url_one, six.binary_type):
79+
job_application_url_one = job_application_url_one.decode("utf-8")
80+
if isinstance(address_one, six.binary_type):
81+
address_one = address_one.decode("utf-8")
82+
if isinstance(language_code_one, six.binary_type):
83+
language_code_one = language_code_one.decode("utf-8")
84+
if isinstance(company_name_two, six.binary_type):
85+
company_name_two = company_name_two.decode("utf-8")
86+
if isinstance(requisition_id_two, six.binary_type):
87+
requisition_id_two = requisition_id_two.decode("utf-8")
88+
if isinstance(title_two, six.binary_type):
89+
title_two = title_two.decode("utf-8")
90+
if isinstance(description_two, six.binary_type):
91+
description_two = description_two.decode("utf-8")
92+
if isinstance(job_application_url_two, six.binary_type):
93+
job_application_url_two = job_application_url_two.decode("utf-8")
94+
if isinstance(address_two, six.binary_type):
95+
address_two = address_two.decode("utf-8")
96+
if isinstance(language_code_two, six.binary_type):
97+
language_code_two = language_code_two.decode("utf-8")
98+
parent = client.tenant_path(project_id, tenant_id)
99+
uris = [job_application_url_one]
100+
application_info = {"uris": uris}
101+
addresses = [address_one]
102+
jobs_element = {
103+
"company": company_name_one,
104+
"requisition_id": requisition_id_one,
105+
"title": title_one,
106+
"description": description_one,
107+
"application_info": application_info,
108+
"addresses": addresses,
109+
"language_code": language_code_one,
110+
}
111+
uris_2 = [job_application_url_two]
112+
application_info_2 = {"uris": uris_2}
113+
addresses_2 = [address_two]
114+
jobs_element_2 = {
115+
"company": company_name_two,
116+
"requisition_id": requisition_id_two,
117+
"title": title_two,
118+
"description": description_two,
119+
"application_info": application_info_2,
120+
"addresses": addresses_2,
121+
"language_code": language_code_two,
122+
}
123+
jobs = [jobs_element, jobs_element_2]
124+
125+
operation = client.batch_create_jobs(parent, jobs)
126+
127+
print("Waiting for operation to complete...")
128+
response = operation.result(90)
129+
130+
print("Batch response: {}".format(response))
131+
132+
133+
# [END job_search_batch_create_jobs]

0 commit comments

Comments
 (0)