summaryrefslogtreecommitdiffstats
path: root/etc/unittest/main.py
blob: cc3c6a184cf248a789b9203a9b93f2b7e893b9db (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
import unittest
import asyncio
import g4f
from g4f import ChatCompletion, get_last_provider
from g4f.Provider import RetryProvider
from .mocks import ProviderMock

DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]

class NoTestChatCompletion(unittest.TestCase):

    def no_test_create_default(self):
        result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES)
        if "Good" not in result and "Hi" not in result:
            self.assertIn("Hello", result)

    def no_test_bing_provider(self):
        provider = g4f.Provider.Bing
        result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, provider)
        self.assertIn("Bing", result)

class TestGetLastProvider(unittest.TestCase):

    def test_get_last_provider(self):
        ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
        self.assertEqual(get_last_provider(), ProviderMock)

    def test_get_last_provider_retry(self):
        ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, RetryProvider([ProviderMock]))
        self.assertEqual(get_last_provider(), ProviderMock)

    def test_get_last_provider_async(self):
        coroutine = ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
        asyncio.run(coroutine)
        self.assertEqual(get_last_provider(), ProviderMock)

    def test_get_last_provider_as_dict(self):
        ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
        last_provider_dict = get_last_provider(True)
        self.assertIsInstance(last_provider_dict, dict)
        self.assertIn('name', last_provider_dict)
        self.assertEqual(ProviderMock.__name__, last_provider_dict['name'])