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:
Gabriel Luiz Freitas Almeida 2024-10-02 14:21:52 -03:00 • committed by GitHub
commit 6febae599b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 444 additions and 322 deletions

View file

@ -1,12 +1,13 @@
import pytest
from uuid import uuid4
from unittest import mock
from uuid import uuid4
from fastapi import status, HTTPException
import pytest
from fastapi import HTTPException, status
from httpx import AsyncClient
@pytest.fixture
def body():
async def body():
return {
"name": "test_variable",
"value": "test_value",
@ -15,8 +16,8 @@ def body():
}
def test_create_variable(client, body, active_user, logged_in_headers):
response = client.post("api/v1/variables", json=body, headers=logged_in_headers)
async def test_create_variable(client: AsyncClient, body, active_user, logged_in_headers):
response = await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
result = response.json()
assert status.HTTP_201_CREATED == response.status_code
@ -27,105 +28,112 @@ def test_create_variable(client, body, active_user, logged_in_headers):
assert body["value"] != result["value"]
def test_create_variable__variable_name_alread_exists(client, body, active_user, logged_in_headers):
client.post("api/v1/variables", json=body, headers=logged_in_headers)
async def test_create_variable__variable_name_already_exists(client: AsyncClient, body, active_user, logged_in_headers):
await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
response = client.post("api/v1/variables", json=body, headers=logged_in_headers)
response = await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
result = response.json()
assert status.HTTP_400_BAD_REQUEST == response.status_code
assert "Variable name already exists" in result["detail"]
def test_create_variable__variable_name_and_value_cannot_be_empty(client, body, active_user, logged_in_headers):
async def test_create_variable__variable_name_and_value_cannot_be_empty(
client: AsyncClient, body, active_user, logged_in_headers
):
body["name"] = ""
body["value"] = ""
response = client.post("api/v1/variables", json=body, headers=logged_in_headers)
response = await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
result = response.json()
assert status.HTTP_400_BAD_REQUEST == response.status_code
assert "Variable name and value cannot be empty" in result["detail"]
def test_create_variable__variable_name_cannot_be_empty(client, body, active_user, logged_in_headers):
async def test_create_variable__variable_name_cannot_be_empty(
client: AsyncClient, body, active_user, logged_in_headers
):
body["name"] = ""
response = client.post("api/v1/variables", json=body, headers=logged_in_headers)
response = await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
result = response.json()
assert status.HTTP_400_BAD_REQUEST == response.status_code
assert "Variable name cannot be empty" in result["detail"]
def test_create_variable__variable_value_cannot_be_empty(client, body, active_user, logged_in_headers):
async def test_create_variable__variable_value_cannot_be_empty(
client: AsyncClient, body, active_user, logged_in_headers
):
body["value"] = ""
response = client.post("api/v1/variables", json=body, headers=logged_in_headers)
response = await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
result = response.json()
assert status.HTTP_400_BAD_REQUEST == response.status_code
assert "Variable value cannot be empty" in result["detail"]
def test_create_variable__HTTPException(client, body, active_user, logged_in_headers):
async def test_create_variable__HTTPException(client: AsyncClient, body, active_user, logged_in_headers):
status_code = 418
generic_message = "I'm a teapot"
with mock.patch("langflow.services.auth.utils.encrypt_api_key") as m:
m.side_effect = HTTPException(status_code=status_code, detail=generic_message)
response = client.post("api/v1/variables", json=body, headers=logged_in_headers)
response = await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
result = response.json()
assert status.HTTP_418_IM_A_TEAPOT == response.status_code
assert generic_message in result["detail"]
def test_create_variable__Exception(client, body, active_user, logged_in_headers):
async def test_create_variable__Exception(client: AsyncClient, body, active_user, logged_in_headers):
generic_message = "Generic error message"
with mock.patch("langflow.services.auth.utils.encrypt_api_key") as m:
m.side_effect = Exception(generic_message)
response = client.post("api/v1/variables", json=body, headers=logged_in_headers)
response = await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
result = response.json()
assert status.HTTP_500_INTERNAL_SERVER_ERROR == response.status_code
assert generic_message in result["detail"]
def test_read_variables(client, body, active_user, logged_in_headers):
async def test_read_variables(client: AsyncClient, body, active_user, logged_in_headers):
names = ["test_variable1", "test_variable2", "test_variable3"]
for name in names:
body["name"] = name
client.post("api/v1/variables", json=body, headers=logged_in_headers)
await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
response = client.get("api/v1/variables", headers=logged_in_headers)
response = await client.get("api/v1/variables/", headers=logged_in_headers)
result = response.json()
assert status.HTTP_200_OK == response.status_code
assert all(name in [r["name"] for r in result] for name in names)
def test_read_variables__empty(client, active_user, logged_in_headers):
all_variables = client.get("api/v1/variables", headers=logged_in_headers).json()
async def test_read_variables__empty(client: AsyncClient, active_user, logged_in_headers):
all_variables = await client.get("api/v1/variables/", headers=logged_in_headers)
all_variables = all_variables.json()
for variable in all_variables:
client.delete(f"api/v1/variables/{variable.get('id')}", headers=logged_in_headers)
await client.delete(f"api/v1/variables/{variable.get('id')}", headers=logged_in_headers)
response = client.get("api/v1/variables", headers=logged_in_headers)
response = await client.get("api/v1/variables/", headers=logged_in_headers)
result = response.json()
assert status.HTTP_200_OK == response.status_code
assert [] == result
def test_read_variables__(client, active_user, logged_in_headers): # TODO check if this is correct
async def test_read_variables__(client: AsyncClient, active_user, logged_in_headers):
generic_message = "Generic error message"
with pytest.raises(Exception) as exc:
with mock.patch("sqlmodel.Session.exec") as m:
m.side_effect = Exception(generic_message)
response = client.get("api/v1/variables", headers=logged_in_headers)
response = await client.get("api/v1/variables/", headers=logged_in_headers)
result = response.json()
assert status.HTTP_500_INTERNAL_SERVER_ERROR == response.status_code
@ -134,47 +142,46 @@ def test_read_variables__(client, active_user, logged_in_headers): # TODO check
assert generic_message in str(exc.value)
def test_update_variable(client, body, active_user, logged_in_headers):
saved = client.post("api/v1/variables", json=body, headers=logged_in_headers).json()
async def test_update_variable(client: AsyncClient, body, active_user, logged_in_headers):
saved = await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
saved = saved.json()
body["id"] = saved.get("id")
body["name"] = "new_name"
body["value"] = "new_value"
body["type"] = "new_type"
body["default_fields"] = ["new_field"]
response = client.patch(f"api/v1/variables/{saved.get('id')}", json=body, headers=logged_in_headers)
response = await client.patch(f"api/v1/variables/{saved.get('id')}", json=body, headers=logged_in_headers)
result = response.json()
assert status.HTTP_200_OK == response.status_code
assert saved["id"] == result["id"]
assert saved["name"] != result["name"]
# assert saved["type"] != result["type"] # TODO check if this is correct
assert saved["default_fields"] != result["default_fields"]
def test_update_variable__Exception(client, body, active_user, logged_in_headers):
async def test_update_variable__Exception(client: AsyncClient, body, active_user, logged_in_headers):
wrong_id = uuid4()
body["id"] = str(wrong_id)
response = client.patch(f"api/v1/variables/{wrong_id}", json=body, headers=logged_in_headers)
response = await client.patch(f"api/v1/variables/{wrong_id}", json=body, headers=logged_in_headers)
result = response.json()
# assert status.HTTP_404_NOT_FOUND == response.status_code # TODO check if this is correct
assert status.HTTP_404_NOT_FOUND == response.status_code
assert "Variable not found" in result["detail"]
def test_delete_variable(client, body, active_user, logged_in_headers):
saved = client.post("api/v1/variables", json=body, headers=logged_in_headers).json()
response = client.delete(f"api/v1/variables/{saved.get('id')}", headers=logged_in_headers)
async def test_delete_variable(client: AsyncClient, body, active_user, logged_in_headers):
response = await client.post("api/v1/variables/", json=body, headers=logged_in_headers)
saved = response.json()
response = await client.delete(f"api/v1/variables/{saved.get('id')}", headers=logged_in_headers)
assert status.HTTP_204_NO_CONTENT == response.status_code
def test_delete_variable__Exception(client, active_user, logged_in_headers):
async def test_delete_variable__Exception(client: AsyncClient, active_user, logged_in_headers):
wrong_id = uuid4()
response = client.delete(f"api/v1/variables/{wrong_id}", headers=logged_in_headers)
response = await client.delete(f"api/v1/variables/{wrong_id}", headers=logged_in_headers)
# assert status.HTTP_404_NOT_FOUND == response.status_code # TODO check if this is correct
assert status.HTTP_500_INTERNAL_SERVER_ERROR == response.status_code