test: add unit tests for routes (#4249)

This commit is contained in:
Ítalo Johnny 2024-10-28 17:06:05 -03:00 • committed by GitHub
commit fadb20115d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 623 additions and 272 deletions

View file

@ -4,10 +4,10 @@ from uuid import UUID, uuid4
import orjson
import pytest
from fastapi.testclient import TestClient
from httpx import AsyncClient
from langflow.api.v1.schemas import FlowListCreate, ResultDataResponse
from langflow.graph.utils import log_transaction, log_vertex_build
from langflow.initial_setup.setup import load_flows_from_directory, load_starter_projects
from langflow.initial_setup.setup import load_starter_projects
from langflow.services.database.models.base import orjson_dumps
from langflow.services.database.models.flow import Flow, FlowCreate, FlowUpdate
from langflow.services.database.models.folder.model import FolderCreate
@ -30,7 +30,7 @@ def json_style():
@pytest.mark.usefixtures("active_user")
async def test_create_flow(client: TestClient, json_flow: str, logged_in_headers):
async def test_create_flow(client: AsyncClient, json_flow: str, logged_in_headers):
flow = orjson.loads(json_flow)
data = flow["data"]
flow = FlowCreate(name=str(uuid4()), description="description", data=data)
@ -47,7 +47,7 @@ async def test_create_flow(client: TestClient, json_flow: str, logged_in_headers
@pytest.mark.usefixtures("active_user")
async def test_read_flows(client: TestClient, json_flow: str, logged_in_headers):
async def test_read_flows(client: AsyncClient, json_flow: str, logged_in_headers):
flow_data = orjson.loads(json_flow)
data = flow_data["data"]
flow = FlowCreate(name=str(uuid4()), description="description", data=data)
@ -67,14 +67,7 @@ async def test_read_flows(client: TestClient, json_flow: str, logged_in_headers)
assert len(response.json()) > 0
async def test_read_flows_pagination_without_params(client: TestClient, logged_in_headers):
response = await client.get("api/v1/flows/", headers=logged_in_headers)
response_json = response.json()
assert response.status_code == 200
assert len(response_json) == 0
async def test_read_flows_pagination_with_params(client: TestClient, logged_in_headers):
async def test_read_flows_pagination_with_params(client: AsyncClient, logged_in_headers):
response = await client.get(
"api/v1/flows/", headers=logged_in_headers, params={"page": 3, "size": 10, "get_all": False}
)
@ -86,7 +79,7 @@ async def test_read_flows_pagination_with_params(client: TestClient, logged_in_h
assert len(response.json()["items"]) == 0
async def test_read_flows_pagination_with_flows(client: TestClient, logged_in_headers):
async def test_read_flows_pagination_with_flows(client: AsyncClient, logged_in_headers):
number_of_flows = 30
flows = [FlowCreate(name=f"Flow {i}", description="description", data={}) for i in range(number_of_flows)]
flow_ids = []
@ -116,7 +109,7 @@ async def test_read_flows_pagination_with_flows(client: TestClient, logged_in_he
assert len(response.json()["items"]) == 0
async def test_read_flows_custom_page_size(client: TestClient, logged_in_headers):
async def test_read_flows_custom_page_size(client: AsyncClient, logged_in_headers):
number_of_flows = 30
flows = [FlowCreate(name=f"Flow {i}", description="description", data={}) for i in range(number_of_flows)]
for flow in flows:
@ -134,7 +127,7 @@ async def test_read_flows_custom_page_size(client: TestClient, logged_in_headers
assert len(response.json()["items"]) == 15
async def test_read_flows_invalid_page(client: TestClient, logged_in_headers):
async def test_read_flows_invalid_page(client: AsyncClient, logged_in_headers):
number_of_flows = 30
flows = [FlowCreate(name=f"Flow {i}", description="description", data={}) for i in range(number_of_flows)]
flow_ids = []
@ -149,7 +142,7 @@ async def test_read_flows_invalid_page(client: TestClient, logged_in_headers):
assert response.status_code == 422 # Assuming 422 is the status code for invalid input
async def test_read_flows_invalid_size(client: TestClient, logged_in_headers):
async def test_read_flows_invalid_size(client: AsyncClient, logged_in_headers):
number_of_flows = 30
flows = [FlowCreate(name=f"Flow {i}", description="description", data={}) for i in range(number_of_flows)]
flow_ids = []
@ -164,7 +157,7 @@ async def test_read_flows_invalid_size(client: TestClient, logged_in_headers):
assert response.status_code == 422 # Assuming 422 is the status code for invalid input
async def test_read_flows_no_pagination_params(client: TestClient, logged_in_headers):
async def test_read_flows_no_pagination_params(client: AsyncClient, logged_in_headers):
number_of_flows = 30
flows = [FlowCreate(name=f"Flow {i}", description="description", data={}) for i in range(number_of_flows)]
for flow in flows:
@ -181,7 +174,7 @@ async def test_read_flows_no_pagination_params(client: TestClient, logged_in_hea
assert len(response.json()["items"]) == number_of_flows
async def test_read_flows_components_only_paginated(client: TestClient, logged_in_headers):
async def test_read_flows_components_only_paginated(client: AsyncClient, logged_in_headers):
number_of_flows = 10
flows = [
FlowCreate(name=f"Flow {i}", description="description", data={}, is_component=True)
@ -202,7 +195,7 @@ async def test_read_flows_components_only_paginated(client: TestClient, logged_i
assert all(flow["is_component"] is True for flow in response_json["items"])
async def test_read_flows_components_only(client: TestClient, logged_in_headers):
async def test_read_flows_components_only(client: AsyncClient, logged_in_headers):
number_of_flows = 10
flows = [
FlowCreate(name=f"Flow {i}", description="description", data={}, is_component=True)
@ -217,7 +210,7 @@ async def test_read_flows_components_only(client: TestClient, logged_in_headers)
assert all(flow["is_component"] is True for flow in response_json)
async def test_read_flow(client: TestClient, json_flow: str, logged_in_headers):
async def test_read_flow(client: AsyncClient, json_flow: str, logged_in_headers):
flow = orjson.loads(json_flow)
data = flow["data"]
unique_name = str(uuid4())
@ -234,7 +227,7 @@ async def test_read_flow(client: TestClient, json_flow: str, logged_in_headers):
@pytest.mark.usefixtures("active_user")
async def test_update_flow(client: TestClient, json_flow: str, logged_in_headers):
async def test_update_flow(client: AsyncClient, json_flow: str, logged_in_headers):
flow = orjson.loads(json_flow)
data = flow["data"]
@ -256,7 +249,7 @@ async def test_update_flow(client: TestClient, json_flow: str, logged_in_headers
@pytest.mark.usefixtures("active_user")
async def test_delete_flow(client: TestClient, json_flow: str, logged_in_headers):
async def test_delete_flow(client: AsyncClient, json_flow: str, logged_in_headers):
flow = orjson.loads(json_flow)
data = flow["data"]
flow = FlowCreate(name="Test Flow", description="description", data=data)
@ -268,7 +261,7 @@ async def test_delete_flow(client: TestClient, json_flow: str, logged_in_headers
@pytest.mark.usefixtures("active_user")
async def test_delete_flows(client: TestClient, logged_in_headers):
async def test_delete_flows(client: AsyncClient, logged_in_headers):
# Create ten flows
number_of_flows = 10
flows = [FlowCreate(name=f"Flow {i}", description="description", data={}) for i in range(number_of_flows)]
@ -285,7 +278,7 @@ async def test_delete_flows(client: TestClient, logged_in_headers):
@pytest.mark.asyncio
@pytest.mark.usefixtures("active_user")
async def test_delete_flows_with_transaction_and_build(client: TestClient, logged_in_headers):
async def test_delete_flows_with_transaction_and_build(client: AsyncClient, logged_in_headers):
# Create ten flows
number_of_flows = 10
flows = [FlowCreate(name=f"Flow {i}", description="description", data={}) for i in range(number_of_flows)]
@ -344,7 +337,7 @@ async def test_delete_flows_with_transaction_and_build(client: TestClient, logge
@pytest.mark.asyncio
@pytest.mark.usefixtures("active_user")
async def test_delete_folder_with_flows_with_transaction_and_build(client: TestClient, logged_in_headers):
async def test_delete_folder_with_flows_with_transaction_and_build(client: AsyncClient, logged_in_headers):
# Create a new folder
folder_name = f"Test Folder {uuid4()}"
folder = FolderCreate(name=folder_name, description="Test folder description", components_list=[], flows_list=[])
@ -411,7 +404,7 @@ async def test_delete_folder_with_flows_with_transaction_and_build(client: TestC
assert response.json() == {"vertex_builds": {}}
async def test_get_flows_from_folder_pagination(client: TestClient, logged_in_headers):
async def test_get_flows_from_folder_pagination(client: AsyncClient, logged_in_headers):
# Create a new folder
folder_name = f"Test Folder {uuid4()}"
folder = FolderCreate(name=folder_name, description="Test folder description", components_list=[], flows_list=[])
@ -435,7 +428,7 @@ async def test_get_flows_from_folder_pagination(client: TestClient, logged_in_he
assert len(response.json()["flows"]["items"]) == 0
async def test_get_flows_from_folder_pagination_with_params(client: TestClient, logged_in_headers):
async def test_get_flows_from_folder_pagination_with_params(client: AsyncClient, logged_in_headers):
# Create a new folder
folder_name = f"Test Folder {uuid4()}"
folder = FolderCreate(name=folder_name, description="Test folder description", components_list=[], flows_list=[])
@ -460,7 +453,7 @@ async def test_get_flows_from_folder_pagination_with_params(client: TestClient,
@pytest.mark.usefixtures("session")
async def test_create_flows(client: TestClient, json_flow: str, logged_in_headers):
async def test_create_flows(client: AsyncClient, json_flow: str, logged_in_headers):
flow = orjson.loads(json_flow)
data = flow["data"]
# Create test data
@ -488,7 +481,7 @@ async def test_create_flows(client: TestClient, json_flow: str, logged_in_header
@pytest.mark.usefixtures("session")
async def test_upload_file(client: TestClient, json_flow: str, logged_in_headers):
async def test_upload_file(client: AsyncClient, json_flow: str, logged_in_headers):
flow = orjson.loads(json_flow)
data = flow["data"]
# Create test data
@ -521,7 +514,7 @@ async def test_upload_file(client: TestClient, json_flow: str, logged_in_headers
@pytest.mark.usefixtures("session")
async def test_download_file(
client: TestClient,
client: AsyncClient,
json_flow,
active_user,
logged_in_headers,
@ -563,21 +556,21 @@ async def test_download_file(
@pytest.mark.usefixtures("active_user")
async def test_create_flow_with_invalid_data(client: TestClient, logged_in_headers):
async def test_create_flow_with_invalid_data(client: AsyncClient, logged_in_headers):
flow = {"name": "a" * 256, "data": "Invalid flow data"}
response = await client.post("api/v1/flows/", json=flow, headers=logged_in_headers)
assert response.status_code == 422
@pytest.mark.usefixtures("active_user")
async def test_get_nonexistent_flow(client: TestClient, logged_in_headers):
async def test_get_nonexistent_flow(client: AsyncClient, logged_in_headers):
uuid = uuid4()
response = await client.get(f"api/v1/flows/{uuid}", headers=logged_in_headers)
assert response.status_code == 404
@pytest.mark.usefixtures("active_user")
async def test_update_flow_idempotency(client: TestClient, json_flow: str, logged_in_headers):
async def test_update_flow_idempotency(client: AsyncClient, json_flow: str, logged_in_headers):
flow_data = orjson.loads(json_flow)
data = flow_data["data"]
flow_data = FlowCreate(name="Test Flow", description="description", data=data)
@ -590,7 +583,7 @@ async def test_update_flow_idempotency(client: TestClient, json_flow: str, logge
@pytest.mark.usefixtures("active_user")
async def test_update_nonexistent_flow(client: TestClient, json_flow: str, logged_in_headers):
async def test_update_nonexistent_flow(client: AsyncClient, json_flow: str, logged_in_headers):
flow_data = orjson.loads(json_flow)
data = flow_data["data"]
uuid = uuid4()
@ -604,34 +597,20 @@ async def test_update_nonexistent_flow(client: TestClient, json_flow: str, logge
@pytest.mark.usefixtures("active_user")
async def test_delete_nonexistent_flow(client: TestClient, logged_in_headers):
async def test_delete_nonexistent_flow(client: AsyncClient, logged_in_headers):
uuid = uuid4()
response = await client.delete(f"api/v1/flows/{uuid}", headers=logged_in_headers)
assert response.status_code == 404
@pytest.mark.usefixtures("active_user")
async def test_read_only_starter_projects(client: TestClient, logged_in_headers):
async def test_read_only_starter_projects(client: AsyncClient, logged_in_headers):
response = await client.get("api/v1/flows/basic_examples/", headers=logged_in_headers)
starter_projects = load_starter_projects()
assert response.status_code == 200
assert len(response.json()) == len(starter_projects)
@pytest.mark.load_flows
async def test_load_flows(client: TestClient):
response = await client.get("api/v1/flows/c54f9130-f2fa-4a3e-b22a-3856d946351b")
assert response.status_code == 200
assert response.json()["name"] == "BasicExample"
assert response.json()["folder_id"] is not None
# re-run to ensure updates work well
load_flows_from_directory()
response = await client.get("api/v1/flows/c54f9130-f2fa-4a3e-b22a-3856d946351b")
assert response.status_code == 200
assert response.json()["name"] == "BasicExample"
assert response.json()["folder_id"] is not None
def test_sqlite_pragmas():
db_service = get_db_service()
@ -643,7 +622,7 @@ def test_sqlite_pragmas():
@pytest.mark.usefixtures("active_user")
async def test_read_folder(client: TestClient, logged_in_headers):
async def test_read_folder(client: AsyncClient, logged_in_headers):
# Create a new folder
folder_name = f"Test Folder {uuid4()}"
folder = FolderCreate(name=folder_name, description="Test folder description")
@ -663,7 +642,7 @@ async def test_read_folder(client: TestClient, logged_in_headers):
@pytest.mark.usefixtures("active_user")
async def test_read_folder_with_pagination(client: TestClient, logged_in_headers):
async def test_read_folder_with_pagination(client: AsyncClient, logged_in_headers):
# Create a new folder
folder_name = f"Test Folder {uuid4()}"
folder = FolderCreate(name=folder_name, description="Test folder description")
@ -689,7 +668,7 @@ async def test_read_folder_with_pagination(client: TestClient, logged_in_headers
@pytest.mark.usefixtures("active_user")
async def test_read_folder_with_flows(client: TestClient, json_flow: str, logged_in_headers):
async def test_read_folder_with_flows(client: AsyncClient, json_flow: str, logged_in_headers):
# Create a new folder
folder_name = f"Test Folder {uuid4()}"
flow_name = f"Test Flow {uuid4()}"
@ -718,7 +697,7 @@ async def test_read_folder_with_flows(client: TestClient, json_flow: str, logged
@pytest.mark.usefixtures("active_user")
async def test_read_nonexistent_folder(client: TestClient, logged_in_headers):
async def test_read_nonexistent_folder(client: AsyncClient, logged_in_headers):
nonexistent_id = str(uuid4())
response = await client.get(f"api/v1/folders/{nonexistent_id}", headers=logged_in_headers)
assert response.status_code == 404
@ -726,7 +705,7 @@ async def test_read_nonexistent_folder(client: TestClient, logged_in_headers):
@pytest.mark.usefixtures("active_user")
async def test_read_folder_with_search(client: TestClient, json_flow: str, logged_in_headers):
async def test_read_folder_with_search(client: AsyncClient, json_flow: str, logged_in_headers):
# Create a new folder
folder_name = f"Test Folder {uuid4()}"
folder = FolderCreate(name=folder_name, description="Test folder description")
@ -762,7 +741,7 @@ async def test_read_folder_with_search(client: TestClient, json_flow: str, logge
@pytest.mark.usefixtures("active_user")
async def test_read_folder_with_component_filter(client: TestClient, json_flow: str, logged_in_headers):
async def test_read_folder_with_component_filter(client: AsyncClient, json_flow: str, logged_in_headers):
# Create a new folder
folder_name = f"Test Folder {uuid4()}"
folder = FolderCreate(name=folder_name, description="Test folder description")