|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 | #
|
15 |
| -from absl.testing import parameterized |
16 | 15 | import cloudpickle
|
17 | 16 | import difflib
|
18 | 17 | import importlib
|
@@ -1147,7 +1146,6 @@ def test_create_agent_engine_with_env_vars_list(
|
1147 | 1146 | retry=_TEST_RETRY,
|
1148 | 1147 | )
|
1149 | 1148 |
|
1150 |
| - # pytest does not allow absl.testing.parameterized.named_parameters. |
1151 | 1149 | @pytest.mark.parametrize(
|
1152 | 1150 | "test_case_name, test_engine_instance, expected_framework",
|
1153 | 1151 | [
|
@@ -1190,7 +1188,6 @@ def test_get_agent_framework(
|
1190 | 1188 | framework = _agent_engines._get_agent_framework(test_engine_instance)
|
1191 | 1189 | assert framework == expected_framework
|
1192 | 1190 |
|
1193 |
| - # pytest does not allow absl.testing.parameterized.named_parameters. |
1194 | 1191 | @pytest.mark.parametrize(
|
1195 | 1192 | "test_case_name, test_kwargs, want_request",
|
1196 | 1193 | [
|
@@ -1601,7 +1598,6 @@ def test_query_agent_engine(
|
1601 | 1598 | test_agent_engine.query(query=_TEST_QUERY_PROMPT)
|
1602 | 1599 | query_mock.assert_called_with(request=_TEST_AGENT_ENGINE_QUERY_REQUEST)
|
1603 | 1600 |
|
1604 |
| - # pytest does not allow absl.testing.parameterized.named_parameters. |
1605 | 1601 | @pytest.mark.parametrize(
|
1606 | 1602 | "test_case_name, test_class_methods_spec, want_operation_schema_api_modes",
|
1607 | 1603 | [
|
@@ -1847,7 +1843,6 @@ def test_query_after_create_agent_engine_with_operation_schema(
|
1847 | 1843 | class_method=method_name,
|
1848 | 1844 | )
|
1849 | 1845 | )
|
1850 |
| - assert invoked_method.__doc__ == test_doc |
1851 | 1846 |
|
1852 | 1847 | # pytest does not allow absl.testing.parameterized.named_parameters.
|
1853 | 1848 | @pytest.mark.parametrize(
|
@@ -2062,7 +2057,6 @@ def test_stream_query_after_create_agent_engine_with_operation_schema(
|
2062 | 2057 | class_method=method_name,
|
2063 | 2058 | )
|
2064 | 2059 | )
|
2065 |
| - assert invoked_method.__doc__ == test_doc |
2066 | 2060 |
|
2067 | 2061 | # pytest does not allow absl.testing.parameterized.named_parameters.
|
2068 | 2062 | @pytest.mark.parametrize(
|
@@ -2243,7 +2237,6 @@ async def test_async_stream_query_after_create_agent_engine_with_operation_schem
|
2243 | 2237 | class_method=method_name,
|
2244 | 2238 | )
|
2245 | 2239 | )
|
2246 |
| - assert invoked_method.__doc__ == test_doc |
2247 | 2240 |
|
2248 | 2241 | # pytest does not allow absl.testing.parameterized.named_parameters.
|
2249 | 2242 | @pytest.mark.parametrize(
|
@@ -2870,7 +2863,7 @@ def test_update_class_methods_spec_with_registered_operation_not_found(self):
|
2870 | 2863 | "register the API methods: "
|
2871 | 2864 | "https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/develop/custom#custom-methods. "
|
2872 | 2865 | "Error: {Unsupported api mode: `UNKNOWN_API_MODE`, "
|
2873 |
| - "Supported modes are: ``, `async`, `stream` and `async_stream`.}" |
| 2866 | + "Supported modes are: ``, `async`, `async_stream`, `stream`.}" |
2874 | 2867 | ),
|
2875 | 2868 | ),
|
2876 | 2869 | ],
|
@@ -2987,161 +2980,169 @@ def assert_called_with_diff(mock_obj, expected_kwargs=None):
|
2987 | 2980 | )
|
2988 | 2981 |
|
2989 | 2982 |
|
2990 |
| -class TestGenerateSchema(parameterized.TestCase): |
2991 |
| - @parameterized.named_parameters( |
2992 |
| - dict( |
2993 |
| - testcase_name="place_tool_query", |
2994 |
| - func=place_tool_query, |
2995 |
| - required=["city", "activity"], |
2996 |
| - expected_operation={ |
2997 |
| - "name": "place_tool_query", |
2998 |
| - "description": ( |
2999 |
| - "Searches the city for recommendations on the activity." |
3000 |
| - ), |
3001 |
| - "parameters": { |
3002 |
| - "type": "object", |
3003 |
| - "properties": { |
3004 |
| - "city": {"type": "string"}, |
3005 |
| - "activity": {"type": "string", "nullable": True}, |
3006 |
| - "page_size": {"type": "integer"}, |
| 2983 | +class TestGenerateSchema: |
| 2984 | + # pytest does not allow absl.testing.parameterized.named_parameters. |
| 2985 | + @pytest.mark.parametrize( |
| 2986 | + "func, required, expected_operation", |
| 2987 | + [ |
| 2988 | + ( |
| 2989 | + # "place_tool_query", |
| 2990 | + place_tool_query, |
| 2991 | + ["city", "activity"], |
| 2992 | + { |
| 2993 | + "name": "place_tool_query", |
| 2994 | + "description": ( |
| 2995 | + "Searches the city for recommendations on the activity." |
| 2996 | + ), |
| 2997 | + "parameters": { |
| 2998 | + "type": "object", |
| 2999 | + "properties": { |
| 3000 | + "city": {"type": "string"}, |
| 3001 | + "activity": {"type": "string", "nullable": True}, |
| 3002 | + "page_size": {"type": "integer"}, |
| 3003 | + }, |
| 3004 | + "required": ["city", "activity"], |
3007 | 3005 | },
|
3008 |
| - "required": ["city", "activity"], |
3009 | 3006 | },
|
3010 |
| - }, |
3011 |
| - ), |
3012 |
| - dict( |
3013 |
| - testcase_name="place_photo_query", |
3014 |
| - func=place_photo_query, |
3015 |
| - required=["photo_reference"], |
3016 |
| - expected_operation={ |
3017 |
| - "name": "place_photo_query", |
3018 |
| - "description": "Returns the photo for a given reference.", |
3019 |
| - "parameters": { |
3020 |
| - "properties": { |
3021 |
| - "photo_reference": {"type": "string"}, |
3022 |
| - "maxwidth": {"type": "integer"}, |
3023 |
| - "maxheight": {"type": "integer", "nullable": True}, |
| 3007 | + ), |
| 3008 | + ( |
| 3009 | + # "place_photo_query", |
| 3010 | + place_photo_query, |
| 3011 | + ["photo_reference"], |
| 3012 | + { |
| 3013 | + "name": "place_photo_query", |
| 3014 | + "description": "Returns the photo for a given reference.", |
| 3015 | + "parameters": { |
| 3016 | + "type": "object", |
| 3017 | + "properties": { |
| 3018 | + "photo_reference": {"type": "string"}, |
| 3019 | + "maxwidth": {"type": "integer"}, |
| 3020 | + "maxheight": {"type": "integer", "nullable": True}, |
| 3021 | + }, |
| 3022 | + "required": ["photo_reference"], |
3024 | 3023 | },
|
3025 |
| - "required": ["photo_reference"], |
3026 |
| - "type": "object", |
3027 | 3024 | },
|
3028 |
| - }, |
3029 |
| - ), |
| 3025 | + ), |
| 3026 | + ], |
3030 | 3027 | )
|
3031 | 3028 | def test_generate_schemas(self, func, required, expected_operation):
|
3032 | 3029 | result = _utils.generate_schema(func, required=required)
|
3033 |
| - self.assertDictEqual(result, expected_operation) |
| 3030 | + assert result == expected_operation |
3034 | 3031 |
|
3035 | 3032 |
|
3036 |
| -class TestToProto(parameterized.TestCase): |
3037 |
| - @parameterized.named_parameters( |
3038 |
| - dict( |
3039 |
| - testcase_name="empty_dict", |
3040 |
| - obj={}, |
3041 |
| - expected_proto=struct_pb2.Struct(fields={}), |
3042 |
| - ), |
3043 |
| - dict( |
3044 |
| - testcase_name="nonempty_dict", |
3045 |
| - obj={"snake_case": 1, "camelCase": 2}, |
3046 |
| - expected_proto=struct_pb2.Struct( |
3047 |
| - fields={ |
3048 |
| - "snake_case": struct_pb2.Value(number_value=1), |
3049 |
| - "camelCase": struct_pb2.Value(number_value=2), |
3050 |
| - }, |
| 3033 | +class TestToProto: |
| 3034 | + # pytest does not allow absl.testing.parameterized.named_parameters. |
| 3035 | + @pytest.mark.parametrize( |
| 3036 | + "obj, expected_proto", |
| 3037 | + [ |
| 3038 | + ( |
| 3039 | + # "empty_dict", |
| 3040 | + {}, |
| 3041 | + struct_pb2.Struct(fields={}), |
3051 | 3042 | ),
|
3052 |
| - ), |
3053 |
| - dict( |
3054 |
| - testcase_name="empty_proto_message", |
3055 |
| - obj=struct_pb2.Struct(fields={}), |
3056 |
| - expected_proto=struct_pb2.Struct(fields={}), |
3057 |
| - ), |
3058 |
| - dict( |
3059 |
| - testcase_name="nonempty_proto_message", |
3060 |
| - obj=struct_pb2.Struct( |
3061 |
| - fields={ |
3062 |
| - "snake_case": struct_pb2.Value(number_value=1), |
3063 |
| - "camelCase": struct_pb2.Value(number_value=2), |
3064 |
| - }, |
| 3043 | + ( |
| 3044 | + # "nonempty_dict", |
| 3045 | + {"snake_case": 1, "camelCase": 2}, |
| 3046 | + struct_pb2.Struct( |
| 3047 | + fields={ |
| 3048 | + "snake_case": struct_pb2.Value(number_value=1), |
| 3049 | + "camelCase": struct_pb2.Value(number_value=2), |
| 3050 | + }, |
| 3051 | + ), |
3065 | 3052 | ),
|
3066 |
| - expected_proto=struct_pb2.Struct( |
3067 |
| - fields={ |
3068 |
| - "snake_case": struct_pb2.Value(number_value=1), |
3069 |
| - "camelCase": struct_pb2.Value(number_value=2), |
3070 |
| - }, |
| 3053 | + ( |
| 3054 | + # "empty_proto_message", |
| 3055 | + struct_pb2.Struct(fields={}), |
| 3056 | + struct_pb2.Struct(fields={}), |
3071 | 3057 | ),
|
3072 |
| - ), |
| 3058 | + ( |
| 3059 | + # "nonempty_proto_message", |
| 3060 | + struct_pb2.Struct( |
| 3061 | + fields={ |
| 3062 | + "snake_case": struct_pb2.Value(number_value=1), |
| 3063 | + "camelCase": struct_pb2.Value(number_value=2), |
| 3064 | + }, |
| 3065 | + ), |
| 3066 | + struct_pb2.Struct( |
| 3067 | + fields={ |
| 3068 | + "snake_case": struct_pb2.Value(number_value=1), |
| 3069 | + "camelCase": struct_pb2.Value(number_value=2), |
| 3070 | + }, |
| 3071 | + ), |
| 3072 | + ), |
| 3073 | + ], |
3073 | 3074 | )
|
3074 | 3075 | def test_to_proto(self, obj, expected_proto):
|
3075 | 3076 | result = _utils.to_proto(obj)
|
3076 |
| - self.assertDictEqual(_utils.to_dict(result), _utils.to_dict(expected_proto)) |
3077 |
| - # converting a new object to proto should not modify earlier objects. |
3078 |
| - new_result = _utils.to_proto({}) |
3079 |
| - self.assertDictEqual(_utils.to_dict(result), _utils.to_dict(expected_proto)) |
3080 |
| - self.assertEmpty(new_result) |
| 3077 | + assert _utils.to_dict(result) == _utils.to_dict(expected_proto) |
3081 | 3078 |
|
3082 | 3079 |
|
3083 |
| -class ToParsedJsonTest(parameterized.TestCase): |
3084 |
| - @parameterized.named_parameters( |
3085 |
| - dict( |
3086 |
| - testcase_name="valid_json", |
3087 |
| - obj=httpbody_pb2.HttpBody( |
3088 |
| - content_type="application/json", data=b'{"a": 1, "b": "hello"}' |
| 3080 | +class ToParsedJsonTest: |
| 3081 | + # pytest does not allow absl.testing.parameterized.named_parameters. |
| 3082 | + @pytest.mark.parametrize( |
| 3083 | + "obj, expected", |
| 3084 | + [ |
| 3085 | + ( |
| 3086 | + # "valid_json", |
| 3087 | + httpbody_pb2.HttpBody( |
| 3088 | + content_type="application/json", data=b'{"a": 1, "b": "hello"}' |
| 3089 | + ), |
| 3090 | + [{"a": 1, "b": "hello"}], |
3089 | 3091 | ),
|
3090 |
| - expected=[{"a": 1, "b": "hello"}], |
3091 |
| - ), |
3092 |
| - dict( |
3093 |
| - testcase_name="invalid_json", |
3094 |
| - obj=httpbody_pb2.HttpBody( |
3095 |
| - content_type="application/json", data=b'{"a": 1, "b": "hello"' |
| 3092 | + ( |
| 3093 | + # "invalid_json", |
| 3094 | + httpbody_pb2.HttpBody( |
| 3095 | + content_type="application/json", data=b'{"a": 1, "b": "hello"' |
| 3096 | + ), |
| 3097 | + ['{"a": 1, "b": "hello"'], # returns the unparsed string |
3096 | 3098 | ),
|
3097 |
| - expected=['{"a": 1, "b": "hello"'], # returns the unparsed string |
3098 |
| - ), |
3099 |
| - dict( |
3100 |
| - testcase_name="missing_content_type", |
3101 |
| - obj=httpbody_pb2.HttpBody(data=b'{"a": 1}'), |
3102 |
| - expected=[httpbody_pb2.HttpBody(data=b'{"a": 1}')], |
3103 |
| - ), |
3104 |
| - dict( |
3105 |
| - testcase_name="missing_data", |
3106 |
| - obj=httpbody_pb2.HttpBody(content_type="application/json"), |
3107 |
| - expected=[None], |
3108 |
| - ), |
3109 |
| - dict( |
3110 |
| - testcase_name="wrong_content_type", |
3111 |
| - obj=httpbody_pb2.HttpBody(content_type="text/plain", data=b"hello"), |
3112 |
| - expected=[httpbody_pb2.HttpBody(content_type="text/plain", data=b"hello")], |
3113 |
| - ), |
3114 |
| - dict( |
3115 |
| - testcase_name="empty_data", |
3116 |
| - obj=httpbody_pb2.HttpBody(content_type="application/json", data=b""), |
3117 |
| - expected=[None], |
3118 |
| - ), |
3119 |
| - dict( |
3120 |
| - testcase_name="unicode_data", |
3121 |
| - obj=httpbody_pb2.HttpBody( |
3122 |
| - content_type="application/json", data='{"a": "你好"}'.encode("utf-8") |
| 3099 | + ( |
| 3100 | + # "missing_content_type", |
| 3101 | + httpbody_pb2.HttpBody(data=b'{"a": 1}'), |
| 3102 | + [httpbody_pb2.HttpBody(data=b'{"a": 1}')], |
3123 | 3103 | ),
|
3124 |
| - expected=[{"a": "你好"}], |
3125 |
| - ), |
3126 |
| - dict( |
3127 |
| - testcase_name="nested_json", |
3128 |
| - obj=httpbody_pb2.HttpBody( |
3129 |
| - content_type="application/json", data=b'{"a": {"b": 1}}' |
| 3104 | + ( |
| 3105 | + # "missing_data", |
| 3106 | + httpbody_pb2.HttpBody(content_type="application/json"), |
| 3107 | + [None], |
3130 | 3108 | ),
|
3131 |
| - expected=[{"a": {"b": 1}}], |
3132 |
| - ), |
3133 |
| - dict( |
3134 |
| - testcase_name="multiline_json", |
3135 |
| - obj=httpbody_pb2.HttpBody( |
3136 |
| - content_type="application/json", |
3137 |
| - data=b'{"a": {"b": 1}}\n{"a": {"b": 2}}', |
| 3109 | + ( |
| 3110 | + # "wrong_content_type", |
| 3111 | + httpbody_pb2.HttpBody(content_type="text/plain", data=b"hello"), |
| 3112 | + [httpbody_pb2.HttpBody(content_type="text/plain", data=b"hello")], |
3138 | 3113 | ),
|
3139 |
| - expected=[{"a": {"b": 1}}, {"a": {"b": 2}}], |
3140 |
| - ), |
| 3114 | + ( |
| 3115 | + # "empty_data", |
| 3116 | + httpbody_pb2.HttpBody(content_type="application/json", data=b""), |
| 3117 | + [None], |
| 3118 | + ), |
| 3119 | + ( |
| 3120 | + # "unicode_data", |
| 3121 | + httpbody_pb2.HttpBody( |
| 3122 | + content_type="application/json", data='{"a": "你好"}'.encode("utf-8") |
| 3123 | + ), |
| 3124 | + [{"a": "你好"}], |
| 3125 | + ), |
| 3126 | + ( |
| 3127 | + # "nested_json", |
| 3128 | + httpbody_pb2.HttpBody( |
| 3129 | + content_type="application/json", data=b'{"a": {"b": 1}}' |
| 3130 | + ), |
| 3131 | + [{"a": {"b": 1}}], |
| 3132 | + ), |
| 3133 | + ( |
| 3134 | + # "multiline_json", |
| 3135 | + httpbody_pb2.HttpBody( |
| 3136 | + content_type="application/json", |
| 3137 | + data=b'{"a": {"b": 1}}\n{"a": {"b": 2}}', |
| 3138 | + ), |
| 3139 | + [{"a": {"b": 1}}, {"a": {"b": 2}}], |
| 3140 | + ), |
| 3141 | + ], |
3141 | 3142 | )
|
3142 | 3143 | def test_to_parsed_json(self, obj, expected):
|
3143 | 3144 | for got, want in zip(_utils.yield_parsed_json(obj), expected):
|
3144 |
| - self.assertEqual(got, want) |
| 3145 | + assert got == want |
3145 | 3146 |
|
3146 | 3147 |
|
3147 | 3148 | class TestRequirements:
|
|
0 commit comments