backend.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import unittest
  2. import asyncio
  3. from unittest.mock import MagicMock
  4. from .mocks import ProviderMock
  5. import g4f
  6. from g4f.errors import MissingRequirementsError
  7. try:
  8. from g4f.gui.server.backend import Backend_Api, get_error_message
  9. has_requirements = True
  10. except:
  11. has_requirements = False
  12. class TestBackendApi(unittest.TestCase):
  13. def setUp(self):
  14. if not has_requirements:
  15. self.skipTest("gui is not installed")
  16. self.app = MagicMock()
  17. self.api = Backend_Api(self.app)
  18. def test_version(self):
  19. response = self.api.get_version()
  20. self.assertIn("version", response)
  21. self.assertIn("latest_version", response)
  22. def test_get_models(self):
  23. response = self.api.get_models()
  24. self.assertIsInstance(response, list)
  25. self.assertTrue(len(response) > 0)
  26. def test_get_providers(self):
  27. response = self.api.get_providers()
  28. self.assertIsInstance(response, list)
  29. self.assertTrue(len(response) > 0)
  30. def test_search(self):
  31. from g4f.gui.server.internet import search
  32. try:
  33. result = asyncio.run(search("Hello"))
  34. except MissingRequirementsError:
  35. self.skipTest("search is not installed")
  36. self.assertEqual(5, len(result))
  37. class TestUtilityFunctions(unittest.TestCase):
  38. def setUp(self):
  39. if not has_requirements:
  40. self.skipTest("gui is not installed")
  41. def test_get_error_message(self):
  42. g4f.debug.last_provider = ProviderMock
  43. exception = Exception("Message")
  44. result = get_error_message(exception)
  45. self.assertEqual("ProviderMock: Exception: Message", result)
  46. if __name__ == '__main__':
  47. unittest.main()