diff --git a/tests/conftest.py b/tests/conftest.py index f4464de..622117c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,23 +1,20 @@ import logging import os -from typing import List +from copy import deepcopy +from pathlib import Path +from typing import Any, Dict, List import pytest # type: ignore from _pytest.monkeypatch import MonkeyPatch # type: ignore from fastapi.testclient import TestClient -from pytest_mock import mocker # type: ignore +from pytest_mock import MockerFixture -# from inboard.app.base.main import App as base_app +from inboard import gunicorn_conf as gunicorn_conf_module +from inboard import logging_conf as logging_conf_module from inboard.app.fastapibase.main import app as fastapi_app from inboard.app.starlettebase.main import app as starlette_app -@pytest.fixture -def clients() -> List[TestClient]: - """Instantiate test client classes.""" - return [TestClient(fastapi_app), TestClient(starlette_app)] - - @pytest.fixture def basic_auth( monkeypatch: MonkeyPatch, @@ -33,12 +30,42 @@ def basic_auth( @pytest.fixture -def mock_logger(mocker: mocker) -> logging.Logger: +def clients() -> List[TestClient]: + """Instantiate test client classes.""" + return [TestClient(fastapi_app), TestClient(starlette_app)] + + +@pytest.fixture +def gunicorn_conf_path(mocker: MockerFixture, monkeypatch: MonkeyPatch) -> Path: + """Set path to default Gunicorn configuration file.""" + path = Path(gunicorn_conf_module.__file__) + monkeypatch.setenv("GUNICORN_CONF", str(path)) + assert os.getenv("GUNICORN_CONF") == str(path) + return path + + +@pytest.fixture +def logging_conf_dict() -> Dict[str, Any]: + """Load logging configuration dictionary from logging configuration module.""" + return deepcopy(logging_conf_module.LOGGING_CONFIG) + + +@pytest.fixture +def logging_conf_path(mocker: MockerFixture, monkeypatch: MonkeyPatch) -> Path: + """Set path to default logging configuration file.""" + path = Path(logging_conf_module.__file__) + monkeypatch.setenv("LOGGING_CONF", str(path)) + assert os.getenv("LOGGING_CONF") == str(path) + return path + + +@pytest.fixture +def mock_logger(mocker: MockerFixture) -> logging.Logger: """Mock the logger with pytest-mock and a pytest fixture. - https://github.com/pytest-dev/pytest-mock - https://docs.pytest.org/en/latest/fixture.html """ - logger = logging.getLogger("pytest") + logger = logging.getLogger() mocker.patch.object(logger, "debug") mocker.patch.object(logger, "error") mocker.patch.object(logger, "info") diff --git a/tests/test_start.py b/tests/test_start.py new file mode 100644 index 0000000..f2325df --- /dev/null +++ b/tests/test_start.py @@ -0,0 +1,21 @@ +from pathlib import Path + +from inboard import start + + +class TestConfPaths: + """Test paths to configuration files. + --- + """ + + def test_set_default_conf_path_gunicorn(self, gunicorn_conf_path: Path) -> None: + """Test default Gunicorn configuration file path (different without Docker).""" + assert "inboard/gunicorn_conf.py" in str(gunicorn_conf_path) + assert "logging" not in str(gunicorn_conf_path) + assert start.set_conf_path("gunicorn") == gunicorn_conf_path + + def test_set_default_conf_path_logging(self, logging_conf_path: Path) -> None: + """Test default logging configuration file path (different without Docker).""" + assert "inboard/logging_conf.py" in str(logging_conf_path) + assert "gunicorn" not in str(logging_conf_path) + assert start.set_conf_path("logging") == logging_conf_path