🚨 test(conftest.py): add basic_graph_data fixture to load basic example data

🚨 test(test_websocket.py): add tests for build init, stream, and websocket endpoint
The `basic_graph_data` fixture is added to load the basic example data. The `test_init_build`, `test_stream_build`, and `test_websocket_endpoint` tests are added to test the build init, stream, and websocket endpoint. The `test_websocket_endpoint_after_build` test is added to test the websocket endpoint after the build.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-12 13:06:39 -03:00
commit a7c9b04611
2 changed files with 51 additions and 39 deletions

View file

@ -63,6 +63,12 @@ def get_graph(_type="basic"):
return Graph(nodes, edges) return Graph(nodes, edges)
@pytest.fixture
def basic_graph_data():
with open(pytest.BASIC_EXAMPLE_PATH, "r") as f:
return json.load(f)
@pytest.fixture @pytest.fixture
def basic_graph(): def basic_graph():
return get_graph() return get_graph()

View file

@ -1,46 +1,52 @@
from fastapi.testclient import TestClient import json
from fastapi import WebSocketDisconnect, WebSocketException
from langflow.graph.graph.base import Graph
from langflow.api.v1.chat import chat_manager
# from langflow.chat.manager import ChatManager
from langflow.utils.logger import logger
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
def test_websocket_connection(client: TestClient): def test_init_build(client):
with client.websocket_connect("api/v1/chat/test_client") as websocket: response = client.post(
assert websocket.scope["client"] == ["testclient", 50000] "api/v1/build/init", json={"id": "test", "data": {"key": "value"}}
assert websocket.scope["path"] == "/api/v1/chat/test_client" )
assert response.status_code == 200
assert response.json() == {"flowId": "test"}
# This does not work anymore because now we require def test_stream_build(client):
# the flow to be built before sending messages client.post("/build/init", json={"id": "stream_test", "data": {"key": "value"}})
# def test_chat_history(client: TestClient):
# # Mock the process_graph function to return a specific value
# with patch("langflow.chat.manager.process_graph") as mock_process_graph:
# mock_process_graph.return_value = ("Hello, I'm a mock response!", "")
# with client.websocket_connect("api/v1/chat/test_client") as websocket: # Test the stream
# # First message should be the history response = client.get("api/v1/build/stream/stream_test")
# history = websocket.receive_json() assert response.status_code == 200
# assert history == [] # Empty history assert response.headers["content-type"] == "text/event-stream; charset=utf-8"
# # Send a message
# payload = {"message": "Hello"}
# websocket.send_json(json.dumps(payload))
# # Receive the response from the server
# response = websocket.receive_json()
# assert response == {
# "is_bot": True,
# "message": None,
# "type": "start",
# "intermediate_steps": "",
# "files": [],
# }
# # Send another message
# payload = {"message": "How are you?"}
# websocket.send_json(json.dumps(payload))
# # Receive the response from the server def test_websocket_endpoint(client):
# response = websocket.receive_json() with pytest.raises(WebSocketDisconnect):
# assert response == { with client.websocket_connect(
# "is_bot": True, "api/v1/chat/non_existing_client_id"
# "message": "Hello, I'm a mock response!", ) as websocket:
# "type": "end", websocket.send_json({"type": "test"})
# "intermediate_steps": "", data = websocket.receive_json()
# "files": [], assert "Please, build the flow before sending messages" in data["message"]
# }
def test_websocket_endpoint_after_build(client, basic_graph_data):
# Assuming your websocket_endpoint uses chat_manager which caches data from stream_build
client.post("/build/init", json=basic_graph_data)
client.get("/build/stream/websocket_test")
# There should be more to test here, but it depends on the inner workings of your websocket handler
# and how your chat_manager and other classes behave. The following is just an example structure.
with pytest.raises(WebSocketDisconnect):
with client.websocket_connect("api/v1/chat/websocket_test") as websocket:
websocket.send_json({"type": "test"})
# Perform assertions here, based on what you expect the websocket to return
# data = websocket.receive_json()
# assert ...