ci: Add temporary database setup for performance tests (#4469)

Add temporary database setup for performance tests and assertions for database URL
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-11-08 11:41:01 -03:00 • committed by GitHub
commit 7114cc4799
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 1 deletions

View file

@ -4,12 +4,29 @@ import pytest
from langflow.services.deps import get_settings_service
@pytest.fixture(autouse=True)
def setup_database_url(tmp_path, monkeypatch):
"""Setup a temporary database URL for testing."""
db_path = tmp_path / "test_performance.db"
original_value = monkeypatch.delenv("LANGFLOW_DATABASE_URL", raising=False)
test_db_url = f"sqlite:///{db_path}"
monkeypatch.setenv("LANGFLOW_DATABASE_URL", test_db_url)
yield
# Restore original value if it existed
if original_value is not None:
monkeypatch.setenv("LANGFLOW_DATABASE_URL", original_value)
else:
monkeypatch.delenv("LANGFLOW_DATABASE_URL", raising=False)
@pytest.mark.benchmark
async def test_initialize_services():
"""Benchmark the initialization of services."""
from langflow.services.utils import initialize_services
await asyncio.to_thread(initialize_services, fix_migration=False)
settings_service = await asyncio.to_thread(get_settings_service)
assert "test_performance.db" in settings_service.settings.database_url
@pytest.mark.benchmark
@ -18,6 +35,8 @@ async def test_setup_llm_caching():
from langflow.interface.utils import setup_llm_caching
await asyncio.to_thread(setup_llm_caching)
settings_service = await asyncio.to_thread(get_settings_service)
assert "test_performance.db" in settings_service.settings.database_url
@pytest.mark.benchmark
@ -28,6 +47,8 @@ async def test_initialize_super_user():
await asyncio.to_thread(initialize_services, fix_migration=False)
await asyncio.to_thread(initialize_super_user_if_needed)
settings_service = await asyncio.to_thread(get_settings_service)
assert "test_performance.db" in settings_service.settings.database_url
@pytest.mark.benchmark
@ -38,6 +59,7 @@ async def test_get_and_cache_all_types_dict():
settings_service = await asyncio.to_thread(get_settings_service)
result = await asyncio.to_thread(get_and_cache_all_types_dict, settings_service)
assert result is not None
assert "test_performance.db" in settings_service.settings.database_url
@pytest.mark.benchmark
@ -51,6 +73,7 @@ async def test_create_starter_projects():
settings_service = await asyncio.to_thread(get_settings_service)
types_dict = await get_and_cache_all_types_dict(settings_service)
await asyncio.to_thread(create_or_update_starter_projects, types_dict)
assert "test_performance.db" in settings_service.settings.database_url
@pytest.mark.benchmark
@ -59,3 +82,5 @@ async def test_load_flows():
from langflow.initial_setup.setup import load_flows_from_directory
await asyncio.to_thread(load_flows_from_directory)
settings_service = await asyncio.to_thread(get_settings_service)
assert "test_performance.db" in settings_service.settings.database_url