|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2024 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 | +from google.generativeai.types import file_types |
| 17 | + |
| 18 | +import collections |
| 19 | +import datetime |
| 20 | +import os |
| 21 | +from typing import Iterable, Union |
| 22 | +import pathlib |
| 23 | + |
| 24 | +import google |
| 25 | +import google.ai.generativelanguage as glm |
| 26 | + |
| 27 | +import google.generativeai as genai |
| 28 | +from google.generativeai import client as client_lib |
| 29 | +from absl.testing import parameterized |
| 30 | + |
| 31 | + |
| 32 | +class FileServiceClient(client_lib.FileServiceClient): |
| 33 | + def __init__(self, test): |
| 34 | + self.test = test |
| 35 | + self.observed_requests = [] |
| 36 | + self.responses = collections.defaultdict(list) |
| 37 | + |
| 38 | + def create_file( |
| 39 | + self, |
| 40 | + path: Union[str, pathlib.Path, os.PathLike], |
| 41 | + *, |
| 42 | + mime_type: Union[str, None] = None, |
| 43 | + name: Union[str, None] = None, |
| 44 | + display_name: Union[str, None] = None, |
| 45 | + resumable: bool = True, |
| 46 | + ) -> glm.File: |
| 47 | + self.observed_requests.append( |
| 48 | + dict( |
| 49 | + path=path, |
| 50 | + mime_type=mime_type, |
| 51 | + name=name, |
| 52 | + display_name=display_name, |
| 53 | + resumable=resumable, |
| 54 | + ) |
| 55 | + ) |
| 56 | + return self.responses["create_file"].pop(0) |
| 57 | + |
| 58 | + def get_file( |
| 59 | + self, |
| 60 | + request: glm.GetFileRequest, |
| 61 | + **kwargs, |
| 62 | + ) -> glm.File: |
| 63 | + self.observed_requests.append(request) |
| 64 | + return self.responses["get_file"].pop(0) |
| 65 | + |
| 66 | + def list_files( |
| 67 | + self, |
| 68 | + request: glm.ListFilesRequest, |
| 69 | + **kwargs, |
| 70 | + ) -> Iterable[glm.File]: |
| 71 | + self.observed_requests.append(request) |
| 72 | + for f in self.responses["list_files"].pop(0): |
| 73 | + yield f |
| 74 | + |
| 75 | + def delete_file( |
| 76 | + self, |
| 77 | + request: glm.DeleteFileRequest, |
| 78 | + **kwargs, |
| 79 | + ): |
| 80 | + self.observed_requests.append(request) |
| 81 | + return |
| 82 | + |
| 83 | + |
| 84 | +class UnitTests(parameterized.TestCase): |
| 85 | + def setUp(self): |
| 86 | + self.client = FileServiceClient(self) |
| 87 | + |
| 88 | + client_lib._client_manager.clients["file"] = self.client |
| 89 | + |
| 90 | + @property |
| 91 | + def observed_requests(self): |
| 92 | + return self.client.observed_requests |
| 93 | + |
| 94 | + @property |
| 95 | + def responses(self): |
| 96 | + return self.client.responses |
| 97 | + |
| 98 | + def test_video_metadata(self): |
| 99 | + self.responses["create_file"].append( |
| 100 | + glm.File( |
| 101 | + uri="https://test", |
| 102 | + state="ACTIVE", |
| 103 | + video_metadata=dict(video_duration=datetime.timedelta(seconds=30)), |
| 104 | + error=dict(code=7, message="ok?"), |
| 105 | + ) |
| 106 | + ) |
| 107 | + |
| 108 | + f = genai.upload_file(path="dummy") |
| 109 | + self.assertEqual(google.rpc.status_pb2.Status(code=7, message="ok?"), f.error) |
| 110 | + self.assertEqual( |
| 111 | + glm.VideoMetadata(dict(video_duration=datetime.timedelta(seconds=30))), f.video_metadata |
| 112 | + ) |
| 113 | + |
| 114 | + @parameterized.named_parameters( |
| 115 | + [ |
| 116 | + dict( |
| 117 | + testcase_name="FileDataDict", |
| 118 | + file_data=dict(file_uri="https://test_uri"), |
| 119 | + ), |
| 120 | + dict( |
| 121 | + testcase_name="FileDict", |
| 122 | + file_data=dict(uri="https://test_uri"), |
| 123 | + ), |
| 124 | + dict( |
| 125 | + testcase_name="FileData", |
| 126 | + file_data=glm.FileData(file_uri="https://test_uri"), |
| 127 | + ), |
| 128 | + dict( |
| 129 | + testcase_name="glm.File", |
| 130 | + file_data=glm.File(uri="https://test_uri"), |
| 131 | + ), |
| 132 | + dict( |
| 133 | + testcase_name="file_types.File", |
| 134 | + file_data=file_types.File(dict(uri="https://test_uri")), |
| 135 | + ), |
| 136 | + ] |
| 137 | + ) |
| 138 | + def test_to_file_data(self, file_data): |
| 139 | + file_data = file_types.to_file_data(file_data) |
| 140 | + self.assertEqual(glm.FileData(file_uri="https://test_uri"), file_data) |
0 commit comments