Skip to content

Commit 002e988

Browse files
authored
fixit: Update to Gen App Builder Tests (GoogleCloudPlatform#10092)
* fix(tests): Update to Gen App Builder Operations Test - fixes GoogleCloudPlatform#10066 * fix(tests): Update tests to return value instead of using prints * fix(tests): Change return type for ListDocuments to `ListDocumentsResponse` * fix: Update listdocuments type to Any - `ListDocumentsPager` the return type for `client.list_documents` is not exposed as a type from the client library * fix(lint): Add extra newline
1 parent c469391 commit 002e988

10 files changed

+72
-68
lines changed

discoveryengine/documents_sample_test.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,34 @@
2828
bigquery_table = "import_documents_test"
2929

3030

31-
def test_import_documents_gcs(capsys):
32-
import_documents_sample.import_documents_sample(
31+
def test_import_documents_gcs():
32+
operation_name = import_documents_sample.import_documents_sample(
3333
project_id=project_id,
3434
location=location,
3535
search_engine_id=search_engine_id,
3636
gcs_uri=gcs_uri,
3737
)
3838

39-
out, _ = capsys.readouterr()
39+
assert "operations/import-documents" in operation_name
4040

41-
assert "operations/import-documents" in out
4241

43-
44-
def test_import_documents_bigquery(capsys):
45-
import_documents_sample.import_documents_sample(
42+
def test_import_documents_bigquery():
43+
operation_name = import_documents_sample.import_documents_sample(
4644
project_id=project_id,
4745
location=location,
4846
search_engine_id=search_engine_id,
4947
bigquery_dataset=bigquery_dataset,
5048
bigquery_table=bigquery_table,
5149
)
5250

53-
out, _ = capsys.readouterr()
54-
55-
assert "operations/import-documents" in out
51+
assert "operations/import-documents" in operation_name
5652

5753

58-
def test_list_documents(capsys):
59-
list_documents_sample.list_documents_sample(
54+
def test_list_documents():
55+
response = list_documents_sample.list_documents_sample(
6056
project_id=project_id,
6157
location=location,
6258
search_engine_id=search_engine_id,
6359
)
6460

65-
out, _ = capsys.readouterr()
66-
67-
assert f"Documents in {search_engine_id}" in out
61+
assert response

discoveryengine/get_operation_sample.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#
1515

1616
# [START genappbuilder_get_operation]
17-
from google.cloud import discoveryengine_v1beta as genappbuilder
17+
from google.cloud import discoveryengine
1818

1919
from google.longrunning import operations_pb2
2020

@@ -23,9 +23,9 @@
2323
# operation_name = "YOUR_OPERATION_NAME"
2424

2525

26-
def get_operation_sample(operation_name: str) -> None:
26+
def get_operation_sample(operation_name: str) -> operations_pb2.Operation:
2727
# Create a client
28-
client = genappbuilder.DocumentServiceClient()
28+
client = discoveryengine.DocumentServiceClient()
2929

3030
# Make GetOperation request
3131
request = operations_pb2.GetOperationRequest(name=operation_name)
@@ -34,5 +34,7 @@ def get_operation_sample(operation_name: str) -> None:
3434
# Print the Operation Information
3535
print(operation)
3636

37+
return operation
38+
3739

3840
# [END genappbuilder_get_operation]

discoveryengine/import_documents_sample.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from __future__ import annotations
1818

1919

20-
from google.cloud import discoveryengine_v1beta as genappbuilder
20+
from google.cloud import discoveryengine
2121

2222
# TODO(developer): Uncomment these variables before running the sample.
2323
# project_id = "YOUR_PROJECT_ID"
@@ -38,9 +38,9 @@ def import_documents_sample(
3838
gcs_uri: str | None = None,
3939
bigquery_dataset: str | None = None,
4040
bigquery_table: str | None = None,
41-
) -> None:
41+
) -> str:
4242
# Create a client
43-
client = genappbuilder.DocumentServiceClient()
43+
client = discoveryengine.DocumentServiceClient()
4444

4545
# The full resource name of the search engine branch.
4646
# e.g. projects/{project}/locations/{location}/dataStores/{data_store}/branches/{branch}
@@ -52,25 +52,25 @@ def import_documents_sample(
5252
)
5353

5454
if gcs_uri:
55-
request = genappbuilder.ImportDocumentsRequest(
55+
request = discoveryengine.ImportDocumentsRequest(
5656
parent=parent,
57-
gcs_source=genappbuilder.GcsSource(
57+
gcs_source=discoveryengine.GcsSource(
5858
input_uris=[gcs_uri], data_schema="custom"
5959
),
6060
# Options: `FULL`, `INCREMENTAL`
61-
reconciliation_mode=genappbuilder.ImportDocumentsRequest.ReconciliationMode.INCREMENTAL,
61+
reconciliation_mode=discoveryengine.ImportDocumentsRequest.ReconciliationMode.INCREMENTAL,
6262
)
6363
else:
64-
request = genappbuilder.ImportDocumentsRequest(
64+
request = discoveryengine.ImportDocumentsRequest(
6565
parent=parent,
66-
bigquery_source=genappbuilder.BigQuerySource(
66+
bigquery_source=discoveryengine.BigQuerySource(
6767
project_id=project_id,
6868
dataset_id=bigquery_dataset,
6969
table_id=bigquery_table,
7070
data_schema="custom",
7171
),
7272
# Options: `FULL`, `INCREMENTAL`
73-
reconciliation_mode=genappbuilder.ImportDocumentsRequest.ReconciliationMode.INCREMENTAL,
73+
reconciliation_mode=discoveryengine.ImportDocumentsRequest.ReconciliationMode.INCREMENTAL,
7474
)
7575

7676
# Make the request
@@ -81,11 +81,13 @@ def import_documents_sample(
8181

8282
# Once the operation is complete,
8383
# get information from operation metadata
84-
metadata = genappbuilder.ImportDocumentsMetadata(operation.metadata)
84+
metadata = discoveryengine.ImportDocumentsMetadata(operation.metadata)
8585

8686
# Handle the response
8787
print(response)
8888
print(metadata)
8989

90+
return operation.operation.name
91+
9092

9193
# [END genappbuilder_import_documents]

discoveryengine/list_documents_sample.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,19 @@
1313
# limitations under the License.
1414
#
1515
# [START genappbuilder_list_documents]
16-
from google.cloud import discoveryengine_v1beta as genappbuilder
16+
from typing import Any
17+
18+
from google.cloud import discoveryengine
1719

1820
# TODO(developer): Uncomment these variables before running the sample.
1921
# project_id = "YOUR_PROJECT_ID"
2022
# location = "YOUR_LOCATION" # Values: "global"
2123
# search_engine_id = "YOUR_SEARCH_ENGINE_ID"
2224

2325

24-
def list_documents_sample(
25-
project_id: str,
26-
location: str,
27-
search_engine_id: str
28-
) -> None:
26+
def list_documents_sample(project_id: str, location: str, search_engine_id: str) -> Any:
2927
# Create a client
30-
client = genappbuilder.DocumentServiceClient()
28+
client = discoveryengine.DocumentServiceClient()
3129

3230
# The full resource name of the search engine branch.
3331
# e.g. projects/{project}/locations/{location}/dataStores/{data_store}/branches/{branch}
@@ -44,5 +42,7 @@ def list_documents_sample(
4442
for result in response:
4543
print(result)
4644

45+
return response
46+
4747

4848
# [END genappbuilder_list_documents]

discoveryengine/list_operations_sample.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from __future__ import annotations
1717

1818

19-
from google.cloud import discoveryengine_v1beta as genappbuilder
19+
from google.cloud import discoveryengine
2020

2121
from google.longrunning import operations_pb2
2222

@@ -34,9 +34,9 @@ def list_operations_sample(
3434
location: str,
3535
search_engine_id: str,
3636
operations_filter: str | None = None,
37-
) -> None:
37+
) -> operations_pb2.ListOperationsResponse:
3838
# Create a client
39-
client = genappbuilder.DocumentServiceClient()
39+
client = discoveryengine.DocumentServiceClient()
4040

4141
# The full resource name of the search engine branch.
4242
name = f"projects/{project_id}/locations/{location}/collections/default_collection/dataStores/{search_engine_id}"
@@ -54,5 +54,7 @@ def list_operations_sample(
5454
for operation in response.operations:
5555
print(operation)
5656

57+
return response
58+
5759

5860
# [END genappbuilder_list_operations]

discoveryengine/operations_sample_test.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,34 @@
2828
operation_name = f"projects/{project_id}/locations/{location}/collections/default_collection/dataStores/{search_engine_id}/branches/0/operations/{operation_id}"
2929

3030

31-
def test_get_operation(capsys):
31+
def test_get_operation():
3232
try:
33-
get_operation_sample.get_operation_sample(operation_name=operation_name)
33+
operation = get_operation_sample.get_operation_sample(
34+
operation_name=operation_name
35+
)
36+
assert operation
3437
except NotFound as e:
3538
print(e.message)
39+
pass
3640

37-
out, _ = capsys.readouterr()
3841

39-
assert operation_id in out
40-
41-
42-
def test_list_operations(capsys):
43-
list_operations_sample.list_operations_sample(
42+
def test_list_operations():
43+
response = list_operations_sample.list_operations_sample(
4444
project_id=project_id,
4545
location=location,
4646
search_engine_id=search_engine_id,
4747
)
4848

49-
out, _ = capsys.readouterr()
50-
51-
assert operation_id in out
49+
assert response
50+
assert response.operations
5251

5352

54-
def test_poll_operation(capsys):
53+
def test_poll_operation():
5554
try:
56-
poll_operation_sample.poll_operation_sample(operation_name=operation_name)
55+
operation = poll_operation_sample.poll_operation_sample(
56+
operation_name=operation_name
57+
)
58+
assert operation
5759
except NotFound as e:
5860
print(e.message)
59-
60-
out, _ = capsys.readouterr()
61-
62-
assert operation_id in out
61+
pass

discoveryengine/poll_operation_sample.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# [START genappbuilder_poll_operation]
1717
from time import sleep
1818

19-
from google.cloud import discoveryengine_v1beta as genappbuilder
19+
from google.cloud import discoveryengine
2020

2121
from google.longrunning import operations_pb2
2222

@@ -25,9 +25,11 @@
2525
# operation_name = "YOUR_OPERATION_NAME"
2626

2727

28-
def poll_operation_sample(operation_name: str, limit: int = 10):
28+
def poll_operation_sample(
29+
operation_name: str, limit: int = 10
30+
) -> operations_pb2.Operation:
2931
# Create a client
30-
client = genappbuilder.DocumentServiceClient()
32+
client = discoveryengine.DocumentServiceClient()
3133

3234
# Make GetOperation request
3335
request = operations_pb2.GetOperationRequest(name=operation_name)
@@ -44,5 +46,7 @@ def poll_operation_sample(operation_name: str, limit: int = 10):
4446
# Wait 10 seconds before polling again
4547
sleep(10)
4648

49+
return operation
50+
4751

4852
# [END genappbuilder_poll_operation]

discoveryengine/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-discoveryengine==0.7.0
1+
google-cloud-discoveryengine==0.8.0
22
google-api-core==2.11.0

discoveryengine/search_sample.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
#
1515

1616
# [START genappbuilder_search]
17+
from typing import List
1718

18-
from google.cloud import discoveryengine_v1beta as genappbuilder
19+
from google.cloud import discoveryengine
1920

2021
# TODO(developer): Uncomment these variables before running the sample.
2122
# project_id = "YOUR_PROJECT_ID"
@@ -31,9 +32,9 @@ def search_sample(
3132
search_engine_id: str,
3233
serving_config_id: str,
3334
search_query: str,
34-
) -> None:
35+
) -> List[discoveryengine.SearchResponse.SearchResult]:
3536
# Create a client
36-
client = genappbuilder.SearchServiceClient()
37+
client = discoveryengine.SearchServiceClient()
3738

3839
# The full resource name of the search engine serving config
3940
# e.g. projects/{project_id}/locations/{location}
@@ -44,13 +45,15 @@ def search_sample(
4445
serving_config=serving_config_id,
4546
)
4647

47-
request = genappbuilder.SearchRequest(
48+
request = discoveryengine.SearchRequest(
4849
serving_config=serving_config,
4950
query=search_query,
5051
)
5152
response = client.search(request)
5253
for result in response.results:
5354
print(result)
5455

56+
return response.results
57+
5558

5659
# [END genappbuilder_search]

discoveryengine/search_sample_test.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424
search_query = "Google"
2525

2626

27-
def test_search(capsys):
28-
search_sample.search_sample(
27+
def test_search():
28+
response = search_sample.search_sample(
2929
project_id=project_id,
3030
location=location,
3131
search_engine_id=search_engine_id,
3232
serving_config_id=serving_config_id,
3333
search_query=search_query,
3434
)
3535

36-
out, _ = capsys.readouterr()
37-
38-
assert search_query in out
36+
assert response

0 commit comments

Comments
 (0)