feat: Add unit tests for run_flow_from_json with fake environment variables (#4015)

* Add tests for run_flow_from_json with fake environment variables

- Implemented test_run_flow_with_fake_env to validate flow execution with a fake .env file.
- Added test_run_flow_with_fake_env_TWEAKS to check flow execution using environment variables loaded from the fake .env file.

* Replace keys in tweaks with their corresponding environment variable values

- Implemented a function to recursively replace keys in the tweaks dictionary with values from the provided environment variables.

* updated to use better way to load test  json file

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes

* refactor: improve test readability and consistency in load tests

- Renamed variable `TWEAKS` to `tweaks_dict` for clarity and consistency across tests.
- Updated test function names to follow a consistent naming convention.
- Enhanced comments for better understanding of test intentions.
- Minor formatting adjustments to improve code readability.

* feat: add aload_flow_from_json and arun_flow_from_json to module exports

* fix: correct file path handling in aload_flow_from_json function

* fix: improve environment variable handling in aload_flow_from_json function

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
This commit is contained in:
Edwin Jose 2025-01-31 13:51:34 -05:00 • committed by GitHub
commit 0514d11d9c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 91 additions and 10 deletions

View file

@ -103,6 +103,7 @@ def pytest_configure(config):
pytest.VECTOR_STORE_PATH = data_path / "Vector_store.json"
pytest.SIMPLE_API_TEST = data_path / "SimpleAPITest.json"
pytest.MEMORY_CHATBOT_NO_LLM = data_path / "MemoryChatbotNoLLM.json"
pytest.ENV_VARIABLE_TEST = data_path / "env_variable_test.json"
pytest.LOOP_TEST = data_path / "LoopTest.json"
pytest.CODE_WITH_SYNTAX_ERROR = """
def get_text():

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,8 @@
import inspect
import os
import pytest
from dotenv import load_dotenv
from langflow.load import run_flow_from_json
@ -26,5 +29,53 @@ def test_run_flow_from_json_params():
params = func_spec.args + func_spec.kwonlyargs
assert expected_params.issubset(params), "Not all expected parameters are present in run_flow_from_json"
# TODO: Add tests by loading a flow and running it need to text with fake llm and check if it returns the
# correct output
# TODO: Add tests by loading a flow and running it need to text with fake llm and check if it
# returns the correct output
@pytest.fixture
def fake_env_file(tmp_path):
# Create a fake .env file
env_file = tmp_path / ".env"
env_file.write_text("TEST_OP=TESTWORKS")
return env_file
def test_run_flow_with_fake_env(fake_env_file):
# Load the flow from the JSON file
# flow_file = Path("src/backend/tests/data/env_variable_test.json")
flow_file = pytest.ENV_VARIABLE_TEST
tweaks_dict = {"Secret-zIbKs": {"secret_key_input": "TEST_OP"}}
# Run the flow from JSON, providing the fake env file
result = run_flow_from_json(
flow=flow_file,
input_value="some_input_value",
env_file=str(fake_env_file), # Pass the path of the fake env file
tweaks=tweaks_dict,
)
# Extract and check the output data
output_data = result[0].outputs[0].results["message"].data["text"]
assert output_data == "TESTWORKS"
def test_run_flow_with_fake_env_tweaks(fake_env_file):
# Load the flow from the JSON file
# flow_file = Path("src/backend/tests/data/env_variable_test.json")
flow_file = pytest.ENV_VARIABLE_TEST
# Load env file and set up tweaks
load_dotenv(str(fake_env_file))
tweaks = {
"Secret-zIbKs": {"secret_key_input": os.environ["TEST_OP"]},
}
# Run the flow from JSON without passing the env_file
result = run_flow_from_json(
flow=flow_file,
input_value="some_input_value",
tweaks=tweaks,
)
# Extract and check the output data
output_data = result[0].outputs[0].results["message"].data["text"]
assert output_data == "TESTWORKS"