From 82469752f89950e469b4e9dcef985d1764bea48c Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 20 Sep 2023 14:32:14 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix(tests/conftest.py):=20remove?= =?UTF-8?q?=20unused=20imports=20and=20commented=20out=20code=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(tests/conftest.py):=20remove=20unused=20fixt?= =?UTF-8?q?ure=20'client'=20=F0=9F=94=A7=20fix(tests/conftest.py):=20remov?= =?UTF-8?q?e=20unused=20import=20of=20'langflow.main.create=5Fapp'=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(tests/conftest.py):=20remove=20unused=20fixt?= =?UTF-8?q?ure=20'client=5Ffixture'=20=F0=9F=94=A7=20fix(tests/conftest.py?= =?UTF-8?q?):=20remove=20unused=20import=20of=20'langflow.services.databas?= =?UTF-8?q?e.manager.DatabaseManager'=20=F0=9F=94=A7=20fix(tests/conftest.?= =?UTF-8?q?py):=20remove=20unused=20import=20of=20'typer.testing.CliRunner?= =?UTF-8?q?'=20=F0=9F=94=A7=20fix(tests/conftest.py):=20remove=20unused=20?= =?UTF-8?q?import=20of=20'sqlmodel.SQLModel'=20=F0=9F=94=A7=20fix(tests/co?= =?UTF-8?q?nftest.py):=20remove=20unused=20import=20of=20'sqlmodel.Session?= =?UTF-8?q?'=20=F0=9F=94=A7=20fix(tests/conftest.py):=20remove=20unused=20?= =?UTF-8?q?import=20of=20'sqlmodel.create=5Fengine'=20=F0=9F=94=A7=20fix(t?= =?UTF-8?q?ests/conftest.py):=20remove=20unused=20import=20of=20'sqlmodel.?= =?UTF-8?q?pool.StaticPool'=20=F0=9F=94=A7=20fix(tests/conftest.py):=20rem?= =?UTF-8?q?ove=20unused=20import=20of=20'tempfile'=20=F0=9F=94=A7=20fix(te?= =?UTF-8?q?sts/conftest.py):=20remove=20unused=20import=20of=20'Path'=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(tests/conftest.py):=20remove=20unused=20impo?= =?UTF-8?q?rt=20of=20'contextmanager'=20=F0=9F=94=A7=20fix(tests/test=5Fcu?= =?UTF-8?q?stom=5Fcomponent.py):=20remove=20unused=20argument=20'session?= =?UTF-8?q?=5Fgetter'=20from=20test=20functions=20=F0=9F=94=A7=20fix(tests?= =?UTF-8?q?/test=5Fcustom=5Fcomponent.py):=20remove=20unused=20import=20of?= =?UTF-8?q?=20'db'=20=F0=9F=94=A7=20fix(tests/test=5Fcustom=5Fcomponent.py?= =?UTF-8?q?):=20remove=20unused=20import=20of=20'Flow'=20=F0=9F=94=A7=20fi?= =?UTF-8?q?x(tests/test=5Fsetup=5Fsuperuser.py):=20remove=20unused=20impor?= =?UTF-8?q?t=20of=20'DEFAULT=5FSUPERUSER=5FPASSWORD'=20=F0=9F=94=A7=20fix(?= =?UTF-8?q?tests/test=5Fsetup=5Fsuperuser.py):=20remove=20unused=20import?= =?UTF-8?q?=20of=20'Mock'=20=F0=9F=94=A7=20fix(tests/test=5Fsetup=5Fsuperu?= =?UTF-8?q?ser.py):=20remove=20unused=20import=20of=20'User'=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(tests/test=5Fsetup=5Fsuperuser.py):=20remove?= =?UTF-8?q?=20unused=20import=20of=20'reset=5Fmock=5Fcredentials'=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(tests/test=5Fsetup=5Fsuperuser.py):=20remove?= =?UTF-8?q?=20unused=20import=20of=20'mock=5Fcreate=5Fsuper=5Fuser'=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(tests/test=5Fsetup=5Fsuperuser.py):=20remove?= =?UTF-8?q?=20unused=20import=20of=20'setup=5Fsuperuser'=20=F0=9F=94=A7=20?= =?UTF-8?q?fix(tests/test=5Fsetup=5Fsuperuser.py):=20remove=20unused=20imp?= =?UTF-8?q?ort=20of=20'mock=5Fuser'=20=F0=9F=94=A7=20fix(tests/test=5Fsetu?= =?UTF-8?q?p=5Fsuperuser.py):=20remove=20unused=20import=20of=20'mock=5Fge?= =?UTF-8?q?t=5Fsession'=20=F0=9F=94=A7=20fix(tests/test=5Fsetup=5Fsuperuse?= =?UTF-8?q?r.py):=20remove=20unused=20import=20of=20'mock=5Fget=5Fsettings?= =?UTF-8?q?=5Fmanager'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/conftest.py | 27 +++++++++++++++++++-------- tests/test_custom_component.py | 8 ++++---- tests/test_setup_superuser.py | 16 +++++++++------- 3 files changed, 32 insertions(+), 19 deletions(-) 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