Skip to content

Commit 0eaac2f

Browse files
committed
Create test_gemini_upload.py
1 parent e2d9f63 commit 0eaac2f

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

tests/test_gemini_upload.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import os
2+
import unittest
3+
4+
from google import genai
5+
from google.genai import errors
6+
7+
8+
class TestGeminiUpload(unittest.TestCase):
9+
def setUp(self):
10+
self.api_key = os.getenv("GEMINI_API_KEY")
11+
if not self.api_key:
12+
self.skipTest("GEMINI_API_KEY not set")
13+
self.client = genai.Client(api_key=self.api_key)
14+
15+
def test_upload_file(self):
16+
# Create a dummy file
17+
filename = "test_gemini_upload.txt"
18+
with open(filename, "w") as f:
19+
f.write("Hello Gemini, this is a test file.")
20+
21+
try:
22+
# Upload the file
23+
print(f"Uploading {filename}...")
24+
# SDK docs: client.files.upload(file='sample.txt', config={'name': 'display_file_name'})
25+
# But 'name' in config seems to be for display? No, config={'display_name': '...'} in some examples, {'name': '...'} in others.
26+
# Only 'display_name' is user-visible. 'name' is usually ID.
27+
# Docs say: config={'name': 'display_file_name'} in one snippet, but confusing.
28+
# Let's check the snippet from Chunk 9: client.files.upload(file='sample.txt', config={'name': 'display_file_name'})
29+
# Actually, standard is usually just path. Let's try simple upload.
30+
31+
# Using keyword arguments as per new SDK style
32+
sample_file = self.client.files.upload(file=filename, config={"display_name": "Test File"})
33+
34+
print(f"Uploaded file '{sample_file.display_name}' as: {sample_file.uri}")
35+
36+
# Verify the file exists
37+
file = self.client.files.get(name=sample_file.name)
38+
self.assertEqual(file.display_name, "Test File")
39+
self.assertIn(file.state, ["PROCESSING", "ACTIVE"])
40+
41+
# Verify file in list_files
42+
print("Listing files...")
43+
files = list(self.client.files.list())
44+
self.assertTrue(any(f.name == sample_file.name for f in files))
45+
print(f"File {sample_file.name} found in list_files")
46+
47+
print(f"File state: {file.state}")
48+
49+
# Clean up
50+
print(f"Deleting file {sample_file.name}...")
51+
self.client.files.delete(name=sample_file.name)
52+
53+
# Verify deletion
54+
# New SDK might raise different exception or HttpError.
55+
# Common pattern is google.genai.errors.ClientError or similar?
56+
# Or just verify it's gone by checking list or get raises exception.
57+
# Let's import the specific exception if we can find it, otherwise using generic exception for now.
58+
# The previous SDK raised google.api_core.exceptions.NotFound
59+
# The new SDK is built differently.
60+
61+
with self.assertRaises(errors.ClientError):
62+
self.client.files.get(name=sample_file.name)
63+
64+
finally:
65+
# Clean up local file
66+
if os.path.exists(filename):
67+
os.remove(filename)
68+
69+
70+
if __name__ == "__main__":
71+
unittest.main()

0 commit comments

Comments
 (0)