summaryrefslogtreecommitdiffstats
path: root/etc/unittest/backend.py
blob: 846c355450565f5244e3fda13c4ca08d9f7ac93f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import unittest
# import asyncio
from unittest.mock import MagicMock
from .mocks import ProviderMock
import g4f
try:
    from g4f.gui.server.backend import Backend_Api, get_error_message
    # from g4f.gui.server.internet import search
    has_requirements = True
except:
    has_requirements = False
        
class TestBackendApi(unittest.TestCase):

    def setUp(self):
        if not has_requirements:
            self.skipTest("gui is not installed")
        self.app = MagicMock()
        self.api = Backend_Api(self.app)

    # def test_version(self):
    #     response = self.api.get_version()
    #     self.assertIn("version", response)
    #     self.assertIn("latest_version", response)
        
    def test_get_models(self):
        response = self.api.get_models()
        self.assertIsInstance(response, list)
        self.assertTrue(len(response) > 0)
        
    def test_get_providers(self):
        response = self.api.get_providers()
        self.assertIsInstance(response, list)
        self.assertTrue(len(response) > 0)
        
    # def test_search(self):
    #     result = asyncio.run(search("Hello"))
    #     self.assertEqual(5, len(result))
        
class TestUtilityFunctions(unittest.TestCase):

    def setUp(self):
        if not has_requirements:
            self.skipTest("gui is not installed")

    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)
        
if __name__ == '__main__':
    unittest.main()