From 1f720a665eee86d5e6aa70d0e85a6730bd78f261 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 22 Sep 2023 11:03:54 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix(test=5Fendpoints.py):=20fix?= =?UTF-8?q?=20typo=20in=20test=5Fprocess=5Fflow=5Fwithout=5Fautologin=20fu?= =?UTF-8?q?nction=20to=20improve=20code=20readability=20=E2=9C=A8=20feat(t?= =?UTF-8?q?est=5Fendpoints.py):=20add=20helper=20functions=20run=5Fpost=20?= =?UTF-8?q?and=20poll=5Ftask=5Fstatus=20to=20improve=20code=20modularity?= =?UTF-8?q?=20and=20reusability=20=F0=9F=94=A7=20fix(test=5Fendpoints.py):?= =?UTF-8?q?=20fix=20typo=20in=20test=5Fbasic=5Fchat=5Fwith=5Ftwo=5Fsession?= =?UTF-8?q?=5Fids=5Fand=5Fnames=20function=20to=20improve=20code=20readabi?= =?UTF-8?q?lity=20=E2=9C=A8=20feat(test=5Fendpoints.py):=20add=20async=5Ft?= =?UTF-8?q?est=20marker=20to=20test=5Fvector=5Fstore=5Fin=5Fprocess=20func?= =?UTF-8?q?tion=20to=20indicate=20it=20is=20an=20asynchronous=20test=20?= =?UTF-8?q?=E2=9C=A8=20feat(test=5Fendpoints.py):=20add=20distributed=5Fcl?= =?UTF-8?q?ient=20parameter=20to=20test=5Fvector=5Fstore=5Fin=5Fprocess=20?= =?UTF-8?q?function=20to=20test=20distributed=20client=20functionality=20?= =?UTF-8?q?=E2=9C=A8=20feat(test=5Fendpoints.py):=20add=20async=5Ftest=20m?= =?UTF-8?q?arker=20to=20test=5Fasync=5Ftask=5Fprocessing=20function=20to?= =?UTF-8?q?=20indicate=20it=20is=20an=20asynchronous=20test=20=E2=9C=A8=20?= =?UTF-8?q?feat(test=5Fendpoints.py):=20add=20distributed=5Fclient=20param?= =?UTF-8?q?eter=20to=20test=5Fasync=5Ftask=5Fprocessing=20function=20to=20?= =?UTF-8?q?test=20distributed=20client=20functionality=20=E2=9C=A8=20feat(?= =?UTF-8?q?test=5Fendpoints.py):=20add=20async=5Ftest=20marker=20to=20test?= =?UTF-8?q?=5Fasync=5Ftask=5Fprocessing=5Fvector=5Fstore=20function=20to?= =?UTF-8?q?=20indicate=20it=20is=20an=20asynchronous=20test=20=E2=9C=A8=20?= =?UTF-8?q?feat(test=5Fendpoints.py):=20add=20distributed=5Fclient=20param?= =?UTF-8?q?eter=20to=20test=5Fasync=5Ftask=5Fprocessing=5Fvector=5Fstore?= =?UTF-8?q?=20function=20to=20test=20distributed=20client=20functionality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_endpoints.py | 47 +++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index 8f16e11b5..12d2340a2 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -8,7 +8,33 @@ from fastapi.testclient import TestClient from langflow.interface.tools.constants import CUSTOM_TOOLS from langflow.template.frontend_node.chains import TimeTravelGuideChainNode -from tests.utils import poll_task_status, run_post +import time + + +def run_post(client, flow_id, headers, post_data): + response = client.post( + f"api/v1/process/{flow_id}", + headers=headers, + json=post_data, + ) + assert response.status_code == 200, response.json() + return response.json() + + +# Helper function to poll task status +def poll_task_status(client, headers, task_id, max_attempts=20, sleep_time=1): + for _ in range(max_attempts): + task_status_response = client.get( + f"api/v1/task/{task_id}/status", + headers=headers, + ) + if ( + task_status_response.status_code == 200 + and task_status_response.json()["status"] == "SUCCESS" + ): + return task_status_response.json() + time.sleep(sleep_time) + return None # Return None if task did not complete in time PROMPT_REQUEST = { @@ -187,7 +213,7 @@ def test_process_flow_without_autologin(client, flow, monkeypatch, created_api_k # Dummy POST data post_data = { - "inputs": {"key": "value"}, + "inputs": {"input": "value"}, "tweaks": None, "clear_cache": False, "session_id": None, @@ -451,11 +477,14 @@ def test_basic_chat_with_two_session_ids_and_names(client, added_flow, created_a assert name in response_json["result"]["text"] -def test_vector_store_in_process(client, added_vector_store, created_api_key): +@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 = client.post( + response = distributed_client.post( f"api/v1/process/{added_vector_store.get('id')}", headers=headers, json=post_data, @@ -470,12 +499,12 @@ def test_vector_store_in_process(client, added_vector_store, created_api_key): # Test function without loop @pytest.mark.async_test -def test_async_task_processing(client, added_flow, created_api_key): +def test_async_task_processing(distributed_client, added_flow, created_api_key): headers = {"x-api-key": created_api_key.api_key} post_data = {"inputs": {"text": "Hi, My name is Gabriel"}} # Run the /api/v1/process/{flow_id} endpoint with sync=False - response = client.post( + response = distributed_client.post( f"api/v1/process/{added_flow.get('id')}", headers=headers, json={**post_data, "sync": False}, @@ -487,7 +516,7 @@ def test_async_task_processing(client, added_flow, created_api_key): assert task_id is not None # Polling the task status using the helper function - task_status_json = poll_task_status(client, headers, task_id) + task_status_json = poll_task_status(distributed_client, headers, task_id) 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 @@ -502,7 +531,7 @@ 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": "What is Langflow?"}} + post_data = {"inputs": {"input": "How do I upload examples?"}} # Run the /api/v1/process/{flow_id} endpoint with sync=False response = client.post( @@ -525,6 +554,6 @@ def test_async_task_processing_vector_store( # 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 is" in task_status_json["result"]["output"], task_status_json[ + assert "Langflow" in task_status_json["result"]["output"], task_status_json[ "result" ]