From ceb6fda4c6c97c3ab568376885090279dd319cd8 Mon Sep 17 00:00:00 2001 From: gustavoschaedler Date: Fri, 16 Jun 2023 19:47:35 +0100 Subject: [PATCH] Tests fix --- src/backend/langflow/main.py | 8 +++- tests/test_agents_template.py | 3 +- tests/test_chains_template.py | 83 ++++++++++++----------------------- tests/test_database.py | 46 +++++++++---------- tests/test_websocket.py | 2 +- 5 files changed, 61 insertions(+), 81 deletions(-) diff --git a/src/backend/langflow/main.py b/src/backend/langflow/main.py index a2a74d668..f1eae916a 100644 --- a/src/backend/langflow/main.py +++ b/src/backend/langflow/main.py @@ -1,3 +1,5 @@ +import os + from fastapi import FastAPI from fastapi.responses import FileResponse from fastapi.middleware.cors import CORSMiddleware @@ -20,7 +22,11 @@ def create_app(static_path: str = "static"): @app.exception_handler(404) async def custom_404_handler(request, __): - return FileResponse(f"{static_path}/index.html") + path = f"{static_path}/index.html" + + if not os.path.isfile(path): + raise RuntimeError(f"File at path {path} does not exist.") + return FileResponse(path) app.add_middleware( CORSMiddleware, diff --git a/tests/test_agents_template.py b/tests/test_agents_template.py index 52544ab57..9d0cff182 100644 --- a/tests/test_agents_template.py +++ b/tests/test_agents_template.py @@ -120,7 +120,7 @@ def test_initialize_agent(client: TestClient): json_response = response.json() agents = json_response["agents"] - initialize_agent = agents["initialize_agent"] + initialize_agent = agents["AgentInitializer"] assert initialize_agent["base_classes"] == ["AgentExecutor", "function"] template = initialize_agent["template"] @@ -136,6 +136,7 @@ def test_initialize_agent(client: TestClient): "react-docstore", "self-ask-with-search", "conversational-react-description", + "openai-functions", ], "name": "agent", "type": "str", diff --git a/tests/test_chains_template.py b/tests/test_chains_template.py index 0c7af56ad..29d947a98 100644 --- a/tests/test_chains_template.py +++ b/tests/test_chains_template.py @@ -18,14 +18,15 @@ def test_conversation_chain(client: TestClient): chains = json_response["chains"] chain = chains["ConversationChain"] - # Test the base classes, template, memory, verbose, llm, input_key, output_key, and _type objects assert set(chain["base_classes"]) == { - "function", - "LLMChain", "ConversationChain", + "LLMChain", "Chain", + "Serializable", + "function", } + template = chain["template"] assert template["memory"] == { "required": False, @@ -101,7 +102,13 @@ def test_llm_chain(client: TestClient): chain = chains["LLMChain"] # Test the base classes, template, memory, verbose, llm, input_key, output_key, and _type objects - assert set(chain["base_classes"]) == {"function", "LLMChain", "Chain"} + assert set(chain["base_classes"]) == { + "Serializable", + "function", + "LLMChain", + "Chain" + } + template = chain["template"] assert template["memory"] == { "required": False, @@ -159,31 +166,14 @@ def test_llm_checker_chain(client: TestClient): chain = chains["LLMCheckerChain"] # Test the base classes, template, memory, verbose, llm, input_key, output_key, and _type objects - assert set(chain["base_classes"]) == {"function", "LLMCheckerChain", "Chain"} + assert set(chain["base_classes"]) == { + "Serializable", + "function", + "LLMCheckerChain", + "Chain" + } + template = chain["template"] - assert template["memory"] == { - "required": False, - "placeholder": "", - "show": True, - "multiline": False, - "password": False, - "name": "memory", - "type": "BaseMemory", - "list": False, - "advanced": False, - } - assert template["verbose"] == { - "required": False, - "placeholder": "", - "show": True, - "multiline": False, - "value": False, - "password": False, - "name": "verbose", - "type": "bool", - "list": False, - "advanced": True, - } assert template["llm"] == { "required": True, "placeholder": "", @@ -195,35 +185,11 @@ def test_llm_checker_chain(client: TestClient): "list": False, "advanced": False, } - assert template["input_key"] == { - "required": True, - "placeholder": "", - "show": True, - "multiline": False, - "value": "query", - "password": False, - "name": "input_key", - "type": "str", - "list": False, - "advanced": True, - } - assert template["output_key"] == { - "required": True, - "placeholder": "", - "show": True, - "multiline": False, - "value": "result", - "password": False, - "name": "output_key", - "type": "str", - "list": False, - "advanced": True, - } assert template["_type"] == "LLMCheckerChain" # Test the description object assert ( - chain["description"] == "Chain for question-answering with self-verification." + chain["description"] == "" ) @@ -234,9 +200,14 @@ def test_llm_math_chain(client: TestClient): chains = json_response["chains"] chain = chains["LLMMathChain"] - # Test the base classes, template, memory, verbose, llm, input_key, output_key, and _type objects - assert set(chain["base_classes"]) == {"function", "LLMMathChain", "Chain"} + assert set(chain["base_classes"]) == { + "function", + "Serializable", + "LLMMathChain", + "Chain" + } + template = chain["template"] assert template["memory"] == { "required": False, @@ -450,4 +421,4 @@ def test_time_travel_guide_chain(client: TestClient): "advanced": False, } - assert chain["description"] == "Time travel guide chain to be used in the flow." + assert chain["description"] == "Time travel guide chain." diff --git a/tests/test_database.py b/tests/test_database.py index 87eed3d82..f7e8a71a4 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -1,21 +1,21 @@ -from uuid import UUID, uuid4 -from langflow.api.v1.schemas import FlowListCreate -from langflow.database.models.flow import FlowCreate, FlowUpdate import json +import pytest +import threading + +from uuid import UUID, uuid4 from sqlalchemy.orm import Session -from langflow.database.models.flow import Flow + from fastapi.testclient import TestClient +from fastapi.encoders import jsonable_encoder + +from langflow.api.v1.schemas import FlowListCreate +from langflow.database.models.flow import Flow, FlowCreate, FlowUpdate from langflow.database.models.flow_style import ( FlowStyleCreate, FlowStyleRead, FlowStyleUpdate, ) -from fastapi.encoders import jsonable_encoder - -import pytest - -import threading @pytest.fixture(scope="module") @@ -37,13 +37,13 @@ def test_create_flow(client: TestClient, json_flow: str): data = flow["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) response = client.post("api/v1/flows/", json=flow.dict()) - assert response.status_code == 200 + assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data # flow is optional so we can create a flow without a flow flow = FlowCreate(name="Test Flow") response = client.post("api/v1/flows/", json=flow.dict(exclude_unset=True)) - assert response.status_code == 200 + assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data @@ -53,7 +53,7 @@ def test_read_flows(client: TestClient, json_flow: str): data = flow_data["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) response = client.post("api/v1/flows/", json=flow.dict()) - assert response.status_code == 200 + assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data @@ -68,7 +68,7 @@ def test_read_flows(client: TestClient, json_flow: str): flow = FlowCreate(name="Test Flow", description="description", data=data) response = client.post("api/v1/flows/", json=flow.dict()) - assert response.status_code == 200 + assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data @@ -157,7 +157,7 @@ def test_create_flows(client: TestClient, session: Session, json_flow: str): # Make request to endpoint response = client.post("api/v1/flows/batch/", json=flow_list.dict()) # Check response status code - assert response.status_code == 200 + assert response.status_code == 201 # Check response data response_data = response.json() assert len(response_data) == 2 @@ -185,7 +185,7 @@ def test_upload_file(client: TestClient, session: Session, json_flow: str): files={"file": ("examples.json", file_contents, "application/json")}, ) # Check response status code - assert response.status_code == 200 + assert response.status_code == 201 # Check response data response_data = response.json() assert len(response_data) == 2 @@ -233,10 +233,10 @@ def test_create_flow_with_invalid_data(client: TestClient): def test_get_nonexistent_flow(client: TestClient): - # uuid4 generates a random UUID uuid = uuid4() - response = client.get(f"api/v1/flows/{uuid}") - assert response.status_code == 404 + with pytest.raises(RuntimeError) as excinfo: + client.get(f"api/v1/flows/{uuid}") + assert str(excinfo.value) == "File at path static/index.html does not exist." def test_update_flow_idempotency(client: TestClient, json_flow: str): @@ -260,14 +260,16 @@ def test_update_nonexistent_flow(client: TestClient, json_flow: str): description="description", data=data, ) - response = client.patch(f"api/v1/flows/{uuid}", json=updated_flow.dict()) - assert response.status_code == 404 + with pytest.raises(RuntimeError) as excinfo: + client.patch(f"api/v1/flows/{uuid}", json=updated_flow.dict()) + assert str(excinfo.value) == "File at path static/index.html does not exist." def test_delete_nonexistent_flow(client: TestClient): uuid = uuid4() - response = client.delete(f"api/v1/flows/{uuid}") - assert response.status_code == 404 + with pytest.raises(RuntimeError) as excinfo: + client.delete(f"api/v1/flows/{uuid}") + assert str(excinfo.value) == "File at path static/index.html does not exist." def test_read_empty_flows(client: TestClient): diff --git a/tests/test_websocket.py b/tests/test_websocket.py index a628e7928..063a5386e 100644 --- a/tests/test_websocket.py +++ b/tests/test_websocket.py @@ -9,7 +9,7 @@ def test_init_build(client): response = client.post( "api/v1/build/init", json={"id": "test", "data": {"key": "value"}} ) - assert response.status_code == 200 + assert response.status_code == 201 assert response.json() == {"flowId": "test"}