Co-authored-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
@@ -7,25 +7,22 @@ from jinja2 import Template
|
||||
|
||||
from core.helper.code_executor.code_executor import CodeExecutor, CodeLanguage
|
||||
|
||||
MOCK = os.getenv('MOCK_SWITCH', 'false') == 'true'
|
||||
MOCK = os.getenv("MOCK_SWITCH", "false") == "true"
|
||||
|
||||
|
||||
class MockedCodeExecutor:
|
||||
@classmethod
|
||||
def invoke(cls, language: Literal['python3', 'javascript', 'jinja2'],
|
||||
code: str, inputs: dict) -> dict:
|
||||
def invoke(cls, language: Literal["python3", "javascript", "jinja2"], code: str, inputs: dict) -> dict:
|
||||
# invoke directly
|
||||
match language:
|
||||
case CodeLanguage.PYTHON3:
|
||||
return {
|
||||
"result": 3
|
||||
}
|
||||
return {"result": 3}
|
||||
case CodeLanguage.JINJA2:
|
||||
return {
|
||||
"result": Template(code).render(inputs)
|
||||
}
|
||||
return {"result": Template(code).render(inputs)}
|
||||
case _:
|
||||
raise Exception("Language not supported")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def setup_code_executor_mock(request, monkeypatch: MonkeyPatch):
|
||||
if not MOCK:
|
||||
|
||||
@@ -6,38 +6,32 @@ import httpx
|
||||
import pytest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
|
||||
MOCK = os.getenv('MOCK_SWITCH', 'false') == 'true'
|
||||
MOCK = os.getenv("MOCK_SWITCH", "false") == "true"
|
||||
|
||||
|
||||
class MockedHttp:
|
||||
def httpx_request(method: Literal['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'],
|
||||
url: str, **kwargs) -> httpx.Response:
|
||||
def httpx_request(
|
||||
method: Literal["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"], url: str, **kwargs
|
||||
) -> httpx.Response:
|
||||
"""
|
||||
Mocked httpx.request
|
||||
"""
|
||||
if url == 'http://404.com':
|
||||
response = httpx.Response(
|
||||
status_code=404,
|
||||
request=httpx.Request(method, url),
|
||||
content=b'Not Found'
|
||||
)
|
||||
if url == "http://404.com":
|
||||
response = httpx.Response(status_code=404, request=httpx.Request(method, url), content=b"Not Found")
|
||||
return response
|
||||
|
||||
# get data, files
|
||||
data = kwargs.get('data', None)
|
||||
files = kwargs.get('files', None)
|
||||
data = kwargs.get("data", None)
|
||||
files = kwargs.get("files", None)
|
||||
if data is not None:
|
||||
resp = dumps(data).encode('utf-8')
|
||||
resp = dumps(data).encode("utf-8")
|
||||
elif files is not None:
|
||||
resp = dumps(files).encode('utf-8')
|
||||
resp = dumps(files).encode("utf-8")
|
||||
else:
|
||||
resp = b'OK'
|
||||
resp = b"OK"
|
||||
|
||||
response = httpx.Response(
|
||||
status_code=200,
|
||||
request=httpx.Request(method, url),
|
||||
headers=kwargs.get('headers', {}),
|
||||
content=resp
|
||||
status_code=200, request=httpx.Request(method, url), headers=kwargs.get("headers", {}), content=resp
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
Reference in New Issue
Block a user