main.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import unittest
  2. import asyncio
  3. import g4f
  4. from g4f import ChatCompletion, get_last_provider
  5. from g4f.errors import VersionNotFoundError
  6. from g4f.Provider import RetryProvider
  7. from .mocks import ProviderMock
  8. DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
  9. class TestGetLastProvider(unittest.TestCase):
  10. def test_get_last_provider(self):
  11. ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
  12. self.assertEqual(get_last_provider(), ProviderMock)
  13. def test_get_last_provider_retry(self):
  14. ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, RetryProvider([ProviderMock]))
  15. self.assertEqual(get_last_provider(), ProviderMock)
  16. def test_get_last_provider_async(self):
  17. coroutine = ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
  18. asyncio.run(coroutine)
  19. self.assertEqual(get_last_provider(), ProviderMock)
  20. def test_get_last_provider_as_dict(self):
  21. ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
  22. last_provider_dict = get_last_provider(True)
  23. self.assertIsInstance(last_provider_dict, dict)
  24. self.assertIn('name', last_provider_dict)
  25. self.assertEqual(ProviderMock.__name__, last_provider_dict['name'])
  26. def test_get_latest_version(self):
  27. try:
  28. self.assertIsInstance(g4f.version.utils.current_version, str)
  29. except VersionNotFoundError:
  30. pass
  31. self.assertIsInstance(g4f.version.utils.latest_version, str)