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 inspect
from pathlib import Path
from typing import Any
from aiofile import async_open
from anyio import Path
from fastapi import status
from httpx import AsyncClient
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):
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": []}
request = UpdateCustomComponentRequest(
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
# find the file by using the class 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 f.read()
code = await Path(agent_component_file).read_text(encoding="utf-8")
# Create the request to update the component
request = UpdateCustomComponentRequest(

View file

@ -37,7 +37,7 @@ def test_run_flow_from_json_params():
def fake_env_file(tmp_path):
# Create a fake .env file
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

View file

@ -16,11 +16,11 @@ def test_files():
with tempfile.TemporaryDirectory() as temp_dir:
# Create a Python file
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
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
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.mkdir()
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
yield temp_dir

View file

@ -1,5 +1,5 @@
import pytest
from aiofile import async_open
from anyio import Path
from langflow.components.inputs import ChatInput, TextInputComponent
from langflow.schema.message import Message
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):
"""Test message response with file attachments."""
# Create a temporary test file
test_file = tmp_path / "test.txt"
async with async_open(test_file, "w") as f:
await f.write("Test content")
test_file = Path(tmp_path) / "test.txt"
await test_file.write_text("Test content", encoding="utf-8")
kwargs = {
"input_value": "Message with file",

View file

@ -1,12 +1,10 @@
import asyncio
import uuid
from datetime import datetime
from pathlib import Path
from unittest.mock import AsyncMock, patch
import anyio
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.initial_setup.constants import STARTER_FOLDER_NAME
from langflow.initial_setup.setup import (
@ -28,7 +26,7 @@ async def test_load_starter_projects():
projects = await load_starter_projects()
assert isinstance(projects, list)
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():
@ -139,7 +137,7 @@ def add_edge(source, target, from_output, to_input):
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)
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 "langflow-bundles-68428ce16729a385fe1bcc0f1ec91fd5f5f420b9/components" in components_paths[0]
async with async_open(Path(components_paths[0]) / "embeddings" / "openai2.py") as f:
content = await f.read()
assert "OpenAIEmbeddings2Component" in content
content = await (Path(components_paths[0]) / "embeddings" / "openai2.py").read_text(encoding="utf-8")
assert "OpenAIEmbeddings2Component" in content
assert len(temp_dirs) == 1