backend.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from __future__ import annotations
  2. import unittest
  3. import asyncio
  4. from unittest.mock import MagicMock
  5. from g4f.errors import MissingRequirementsError
  6. try:
  7. from g4f.gui.server.backend import Backend_Api
  8. has_requirements = True
  9. except:
  10. has_requirements = False
  11. try:
  12. from duckduckgo_search.exceptions import DuckDuckGoSearchException
  13. except ImportError:
  14. class DuckDuckGoSearchException:
  15. pass
  16. class TestBackendApi(unittest.TestCase):
  17. def setUp(self):
  18. if not has_requirements:
  19. self.skipTest("gui is not installed")
  20. self.app = MagicMock()
  21. self.api = Backend_Api(self.app)
  22. def test_version(self):
  23. response = self.api.get_version()
  24. self.assertIn("version", response)
  25. self.assertIn("latest_version", response)
  26. def test_get_models(self):
  27. response = self.api.get_models()
  28. self.assertIsInstance(response, list)
  29. self.assertTrue(len(response) > 0)
  30. def test_get_providers(self):
  31. response = self.api.get_providers()
  32. self.assertIsInstance(response, list)
  33. self.assertTrue(len(response) > 0)
  34. def test_search(self):
  35. from g4f.gui.server.internet import search
  36. try:
  37. result = asyncio.run(search("Hello"))
  38. except DuckDuckGoSearchException as e:
  39. self.skipTest(e)
  40. except MissingRequirementsError:
  41. self.skipTest("search is not installed")
  42. self.assertTrue(len(result) >= 4)