|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2025 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +"""Tests for file state handling in content generation.""" |
| 18 | + |
| 19 | +import unittest |
| 20 | +from unittest import mock |
| 21 | +import time |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +from google.genai import types |
| 26 | +from google.genai import errors |
| 27 | +from google.genai.models import _ensure_file_active, _process_contents_for_generation |
| 28 | +from google.genai.types import FileState |
| 29 | + |
| 30 | + |
| 31 | +class TestFileStateHandling(unittest.TestCase): |
| 32 | + """Test file state handling functionality.""" |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + """Set up test fixtures.""" |
| 36 | + self.api_client = mock.MagicMock() |
| 37 | + self.file_obj = types.File( |
| 38 | + name="files/test123", |
| 39 | + display_name="Test File", |
| 40 | + mime_type="video/mp4", |
| 41 | + uri="https://example.com/files/test123", |
| 42 | + state=types.FileState.PROCESSING, |
| 43 | + ) |
| 44 | + |
| 45 | + @mock.patch("google.genai.files._File_from_mldev") |
| 46 | + def test_ensure_file_active_with_processing_file(self, mock_file_from_mldev): |
| 47 | + """Test that _ensure_file_active properly handles a file in PROCESSING state.""" |
| 48 | + |
| 49 | + response_mock = mock.MagicMock() |
| 50 | + response_mock.json = { |
| 51 | + "file": { |
| 52 | + "name": "files/test123", |
| 53 | + "displayName": "Test File", |
| 54 | + "mimeType": "video/mp4", |
| 55 | + "state": "ACTIVE", |
| 56 | + } |
| 57 | + } |
| 58 | + self.api_client.call_api.return_value = response_mock |
| 59 | + |
| 60 | + # Set up the mock to return a dict that will create an ACTIVE file |
| 61 | + mock_file_from_mldev.return_value = { |
| 62 | + "name": "files/test123", |
| 63 | + "display_name": "Test File", |
| 64 | + "mime_type": "video/mp4", |
| 65 | + "state": types.FileState.ACTIVE, |
| 66 | + } |
| 67 | + |
| 68 | + result = _ensure_file_active( |
| 69 | + self.api_client, self.file_obj, max_retries=1, retry_delay_seconds=0.1 |
| 70 | + ) |
| 71 | + |
| 72 | + self.api_client.call_api.assert_called_once_with( |
| 73 | + method="GET", |
| 74 | + url="files/test123", |
| 75 | + api_client_type="mldev", |
| 76 | + ) |
| 77 | + |
| 78 | + # Verify the result has ACTIVE state |
| 79 | + self.assertEqual(result.state, types.FileState.ACTIVE) |
| 80 | + |
| 81 | + def test_ensure_file_active_with_failed_file(self): |
| 82 | + """Test that _ensure_file_active properly handles a file in FAILED state.""" |
| 83 | + |
| 84 | + response_mock = mock.MagicMock() |
| 85 | + response_mock.json = { |
| 86 | + "file": { |
| 87 | + "name": "files/test123", |
| 88 | + "displayName": "Test File", |
| 89 | + "mimeType": "video/mp4", |
| 90 | + "state": "FAILED", |
| 91 | + "error": {"message": "File processing failed"}, |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + # Set up a side effect for call_api that returns the response with FAILED state |
| 96 | + def mock_call_api(**kwargs): |
| 97 | + # Only return the mock for the expected file API call |
| 98 | + if kwargs.get("method") == "GET" and "files/" in kwargs.get("url", ""): |
| 99 | + return response_mock |
| 100 | + return mock.DEFAULT |
| 101 | + |
| 102 | + self.api_client.call_api.side_effect = mock_call_api |
| 103 | + |
| 104 | + |
| 105 | + with pytest.raises(errors.FileProcessingError) as excinfo: |
| 106 | + _ensure_file_active( |
| 107 | + self.api_client, self.file_obj, max_retries=1, retry_delay_seconds=0.1 |
| 108 | + ) |
| 109 | + |
| 110 | + assert "File processing failed" in str(excinfo.value) |
| 111 | + |
| 112 | + def test_ensure_file_active_with_retries_exhausted(self): |
| 113 | + """Test that _ensure_file_active handles a file that remains in PROCESSING state after all retries.""" |
| 114 | + # Mock the response for file state check - file stays in PROCESSING |
| 115 | + response_mock = mock.MagicMock() |
| 116 | + response_mock.json = { |
| 117 | + "file": { |
| 118 | + "name": "files/test123", |
| 119 | + "displayName": "Test File", |
| 120 | + "mimeType": "video/mp4", |
| 121 | + "state": "PROCESSING", |
| 122 | + } |
| 123 | + } |
| 124 | + self.api_client.call_api.return_value = response_mock |
| 125 | + |
| 126 | + # Call the function |
| 127 | + result = _ensure_file_active( |
| 128 | + self.api_client, self.file_obj, max_retries=2, retry_delay_seconds=0.1 |
| 129 | + ) |
| 130 | + |
| 131 | + # Verify the file state was checked multiple times |
| 132 | + self.assertEqual(self.api_client.call_api.call_count, 2) |
| 133 | + |
| 134 | + # Verify the original file is returned |
| 135 | + self.assertEqual(result, self.file_obj) |
| 136 | + self.assertEqual(result.state, types.FileState.PROCESSING) |
| 137 | + |
| 138 | + def test_ensure_file_active_with_already_active_file(self): |
| 139 | + """Test that _ensure_file_active returns immediately for an already ACTIVE file.""" |
| 140 | + active_file = types.File( |
| 141 | + name="files/active123", |
| 142 | + display_name="Active File", |
| 143 | + mime_type="video/mp4", |
| 144 | + state=types.FileState.ACTIVE, |
| 145 | + ) |
| 146 | + |
| 147 | + result = _ensure_file_active( |
| 148 | + self.api_client, active_file, max_retries=1, retry_delay_seconds=0.1 |
| 149 | + ) |
| 150 | + |
| 151 | + # Verify no API calls were made |
| 152 | + self.api_client.call_api.assert_not_called() |
| 153 | + |
| 154 | + # Verify the original file is returned unchanged |
| 155 | + self.assertEqual(result, active_file) |
| 156 | + self.assertEqual(result.state, types.FileState.ACTIVE) |
| 157 | + |
| 158 | + |
| 159 | +class TestProcessContentsFunction(unittest.TestCase): |
| 160 | + """Test the _process_contents_for_generation function.""" |
| 161 | + |
| 162 | + def setUp(self): |
| 163 | + """Set up test fixtures.""" |
| 164 | + self.api_client = mock.MagicMock() |
| 165 | + self.processing_file = types.File( |
| 166 | + name="files/processing123", |
| 167 | + display_name="Processing File", |
| 168 | + mime_type="video/mp4", |
| 169 | + uri="https://example.com/files/processing123", |
| 170 | + state=types.FileState.PROCESSING |
| 171 | + ) |
| 172 | + self.active_file = types.File( |
| 173 | + name="files/active123", |
| 174 | + display_name="Active File", |
| 175 | + mime_type="video/mp4", |
| 176 | + uri="https://example.com/files/active123", |
| 177 | + state=types.FileState.ACTIVE |
| 178 | + ) |
| 179 | + |
| 180 | + def test_process_contents_with_files(self): |
| 181 | + """Test that _process_contents_for_generation can handle various file scenarios.""" |
| 182 | + # Scenarios: |
| 183 | + # - File directly in content list |
| 184 | + # - File in content parts |
| 185 | + # - Multiple files in different parts |
| 186 | + |
| 187 | + # Test data |
| 188 | + file_in_list = [self.processing_file, "Process this file"] |
| 189 | + |
| 190 | + file_in_parts = types.Content( |
| 191 | + role="user", |
| 192 | + parts=[types.Part(text="Here's a video:"), self.processing_file] |
| 193 | + ) |
| 194 | + |
| 195 | + multiple_files = [ |
| 196 | + types.Content( |
| 197 | + role="user", |
| 198 | + parts=[types.Part(text="First video:"), self.processing_file] |
| 199 | + ), |
| 200 | + types.Content( |
| 201 | + role="user", |
| 202 | + parts=[types.Part(text="Second video:"), self.active_file] |
| 203 | + ) |
| 204 | + ] |
| 205 | + |
| 206 | + # Mock _ensure_file_active to return the file unchanged |
| 207 | + # This allows us to test the function without changing file states |
| 208 | + with mock.patch("google.genai.models._ensure_file_active", |
| 209 | + side_effect=lambda client, file: file): |
| 210 | + |
| 211 | + # Test all three cases |
| 212 | + for test_content in [file_in_list, file_in_parts, multiple_files]: |
| 213 | + with mock.patch("google.genai.models.t.t_contents", |
| 214 | + return_value=test_content if isinstance(test_content, list) else [test_content]): |
| 215 | + # Just verify it runs without errors |
| 216 | + result = _process_contents_for_generation(self.api_client, test_content) |
| 217 | + # Basic assertion that we got something back |
| 218 | + self.assertTrue(result) |
| 219 | + |
| 220 | + |
| 221 | +if __name__ == "__main__": |
| 222 | + unittest.main() |
0 commit comments