Skip to content

Commit 69ad351

Browse files
Add tests
1 parent d087b13 commit 69ad351

File tree

2 files changed

+127
-9
lines changed

2 files changed

+127
-9
lines changed

fishjam/integrations/gemini.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"Install it with `pip install 'fishjam-server-sdk[gemini]'`"
1010
)
1111

12-
from functools import singledispatch
1312
from typing import Optional, Union
1413

1514
from fishjam import AgentOutputOptions
@@ -20,26 +19,29 @@ def _get_headers():
2019
return {"x-goog-api-client": f"fishjam-python-server-sdk/{get_version()}"}
2120

2221

23-
@singledispatch
2422
def _add_fishjam_header(
2523
http_options: Optional[Union[types.HttpOptions, types.HttpOptionsDict]],
26-
) -> Union[types.HttpOptions, types.HttpOptionsDict]: ...
24+
) -> Union[types.HttpOptions, types.HttpOptionsDict]:
25+
if http_options is None:
26+
return _add_fishjam_header_none()
27+
if isinstance(http_options, types.HttpOptions):
28+
return _add_fishjam_header_object(http_options)
29+
return _add_fishjam_header_dict(http_options)
2730

2831

29-
@_add_fishjam_header.register
30-
def _(http_options: types.HttpOptions) -> types.HttpOptions:
32+
def _add_fishjam_header_object(http_options: types.HttpOptions) -> types.HttpOptions:
3133
http_options.headers = (http_options.headers or {}) | _get_headers()
3234
return http_options
3335

3436

35-
@_add_fishjam_header.register
36-
def _(http_options: types.HttpOptionsDict) -> types.HttpOptionsDict:
37+
def _add_fishjam_header_dict(
38+
http_options: types.HttpOptionsDict,
39+
) -> types.HttpOptionsDict:
3740
headers = (http_options.get("headers") or {}) | _get_headers()
3841
return http_options | {"headers": headers}
3942

4043

41-
@_add_fishjam_header.register
42-
def _(_http_options: None) -> types.HttpOptionsDict:
44+
def _add_fishjam_header_none() -> types.HttpOptionsDict:
4345
return {"headers": _get_headers()}
4446

4547

tests/test_gemini.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
from unittest.mock import MagicMock, patch
2+
3+
import pytest
4+
from google.genai import types
5+
6+
from fishjam.integrations.gemini import GeminiIntegration
7+
from fishjam.version import get_version
8+
9+
10+
@pytest.fixture
11+
def version():
12+
return get_version()
13+
14+
15+
@patch("google.genai.Client")
16+
def test_create_client_passes_all_args(mock_client_cls: MagicMock, version: str):
17+
dummy_credentials = MagicMock()
18+
dummy_debug_config = MagicMock()
19+
20+
GeminiIntegration.create_client(
21+
vertexai=True,
22+
api_key="test-key",
23+
credentials=dummy_credentials,
24+
project="my-project",
25+
location="us-central1",
26+
debug_config=dummy_debug_config,
27+
)
28+
29+
mock_client_cls.assert_called_once()
30+
31+
kwargs = mock_client_cls.call_args.kwargs
32+
33+
assert kwargs["vertexai"] is True
34+
assert kwargs["api_key"] == "test-key"
35+
assert kwargs["credentials"] is dummy_credentials
36+
assert kwargs["project"] == "my-project"
37+
assert kwargs["location"] == "us-central1"
38+
assert kwargs["debug_config"] is dummy_debug_config
39+
40+
assert kwargs["http_options"] == {
41+
"headers": {"x-goog-api-client": f"fishjam-python-server-sdk/{version}"}
42+
}
43+
44+
45+
@patch("google.genai.Client")
46+
def test_create_client_with_dict_options_no_headers(
47+
mock_client_cls: MagicMock, version: str
48+
):
49+
GeminiIntegration.create_client(http_options={"timeout": 30})
50+
51+
mock_client_cls.assert_called_once()
52+
53+
assert mock_client_cls.call_args.kwargs["http_options"] == {
54+
"timeout": 30,
55+
"headers": {"x-goog-api-client": f"fishjam-python-server-sdk/{version}"},
56+
}
57+
58+
59+
@patch("google.genai.Client")
60+
def test_create_client_with_dict_options_existing_headers(
61+
mock_client_cls: MagicMock, version: str
62+
):
63+
GeminiIntegration.create_client(
64+
http_options={
65+
"headers": {
66+
"existing-header": "value",
67+
"x-goog-api-client": "other",
68+
}
69+
}
70+
)
71+
72+
mock_client_cls.assert_called_once()
73+
74+
assert mock_client_cls.call_args.kwargs["http_options"] == {
75+
"headers": {
76+
"existing-header": "value",
77+
"x-goog-api-client": f"fishjam-python-server-sdk/{version}",
78+
},
79+
}
80+
81+
82+
@patch("google.genai.Client")
83+
def test_create_client_with_object_options(mock_client_cls: MagicMock, version: str):
84+
http_options = types.HttpOptions()
85+
86+
GeminiIntegration.create_client(http_options=http_options)
87+
88+
mock_client_cls.assert_called_once()
89+
90+
# Verify the object passed has the correct headers set
91+
actual_options = mock_client_cls.call_args.kwargs["http_options"]
92+
assert actual_options.headers == {
93+
"x-goog-api-client": f"fishjam-python-server-sdk/{version}"
94+
}
95+
96+
97+
@patch("google.genai.Client")
98+
def test_create_client_with_object_options_existing_headers(
99+
mock_client_cls: MagicMock, version: str
100+
):
101+
http_options = types.HttpOptions(
102+
headers={
103+
"user-header": "123",
104+
"x-goog-api-client": "other",
105+
}
106+
)
107+
108+
GeminiIntegration.create_client(http_options=http_options)
109+
110+
mock_client_cls.assert_called_once()
111+
112+
actual_options = mock_client_cls.call_args.kwargs["http_options"]
113+
assert actual_options.headers == {
114+
"user-header": "123",
115+
"x-goog-api-client": f"fishjam-python-server-sdk/{version}",
116+
}

0 commit comments

Comments
 (0)