refactor: Replace async_open by anyio.Path methods in tests (#6847)

Replace async_open by anyio.Path methods in tests

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
This commit is contained in:
Christophe Bornet 2025-02-27 02:00:39 +01:00 • committed by GitHub
commit 5133f1cc19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 16 additions and 22 deletions

View file

@ -1,9 +1,8 @@
import asyncio import asyncio
import inspect import inspect
from pathlib import Path
from typing import Any from typing import Any
from aiofile import async_open from anyio import Path
from fastapi import status from fastapi import status
from httpx import AsyncClient from httpx import AsyncClient
from langflow.api.v1.schemas import UpdateCustomComponentRequest from langflow.api.v1.schemas import UpdateCustomComponentRequest
@ -36,8 +35,8 @@ async def test_get_config(client: AsyncClient):
async def test_update_component_outputs(client: AsyncClient, logged_in_headers: dict): async def test_update_component_outputs(client: AsyncClient, logged_in_headers: dict):
path = Path(__file__).parent.parent.parent.parent / "data" / "dynamic_output_component.py" path = Path(__file__).parent.parent.parent.parent / "data" / "dynamic_output_component.py"
async with async_open(path, encoding="utf-8") as f:
code = await f.read() code = await path.read_text(encoding="utf-8")
frontend_node: dict[str, Any] = {"outputs": []} frontend_node: dict[str, Any] = {"outputs": []}
request = UpdateCustomComponentRequest( request = UpdateCustomComponentRequest(
code=code, code=code,
@ -69,8 +68,7 @@ async def test_update_component_model_name_options(client: AsyncClient, logged_i
# we are at str/backend/tests/unit/api/v1/test_endpoints.py # we are at str/backend/tests/unit/api/v1/test_endpoints.py
# find the file by using the class AgentComponent # find the file by using the class AgentComponent
agent_component_file = await asyncio.to_thread(inspect.getsourcefile, AgentComponent) agent_component_file = await asyncio.to_thread(inspect.getsourcefile, AgentComponent)
async with async_open(agent_component_file, encoding="utf-8") as f: code = await Path(agent_component_file).read_text(encoding="utf-8")
code = await f.read()
# Create the request to update the component # Create the request to update the component
request = UpdateCustomComponentRequest( request = UpdateCustomComponentRequest(

View file

@ -37,7 +37,7 @@ def test_run_flow_from_json_params():
def fake_env_file(tmp_path): def fake_env_file(tmp_path):
# Create a fake .env file # Create a fake .env file
env_file = tmp_path / ".env" env_file = tmp_path / ".env"
env_file.write_text("TEST_OP=TESTWORKS") env_file.write_text("TEST_OP=TESTWORKS", encoding="utf-8")
return env_file return env_file

View file

@ -16,11 +16,11 @@ def test_files():
with tempfile.TemporaryDirectory() as temp_dir: with tempfile.TemporaryDirectory() as temp_dir:
# Create a Python file # Create a Python file
python_file = Path(temp_dir) / "test.py" python_file = Path(temp_dir) / "test.py"
python_file.write_text("import langchain\nclass TestComponent:\n pass") python_file.write_text("import langchain\nclass TestComponent:\n pass", encoding="utf-8")
# Create a text file # Create a text file
text_file = Path(temp_dir) / "test.txt" text_file = Path(temp_dir) / "test.txt"
text_file.write_text("This is a test file") text_file.write_text("This is a test file", encoding="utf-8")
# Create a binary file # Create a binary file
binary_file = Path(temp_dir) / "test.bin" binary_file = Path(temp_dir) / "test.bin"
@ -30,7 +30,7 @@ def test_files():
no_access_dir = Path(temp_dir) / "no_access" no_access_dir = Path(temp_dir) / "no_access"
no_access_dir.mkdir() no_access_dir.mkdir()
no_access_file = no_access_dir / "secret.txt" no_access_file = no_access_dir / "secret.txt"
no_access_file.write_text("secret") no_access_file.write_text("secret", encoding="utf-8")
no_access_file.chmod(0o000) # Remove all permissions no_access_file.chmod(0o000) # Remove all permissions
yield temp_dir yield temp_dir

View file

@ -1,5 +1,5 @@
import pytest import pytest
from aiofile import async_open from anyio import Path
from langflow.components.inputs import ChatInput, TextInputComponent from langflow.components.inputs import ChatInput, TextInputComponent
from langflow.schema.message import Message from langflow.schema.message import Message
from langflow.utils.constants import MESSAGE_SENDER_AI, MESSAGE_SENDER_NAME_USER, MESSAGE_SENDER_USER from langflow.utils.constants import MESSAGE_SENDER_AI, MESSAGE_SENDER_NAME_USER, MESSAGE_SENDER_USER
@ -90,9 +90,8 @@ class TestChatInput(ComponentTestBaseWithClient):
async def test_message_response_with_files(self, component_class, tmp_path): async def test_message_response_with_files(self, component_class, tmp_path):
"""Test message response with file attachments.""" """Test message response with file attachments."""
# Create a temporary test file # Create a temporary test file
test_file = tmp_path / "test.txt" test_file = Path(tmp_path) / "test.txt"
async with async_open(test_file, "w") as f: await test_file.write_text("Test content", encoding="utf-8")
await f.write("Test content")
kwargs = { kwargs = {
"input_value": "Message with file", "input_value": "Message with file",

View file

@ -1,12 +1,10 @@
import asyncio import asyncio
import uuid import uuid
from datetime import datetime from datetime import datetime
from pathlib import Path
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
import anyio
import pytest import pytest
from aiofile import async_open from anyio import Path
from langflow.custom.directory_reader.utils import abuild_custom_component_list_from_path from langflow.custom.directory_reader.utils import abuild_custom_component_list_from_path
from langflow.initial_setup.constants import STARTER_FOLDER_NAME from langflow.initial_setup.constants import STARTER_FOLDER_NAME
from langflow.initial_setup.setup import ( from langflow.initial_setup.setup import (
@ -28,7 +26,7 @@ async def test_load_starter_projects():
projects = await load_starter_projects() projects = await load_starter_projects()
assert isinstance(projects, list) assert isinstance(projects, list)
assert all(isinstance(project[1], dict) for project in projects) assert all(isinstance(project[1], dict) for project in projects)
assert all(isinstance(project[0], anyio.Path) for project in projects) assert all(isinstance(project[0], Path) for project in projects)
async def test_get_project_data(): async def test_get_project_data():
@ -139,7 +137,7 @@ def add_edge(source, target, from_output, to_input):
async def test_refresh_starter_projects(): async def test_refresh_starter_projects():
data_path = str(await anyio.Path(__file__).parent.parent.parent.absolute() / "base" / "langflow" / "components") data_path = str(await Path(__file__).parent.parent.parent.absolute() / "base" / "langflow" / "components")
components = await abuild_custom_component_list_from_path(data_path) components = await abuild_custom_component_list_from_path(data_path)
chat_input = find_component_by_name(components, "ChatInput") chat_input = find_component_by_name(components, "ChatInput")
@ -243,9 +241,8 @@ async def test_load_bundles_from_urls():
assert len(components_paths) == 1 assert len(components_paths) == 1
assert "langflow-bundles-68428ce16729a385fe1bcc0f1ec91fd5f5f420b9/components" in components_paths[0] assert "langflow-bundles-68428ce16729a385fe1bcc0f1ec91fd5f5f420b9/components" in components_paths[0]
async with async_open(Path(components_paths[0]) / "embeddings" / "openai2.py") as f: content = await (Path(components_paths[0]) / "embeddings" / "openai2.py").read_text(encoding="utf-8")
content = await f.read() assert "OpenAIEmbeddings2Component" in content
assert "OpenAIEmbeddings2Component" in content
assert len(temp_dirs) == 1 assert len(temp_dirs) == 1