|
| 1 | +"""Tests to verify both LiveClient classes and list[pydantic.BaseModel] support.""" |
| 2 | + |
| 3 | +import inspect |
| 4 | +from typing import List, Optional |
| 5 | + |
| 6 | +import pytest |
| 7 | +from pydantic import BaseModel, Field |
| 8 | + |
| 9 | +from google import genai |
| 10 | +from google.genai import types |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def client(): |
| 15 | + """Return a client that uses the replay_session.""" |
| 16 | + client = genai.Client(api_key="test-api-key") |
| 17 | + return client |
| 18 | + |
| 19 | + |
| 20 | +def test_live_client_classes_exist(): |
| 21 | + """Verify that LiveClient classes exist and have expected attributes.""" |
| 22 | + # Check that LiveClientMessage exists |
| 23 | + assert hasattr(types, "LiveClientMessage") |
| 24 | + assert inspect.isclass(types.LiveClientMessage) |
| 25 | + |
| 26 | + # Check that LiveClientContent exists |
| 27 | + assert hasattr(types, "LiveClientContent") |
| 28 | + assert inspect.isclass(types.LiveClientContent) |
| 29 | + |
| 30 | + # Check that LiveClientRealtimeInput exists |
| 31 | + assert hasattr(types, "LiveClientRealtimeInput") |
| 32 | + assert inspect.isclass(types.LiveClientRealtimeInput) |
| 33 | + |
| 34 | + # Check that LiveClientSetup exists |
| 35 | + assert hasattr(types, "LiveClientSetup") |
| 36 | + assert inspect.isclass(types.LiveClientSetup) |
| 37 | + |
| 38 | + # Check for Dict versions |
| 39 | + assert hasattr(types, "LiveClientMessageDict") |
| 40 | + assert hasattr(types, "LiveClientContentDict") |
| 41 | + assert hasattr(types, "LiveClientRealtimeInputDict") |
| 42 | + assert hasattr(types, "LiveClientSetupDict") |
| 43 | + |
| 44 | + |
| 45 | +def test_live_client_message_fields(): |
| 46 | + """Verify that LiveClientMessage has expected fields.""" |
| 47 | + # Get the field details |
| 48 | + fields = types.LiveClientMessage.__fields__ |
| 49 | + |
| 50 | + # Check for expected fields |
| 51 | + assert "setup" in fields |
| 52 | + assert "client_content" in fields |
| 53 | + assert "realtime_input" in fields |
| 54 | + assert "tool_response" in fields |
| 55 | + |
| 56 | + |
| 57 | +def test_list_pydantic_in_generate_content_response(): |
| 58 | + """Verify that GenerateContentResponse can handle list[pydantic.BaseModel].""" |
| 59 | + |
| 60 | + class Recipe(BaseModel): |
| 61 | + recipe_name: str |
| 62 | + ingredients: List[str] |
| 63 | + |
| 64 | + # Create a test response |
| 65 | + response = types.GenerateContentResponse() |
| 66 | + |
| 67 | + # Assign a list of pydantic models |
| 68 | + recipes = [ |
| 69 | + Recipe( |
| 70 | + recipe_name="Chocolate Chip Cookies", |
| 71 | + ingredients=["Flour", "Sugar", "Chocolate"], |
| 72 | + ), |
| 73 | + Recipe( |
| 74 | + recipe_name="Oatmeal Cookies", ingredients=["Oats", "Flour", "Brown Sugar"] |
| 75 | + ), |
| 76 | + ] |
| 77 | + |
| 78 | + # This assignment would fail with mypy if the type annotation is incorrect |
| 79 | + response.parsed = recipes |
| 80 | + |
| 81 | + # Verify assignment worked properly |
| 82 | + assert response.parsed is not None |
| 83 | + assert isinstance(response.parsed, list) |
| 84 | + assert len(response.parsed) == 2 |
| 85 | + assert all(isinstance(item, Recipe) for item in response.parsed) |
| 86 | + |
| 87 | + |
| 88 | +def test_combined_functionality(client): |
| 89 | + """Test that combines verification of both LiveClient classes and list[pydantic.BaseModel] support.""" |
| 90 | + # 1. Verify LiveClient classes exist |
| 91 | + assert hasattr(types, "LiveClientMessage") |
| 92 | + assert inspect.isclass(types.LiveClientMessage) |
| 93 | + |
| 94 | + # 2. Test list[pydantic.BaseModel] support in generate_content |
| 95 | + class Recipe(BaseModel): |
| 96 | + recipe_name: str |
| 97 | + ingredients: List[str] |
| 98 | + instructions: Optional[List[str]] = None |
| 99 | + |
| 100 | + response = client.models.generate_content( |
| 101 | + model="gemini-1.5-flash", |
| 102 | + contents="List 2 simple cookie recipes.", |
| 103 | + config=types.GenerateContentConfig( |
| 104 | + response_mime_type="application/json", |
| 105 | + response_schema=list[Recipe], |
| 106 | + ), |
| 107 | + ) |
| 108 | + |
| 109 | + # Verify the parsed field contains a list of Recipe objects |
| 110 | + assert isinstance(response.parsed, list) |
| 111 | + assert len(response.parsed) > 0 |
| 112 | + assert all(isinstance(item, Recipe) for item in response.parsed) |
| 113 | + |
| 114 | + # Access a property to verify the type annotation works correctly |
| 115 | + recipe = response.parsed[0] |
| 116 | + assert isinstance(recipe.recipe_name, str) |
| 117 | + assert isinstance(recipe.ingredients, list) |
| 118 | + |
| 119 | + |
| 120 | +def test_live_connect_config_exists(): |
| 121 | + """Verify that LiveConnectConfig exists and has expected attributes.""" |
| 122 | + # Check that LiveConnectConfig exists |
| 123 | + assert hasattr(types, "LiveConnectConfig") |
| 124 | + assert inspect.isclass(types.LiveConnectConfig) |
| 125 | + |
| 126 | + # Check that LiveConnectConfigDict exists |
| 127 | + assert hasattr(types, "LiveConnectConfigDict") |
| 128 | + |
| 129 | + # Get the field details if it's a pydantic model |
| 130 | + if hasattr(types.LiveConnectConfig, "__fields__"): |
| 131 | + fields = types.LiveConnectConfig.__fields__ |
| 132 | + |
| 133 | + # Check for expected fields (these might vary based on actual implementation) |
| 134 | + assert "model" in fields |
| 135 | + |
| 136 | + |
| 137 | +def test_live_client_tool_response(): |
| 138 | + """Verify that LiveClientToolResponse exists and has expected attributes.""" |
| 139 | + # Check that LiveClientToolResponse exists |
| 140 | + assert hasattr(types, "LiveClientToolResponse") |
| 141 | + assert inspect.isclass(types.LiveClientToolResponse) |
| 142 | + |
| 143 | + # Check that LiveClientToolResponseDict exists |
| 144 | + assert hasattr(types, "LiveClientToolResponseDict") |
| 145 | + |
| 146 | + # Get the field details |
| 147 | + fields = types.LiveClientToolResponse.__fields__ |
| 148 | + |
| 149 | + # Check for expected fields (these might vary based on actual implementation) |
| 150 | + assert "function_response" in fields or "tool_outputs" in fields |
0 commit comments