langflow/src/backend/tests/unit/test_webhook.py
Gabriel Luiz Freitas Almeida 9c23759c7d
refactor: add graph utility tests and refactor sorting methods (#5538)
* refactor: turn sorting methods into functions in a separate module

- Added `layered_topological_sort` function to perform layered topological sorting of graph vertices, accommodating cycles and input vertex checks.
- Introduced `refine_layers` function to ensure proper dependency ordering among vertices.
- Implemented helper functions for sorting layers by dependency and filtering vertices based on predecessors.
- Enhanced utility functions to support better graph traversal and layer management.

This update improves the graph processing capabilities, allowing for more efficient handling of complex graph structures.

* feat(tests): enhance graph utility tests with cycle detection and sorting functionality

- Added a new fixture `graph_with_loop` to simulate a graph containing cycles for testing purposes.
- Improved the `test_large_graph_efficiency` to validate cycle detection in large graphs.
- Introduced multiple tests for sorting vertices in graphs with cycles, ensuring correct order and handling of input vertices.
- Enhanced assertions to provide clearer error messages for failed tests, improving debugging experience.

These changes strengthen the testing framework for graph utilities, ensuring robust handling of complex graph structures.

* refactor(graph): remove unused parent_node_map from Graph class initialization

- Eliminated the `parent_node_map` parameter from the Graph class constructor, streamlining the graph initialization process.
- This change enhances code clarity and reduces unnecessary complexity in graph management.

This update contributes to cleaner and more maintainable graph-related code.

* refactor(graph): optimize dependency sorting and vertex filtering

- Improved the `_max_dependency_index` function by utilizing `index_map.get()` for cleaner code and better handling of missing successors.
- Enhanced the `_sort_single_layer_by_dependency` function with a caching mechanism to avoid redundant calculations, improving performance during vertex sorting.
- Updated `filter_vertices_up_to_vertex` to use a set for `vertices_ids`, optimizing membership checks and enhancing efficiency in vertex filtering.

These changes contribute to more efficient graph processing and improved code readability.

* chore: remove unused 'parent_node_map' parameter

* [autofix.ci] apply automated fixes

* fix: replace old method call with a new func

* test: enhance assertions for file existence in webhook tests

* refactor(graph): enhance component ID retrieval and chat input sorting

- Updated `find_start_component_id` to accept an optional `is_webhook` parameter, allowing for dynamic priority input selection based on the flow type.
- Improved `sort_chat_inputs_first` to handle chat input positioning more efficiently, ensuring only one chat input exists and adjusting its position within the layers as needed.
- These changes enhance the flexibility and efficiency of graph processing, particularly for webhook flows.

* test(graph): update assertions in sort_chat_inputs_first test for accuracy

- Modified assertions in the `test_chat_inputs_at_start` function to reflect the correct expected output of the `sort_chat_inputs_first` utility.
- Adjusted the expected length and order of the result to ensure accurate validation of chat input sorting functionality.

These changes enhance the reliability of the test suite for graph utilities, ensuring that the sorting logic is correctly validated.

* test(chat): update assertion in consume_and_assert_stream for accurate ID validation

- Modified the assertion in the `consume_and_assert_stream` function to include an additional expected ID, ensuring the test accurately reflects the current output of the chat endpoint.
- This change enhances the reliability of the test suite by validating the correct behavior of the chat input sorting functionality.

* test(endpoints): update assertion in test_get_vertices for accurate ID validation

- Modified the assertion in the `test_get_vertices` function to include an additional expected ID, "Webhook", alongside "ChatInput".
- This change ensures the test accurately reflects the current output of the endpoint, enhancing the reliability of the test suite for endpoint functionality.

---------

Co-authored-by: italojohnny <italojohnnydosanjos@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-01-09 20:15:24 +00:00

58 lines
2.3 KiB
Python

import aiofiles
import anyio
import pytest
@pytest.fixture(autouse=True)
def _check_openai_api_key_in_environment_variables():
pass
async def test_webhook_endpoint(client, added_webhook_test):
# The test is as follows:
# 1. The flow when run will get a "path" from the payload and save a file with the path as the name.
# We will create a temporary file path and send it to the webhook endpoint, then check if the file exists.
# 2. we will delete the file, then send an invalid payload to the webhook endpoint and check if the file exists.
endpoint_name = added_webhook_test["endpoint_name"]
endpoint = f"api/v1/webhook/{endpoint_name}"
# Create a temporary file
async with aiofiles.tempfile.TemporaryDirectory() as tmp:
file_path = anyio.Path(tmp) / "test_file.txt"
payload = {"path": str(file_path)}
response = await client.post(endpoint, json=payload)
assert response.status_code == 202
# Wait a few seconds for the file to be created
assert await file_path.exists(), f"File {file_path} does not exist"
file_does_not_exist = not await file_path.exists()
assert file_does_not_exist, f"File {file_path} still exists"
# Send an invalid payload
payload = {"invalid_key": "invalid_value"}
response = await client.post(endpoint, json=payload)
assert response.status_code == 202
assert not await file_path.exists(), f"File {file_path} should not exist"
async def test_webhook_flow_on_run_endpoint(client, added_webhook_test, created_api_key):
endpoint_name = added_webhook_test["endpoint_name"]
endpoint = f"api/v1/run/{endpoint_name}?stream=false"
# Just test that "Random Payload" returns 202
# returns 202
payload = {
"output_type": "any",
}
response = await client.post(endpoint, headers={"x-api-key": created_api_key.api_key}, json=payload)
assert response.status_code == 200, response.json()
async def test_webhook_with_random_payload(client, added_webhook_test):
endpoint_name = added_webhook_test["endpoint_name"]
endpoint = f"api/v1/webhook/{endpoint_name}"
# Just test that "Random Payload" returns 202
response = await client.post(
endpoint,
json="Random Payload",
)
assert response.status_code == 202