Fix unittests (#1472)

* Fix unittests

* Fix missing distutils, install setuptools

* Fix version not found

* Remove deprecation in get_event_loop
This commit is contained in:
H Lohaus 2024-01-14 16:37:21 +01:00 committed by GitHub
parent faa5f0f9bc
commit 42709f55b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 14 deletions

View File

@ -14,6 +14,6 @@ jobs:
python-version: "3.x" python-version: "3.x"
cache: 'pip' cache: 'pip'
- name: Install requirements - name: Install requirements
- run: pip install -r requirements.txt run: pip install -r requirements.txt
- name: Run tests - name: Run tests
- run: python -m etc.unittest.main run: python -m etc.unittest.main

View File

@ -11,6 +11,7 @@ from g4f.gui.server.backend import Backend_Api, get_error_message
from g4f.base_provider import BaseProvider from g4f.base_provider import BaseProvider
g4f.debug.logging = False g4f.debug.logging = False
g4f.debug.version_check = False
class MockProvider(BaseProvider): class MockProvider(BaseProvider):
working = True working = True

View File

@ -5,7 +5,7 @@ import os
import random import random
import secrets import secrets
import string import string
from asyncio import AbstractEventLoop, BaseEventLoop from asyncio import AbstractEventLoop
from platformdirs import user_config_dir from platformdirs import user_config_dir
from browser_cookie3 import ( from browser_cookie3 import (
chrome, chromium, opera, opera_gx, chrome, chromium, opera, opera_gx,
@ -27,19 +27,13 @@ def get_event_loop() -> AbstractEventLoop:
AbstractEventLoop: The current or new event loop. AbstractEventLoop: The current or new event loop.
""" """
try: try:
loop = asyncio.get_event_loop() loop = asyncio.get_running_loop()
if isinstance(loop, BaseEventLoop):
loop._check_closed()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
asyncio.get_running_loop()
if not hasattr(loop.__class__, "_nest_patched"): if not hasattr(loop.__class__, "_nest_patched"):
import nest_asyncio import nest_asyncio
nest_asyncio.apply(loop) nest_asyncio.apply(loop)
except RuntimeError: except RuntimeError:
pass loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
except ImportError: except ImportError:
raise RuntimeError( raise RuntimeError(
'Use "create_async" instead of "create" function in a running event loop. Or install "nest_asyncio" package.' 'Use "create_async" instead of "create" function in a running event loop. Or install "nest_asyncio" package.'

View File

@ -5,6 +5,7 @@ from typing import Generator
from g4f import debug, version, models from g4f import debug, version, models
from g4f import _all_models, get_last_provider, ChatCompletion from g4f import _all_models, get_last_provider, ChatCompletion
from g4f.image import is_allowed_extension, to_image from g4f.image import is_allowed_extension, to_image
from g4f.errors import VersionNotFoundError
from g4f.Provider import __providers__ from g4f.Provider import __providers__
from g4f.Provider.bing.create_images import patch_provider from g4f.Provider.bing.create_images import patch_provider
from .internet import get_search_message from .internet import get_search_message
@ -91,8 +92,12 @@ class Backend_Api:
Returns: Returns:
dict: A dictionary containing the current and latest version. dict: A dictionary containing the current and latest version.
""" """
try:
current_version = version.utils.current_version
except VersionNotFoundError:
current_version = None
return { return {
"version": version.utils.current_version, "version": current_version,
"latest_version": version.get_latest_version(), "latest_version": version.get_latest_version(),
} }

View File

@ -20,4 +20,5 @@ asyncstdlib
async-property async-property
undetected-chromedriver undetected-chromedriver
brotli brotli
beautifulsoup4 beautifulsoup4
setuptools

View File

@ -32,6 +32,7 @@ install_requires = [
"undetected-chromedriver", "undetected-chromedriver",
"brotli", "brotli",
"beautifulsoup4", "beautifulsoup4",
"setuptools",
] ]
DESCRIPTION = ( DESCRIPTION = (