Skip to content

Commit

Permalink
test: add unit tests for client features and saving image
Browse files Browse the repository at this point in the history
  • Loading branch information
HanaokaYuzu committed Feb 15, 2024
1 parent 52a3d21 commit e1f867a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 27 deletions.
68 changes: 68 additions & 0 deletions tests/test_client_features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os
import unittest

from loguru import logger

from gemini import GeminiClient, AuthError


class TestGeminiClient(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
self.geminiclient = GeminiClient(
os.getenv("SECURE_1PSID"), os.getenv("SECURE_1PSIDTS")
)

try:
await self.geminiclient.init()
except AuthError:
self.skipTest("Test was skipped due to invalid cookies")

async def test_successful_request(self):
response = await self.geminiclient.generate_content("Hello World!")
self.assertTrue(response.text)

async def test_continuous_conversation(self):
chat = self.geminiclient.start_chat()
response1 = await chat.send_message("Briefly introduce Europe")
self.assertTrue(response1.text)
logger.debug(response1.text)
response2 = await chat.send_message("What's the population there?")
self.assertTrue(response2.text)
logger.debug(response2.text)

async def test_send_web_image(self):
response = await self.geminiclient.generate_content(
"Send me some pictures of cats"
)
self.assertTrue(response.images)
for image in response.images:
self.assertTrue(image.url)
logger.debug(image)

async def test_ai_image_generation(self):
response = await self.geminiclient.generate_content(
"Generate some pictures of cats"
)
self.assertTrue(response.images)
for image in response.images:
self.assertTrue(image.url)
logger.debug(image)

async def test_reply_candidates(self):
chat = self.geminiclient.start_chat()
response = await chat.send_message(
"What's the best Japanese dish? Recommend one only."
)
self.assertTrue(len(response.candidates) > 1)
for candidate in response.candidates:
logger.debug(candidate)

new_candidate = chat.choose_candidate(index=1)
self.assertEqual(response.chosen, 1)
followup_response = await chat.send_message("Tell me more about it.")
logger.warning(new_candidate.text)
logger.warning(followup_response.text)


if __name__ == "__main__":
unittest.main()
27 changes: 0 additions & 27 deletions tests/test_generate_content.py

This file was deleted.

38 changes: 38 additions & 0 deletions tests/test_save_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import unittest

from gemini import GeminiClient, AuthError


class TestGeminiClient(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
self.geminiclient = GeminiClient(
os.getenv("SECURE_1PSID"), os.getenv("SECURE_1PSIDTS")
)

try:
await self.geminiclient.init()
except AuthError:
self.skipTest("Test was skipped due to invalid cookies")

async def test_save_web_image(self):
response = await self.geminiclient.generate_content(
"Send me some pictures of cats"
)
self.assertTrue(response.images)
for i, image in enumerate(response.images):
self.assertTrue(image.url)
await image.save()

async def test_save_generated_image(self):
response = await self.geminiclient.generate_content(
"Generate some pictures of cats"
)
self.assertTrue(response.images)
for i, image in enumerate(response.images):
self.assertTrue(image.url)
await image.save()


if __name__ == "__main__":
unittest.main()

0 comments on commit e1f867a

Please sign in to comment.