2024-01-20 20:20:23 -05:00
|
|
|
import unittest
|
2024-02-21 22:35:11 -05:00
|
|
|
import asyncio
|
2024-01-20 20:20:23 -05:00
|
|
|
from unittest.mock import MagicMock
|
|
|
|
from .mocks import ProviderMock
|
|
|
|
import g4f
|
2024-03-11 21:15:22 -04:00
|
|
|
from g4f.errors import MissingRequirementsError
|
2024-02-21 22:35:11 -05:00
|
|
|
|
2024-01-26 01:54:13 -05:00
|
|
|
try:
|
|
|
|
from g4f.gui.server.backend import Backend_Api, get_error_message
|
|
|
|
has_requirements = True
|
|
|
|
except:
|
|
|
|
has_requirements = False
|
2024-02-21 22:35:11 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
class TestBackendApi(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2024-01-26 01:54:13 -05:00
|
|
|
if not has_requirements:
|
2024-01-26 06:49:52 -05:00
|
|
|
self.skipTest("gui is not installed")
|
2024-01-20 20:20:23 -05:00
|
|
|
self.app = MagicMock()
|
|
|
|
self.api = Backend_Api(self.app)
|
|
|
|
|
2024-02-21 22:35:11 -05:00
|
|
|
def test_version(self):
|
|
|
|
response = self.api.get_version()
|
|
|
|
self.assertIn("version", response)
|
|
|
|
self.assertIn("latest_version", response)
|
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
def test_get_models(self):
|
|
|
|
response = self.api.get_models()
|
|
|
|
self.assertIsInstance(response, list)
|
|
|
|
self.assertTrue(len(response) > 0)
|
2024-02-21 22:35:11 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
def test_get_providers(self):
|
|
|
|
response = self.api.get_providers()
|
|
|
|
self.assertIsInstance(response, list)
|
|
|
|
self.assertTrue(len(response) > 0)
|
2024-02-21 22:35:11 -05:00
|
|
|
|
|
|
|
def test_search(self):
|
|
|
|
from g4f.gui.server.internet import search
|
2024-03-11 21:15:22 -04:00
|
|
|
try:
|
|
|
|
result = asyncio.run(search("Hello"))
|
|
|
|
except MissingRequirementsError:
|
|
|
|
self.skipTest("search is not installed")
|
2024-02-21 22:35:11 -05:00
|
|
|
self.assertEqual(5, len(result))
|
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
class TestUtilityFunctions(unittest.TestCase):
|
|
|
|
|
2024-01-26 01:54:13 -05:00
|
|
|
def setUp(self):
|
|
|
|
if not has_requirements:
|
2024-01-26 06:49:52 -05:00
|
|
|
self.skipTest("gui is not installed")
|
2024-01-26 01:54:13 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
def test_get_error_message(self):
|
|
|
|
g4f.debug.last_provider = ProviderMock
|
|
|
|
exception = Exception("Message")
|
|
|
|
result = get_error_message(exception)
|
|
|
|
self.assertEqual("ProviderMock: Exception: Message", result)
|
2024-02-21 22:35:11 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|