@@ -1415,8 +1415,9 @@ def test_get_application_rest(request_type):
14151415 # Wrap the value into a proper Response obj
14161416 response_value = Response ()
14171417 response_value .status_code = 200
1418- pb_return_value = application .Application .pb (return_value )
1419- json_return_value = json_format .MessageToJson (pb_return_value )
1418+ # Convert return value to protobuf type
1419+ return_value = application .Application .pb (return_value )
1420+ json_return_value = json_format .MessageToJson (return_value )
14201421
14211422 response_value ._content = json_return_value .encode ("UTF-8" )
14221423 req .return_value = response_value
@@ -1543,8 +1544,9 @@ def test_get_application_rest_flattened():
15431544 # Wrap the value into a proper Response obj
15441545 response_value = Response ()
15451546 response_value .status_code = 200
1546- pb_return_value = application .Application .pb (return_value )
1547- json_return_value = json_format .MessageToJson (pb_return_value )
1547+ # Convert return value to protobuf type
1548+ return_value = application .Application .pb (return_value )
1549+ json_return_value = json_format .MessageToJson (return_value )
15481550 response_value ._content = json_return_value .encode ("UTF-8" )
15491551 req .return_value = response_value
15501552
@@ -1622,6 +1624,73 @@ def test_create_application_rest(request_type):
16221624 "use_container_optimized_os" : True ,
16231625 },
16241626 }
1627+ # The version of a generated dependency at test runtime may differ from the version used during generation.
1628+ # Delete any fields which are not present in the current runtime dependency
1629+ # See https://github.com/googleapis/gapic-generator-python/issues/1748
1630+
1631+ # Determine if the message type is proto-plus or protobuf
1632+ test_field = appengine .CreateApplicationRequest .meta .fields ["application" ]
1633+
1634+ def get_message_fields (field ):
1635+ # Given a field which is a message (composite type), return a list with
1636+ # all the fields of the message.
1637+ # If the field is not a composite type, return an empty list.
1638+ message_fields = []
1639+
1640+ if hasattr (field , "message" ) and field .message :
1641+ is_field_type_proto_plus_type = not hasattr (field .message , "DESCRIPTOR" )
1642+
1643+ if is_field_type_proto_plus_type :
1644+ message_fields = field .message .meta .fields .values ()
1645+ # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types
1646+ else : # pragma: NO COVER
1647+ message_fields = field .message .DESCRIPTOR .fields
1648+ return message_fields
1649+
1650+ runtime_nested_fields = [
1651+ (field .name , nested_field .name )
1652+ for field in get_message_fields (test_field )
1653+ for nested_field in get_message_fields (field )
1654+ ]
1655+
1656+ subfields_not_in_runtime = []
1657+
1658+ # For each item in the sample request, create a list of sub fields which are not present at runtime
1659+ # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime
1660+ for field , value in request_init ["application" ].items (): # pragma: NO COVER
1661+ result = None
1662+ is_repeated = False
1663+ # For repeated fields
1664+ if isinstance (value , list ) and len (value ):
1665+ is_repeated = True
1666+ result = value [0 ]
1667+ # For fields where the type is another message
1668+ if isinstance (value , dict ):
1669+ result = value
1670+
1671+ if result and hasattr (result , "keys" ):
1672+ for subfield in result .keys ():
1673+ if (field , subfield ) not in runtime_nested_fields :
1674+ subfields_not_in_runtime .append (
1675+ {
1676+ "field" : field ,
1677+ "subfield" : subfield ,
1678+ "is_repeated" : is_repeated ,
1679+ }
1680+ )
1681+
1682+ # Remove fields from the sample request which are not present in the runtime version of the dependency
1683+ # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime
1684+ for subfield_to_delete in subfields_not_in_runtime : # pragma: NO COVER
1685+ field = subfield_to_delete .get ("field" )
1686+ field_repeated = subfield_to_delete .get ("is_repeated" )
1687+ subfield = subfield_to_delete .get ("subfield" )
1688+ if subfield :
1689+ if field_repeated :
1690+ for i in range (0 , len (request_init ["application" ][field ])):
1691+ del request_init ["application" ][field ][i ][subfield ]
1692+ else :
1693+ del request_init ["application" ][field ][subfield ]
16251694 request = request_type (** request_init )
16261695
16271696 # Mock the http request call within the method and fake a response.
@@ -1711,33 +1780,6 @@ def test_create_application_rest_bad_request(
17111780
17121781 # send a request that will satisfy transcoding
17131782 request_init = {}
1714- request_init ["application" ] = {
1715- "name" : "name_value" ,
1716- "id" : "id_value" ,
1717- "dispatch_rules" : [
1718- {"domain" : "domain_value" , "path" : "path_value" , "service" : "service_value" }
1719- ],
1720- "auth_domain" : "auth_domain_value" ,
1721- "location_id" : "location_id_value" ,
1722- "code_bucket" : "code_bucket_value" ,
1723- "default_cookie_expiration" : {"seconds" : 751 , "nanos" : 543 },
1724- "serving_status" : 1 ,
1725- "default_hostname" : "default_hostname_value" ,
1726- "default_bucket" : "default_bucket_value" ,
1727- "service_account" : "service_account_value" ,
1728- "iap" : {
1729- "enabled" : True ,
1730- "oauth2_client_id" : "oauth2_client_id_value" ,
1731- "oauth2_client_secret" : "oauth2_client_secret_value" ,
1732- "oauth2_client_secret_sha256" : "oauth2_client_secret_sha256_value" ,
1733- },
1734- "gcr_domain" : "gcr_domain_value" ,
1735- "database_type" : 1 ,
1736- "feature_settings" : {
1737- "split_health_checks" : True ,
1738- "use_container_optimized_os" : True ,
1739- },
1740- }
17411783 request = request_type (** request_init )
17421784
17431785 # Mock the http request call within the method and fake a BadRequest error.
@@ -1800,6 +1842,73 @@ def test_update_application_rest(request_type):
18001842 "use_container_optimized_os" : True ,
18011843 },
18021844 }
1845+ # The version of a generated dependency at test runtime may differ from the version used during generation.
1846+ # Delete any fields which are not present in the current runtime dependency
1847+ # See https://github.com/googleapis/gapic-generator-python/issues/1748
1848+
1849+ # Determine if the message type is proto-plus or protobuf
1850+ test_field = appengine .UpdateApplicationRequest .meta .fields ["application" ]
1851+
1852+ def get_message_fields (field ):
1853+ # Given a field which is a message (composite type), return a list with
1854+ # all the fields of the message.
1855+ # If the field is not a composite type, return an empty list.
1856+ message_fields = []
1857+
1858+ if hasattr (field , "message" ) and field .message :
1859+ is_field_type_proto_plus_type = not hasattr (field .message , "DESCRIPTOR" )
1860+
1861+ if is_field_type_proto_plus_type :
1862+ message_fields = field .message .meta .fields .values ()
1863+ # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types
1864+ else : # pragma: NO COVER
1865+ message_fields = field .message .DESCRIPTOR .fields
1866+ return message_fields
1867+
1868+ runtime_nested_fields = [
1869+ (field .name , nested_field .name )
1870+ for field in get_message_fields (test_field )
1871+ for nested_field in get_message_fields (field )
1872+ ]
1873+
1874+ subfields_not_in_runtime = []
1875+
1876+ # For each item in the sample request, create a list of sub fields which are not present at runtime
1877+ # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime
1878+ for field , value in request_init ["application" ].items (): # pragma: NO COVER
1879+ result = None
1880+ is_repeated = False
1881+ # For repeated fields
1882+ if isinstance (value , list ) and len (value ):
1883+ is_repeated = True
1884+ result = value [0 ]
1885+ # For fields where the type is another message
1886+ if isinstance (value , dict ):
1887+ result = value
1888+
1889+ if result and hasattr (result , "keys" ):
1890+ for subfield in result .keys ():
1891+ if (field , subfield ) not in runtime_nested_fields :
1892+ subfields_not_in_runtime .append (
1893+ {
1894+ "field" : field ,
1895+ "subfield" : subfield ,
1896+ "is_repeated" : is_repeated ,
1897+ }
1898+ )
1899+
1900+ # Remove fields from the sample request which are not present in the runtime version of the dependency
1901+ # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime
1902+ for subfield_to_delete in subfields_not_in_runtime : # pragma: NO COVER
1903+ field = subfield_to_delete .get ("field" )
1904+ field_repeated = subfield_to_delete .get ("is_repeated" )
1905+ subfield = subfield_to_delete .get ("subfield" )
1906+ if subfield :
1907+ if field_repeated :
1908+ for i in range (0 , len (request_init ["application" ][field ])):
1909+ del request_init ["application" ][field ][i ][subfield ]
1910+ else :
1911+ del request_init ["application" ][field ][subfield ]
18031912 request = request_type (** request_init )
18041913
18051914 # Mock the http request call within the method and fake a response.
@@ -1889,33 +1998,6 @@ def test_update_application_rest_bad_request(
18891998
18901999 # send a request that will satisfy transcoding
18912000 request_init = {"name" : "apps/sample1" }
1892- request_init ["application" ] = {
1893- "name" : "name_value" ,
1894- "id" : "id_value" ,
1895- "dispatch_rules" : [
1896- {"domain" : "domain_value" , "path" : "path_value" , "service" : "service_value" }
1897- ],
1898- "auth_domain" : "auth_domain_value" ,
1899- "location_id" : "location_id_value" ,
1900- "code_bucket" : "code_bucket_value" ,
1901- "default_cookie_expiration" : {"seconds" : 751 , "nanos" : 543 },
1902- "serving_status" : 1 ,
1903- "default_hostname" : "default_hostname_value" ,
1904- "default_bucket" : "default_bucket_value" ,
1905- "service_account" : "service_account_value" ,
1906- "iap" : {
1907- "enabled" : True ,
1908- "oauth2_client_id" : "oauth2_client_id_value" ,
1909- "oauth2_client_secret" : "oauth2_client_secret_value" ,
1910- "oauth2_client_secret_sha256" : "oauth2_client_secret_sha256_value" ,
1911- },
1912- "gcr_domain" : "gcr_domain_value" ,
1913- "database_type" : 1 ,
1914- "feature_settings" : {
1915- "split_health_checks" : True ,
1916- "use_container_optimized_os" : True ,
1917- },
1918- }
19192001 request = request_type (** request_init )
19202002
19212003 # Mock the http request call within the method and fake a BadRequest error.
0 commit comments