backend.py 1.4 KB

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