Skip to content

Commit

Permalink
Update unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
hlohaus committed Nov 18, 2024
1 parent 8bc456f commit 2fe4316
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 29 deletions.
1 change: 0 additions & 1 deletion etc/unittest/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
from .model import *
from .client import *
from .include import *
from .integration import *

unittest.main()
2 changes: 1 addition & 1 deletion etc/unittest/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ def test_search(self):
self.skipTest(e)
except MissingRequirementsError:
self.skipTest("search is not installed")
self.assertEqual(4, len(result))
self.assertTrue(len(result) >= 4)
33 changes: 18 additions & 15 deletions etc/unittest/integration.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import unittest
import json

try:
import nest_asyncio
has_nest_asyncio = True
except ImportError:
has_nest_asyncio = False

from g4f.client import Client, ChatCompletion
from g4f.Provider import Bing, OpenaiChat
from g4f.client import Client, AsyncClient, ChatCompletion
from g4f.Provider import Copilot, DDG

DEFAULT_MESSAGES = [{"role": "system", "content": 'Response in json, Example: {"success": false}'},
{"role": "user", "content": "Say success true in json"}]

class TestProviderIntegration(unittest.TestCase):
def setUp(self):
if not has_nest_asyncio:
self.skipTest("nest_asyncio is not installed")

def test_bing(self):
self.skipTest("Not working")
client = Client(provider=Bing)
client = Client(provider=Copilot)
response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
self.assertIsInstance(response, ChatCompletion)
self.assertIn("success", json.loads(response.choices[0].message.content))

def test_openai(self):
self.skipTest("not working in this network")
client = Client(provider=OpenaiChat)
client = Client(provider=DDG)
response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
self.assertIsInstance(response, ChatCompletion)
self.assertIn("success", json.loads(response.choices[0].message.content))

class TestChatCompletionAsync(unittest.IsolatedAsyncioTestCase):

async def test_bing(self):
client = AsyncClient(provider=Copilot)
response = await client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
self.assertIsInstance(response, ChatCompletion)
self.assertIn("success", json.loads(response.choices[0].message.content))

async def test_openai(self):
client = AsyncClient(provider=DDG)
response = await client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
self.assertIsInstance(response, ChatCompletion)
self.assertIn("success", json.loads(response.choices[0].message.content))

if __name__ == '__main__':
unittest.main()
12 changes: 0 additions & 12 deletions etc/unittest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@

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):
Expand Down
1 change: 1 addition & 0 deletions g4f/Provider/Copilot.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def create_completion(

websocket_url = cls.websocket_url
access_token = None
headers = None
cookies = conversation.cookie_jar if conversation is not None else None
if cls.needs_auth:
if conversation is None or conversation.access_token is None:
Expand Down

0 comments on commit 2fe4316

Please sign in to comment.