Skip to content

Commit dd80b69

Browse files
Ark-kuncopybara-github
authored andcommitted
chore: GenAI - De-duplicated unit test mocks
PiperOrigin-RevId: 607139237
1 parent e7828d9 commit dd80b69

File tree

1 file changed

+5
-43
lines changed

1 file changed

+5
-43
lines changed

tests/unit/vertexai/test_generative_models.py

+5-43
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
}
114114

115115

116-
def mock_stream_generate_content(
116+
def mock_generate_content(
117117
self,
118118
request: gapic_prediction_service_types.GenerateContentRequest,
119119
*,
@@ -161,56 +161,18 @@ def mock_stream_generate_content(
161161
),
162162
],
163163
)
164-
yield response
164+
return response
165165

166166

167-
def mock_generate_content(
167+
def mock_stream_generate_content(
168168
self,
169169
request: gapic_prediction_service_types.GenerateContentRequest,
170170
*,
171171
model: Optional[str] = None,
172172
contents: Optional[MutableSequence[gapic_content_types.Content]] = None,
173173
) -> Iterable[gapic_prediction_service_types.GenerateContentResponse]:
174-
is_continued_chat = len(request.contents) > 1
175-
has_tools = bool(request.tools)
176-
177-
if has_tools:
178-
has_function_response = any(
179-
"function_response" in content.parts[0] for content in request.contents
180-
)
181-
needs_function_call = not has_function_response
182-
if needs_function_call:
183-
response_part_struct = _RESPONSE_FUNCTION_CALL_PART_STRUCT
184-
else:
185-
response_part_struct = _RESPONSE_AFTER_FUNCTION_CALL_PART_STRUCT
186-
elif is_continued_chat:
187-
response_part_struct = {"text": "Other planets may have different sky color."}
188-
else:
189-
response_part_struct = _RESPONSE_TEXT_PART_STRUCT
190-
191-
return gapic_prediction_service_types.GenerateContentResponse(
192-
candidates=[
193-
gapic_content_types.Candidate(
194-
index=0,
195-
content=gapic_content_types.Content(
196-
# Model currently does not identify itself
197-
# role="model",
198-
parts=[
199-
gapic_content_types.Part(response_part_struct),
200-
],
201-
),
202-
finish_reason=gapic_content_types.Candidate.FinishReason.STOP,
203-
safety_ratings=[
204-
gapic_content_types.SafetyRating(rating)
205-
for rating in _RESPONSE_SAFETY_RATINGS_STRUCT
206-
],
207-
citation_metadata=gapic_content_types.CitationMetadata(
208-
citations=[
209-
gapic_content_types.Citation(_RESPONSE_CITATION_STRUCT),
210-
]
211-
),
212-
),
213-
],
174+
yield mock_generate_content(
175+
self=self, request=request, model=model, contents=contents
214176
)
215177

216178

0 commit comments

Comments
 (0)