tests: update env.py and conftest so tests use unique databases (#3654)

* Refactor Alembic environment script to simplify database connection logic and remove unused imports

* Refactor test client fixture to use TemporaryDirectory and UUID for database path

* Add `reload_engine` method to reinitialize the database engine

* Add cleanup steps to test fixtures to ensure proper resource management

- Added cleanup steps to various pytest fixtures to ensure temporary directories, database entries, and other resources are properly cleaned up after tests.
- Modified fixtures to use `yield` for better resource management.
- Ensured database connections are closed and tables are dropped after tests.
- Improved temporary directory handling with context managers.

* Add unit test to verify return type of list_flows method in custom component

* Refactor tests in `test_custom_component.py` to remove unused imports and fixtures, and update existing fixtures for consistency.

* Add debug checks for user existence in `test_user_waiting_for_approval` test

* Fix import order and add flow_id validation in transaction handling

- Corrected the import order in `utils.py`.
- Added validation for `flow_id` in the transaction handling logic to ensure it is set correctly.

* Add function to delete transactions by flow ID in CRUD module

* Add cleanup for transactions and vertex builds in test teardown

- Introduced `_delete_transactions_and_vertex_builds` function to remove transactions and vertex builds associated with a user.
- Updated `user` fixture to call the new cleanup function before deleting the user.

* Refactor flow_id assignment logic in `utils.py` to improve readability and correctness

* [autofix.ci] apply automated fixes

* Refactor test to use pytest's tmp_path fixture for temporary directory creation

* Convert `test_user_waiting_for_approval` to an async test function

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-10-10 08:45:11 -03:00 • committed by GitHub
commit 2adda780c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 179 additions and 149 deletions

View file

@ -2,6 +2,7 @@ from datetime import datetime
import pytest
from httpx import AsyncClient
from sqlmodel import select
from langflow.services.auth.utils import create_super_user, get_password_hash
from langflow.services.database.models.user import UserUpdate
@ -53,13 +54,23 @@ def deactivated_user():
return user
@pytest.mark.api_key_required
async def test_user_waiting_for_approval(client: AsyncClient):
async def test_user_waiting_for_approval(client):
username = "waitingforapproval"
password = "testpassword"
# Debug: Check if the user already exists
with session_getter(get_db_service()) as session:
existing_user = session.exec(select(User).where(User.username == username)).first()
if existing_user:
pytest.fail(
f"User {username} already exists before the test. Database URL: {get_db_service().database_url}"
)
# Create a user that is not active and has never logged in
with session_getter(get_db_service()) as session:
user = User(
username="waitingforapproval",
password=get_password_hash("testpassword"),
username=username,
password=get_password_hash(password),
is_active=False,
last_login_at=None,
)
@ -71,6 +82,14 @@ async def test_user_waiting_for_approval(client: AsyncClient):
assert response.status_code == 400
assert response.json()["detail"] == "Waiting for approval"
# Debug: Check if the user still exists after the test
with session_getter(get_db_service()) as session:
existing_user = session.exec(select(User).where(User.username == username)).first()
if existing_user:
print(f"User {username} still exists after the test. This is expected.")
else:
pytest.fail(f"User {username} does not exist after the test. This is unexpected.")
@pytest.mark.api_key_required
async def test_deactivated_user_cannot_login(client: AsyncClient, deactivated_user):