From 1a0aef4290128c90007d3ab44ad7a28ecd5cc8c5 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Fri, 20 Oct 2023 10:00:07 -0400 Subject: [PATCH] chore: Update gapic-generator-python to v1.11.9 (#559) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: Update gapic-generator-python to v1.11.7 PiperOrigin-RevId: 573230664 Source-Link: https://github.com/googleapis/googleapis/commit/93beed334607e70709cc60e6145be65fdc8ec386 Source-Link: https://github.com/googleapis/googleapis-gen/commit/f4a4edaa8057639fcf6adf9179872280d1a8f651 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZjRhNGVkYWE4MDU3NjM5ZmNmNmFkZjkxNzk4NzIyODBkMWE4ZjY1MSJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: Update gapic-generator-python to v1.11.8 PiperOrigin-RevId: 574178735 Source-Link: https://github.com/googleapis/googleapis/commit/7307199008ee2d57a4337066de29f9cd8c444bc6 Source-Link: https://github.com/googleapis/googleapis-gen/commit/ce3af21b7c559a87c2befc076be0e3aeda3a26f0 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiY2UzYWYyMWI3YzU1OWE4N2MyYmVmYzA3NmJlMGUzYWVkYTNhMjZmMCJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: Update gapic-generator-python to v1.11.9 PiperOrigin-RevId: 574520922 Source-Link: https://github.com/googleapis/googleapis/commit/5183984d611beb41e90f65f08609b9d926f779bd Source-Link: https://github.com/googleapis/googleapis-gen/commit/a59af19d4ac6509faedf1cc39029141b6a5b8968 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiYTU5YWYxOWQ0YWM2NTA5ZmFlZGYxY2MzOTAyOTE0MWI2YTViODk2OCJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * update index.rst; remove unused files --------- Co-authored-by: Owl Bot Co-authored-by: Anthonios Partheniou --- docs/index.rst | 16 +- .../vision_v1/{services.rst => services_.rst} | 0 docs/vision_v1/{types.rst => types_.rst} | 0 .../{services.rst => services_.rst} | 0 .../{types.rst => types_.rst} | 0 .../{services.rst => services_.rst} | 0 .../{types.rst => types_.rst} | 0 .../{services.rst => services_.rst} | 0 .../{types.rst => types_.rst} | 0 .../gapic/vision_v1/test_image_annotator.py | 34 +- .../gapic/vision_v1/test_product_search.py | 583 ++++++++++++++---- .../vision_v1p1beta1/test_image_annotator.py | 17 +- .../vision_v1p2beta1/test_image_annotator.py | 17 +- .../vision_v1p3beta1/test_image_annotator.py | 17 +- .../vision_v1p3beta1/test_product_search.py | 583 ++++++++++++++---- .../vision_v1p4beta1/test_image_annotator.py | 34 +- .../vision_v1p4beta1/test_product_search.py | 583 ++++++++++++++---- 17 files changed, 1421 insertions(+), 463 deletions(-) rename docs/vision_v1/{services.rst => services_.rst} (100%) rename docs/vision_v1/{types.rst => types_.rst} (100%) rename docs/vision_v1p2beta1/{services.rst => services_.rst} (100%) rename docs/vision_v1p2beta1/{types.rst => types_.rst} (100%) rename docs/vision_v1p3beta1/{services.rst => services_.rst} (100%) rename docs/vision_v1p3beta1/{types.rst => types_.rst} (100%) rename docs/vision_v1p4beta1/{services.rst => services_.rst} (100%) rename docs/vision_v1p4beta1/{types.rst => types_.rst} (100%) diff --git a/docs/index.rst b/docs/index.rst index 3b6c9404..c1f39aa4 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -84,8 +84,8 @@ API. By default, you will get ``v1``, the latest stable version. .. toctree:: :maxdepth: 2 - vision_v1/services - vision_v1/types + vision_v1/services_ + vision_v1/types_ A new beta release with additional features over the current stable version, @@ -96,8 +96,8 @@ usual beta admonishment (changes are possible, etc.) applies. .. toctree:: :maxdepth: 2 - vision_v1p4beta1/services - vision_v1p4beta1/types + vision_v1p4beta1/services_ + vision_v1p4beta1/types_ Previous beta releases ``v1p3beta1`` and ``v1p2beta1`` are provided as well. @@ -106,8 +106,8 @@ An API and type reference is provided for ``v1p3beta1``: .. toctree:: :maxdepth: 2 - vision_v1p3beta1/services - vision_v1p3beta1/types + vision_v1p3beta1/services_ + vision_v1p3beta1/types_ An API and type reference is provided for ``v1p2beta1``: @@ -115,8 +115,8 @@ An API and type reference is provided for ``v1p2beta1``: .. toctree:: :maxdepth: 2 - vision_v1p2beta1/services - vision_v1p2beta1/types + vision_v1p2beta1/services_ + vision_v1p2beta1/types_ Migration Guide --------------- diff --git a/docs/vision_v1/services.rst b/docs/vision_v1/services_.rst similarity index 100% rename from docs/vision_v1/services.rst rename to docs/vision_v1/services_.rst diff --git a/docs/vision_v1/types.rst b/docs/vision_v1/types_.rst similarity index 100% rename from docs/vision_v1/types.rst rename to docs/vision_v1/types_.rst diff --git a/docs/vision_v1p2beta1/services.rst b/docs/vision_v1p2beta1/services_.rst similarity index 100% rename from docs/vision_v1p2beta1/services.rst rename to docs/vision_v1p2beta1/services_.rst diff --git a/docs/vision_v1p2beta1/types.rst b/docs/vision_v1p2beta1/types_.rst similarity index 100% rename from docs/vision_v1p2beta1/types.rst rename to docs/vision_v1p2beta1/types_.rst diff --git a/docs/vision_v1p3beta1/services.rst b/docs/vision_v1p3beta1/services_.rst similarity index 100% rename from docs/vision_v1p3beta1/services.rst rename to docs/vision_v1p3beta1/services_.rst diff --git a/docs/vision_v1p3beta1/types.rst b/docs/vision_v1p3beta1/types_.rst similarity index 100% rename from docs/vision_v1p3beta1/types.rst rename to docs/vision_v1p3beta1/types_.rst diff --git a/docs/vision_v1p4beta1/services.rst b/docs/vision_v1p4beta1/services_.rst similarity index 100% rename from docs/vision_v1p4beta1/services.rst rename to docs/vision_v1p4beta1/services_.rst diff --git a/docs/vision_v1p4beta1/types.rst b/docs/vision_v1p4beta1/types_.rst similarity index 100% rename from docs/vision_v1p4beta1/types.rst rename to docs/vision_v1p4beta1/types_.rst diff --git a/tests/unit/gapic/vision_v1/test_image_annotator.py b/tests/unit/gapic/vision_v1/test_image_annotator.py index 70675a95..86174548 100644 --- a/tests/unit/gapic/vision_v1/test_image_annotator.py +++ b/tests/unit/gapic/vision_v1/test_image_annotator.py @@ -1585,8 +1585,9 @@ def test_batch_annotate_images_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1656,10 +1657,9 @@ def test_batch_annotate_images_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1788,8 +1788,9 @@ def test_batch_annotate_images_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1854,8 +1855,9 @@ def test_batch_annotate_files_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateFilesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateFilesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1925,10 +1927,9 @@ def test_batch_annotate_files_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateFilesResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateFilesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -2059,8 +2060,9 @@ def test_batch_annotate_files_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateFilesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateFilesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value diff --git a/tests/unit/gapic/vision_v1/test_product_search.py b/tests/unit/gapic/vision_v1/test_product_search.py index 3f99bf1c..c609e361 100644 --- a/tests/unit/gapic/vision_v1/test_product_search.py +++ b/tests/unit/gapic/vision_v1/test_product_search.py @@ -6253,6 +6253,75 @@ def test_create_product_set_rest(request_type): ], }, } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.CreateProductSetRequest.meta.fields[ + "product_set" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product_set"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product_set"][field])): + del request_init["product_set"][field][i][subfield] + else: + del request_init["product_set"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -6266,8 +6335,9 @@ def test_create_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6346,8 +6416,9 @@ def test_create_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6443,21 +6514,6 @@ def test_create_product_set_rest_bad_request( # send a request that will satisfy transcoding request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["product_set"] = { - "name": "name_value", - "display_name": "display_name_value", - "index_time": {"seconds": 751, "nanos": 543}, - "index_error": { - "code": 411, - "message": "message_value", - "details": [ - { - "type_url": "type.googleapis.com/google.protobuf.Duration", - "value": b"\x08\x0c\x10\xdb\x07", - } - ], - }, - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -6497,8 +6553,9 @@ def test_create_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6565,10 +6622,9 @@ def test_list_product_sets_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductSetsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductSetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6650,10 +6706,11 @@ def test_list_product_sets_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductSetsResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListProductSetsResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6788,10 +6845,9 @@ def test_list_product_sets_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductSetsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductSetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6914,8 +6970,9 @@ def test_get_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6991,8 +7048,9 @@ def test_get_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7119,8 +7177,9 @@ def test_get_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7192,6 +7251,75 @@ def test_update_product_set_rest(request_type): ], }, } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.UpdateProductSetRequest.meta.fields[ + "product_set" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product_set"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product_set"][field])): + del request_init["product_set"][field][i][subfield] + else: + del request_init["product_set"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -7205,8 +7333,9 @@ def test_update_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7280,8 +7409,9 @@ def test_update_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7373,21 +7503,6 @@ def test_update_product_set_rest_bad_request( "name": "projects/sample1/locations/sample2/productSets/sample3" } } - request_init["product_set"] = { - "name": "projects/sample1/locations/sample2/productSets/sample3", - "display_name": "display_name_value", - "index_time": {"seconds": 751, "nanos": 543}, - "index_error": { - "code": 411, - "message": "message_value", - "details": [ - { - "type_url": "type.googleapis.com/google.protobuf.Duration", - "value": b"\x08\x0c\x10\xdb\x07", - } - ], - }, - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -7430,8 +7545,9 @@ def test_update_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7747,6 +7863,73 @@ def test_create_product_rest(request_type): "product_category": "product_category_value", "product_labels": [{"key": "key_value", "value": "value_value"}], } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.CreateProductRequest.meta.fields["product"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product"][field])): + del request_init["product"][field][i][subfield] + else: + del request_init["product"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -7762,8 +7945,9 @@ def test_create_product_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7844,8 +8028,9 @@ def test_create_product_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7941,13 +8126,6 @@ def test_create_product_rest_bad_request( # send a request that will satisfy transcoding request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["product"] = { - "name": "name_value", - "display_name": "display_name_value", - "description": "description_value", - "product_category": "product_category_value", - "product_labels": [{"key": "key_value", "value": "value_value"}], - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -7987,8 +8165,9 @@ def test_create_product_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8054,8 +8233,9 @@ def test_list_products_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8137,10 +8317,9 @@ def test_list_products_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8273,8 +8452,9 @@ def test_list_products_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8398,8 +8578,9 @@ def test_get_product_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8477,8 +8658,9 @@ def test_get_product_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8603,8 +8785,9 @@ def test_get_product_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8665,6 +8848,73 @@ def test_update_product_rest(request_type): "product_category": "product_category_value", "product_labels": [{"key": "key_value", "value": "value_value"}], } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.UpdateProductRequest.meta.fields["product"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product"][field])): + del request_init["product"][field][i][subfield] + else: + del request_init["product"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -8680,8 +8930,9 @@ def test_update_product_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8757,8 +9008,9 @@ def test_update_product_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8848,13 +9100,6 @@ def test_update_product_rest_bad_request( request_init = { "product": {"name": "projects/sample1/locations/sample2/products/sample3"} } - request_init["product"] = { - "name": "projects/sample1/locations/sample2/products/sample3", - "display_name": "display_name_value", - "description": "description_value", - "product_category": "product_category_value", - "product_labels": [{"key": "key_value", "value": "value_value"}], - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -8895,8 +9140,9 @@ def test_update_product_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9212,6 +9458,75 @@ def test_create_reference_image_rest(request_type): } ], } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.CreateReferenceImageRequest.meta.fields[ + "reference_image" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["reference_image"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["reference_image"][field])): + del request_init["reference_image"][field][i][subfield] + else: + del request_init["reference_image"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -9225,8 +9540,9 @@ def test_create_reference_image_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9305,8 +9621,9 @@ def test_create_reference_image_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9403,16 +9720,6 @@ def test_create_reference_image_rest_bad_request( # send a request that will satisfy transcoding request_init = {"parent": "projects/sample1/locations/sample2/products/sample3"} - request_init["reference_image"] = { - "name": "name_value", - "uri": "uri_value", - "bounding_polys": [ - { - "vertices": [{"x": 120, "y": 121}], - "normalized_vertices": [{"x": 0.12, "y": 0.121}], - } - ], - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -9454,8 +9761,9 @@ def test_create_reference_image_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9783,10 +10091,11 @@ def test_list_reference_images_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListReferenceImagesResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListReferenceImagesResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9869,10 +10178,11 @@ def test_list_reference_images_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListReferenceImagesResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListReferenceImagesResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10010,10 +10320,11 @@ def test_list_reference_images_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListReferenceImagesResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListReferenceImagesResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10143,8 +10454,9 @@ def test_get_reference_image_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10220,8 +10532,9 @@ def test_get_reference_image_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10351,8 +10664,9 @@ def test_get_reference_image_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10961,10 +11275,11 @@ def test_list_products_in_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsInProductSetResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListProductsInProductSetResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -11046,10 +11361,11 @@ def test_list_products_in_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = ( - product_search_service.ListProductsInProductSetResponse.pb(return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsInProductSetResponse.pb( + return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -11187,10 +11503,11 @@ def test_list_products_in_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsInProductSetResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListProductsInProductSetResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value diff --git a/tests/unit/gapic/vision_v1p1beta1/test_image_annotator.py b/tests/unit/gapic/vision_v1p1beta1/test_image_annotator.py index c754df06..6e77a466 100644 --- a/tests/unit/gapic/vision_v1p1beta1/test_image_annotator.py +++ b/tests/unit/gapic/vision_v1p1beta1/test_image_annotator.py @@ -934,8 +934,9 @@ def test_batch_annotate_images_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1005,10 +1006,9 @@ def test_batch_annotate_images_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1137,8 +1137,9 @@ def test_batch_annotate_images_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value diff --git a/tests/unit/gapic/vision_v1p2beta1/test_image_annotator.py b/tests/unit/gapic/vision_v1p2beta1/test_image_annotator.py index e74b0988..627bbc47 100644 --- a/tests/unit/gapic/vision_v1p2beta1/test_image_annotator.py +++ b/tests/unit/gapic/vision_v1p2beta1/test_image_annotator.py @@ -1151,8 +1151,9 @@ def test_batch_annotate_images_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1222,10 +1223,9 @@ def test_batch_annotate_images_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1354,8 +1354,9 @@ def test_batch_annotate_images_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value diff --git a/tests/unit/gapic/vision_v1p3beta1/test_image_annotator.py b/tests/unit/gapic/vision_v1p3beta1/test_image_annotator.py index f70d8333..8553726b 100644 --- a/tests/unit/gapic/vision_v1p3beta1/test_image_annotator.py +++ b/tests/unit/gapic/vision_v1p3beta1/test_image_annotator.py @@ -1153,8 +1153,9 @@ def test_batch_annotate_images_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1224,10 +1225,9 @@ def test_batch_annotate_images_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1356,8 +1356,9 @@ def test_batch_annotate_images_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value diff --git a/tests/unit/gapic/vision_v1p3beta1/test_product_search.py b/tests/unit/gapic/vision_v1p3beta1/test_product_search.py index 8ec70fd6..7f3d167b 100644 --- a/tests/unit/gapic/vision_v1p3beta1/test_product_search.py +++ b/tests/unit/gapic/vision_v1p3beta1/test_product_search.py @@ -6027,6 +6027,75 @@ def test_create_product_set_rest(request_type): ], }, } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.CreateProductSetRequest.meta.fields[ + "product_set" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product_set"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product_set"][field])): + del request_init["product_set"][field][i][subfield] + else: + del request_init["product_set"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -6040,8 +6109,9 @@ def test_create_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6120,8 +6190,9 @@ def test_create_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6217,21 +6288,6 @@ def test_create_product_set_rest_bad_request( # send a request that will satisfy transcoding request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["product_set"] = { - "name": "name_value", - "display_name": "display_name_value", - "index_time": {"seconds": 751, "nanos": 543}, - "index_error": { - "code": 411, - "message": "message_value", - "details": [ - { - "type_url": "type.googleapis.com/google.protobuf.Duration", - "value": b"\x08\x0c\x10\xdb\x07", - } - ], - }, - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -6271,8 +6327,9 @@ def test_create_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6339,10 +6396,9 @@ def test_list_product_sets_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductSetsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductSetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6424,10 +6480,11 @@ def test_list_product_sets_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductSetsResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListProductSetsResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6562,10 +6619,9 @@ def test_list_product_sets_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductSetsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductSetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6688,8 +6744,9 @@ def test_get_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6765,8 +6822,9 @@ def test_get_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6893,8 +6951,9 @@ def test_get_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6966,6 +7025,75 @@ def test_update_product_set_rest(request_type): ], }, } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.UpdateProductSetRequest.meta.fields[ + "product_set" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product_set"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product_set"][field])): + del request_init["product_set"][field][i][subfield] + else: + del request_init["product_set"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -6979,8 +7107,9 @@ def test_update_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7054,8 +7183,9 @@ def test_update_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7147,21 +7277,6 @@ def test_update_product_set_rest_bad_request( "name": "projects/sample1/locations/sample2/productSets/sample3" } } - request_init["product_set"] = { - "name": "projects/sample1/locations/sample2/productSets/sample3", - "display_name": "display_name_value", - "index_time": {"seconds": 751, "nanos": 543}, - "index_error": { - "code": 411, - "message": "message_value", - "details": [ - { - "type_url": "type.googleapis.com/google.protobuf.Duration", - "value": b"\x08\x0c\x10\xdb\x07", - } - ], - }, - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -7204,8 +7319,9 @@ def test_update_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7521,6 +7637,73 @@ def test_create_product_rest(request_type): "product_category": "product_category_value", "product_labels": [{"key": "key_value", "value": "value_value"}], } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.CreateProductRequest.meta.fields["product"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product"][field])): + del request_init["product"][field][i][subfield] + else: + del request_init["product"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -7536,8 +7719,9 @@ def test_create_product_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7618,8 +7802,9 @@ def test_create_product_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7715,13 +7900,6 @@ def test_create_product_rest_bad_request( # send a request that will satisfy transcoding request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["product"] = { - "name": "name_value", - "display_name": "display_name_value", - "description": "description_value", - "product_category": "product_category_value", - "product_labels": [{"key": "key_value", "value": "value_value"}], - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -7761,8 +7939,9 @@ def test_create_product_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7829,8 +8008,9 @@ def test_list_products_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7912,10 +8092,9 @@ def test_list_products_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8048,8 +8227,9 @@ def test_list_products_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8174,8 +8354,9 @@ def test_get_product_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8253,8 +8434,9 @@ def test_get_product_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8379,8 +8561,9 @@ def test_get_product_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8442,6 +8625,73 @@ def test_update_product_rest(request_type): "product_category": "product_category_value", "product_labels": [{"key": "key_value", "value": "value_value"}], } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.UpdateProductRequest.meta.fields["product"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product"][field])): + del request_init["product"][field][i][subfield] + else: + del request_init["product"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -8457,8 +8707,9 @@ def test_update_product_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8534,8 +8785,9 @@ def test_update_product_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8625,13 +8877,6 @@ def test_update_product_rest_bad_request( request_init = { "product": {"name": "projects/sample1/locations/sample2/products/sample3"} } - request_init["product"] = { - "name": "projects/sample1/locations/sample2/products/sample3", - "display_name": "display_name_value", - "description": "description_value", - "product_category": "product_category_value", - "product_labels": [{"key": "key_value", "value": "value_value"}], - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -8672,8 +8917,9 @@ def test_update_product_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8990,6 +9236,75 @@ def test_create_reference_image_rest(request_type): } ], } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.CreateReferenceImageRequest.meta.fields[ + "reference_image" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["reference_image"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["reference_image"][field])): + del request_init["reference_image"][field][i][subfield] + else: + del request_init["reference_image"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -9003,8 +9318,9 @@ def test_create_reference_image_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9083,8 +9399,9 @@ def test_create_reference_image_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9181,16 +9498,6 @@ def test_create_reference_image_rest_bad_request( # send a request that will satisfy transcoding request_init = {"parent": "projects/sample1/locations/sample2/products/sample3"} - request_init["reference_image"] = { - "name": "name_value", - "uri": "uri_value", - "bounding_polys": [ - { - "vertices": [{"x": 120, "y": 121}], - "normalized_vertices": [{"x": 0.12, "y": 0.121}], - } - ], - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -9232,8 +9539,9 @@ def test_create_reference_image_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9561,10 +9869,11 @@ def test_list_reference_images_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListReferenceImagesResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListReferenceImagesResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9647,10 +9956,11 @@ def test_list_reference_images_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListReferenceImagesResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListReferenceImagesResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9788,10 +10098,11 @@ def test_list_reference_images_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListReferenceImagesResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListReferenceImagesResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9921,8 +10232,9 @@ def test_get_reference_image_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9998,8 +10310,9 @@ def test_get_reference_image_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10129,8 +10442,9 @@ def test_get_reference_image_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10739,10 +11053,11 @@ def test_list_products_in_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsInProductSetResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListProductsInProductSetResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10824,10 +11139,11 @@ def test_list_products_in_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = ( - product_search_service.ListProductsInProductSetResponse.pb(return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsInProductSetResponse.pb( + return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10965,10 +11281,11 @@ def test_list_products_in_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsInProductSetResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListProductsInProductSetResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value diff --git a/tests/unit/gapic/vision_v1p4beta1/test_image_annotator.py b/tests/unit/gapic/vision_v1p4beta1/test_image_annotator.py index 91e44751..b978df1b 100644 --- a/tests/unit/gapic/vision_v1p4beta1/test_image_annotator.py +++ b/tests/unit/gapic/vision_v1p4beta1/test_image_annotator.py @@ -1588,8 +1588,9 @@ def test_batch_annotate_images_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1659,10 +1660,9 @@ def test_batch_annotate_images_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1791,8 +1791,9 @@ def test_batch_annotate_images_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateImagesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1857,8 +1858,9 @@ def test_batch_annotate_files_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateFilesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateFilesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -1928,10 +1930,9 @@ def test_batch_annotate_files_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateFilesResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateFilesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -2062,8 +2063,9 @@ def test_batch_annotate_files_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = image_annotator.BatchAnnotateFilesResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = image_annotator.BatchAnnotateFilesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value diff --git a/tests/unit/gapic/vision_v1p4beta1/test_product_search.py b/tests/unit/gapic/vision_v1p4beta1/test_product_search.py index f6f0c66d..4b146960 100644 --- a/tests/unit/gapic/vision_v1p4beta1/test_product_search.py +++ b/tests/unit/gapic/vision_v1p4beta1/test_product_search.py @@ -6255,6 +6255,75 @@ def test_create_product_set_rest(request_type): ], }, } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.CreateProductSetRequest.meta.fields[ + "product_set" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product_set"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product_set"][field])): + del request_init["product_set"][field][i][subfield] + else: + del request_init["product_set"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -6268,8 +6337,9 @@ def test_create_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6348,8 +6418,9 @@ def test_create_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6445,21 +6516,6 @@ def test_create_product_set_rest_bad_request( # send a request that will satisfy transcoding request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["product_set"] = { - "name": "name_value", - "display_name": "display_name_value", - "index_time": {"seconds": 751, "nanos": 543}, - "index_error": { - "code": 411, - "message": "message_value", - "details": [ - { - "type_url": "type.googleapis.com/google.protobuf.Duration", - "value": b"\x08\x0c\x10\xdb\x07", - } - ], - }, - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -6499,8 +6555,9 @@ def test_create_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6567,10 +6624,9 @@ def test_list_product_sets_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductSetsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductSetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6652,10 +6708,11 @@ def test_list_product_sets_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductSetsResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListProductSetsResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6790,10 +6847,9 @@ def test_list_product_sets_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductSetsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductSetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6916,8 +6972,9 @@ def test_get_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -6993,8 +7050,9 @@ def test_get_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7121,8 +7179,9 @@ def test_get_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7194,6 +7253,75 @@ def test_update_product_set_rest(request_type): ], }, } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.UpdateProductSetRequest.meta.fields[ + "product_set" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product_set"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product_set"][field])): + del request_init["product_set"][field][i][subfield] + else: + del request_init["product_set"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -7207,8 +7335,9 @@ def test_update_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7282,8 +7411,9 @@ def test_update_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7375,21 +7505,6 @@ def test_update_product_set_rest_bad_request( "name": "projects/sample1/locations/sample2/productSets/sample3" } } - request_init["product_set"] = { - "name": "projects/sample1/locations/sample2/productSets/sample3", - "display_name": "display_name_value", - "index_time": {"seconds": 751, "nanos": 543}, - "index_error": { - "code": 411, - "message": "message_value", - "details": [ - { - "type_url": "type.googleapis.com/google.protobuf.Duration", - "value": b"\x08\x0c\x10\xdb\x07", - } - ], - }, - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -7432,8 +7547,9 @@ def test_update_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ProductSet.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ProductSet.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7749,6 +7865,73 @@ def test_create_product_rest(request_type): "product_category": "product_category_value", "product_labels": [{"key": "key_value", "value": "value_value"}], } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.CreateProductRequest.meta.fields["product"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product"][field])): + del request_init["product"][field][i][subfield] + else: + del request_init["product"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -7764,8 +7947,9 @@ def test_create_product_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7846,8 +8030,9 @@ def test_create_product_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -7943,13 +8128,6 @@ def test_create_product_rest_bad_request( # send a request that will satisfy transcoding request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["product"] = { - "name": "name_value", - "display_name": "display_name_value", - "description": "description_value", - "product_category": "product_category_value", - "product_labels": [{"key": "key_value", "value": "value_value"}], - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -7989,8 +8167,9 @@ def test_create_product_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8057,8 +8236,9 @@ def test_list_products_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8140,10 +8320,9 @@ def test_list_products_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8276,8 +8455,9 @@ def test_list_products_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8402,8 +8582,9 @@ def test_get_product_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8481,8 +8662,9 @@ def test_get_product_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8607,8 +8789,9 @@ def test_get_product_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8670,6 +8853,73 @@ def test_update_product_rest(request_type): "product_category": "product_category_value", "product_labels": [{"key": "key_value", "value": "value_value"}], } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.UpdateProductRequest.meta.fields["product"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["product"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["product"][field])): + del request_init["product"][field][i][subfield] + else: + del request_init["product"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -8685,8 +8935,9 @@ def test_update_product_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8762,8 +9013,9 @@ def test_update_product_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -8853,13 +9105,6 @@ def test_update_product_rest_bad_request( request_init = { "product": {"name": "projects/sample1/locations/sample2/products/sample3"} } - request_init["product"] = { - "name": "projects/sample1/locations/sample2/products/sample3", - "display_name": "display_name_value", - "description": "description_value", - "product_category": "product_category_value", - "product_labels": [{"key": "key_value", "value": "value_value"}], - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -8900,8 +9145,9 @@ def test_update_product_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.Product.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.Product.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9218,6 +9464,75 @@ def test_create_reference_image_rest(request_type): } ], } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = product_search_service.CreateReferenceImageRequest.meta.fields[ + "reference_image" + ] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["reference_image"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["reference_image"][field])): + del request_init["reference_image"][field][i][subfield] + else: + del request_init["reference_image"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -9231,8 +9546,9 @@ def test_create_reference_image_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9311,8 +9627,9 @@ def test_create_reference_image_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9409,16 +9726,6 @@ def test_create_reference_image_rest_bad_request( # send a request that will satisfy transcoding request_init = {"parent": "projects/sample1/locations/sample2/products/sample3"} - request_init["reference_image"] = { - "name": "name_value", - "uri": "uri_value", - "bounding_polys": [ - { - "vertices": [{"x": 120, "y": 121}], - "normalized_vertices": [{"x": 0.12, "y": 0.121}], - } - ], - } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -9460,8 +9767,9 @@ def test_create_reference_image_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9789,10 +10097,11 @@ def test_list_reference_images_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListReferenceImagesResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListReferenceImagesResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -9875,10 +10184,11 @@ def test_list_reference_images_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListReferenceImagesResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListReferenceImagesResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10016,10 +10326,11 @@ def test_list_reference_images_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListReferenceImagesResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListReferenceImagesResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10149,8 +10460,9 @@ def test_get_reference_image_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10226,8 +10538,9 @@ def test_get_reference_image_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10357,8 +10670,9 @@ def test_get_reference_image_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ReferenceImage.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Convert return value to protobuf type + return_value = product_search_service.ReferenceImage.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -10967,10 +11281,11 @@ def test_list_products_in_product_set_rest(request_type): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsInProductSetResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListProductsInProductSetResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -11052,10 +11367,11 @@ def test_list_products_in_product_set_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = ( - product_search_service.ListProductsInProductSetResponse.pb(return_value) + # Convert return value to protobuf type + return_value = product_search_service.ListProductsInProductSetResponse.pb( + return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value @@ -11193,10 +11509,11 @@ def test_list_products_in_product_set_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = product_search_service.ListProductsInProductSetResponse.pb( + # Convert return value to protobuf type + return_value = product_search_service.ListProductsInProductSetResponse.pb( return_value ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value