Skip to content

Commit 8090f31

Browse files
yyyu-googlecopybara-github
authored andcommitted
test: first sdk schema test across Vertex AI and Google AI SDKs
PiperOrigin-RevId: 660572984
1 parent 19c4084 commit 8090f31

File tree

8 files changed

+191
-0
lines changed

8 files changed

+191
-0
lines changed

sdk_schema_tests/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
expected_generate_content_common_arg_keys = (
17+
"self",
18+
"contents",
19+
"generation_config",
20+
"safety_settings",
21+
"tools",
22+
"tool_config",
23+
"stream",
24+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
expected_generate_content_return_annotation = "generation_types.GenerateContentResponse"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
from inspect import signature
17+
import unittest
18+
19+
from google.generativeai import GenerativeModel
20+
from sdk_schema_tests import common_contract
21+
from sdk_schema_tests import google_ai_only_contract as specific_contract
22+
23+
_SDK_NAME = "GoogleAI"
24+
25+
26+
class TestGenerativeModelMethodSignatures(unittest.TestCase):
27+
def test_generate_content_signature(self):
28+
generate_content_signature = signature(GenerativeModel.generate_content)
29+
actual_method_arg_keys = generate_content_signature.parameters.keys()
30+
actual_return_annotation = generate_content_signature.return_annotation
31+
32+
for expected_key in common_contract.expected_generate_content_common_arg_keys:
33+
self.assertIn(
34+
member=expected_key,
35+
container=actual_method_arg_keys,
36+
msg=(
37+
f"[{_SDK_NAME}]: expected common key {expected_key} not found in"
38+
f" actual keys: {actual_method_arg_keys}"
39+
),
40+
)
41+
42+
self.assertEqual(
43+
actual_return_annotation,
44+
specific_contract.expected_generate_content_return_annotation,
45+
msg=(
46+
f"[{_SDK_NAME}]: expected return annotation"
47+
f" {specific_contract.expected_generate_content_return_annotation}"
48+
f" not equal to actual return annotation {actual_return_annotation}"
49+
),
50+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
from typing import ForwardRef, Iterable, Union
17+
18+
expected_generate_content_return_annotation = Union[
19+
ForwardRef("GenerationResponse"), # noqa: F821
20+
Iterable[ForwardRef("GenerationResponse")], # noqa: F821
21+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Tests for method_signature."""
2+
3+
import inspect
4+
import unittest
5+
6+
from vertexai.generative_models import GenerativeModel
7+
8+
from sdk_schema_tests import common_contract
9+
from sdk_schema_tests import vertex_ai_only_contract as specific_contract
10+
11+
_SDK_NAME = "VertexAI"
12+
13+
14+
class TestGenerativeModelMethodSignatures(unittest.TestCase):
15+
def test_generate_content_signature(self):
16+
generate_content_signature = inspect.signature(GenerativeModel.generate_content)
17+
actual_method_arg_keys = generate_content_signature.parameters.keys()
18+
actual_return_annotation = generate_content_signature.return_annotation
19+
20+
for expected_key in common_contract.expected_generate_content_common_arg_keys:
21+
self.assertIn(
22+
member=expected_key,
23+
container=actual_method_arg_keys,
24+
msg=(
25+
f"[{_SDK_NAME}]: expected common key {expected_key} not found in"
26+
f" actual keys: {actual_method_arg_keys}"
27+
),
28+
)
29+
30+
self.assertEqual(
31+
actual_return_annotation,
32+
specific_contract.expected_generate_content_return_annotation,
33+
msg=(
34+
f"[{_SDK_NAME}]: expected return annotation"
35+
f" {specific_contract.expected_generate_content_return_annotation}"
36+
f" not equal to actual return annotation {actual_return_annotation}"
37+
),
38+
)

0 commit comments

Comments
 (0)