|
23 | 23 | from google.api import routing_pb2
|
24 | 24 | from google.cloud import extended_operations_pb2 as ex_ops_pb2
|
25 | 25 | from google.protobuf import descriptor_pb2
|
| 26 | +from google.protobuf import wrappers_pb2 |
26 | 27 |
|
27 | 28 | from gapic.schema import metadata
|
28 | 29 | from gapic.schema import wrappers
|
@@ -189,6 +190,63 @@ def test_method_paged_result_field_no_page_field():
|
189 | 190 | assert method.paged_result_field is None
|
190 | 191 |
|
191 | 192 |
|
| 193 | +def test_method_paged_result_field_invalid_wrapper_type(): |
| 194 | + """Validate paged_result_field() returns None if page_size/max_results wrappertypes |
| 195 | + are not allowed types. |
| 196 | + """ |
| 197 | + |
| 198 | + # page_size is not allowed wrappertype |
| 199 | + parent = make_field(name="parent", type="TYPE_STRING") |
| 200 | + page_size = make_field(name="page_size", type="TYPE_DOUBLE") # not an allowed type |
| 201 | + page_token = make_field(name="page_token", type="TYPE_STRING") |
| 202 | + foos = make_field(name="foos", message=make_message("Foo"), repeated=True) |
| 203 | + next_page_token = make_field(name="next_page_token", type="TYPE_STRING") |
| 204 | + |
| 205 | + input_msg = make_message( |
| 206 | + name="ListFoosRequest", |
| 207 | + fields=( |
| 208 | + parent, |
| 209 | + page_size, |
| 210 | + page_token, |
| 211 | + ), |
| 212 | + ) |
| 213 | + output_msg = make_message( |
| 214 | + name="ListFoosResponse", |
| 215 | + fields=( |
| 216 | + foos, |
| 217 | + next_page_token, |
| 218 | + ), |
| 219 | + ) |
| 220 | + method = make_method( |
| 221 | + "ListFoos", |
| 222 | + input_message=input_msg, |
| 223 | + output_message=output_msg, |
| 224 | + ) |
| 225 | + assert method.paged_result_field is None |
| 226 | + |
| 227 | + # max_results is not allowed wrappertype |
| 228 | + max_results = make_field( |
| 229 | + name="max_results", type="TYPE_STRING" |
| 230 | + ) # not an allowed type |
| 231 | + |
| 232 | + input_msg = make_message( |
| 233 | + name="ListFoosRequest", |
| 234 | + fields=( |
| 235 | + parent, |
| 236 | + max_results, |
| 237 | + page_token, |
| 238 | + ), |
| 239 | + ) |
| 240 | + |
| 241 | + method = make_method( |
| 242 | + "ListFoos", |
| 243 | + input_message=input_msg, |
| 244 | + output_message=output_msg, |
| 245 | + ) |
| 246 | + |
| 247 | + assert method.paged_result_field is None |
| 248 | + |
| 249 | + |
192 | 250 | def test_method_paged_result_ref_types():
|
193 | 251 | input_msg = make_message(
|
194 | 252 | name="ListSquidsRequest",
|
@@ -999,3 +1057,63 @@ def test_mixin_rule():
|
999 | 1057 | "city": {},
|
1000 | 1058 | }
|
1001 | 1059 | assert e == m.sample_request
|
| 1060 | + |
| 1061 | + |
| 1062 | +@pytest.mark.parametrize( |
| 1063 | + "field_type, pb_type, expected", |
| 1064 | + [ |
| 1065 | + # valid paged_result_field candidates |
| 1066 | + (int, "TYPE_INT32", True), |
| 1067 | + (wrappers_pb2.UInt32Value, "TYPE_MESSAGE", True), |
| 1068 | + (wrappers_pb2.Int32Value, "TYPE_MESSAGE", True), |
| 1069 | + # invalid paged_result_field candidates |
| 1070 | + (float, "TYPE_DOUBLE", False), |
| 1071 | + (wrappers_pb2.UInt32Value, "TYPE_DOUBLE", False), |
| 1072 | + (wrappers_pb2.Int32Value, "TYPE_DOUBLE", False), |
| 1073 | + ], |
| 1074 | +) |
| 1075 | +def test__validate_paged_field_size_type(field_type, pb_type, expected): |
| 1076 | + """Test _validate_paged_field_size_type with wrapper types and type indicators.""" |
| 1077 | + |
| 1078 | + # Setup |
| 1079 | + if pb_type in {"TYPE_INT32", "TYPE_DOUBLE"}: |
| 1080 | + page_size = make_field(name="page_size", type=pb_type) |
| 1081 | + else: |
| 1082 | + # expecting TYPE_MESSAGE which in this context is associated with |
| 1083 | + # *Int32Value in legacy APIs such as BigQuery. |
| 1084 | + # See: https://github.com/googleapis/gapic-generator-python/blob/c8b7229ba2865d6a2f5966aa151be121de81f92d/gapic/schema/wrappers.py#L378C1-L411C10 |
| 1085 | + page_size = make_field( |
| 1086 | + name="max_results", |
| 1087 | + type=pb_type, |
| 1088 | + message=make_message(name=field_type.DESCRIPTOR.name), |
| 1089 | + ) |
| 1090 | + |
| 1091 | + parent = make_field(name="parent", type="TYPE_STRING") |
| 1092 | + page_token = make_field(name="page_token", type="TYPE_STRING") |
| 1093 | + next_page_token = make_field(name="next_page_token", type="TYPE_STRING") |
| 1094 | + |
| 1095 | + input_msg = make_message( |
| 1096 | + name="ListFoosRequest", |
| 1097 | + fields=( |
| 1098 | + parent, |
| 1099 | + page_size, |
| 1100 | + page_token, |
| 1101 | + ), |
| 1102 | + ) |
| 1103 | + |
| 1104 | + output_msg = make_message( |
| 1105 | + name="ListFoosResponse", |
| 1106 | + fields=( |
| 1107 | + make_field(name="foos", message=make_message("Foo"), repeated=True), |
| 1108 | + next_page_token, |
| 1109 | + ), |
| 1110 | + ) |
| 1111 | + |
| 1112 | + method = make_method( |
| 1113 | + "ListFoos", |
| 1114 | + input_message=input_msg, |
| 1115 | + output_message=output_msg, |
| 1116 | + ) |
| 1117 | + |
| 1118 | + actual = method._validate_paged_field_size_type(page_field_size=page_size) |
| 1119 | + assert actual == expected |
0 commit comments