main.py 1.2 KB

123456789101112131415161718192021222324252627282930
  1. import unittest
  2. import asyncio
  3. import g4f
  4. from g4f import ChatCompletion, get_last_provider
  5. from g4f.Provider import RetryProvider
  6. from .mocks import ProviderMock
  7. DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
  8. class TestGetLastProvider(unittest.TestCase):
  9. def test_get_last_provider(self):
  10. ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
  11. self.assertEqual(get_last_provider(), ProviderMock)
  12. def test_get_last_provider_retry(self):
  13. ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, RetryProvider([ProviderMock]))
  14. self.assertEqual(get_last_provider(), ProviderMock)
  15. def test_get_last_provider_async(self):
  16. coroutine = ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
  17. asyncio.run(coroutine)
  18. self.assertEqual(get_last_provider(), ProviderMock)
  19. def test_get_last_provider_as_dict(self):
  20. ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
  21. last_provider_dict = get_last_provider(True)
  22. self.assertIsInstance(last_provider_dict, dict)
  23. self.assertIn('name', last_provider_dict)
  24. self.assertEqual(ProviderMock.__name__, last_provider_dict['name'])