Add first api tests

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-03-08 14:29:17 -03:00
commit b2c3b3b6d2
2 changed files with 117 additions and 244 deletions

View file

@ -1,6 +1,7 @@
import time
import pytest
from fastapi import status
from fastapi.testclient import TestClient
from langflow.interface.custom.directory_reader.directory_reader import DirectoryReader
@ -467,232 +468,42 @@ def test_build_vertex_invalid_vertex_id(
assert response.status_code == 500
# TODO: Add flow with new components
# def test_build_all_vertices_in_sequence_with_chat_input(
# client, added_flow_chat_input, logged_in_headers
# ):
# flow_id = added_flow_chat_input["id"]
# # First, get all the vertices in the correct sequence
# response = client.get(
# f"/api/v1/build/{flow_id}/vertices", headers=logged_in_headers
# )
# assert response.status_code == 200
# assert "ids" in response.json()
# vertex_ids = response.json()["ids"]
# # Now, iterate through each vertex and build it
# for layer in vertex_ids:
# for vertex_id in layer:
# response = client.post(
# f"/api/v1/build/{flow_id}/vertices/{vertex_id}",
# headers=logged_in_headers,
# )
# json_response = response.json()
# assert (
# response.status_code == 200
# ), f"Failed at vertex {vertex_id}: {json_response}"
# assert "valid" in json_response
# assert json_response["valid"], json_response["params"]
def test_successful_run(client, starter_project, created_api_key):
headers = {"x-api-key": created_api_key.api_key}
flow_id = starter_project["id"]
response = client.post(f"/api/v1/run/{flow_id}", headers=headers)
assert response.status_code == status.HTTP_200_OK, response.text
# Add more assertions here to validate the response content
json_response = response.json()
assert "outputs" in json_response
outer_outputs = json_response["outputs"]
assert len(outer_outputs) == 1
outputs = outer_outputs[0]
assert len(outputs) == 2
keys = ["results", "artifacts", "messages"]
for output in outputs:
assert all(key in output for key in keys)
output = outputs[0]
result = output["results"]["result"]
assert result == "Write a press release \n\n- Cars\n- Bottle\n\n\nAnswer:\n\n"
assert "session_id" in json_response
# def test_build_all_vertices_in_sequence_with_two_outputs(
# client, added_flow_two_outputs, logged_in_headers
# ):
# """This tests the case where a node has two outputs, one of which is Text and the other (in this case) is
# a LLMChain. We need to make sure the correct output is passed in both cases."""
# flow_id = added_flow_two_outputs["id"]
# # First, get all the vertices in the correct sequence
# response = client.get(
# f"/api/v1/build/{flow_id}/vertices", headers=logged_in_headers
# )
# assert response.status_code == 200
# assert "ids" in response.json()
# vertex_ids = response.json()["ids"]
# # Now, iterate through each vertex and build it
# for vertex_id in vertex_ids:
# response = client.post(
# f"/api/v1/build/{flow_id}/vertices/{vertex_id}", headers=logged_in_headers
# )
# json_response = response.json()
# assert (
# response.status_code == 200
# ), f"Failed at vertex {vertex_id}: {json_response}"
# assert "valid" in json_response
# assert json_response["valid"], json_response["params"]
def test_run_with_inputs_and_outputs(client, starter_project, created_api_key):
headers = {"x-api-key": created_api_key.api_key}
flow_id = starter_project["id"]
payload = {
"inputs": [{"components": ["component1"], "input_value": "value1"}],
"outputs": ["Component Name", "component_id"],
}
response = client.post(f"/api/v1/run/{flow_id}", json=payload, headers=headers)
assert response.status_code == status.HTTP_200_OK
# Validate the response structure and content
# def test_basic_chat_in_process(client, flow, created_api_key):
# # Run the /api/v1/process/{flow_id} endpoint
# headers = {"x-api-key": created_api_key.api_key}
# post_data = {"inputs": {"text": "Hi, My name is Gabriel"}}
# response = client.post(
# f"api/v1/process/{flow.get('id')}",
# headers=headers,
# json=post_data,
# )
# assert response.status_code == 200, response.json()
# # Check the response
# assert "Gabriel" in response.json()["result"]["text"]
# # session_id should be returned
# assert "session_id" in response.json()
# assert response.json()["session_id"] is not None
# # New request with the same session_id
# # asking "What is my name?" should return "Gabriel"
# post_data = {
# "inputs": {"text": "What is my name?"},
# "session_id": response.json()["session_id"],
# }
# response = client.post(
# f"api/v1/process/{flow.get('id')}",
# headers=headers,
# json=post_data,
# )
# assert response.status_code == 200, response.json()
# assert "Gabriel" in response.json()["result"]["text"]
# def test_basic_chat_different_session_ids(client, flow, created_api_key):
# # Run the /api/v1/process/{flow_id} endpoint
# headers = {"x-api-key": created_api_key.api_key}
# post_data = {"inputs": {"text": "Hi, My name is Gabriel"}}
# response = client.post(
# f"api/v1/process/{flow.get('id')}",
# headers=headers,
# json=post_data,
# )
# assert response.status_code == 200, response.json()
# # Check the response
# assert "Gabriel" in response.json()["result"]["text"]
# # session_id should be returned
# assert "session_id" in response.json()
# assert response.json()["session_id"] is not None
# session_id1 = response.json()["session_id"]
# # New request with a different session_id
# # asking "What is my name?" should return "Gabriel"
# post_data = {
# "inputs": {"text": "What is my name?"},
# }
# response = client.post(
# f"api/v1/process/{flow.get('id')}",
# headers=headers,
# json=post_data,
# )
# assert response.status_code == 200, response.json()
# assert "Gabriel" not in response.json()["result"]["text"]
# assert session_id1 != response.json()["session_id"]
# def test_basic_chat_with_two_session_ids_and_names(client, flow, created_api_key):
# headers = {"x-api-key": created_api_key.api_key}
# flow_id = flow.get("id")
# names = ["Gabriel", "John"]
# session_ids = []
# for name in names:
# post_data = {"inputs": {"text": f"Hi, My name is {name}"}}
# response_json = run_post(client, flow_id, headers, post_data)
# assert name in response_json["result"]["text"]
# assert "session_id" in response_json
# assert response_json["session_id"] is not None
# session_ids.append(response_json["session_id"])
# for i, name in enumerate(names):
# post_data = {
# "inputs": {"text": "What is my name?"},
# "session_id": session_ids[i],
# }
# response_json = run_post(client, flow_id, headers, post_data)
# assert name in response_json["result"]["text"]
# @pytest.mark.async_test
# def test_vector_store_in_process(
# distributed_client, added_vector_store, created_api_key
# ):
# # Run the /api/v1/process/{flow_id} endpoint
# headers = {"x-api-key": created_api_key.api_key}
# post_data = {"inputs": {"input": "What is Langflow?"}}
# response = distributed_client.post(
# f"api/v1/process/{added_vector_store.get('id')}",
# headers=headers,
# json=post_data,
# )
# assert response.status_code == 200, response.json()
# # Check the response
# assert "Langflow" in response.json()["result"]["output"]
# # session_id should be returned
# assert "session_id" in response.json()
# assert response.json()["session_id"] is not None
# Test function without loop
# @pytest.mark.async_test
# def test_async_task_processing(distributed_client, flow, created_api_key):
# headers = {"x-api-key": created_api_key.api_key}
# post_data = {"inputs": {"text": "Hi, My name is Gabriel"}}
# flow = flow.model_dump()
# # Run the /api/v1/process/{flow_id} endpoint with sync=False
# response = distributed_client.post(
# f"api/v1/process/{flow.get('id')}",
# headers=headers,
# json={**post_data, "sync": False},
# )
# assert response.status_code == 200, response.json()
# # Extract the task ID from the response
# task = response.json().get("task")
# task_id = task.get("id")
# task_href = task.get("href")
# assert task_id is not None
# assert task_href is not None
# assert task_href == f"api/v1/task/{task_id}"
# # Polling the task status using the helper function
# task_status_json = poll_task_status(distributed_client, headers, task_href)
# assert task_status_json is not None, "Task did not complete in time"
# # Validate that the task completed successfully and the result is as expected
# assert "result" in task_status_json, task_status_json
# assert "text" in task_status_json["result"], task_status_json["result"]
# assert "Gabriel" in task_status_json["result"]["text"], task_status_json["result"]
# ! Deactivating this until updating the test
# Test function without loop
# @pytest.mark.async_test
# def test_async_task_processing_vector_store(client, added_vector_store, created_api_key):
# headers = {"x-api-key": created_api_key.api_key}
# post_data = {"inputs": {"input": "How do I upload examples?"}}
# # Run the /api/v1/process/{flow_id} endpoint with sync=False
# response = client.post(
# f"api/v1/process/{added_vector_store.get('id')}",
# headers=headers,
# json={**post_data, "sync": False},
# )
# assert response.status_code == 200, response.json()
# assert "result" in response.json()
# assert "FAILURE" not in response.json()["result"]
# # Extract the task ID from the response
# task = response.json().get("task")
# task_id = task.get("id")
# task_href = task.get("href")
# assert task_id is not None
# assert task_href is not None
# assert task_href == f"api/v1/task/{task_id}"
# # Polling the task status using the helper function
# task_status_json = poll_task_status(client, headers, task_href)
# assert task_status_json is not None, "Task did not complete in time"
# # Validate that the task completed successfully and the result is as expected
# assert "result" in task_status_json, task_status_json
# assert "output" in task_status_json["result"], task_status_json["result"]
# assert "Langflow" in task_status_json["result"]["output"], task_status_json["result"]
def test_invalid_flow_id(client, created_api_key):
headers = {"x-api-key": created_api_key.api_key}
flow_id = "invalid-flow-id"
response = client.post(f"/api/v1/run/{flow_id}", headers=headers)
assert response.status_code == status.HTTP_404_NOT_FOUND
# Check if the error detail is as expected