summaryrefslogtreecommitdiffstats
path: root/etc/unittest/asyncio.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-01-21 09:37:35 +0100
committerGitHub <noreply@github.com>2024-01-21 09:37:35 +0100
commitfea4f546a7fb7c7af5a3da2247b5ea966e179f08 (patch)
tree59266fc31ec6df5fa53ef97b22f024f2f3f339f8 /etc/unittest/asyncio.py
parentMerge pull request #1484 from hlohaus/copilot (diff)
parentUpdate workflow (diff)
downloadgpt4free-fea4f546a7fb7c7af5a3da2247b5ea966e179f08.tar
gpt4free-fea4f546a7fb7c7af5a3da2247b5ea966e179f08.tar.gz
gpt4free-fea4f546a7fb7c7af5a3da2247b5ea966e179f08.tar.bz2
gpt4free-fea4f546a7fb7c7af5a3da2247b5ea966e179f08.tar.lz
gpt4free-fea4f546a7fb7c7af5a3da2247b5ea966e179f08.tar.xz
gpt4free-fea4f546a7fb7c7af5a3da2247b5ea966e179f08.tar.zst
gpt4free-fea4f546a7fb7c7af5a3da2247b5ea966e179f08.zip
Diffstat (limited to 'etc/unittest/asyncio.py')
-rw-r--r--etc/unittest/asyncio.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/etc/unittest/asyncio.py b/etc/unittest/asyncio.py
new file mode 100644
index 00000000..74e29986
--- /dev/null
+++ b/etc/unittest/asyncio.py
@@ -0,0 +1,57 @@
+from .include import DEFAULT_MESSAGES
+import asyncio
+import nest_asyncio
+import unittest
+import g4f
+from g4f import ChatCompletion
+from .mocks import ProviderMock, AsyncProviderMock, AsyncGeneratorProviderMock
+
+class TestChatCompletion(unittest.TestCase):
+
+ async def run_exception(self):
+ return ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
+
+ def test_exception(self):
+ self.assertRaises(g4f.errors.NestAsyncioError, asyncio.run, self.run_exception())
+
+ def test_create(self):
+ result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
+ self.assertEqual("Mock",result)
+
+ def test_create_generator(self):
+ result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncGeneratorProviderMock)
+ self.assertEqual("Mock",result)
+
+class TestChatCompletionAsync(unittest.IsolatedAsyncioTestCase):
+
+ async def test_base(self):
+ result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
+ self.assertEqual("Mock",result)
+
+ async def test_async(self):
+ result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
+ self.assertEqual("Mock",result)
+
+ async def test_create_generator(self):
+ result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, AsyncGeneratorProviderMock)
+ self.assertEqual("Mock",result)
+
+class TestChatCompletionNestAsync(unittest.IsolatedAsyncioTestCase):
+
+ def setUp(self) -> None:
+ nest_asyncio.apply()
+
+ async def test_create(self):
+ result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
+ self.assertEqual("Mock",result)
+
+ async def test_nested(self):
+ result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
+ self.assertEqual("Mock",result)
+
+ async def test_nested_generator(self):
+ result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncGeneratorProviderMock)
+ self.assertEqual("Mock",result)
+
+if __name__ == '__main__':
+ unittest.main() \ No newline at end of file