-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests for client features and saving image
- Loading branch information
1 parent
52a3d21
commit e1f867a
Showing
3 changed files
with
106 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |