"""Test for the main app dashboard.""" import sys from collections.abc import Generator from unittest.mock import MagicMock import pytest from _pytest.monkeypatch import MonkeyPatch from pytest_mock import MockerFixture from sqlalchemy.orm import Session from aki_prj23_transparenzregister.ui import app from aki_prj23_transparenzregister.ui.session_handler import SessionHandler @pytest.fixture(autouse=True) def _set_session(full_db: Session) -> Generator[None, None, None]: """Sets a session for the dash application to be used.""" SessionHandler.session = full_db yield SessionHandler.session = None def test_import() -> None: """Checks if an import of the dash app can be made.""" assert app is not None def test_go_to_home() -> None: """Checks if the go_to_home callback yields a result.""" output = app.go_to_home(1) assert output == "/" def test_go_to_detail_page1(monkeypatch: MonkeyPatch) -> None: """Checks if the go_to_detail_page callback yields a result.""" monkeypatch.setenv("DASH_URL_BASE_PATHNAME", "/monkey_path/") output = app.go_to_detail_page("c_1") assert output == "/monkey_path/unternehmensdetails/1" def test_go_to_detail_page2() -> None: """Checks if the go_to_detail_page callback yields a result.""" output = app.go_to_detail_page("c_1") assert output == "/unternehmensdetails/1" def test_main_of_app(monkeypatch: MonkeyPatch) -> None: monkeypatch.setattr(sys, "argv", [sys.argv[0]]) with pytest.raises(SystemExit): app.main() def test_main_of_app_help(monkeypatch: MonkeyPatch) -> None: monkeypatch.setattr(sys, "argv", [sys.argv[0], "-h"]) with pytest.raises(SystemExit): app.main() @pytest.mark.parametrize("upper", [True, False]) def test_main_of_app_env( monkeypatch: MonkeyPatch, upper: bool, mocker: MockerFixture ) -> None: MagicMock() monkeypatch.setattr(sys, "argv", [sys.argv[0], "ENV" if upper else "env"]) mocked = mocker.patch("aki_prj23_transparenzregister.ui.app.Dash.run") mocked_config = mocker.patch( "aki_prj23_transparenzregister.ui.app.get_config_provider" ) mocked_connector = mocker.patch( "aki_prj23_transparenzregister.ui.app.connector.get_session" ) mocked.return_value = None mocked_config.return_value = None mocked_connector.return_value = None app.main() mocked.assert_called_once()