diff --git a/tests/conftest.py b/tests/conftest.py index 95aba4462..b64851aba 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,6 +15,9 @@ from sqlmodel import SQLModel, Session, create_engine from sqlmodel.pool import StaticPool from typer.testing import CliRunner +# we need to import tmpdir +import tempfile + if TYPE_CHECKING: from langflow.services.database.manager import DatabaseManager @@ -46,14 +49,14 @@ async def async_client() -> AsyncGenerator: # Create client fixture for FastAPI -@pytest.fixture(scope="module", autouse=True) -def client(): - from langflow.main import create_app +# @pytest.fixture(scope="module", autouse=True) +# def client(): +# from langflow.main import create_app - app = create_app() +# app = create_app() - with TestClient(app) as client: - yield client +# with TestClient(app) as client: +# yield client def get_graph(_type="basic"): @@ -111,8 +114,13 @@ def session_fixture(): yield session -@pytest.fixture(name="client") -def client_fixture(session: Session): +@pytest.fixture(name="client", autouse=True) +def client_fixture(session: Session, monkeypatch): + # Set the database url to a test database + db_dir = tempfile.mkdtemp() + db_path = Path(db_dir) / "test.db" + monkeypatch.setenv("LANGFLOW_DATABASE_URL", f"sqlite:///{db_path}") + def get_session_override(): return session @@ -124,6 +132,9 @@ def client_fixture(session: Session): with TestClient(app) as client: yield client app.dependency_overrides.clear() + monkeypatch.undo() + # clear the temp db + db_path.unlink() # @contextmanager diff --git a/tests/test_custom_component.py b/tests/test_custom_component.py index e75dc0e5b..1695cfd38 100644 --- a/tests/test_custom_component.py +++ b/tests/test_custom_component.py @@ -518,13 +518,13 @@ def db(app): app.db.drop_all() -def test_list_flows_return_type(component, session_getter): - flows = component.list_flows(get_session=session_getter) +def test_list_flows_return_type(component): + flows = component.list_flows() assert isinstance(flows, list) -def test_list_flows_flow_objects(component, session_getter): - flows = component.list_flows(get_session=session_getter) +def test_list_flows_flow_objects(component): + flows = component.list_flows() assert all(isinstance(flow, Flow) for flow in flows) diff --git a/tests/test_setup_superuser.py b/tests/test_setup_superuser.py index 202910737..5c05e8ba7 100644 --- a/tests/test_setup_superuser.py +++ b/tests/test_setup_superuser.py @@ -27,10 +27,11 @@ def test_setup_superuser( DEFAULT_SUPERUSER_PASSWORD ) + ADMIN_USER_NAME = "admin_user" # Test when username and password are default mock_settings_manager.auth_settings = Mock() mock_settings_manager.auth_settings.AUTO_LOGIN = False - mock_settings_manager.auth_settings.SUPERUSER = "admin" + mock_settings_manager.auth_settings.SUPERUSER = ADMIN_USER_NAME mock_settings_manager.auth_settings.SUPERUSER_PASSWORD = "password" mock_settings_manager.auth_settings.reset_credentials = Mock( side_effect=reset_mock_credentials @@ -46,20 +47,20 @@ def test_setup_superuser( setup_superuser() mock_session.query.assert_called_once_with(User) actual_expr = mock_session.query.return_value.filter.call_args[0][0] - expected_expr = User.username == "admin" + expected_expr = User.username == ADMIN_USER_NAME assert str(actual_expr) == str(expected_expr) mock_create_super_user.assert_called_once_with( - db=mock_session, username="admin", password="password" + db=mock_session, username=ADMIN_USER_NAME, password="password" ) # Test that superuser credentials are reset mock_settings_manager.auth_settings.reset_credentials.assert_called_once() - assert mock_settings_manager.auth_settings.SUPERUSER != "admin" + assert mock_settings_manager.auth_settings.SUPERUSER != ADMIN_USER_NAME assert mock_settings_manager.auth_settings.SUPERUSER_PASSWORD != "password" # Test when superuser already exists mock_settings_manager.auth_settings.AUTO_LOGIN = False - mock_settings_manager.auth_settings.SUPERUSER = "admin" + mock_settings_manager.auth_settings.SUPERUSER = ADMIN_USER_NAME mock_settings_manager.auth_settings.SUPERUSER_PASSWORD = "password" mock_user = Mock() mock_user.is_superuser = True @@ -67,7 +68,7 @@ def test_setup_superuser( setup_superuser() mock_session.query.assert_called_with(User) actual_expr = mock_session.query.return_value.filter.call_args[0][0] - expected_expr = User.username == "admin" + expected_expr = User.username == ADMIN_USER_NAME assert str(actual_expr) == str(expected_expr) @@ -108,9 +109,10 @@ def test_teardown_superuser_default_superuser( def test_teardown_superuser_no_default_superuser( mock_get_session, mock_get_settings_manager ): + ADMIN_USER_NAME = "admin_user" mock_settings_manager = MagicMock() mock_settings_manager.auth_settings.AUTO_LOGIN = False - mock_settings_manager.auth_settings.SUPERUSER = "admin" + mock_settings_manager.auth_settings.SUPERUSER = ADMIN_USER_NAME mock_settings_manager.auth_settings.SUPERUSER_PASSWORD = "password" mock_get_settings_manager.return_value = mock_settings_manager