tests: update tests to use httpx.AsyncClient (#3984)
* Add async support and dependencies to pyproject.toml files - Added `asgi-lifespan>=2.1.0` to dependencies. - Configured `asyncio_mode` and `asyncio_default_fixture_loop_scope` for pytest. - Updated `tool.uv` section with `asgi-lifespan` in dev-dependencies. * Convert test fixtures to async and use AsyncClient for HTTP requests * Handle 'ImportFrom' nodes in AST validation to support module attribute imports * Convert test cases to use async HTTP client - Updated test cases in `test_database.py`, `test_endpoints.py`, `test_user.py`, `test_variable.py`, `test_files.py`, `test_chat_endpoint.py`, `test_misc.py`, `test_messages_endpoints.py`, `test_api_key.py`, `test_webhook.py`, and `test_login.py` to use `httpx.AsyncClient` instead of `fastapi.TestClient`. - Modified test functions to be asynchronous and use `await` for HTTP requests. - Adjusted fixtures and helper functions to support asynchronous operations. - Ensured consistency in endpoint paths and request methods across all test cases. * Refactor string concatenation to f-string in test_chat_endpoint.py * [autofix.ci] apply automated fixes * Refactor import validation to use pattern matching for AST nodes * Set `startup_timeout` and `shutdown_timeout` to `None` in `LifespanManager` for test files. * Convert test functions to async in `test_messages_endpoints.py` * Add `api_key_required` marker to assistant component tests --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
7e3d470845
commit
6febae599b
19 changed files with 444 additions and 322 deletions
|
|
@ -1,46 +1,49 @@
|
|||
import json
|
||||
from uuid import UUID
|
||||
|
||||
from orjson import orjson
|
||||
|
||||
from langflow.memory import get_messages
|
||||
from langflow.services.database.models.flow import FlowCreate, FlowUpdate
|
||||
|
||||
|
||||
def test_build_flow(client, json_memory_chatbot_no_llm, logged_in_headers):
|
||||
flow_id = _create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
|
||||
async def test_build_flow(client, json_memory_chatbot_no_llm, logged_in_headers):
|
||||
flow_id = await _create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
|
||||
|
||||
with client.stream("POST", f"api/v1/build/{flow_id}/flow", json={}, headers=logged_in_headers) as r:
|
||||
consume_and_assert_stream(r)
|
||||
async with client.stream("POST", f"api/v1/build/{flow_id}/flow", json={}, headers=logged_in_headers) as r:
|
||||
await consume_and_assert_stream(r)
|
||||
|
||||
check_messages(flow_id)
|
||||
|
||||
|
||||
def test_build_flow_from_request_data(client, json_memory_chatbot_no_llm, logged_in_headers):
|
||||
flow_id = _create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
|
||||
flow_data = client.get("api/v1/flows/" + str(flow_id), headers=logged_in_headers).json()
|
||||
async def test_build_flow_from_request_data(client, json_memory_chatbot_no_llm, logged_in_headers):
|
||||
flow_id = await _create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
|
||||
response = await client.get("api/v1/flows/" + str(flow_id), headers=logged_in_headers)
|
||||
flow_data = response.json()
|
||||
|
||||
with client.stream(
|
||||
async with client.stream(
|
||||
"POST", f"api/v1/build/{flow_id}/flow", json={"data": flow_data["data"]}, headers=logged_in_headers
|
||||
) as r:
|
||||
consume_and_assert_stream(r)
|
||||
await consume_and_assert_stream(r)
|
||||
|
||||
check_messages(flow_id)
|
||||
|
||||
|
||||
def test_build_flow_with_frozen_path(client, json_memory_chatbot_no_llm, logged_in_headers):
|
||||
flow_id = _create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
|
||||
async def test_build_flow_with_frozen_path(client, json_memory_chatbot_no_llm, logged_in_headers):
|
||||
flow_id = await _create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
|
||||
|
||||
flow_data = client.get("api/v1/flows/" + str(flow_id), headers=logged_in_headers).json()
|
||||
response = await client.get("api/v1/flows/" + str(flow_id), headers=logged_in_headers)
|
||||
flow_data = response.json()
|
||||
flow_data["data"]["nodes"][0]["data"]["node"]["frozen"] = True
|
||||
response = client.patch(
|
||||
"api/v1/flows/" + str(flow_id),
|
||||
response = await client.patch(
|
||||
f"api/v1/flows/{flow_id}",
|
||||
json=FlowUpdate(name="Flow", description="description", data=flow_data["data"]).model_dump(),
|
||||
headers=logged_in_headers,
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
with client.stream("POST", f"api/v1/build/{flow_id}/flow", json={}, headers=logged_in_headers) as r:
|
||||
consume_and_assert_stream(r)
|
||||
async with client.stream("POST", f"api/v1/build/{flow_id}/flow", json={}, headers=logged_in_headers) as r:
|
||||
await consume_and_assert_stream(r)
|
||||
|
||||
check_messages(flow_id)
|
||||
|
||||
|
|
@ -57,9 +60,9 @@ def check_messages(flow_id):
|
|||
assert messages[1].sender_name == "AI"
|
||||
|
||||
|
||||
def consume_and_assert_stream(r):
|
||||
async def consume_and_assert_stream(r):
|
||||
count = 0
|
||||
for line in r.iter_lines():
|
||||
async for line in r.aiter_lines():
|
||||
# httpx split by \n, but ndjson sends two \n for each line
|
||||
if not line:
|
||||
continue
|
||||
|
|
@ -83,11 +86,11 @@ def consume_and_assert_stream(r):
|
|||
count += 1
|
||||
|
||||
|
||||
def _create_flow(client, json_memory_chatbot_no_llm, logged_in_headers):
|
||||
async def _create_flow(client, json_memory_chatbot_no_llm, logged_in_headers):
|
||||
vector_store = orjson.loads(json_memory_chatbot_no_llm)
|
||||
data = vector_store["data"]
|
||||
vector_store = FlowCreate(name="Flow", description="description", data=data, endpoint_name="f")
|
||||
response = client.post("api/v1/flows/", json=vector_store.model_dump(), headers=logged_in_headers)
|
||||
response = await client.post("api/v1/flows/", json=vector_store.model_dump(), headers=logged_in_headers)
|
||||
response.raise_for_status()
|
||||
flow_id = response.json()["id"]
|
||||
return flow_id
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue