Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

9.6 KB
Raw Blame
"""Tests for db_utils module.

Post-FastAPI-migration get_db_session no longer consults Flask
``has_app_context``/``g``/``flask.session``: the background-thread guard is a
plain ``thread_name != "MainThread"`` check, and the user is resolved from the
request-context contextvar via ``get_current_username()``. These tests drive
that path.
"""

import threading
from unittest.mock import Mock, patch

import pytest

from local_deep_research.exceptions import NoUserDatabaseError


class TestGetDbSession:
    """Tests for get_db_session function."""

    def test_raises_error_from_background_thread_without_context(self):
        """Should raise error when called from a background (non-main) thread."""
        from local_deep_research.utilities.db_utils import get_db_session

        error_raised = [False]
        error_message = [""]

        def background_task():
            try:
                get_db_session()
            except RuntimeError as e:
                error_raised[0] = True
                error_message[0] = str(e)

        # Run in a background thread
        thread = threading.Thread(
            target=background_task, name="TestBackgroundThread"
        )
        thread.start()
        thread.join()

        assert error_raised[0], (
            "Should raise RuntimeError from background thread"
        )
        assert "background thread" in error_message[0].lower()

    def test_returns_session_when_username_provided(self):
        """Should return session when username is explicitly provided."""
        from local_deep_research.utilities.db_utils import get_db_session

        mock_session = Mock()

        with patch(
            "local_deep_research.utilities.db_utils.db_manager"
        ) as mock_manager:
            mock_manager.get_session.return_value = mock_session

            # Unique _namespace to avoid thread_specific_cache hits.
            result = get_db_session(
                username="testuser", _namespace="test_username_provided"
            )

            assert result == mock_session
            mock_manager.get_session.assert_called_with("testuser")

    def test_raises_error_when_no_db_for_user(self):
        """Should raise error when no database found for user."""
        from local_deep_research.utilities.db_utils import get_db_session

        with patch(
            "local_deep_research.utilities.db_utils.db_manager"
        ) as mock_manager:
            mock_manager.get_session.return_value = None

            with pytest.raises(RuntimeError, match="No database found"):
                get_db_session(username="nonexistent")

    def test_returns_session_from_current_username(self):
        """With no username arg, resolves the user via get_current_username()."""
        from local_deep_research.utilities.db_utils import get_db_session

        mock_session = Mock()

        with patch(
            "local_deep_research.utilities.db_utils.get_current_username",
            return_value="ctxuser",
        ):
            with patch(
                "local_deep_research.utilities.db_utils.db_manager"
            ) as mock_manager:
                mock_manager.get_session.return_value = mock_session

                # Unique _namespace to avoid thread_specific_cache hits.
                result = get_db_session(_namespace="test_ctx_user")

                assert result == mock_session
                mock_manager.get_session.assert_called_with("ctxuser")

    def test_returns_none_when_no_authenticated_user(self):
        """With no username arg and no authenticated user, returns None."""
        from local_deep_research.utilities.db_utils import get_db_session

        with patch(
            "local_deep_research.utilities.db_utils.get_current_username",
            return_value=None,
        ):
            result = get_db_session(_namespace="test_no_auth_user")
            assert result is None


class TestGetSettingsManager:
    """Tests for get_settings_manager function.

    Two of these drive the "no database available" fallback. They signal it
    with ``NoUserDatabaseError`` rather than a bare ``RuntimeError``: their
    subject is the fallback, and that is the type which now means this
    condition. ``get_settings_manager`` no longer catches ``RuntimeError``
    broadly, because doing so also swallowed the background-thread guard and
    any failure inside ``db_manager.get_session`` -- see
    ``test_db_utils_error_discrimination.py``, which pins that these three
    cases stay distinguishable.
    """

    def test_returns_settings_manager(self):
        """Should return a SettingsManager instance."""
        from local_deep_research.utilities.db_utils import get_settings_manager

        mock_manager = Mock()

        with patch(
            "local_deep_research.utilities.db_utils.get_current_username",
            return_value=None,
        ):
            with patch(
                "local_deep_research.utilities.db_utils.get_db_session",
                side_effect=NoUserDatabaseError("No database for user"),
            ):
                with patch(
                    "local_deep_research.settings.SettingsManager",
                    return_value=mock_manager,
                ):
                    result = get_settings_manager()

                    # Should return a SettingsManager (mocked)
                    assert result is not None

    def test_uses_provided_db_session(self):
        """Should use provided db_session."""
        from local_deep_research.utilities.db_utils import get_settings_manager

        mock_session = Mock()
        mock_manager = Mock()

        with patch(
            "local_deep_research.settings.SettingsManager",
            return_value=mock_manager,
        ):
            result = get_settings_manager(db_session=mock_session)

            # The function should return a SettingsManager initialized with the session
            assert result is not None

    def test_handles_no_session(self):
        """Should handle case when no session available."""
        from local_deep_research.utilities.db_utils import get_settings_manager

        with patch(
            "local_deep_research.utilities.db_utils.get_current_username",
            return_value=None,
        ):
            with patch(
                "local_deep_research.utilities.db_utils.get_db_session",
                side_effect=NoUserDatabaseError("No database for user"),
            ):
                result = get_settings_manager()

                # Should still return a SettingsManager (with None session)
                assert result is not None


class TestNoDbSettings:
    """Tests for no_db_settings decorator."""

    def test_disables_db_during_function_execution(self):
        """Should disable DB session during function execution."""
        from local_deep_research.utilities.db_utils import no_db_settings

        mock_session = Mock()
        mock_manager = Mock()
        mock_manager.db_session = mock_session

        session_during_call = []

        @no_db_settings
        def test_func():
            session_during_call.append(mock_manager.db_session)
            return "result"

        with patch(
            "local_deep_research.utilities.db_utils.get_settings_manager",
            return_value=mock_manager,
        ):
            result = test_func()

            assert result == "result"
            assert session_during_call[0] is None
            # Session should be restored after
            assert mock_manager.db_session == mock_session

    def test_restores_db_on_exception(self):
        """Should restore DB session even if function raises."""
        from local_deep_research.utilities.db_utils import no_db_settings

        mock_session = Mock()
        mock_manager = Mock()
        mock_manager.db_session = mock_session

        @no_db_settings
        def failing_func():
            raise ValueError("Test error")

        with patch(
            "local_deep_research.utilities.db_utils.get_settings_manager",
            return_value=mock_manager,
        ):
            with pytest.raises(ValueError):
                failing_func()

            # Session should still be restored
            assert mock_manager.db_session == mock_session

    def test_preserves_function_metadata(self):
        """Should preserve function name and docstring."""
        from local_deep_research.utilities.db_utils import no_db_settings

        @no_db_settings
        def my_function():
            """My docstring."""
            pass

        assert my_function.__name__ == "my_function"
        assert my_function.__doc__ == "My docstring."


class TestThreadSafety:
    """Tests for thread safety mechanisms."""

    def test_thread_name_included_in_error(self):
        """Error message should include thread name."""
        from local_deep_research.utilities.db_utils import get_db_session

        error_message = [""]

        def background_task():
            try:
                get_db_session()
            except RuntimeError as e:
                error_message[0] = str(e)

        thread = threading.Thread(
            target=background_task, name="CustomThreadName"
        )
        thread.start()
        thread.join()

        assert "CustomThreadName" in error_message[0]

    def test_main_thread_not_blocked_without_context(self):
        """MainThread should not be blocked by thread safety check."""
        # Verify that MainThread can proceed past the thread check
        # even without app context (unlike background threads)
        import threading

        # This test runs in MainThread
        assert threading.current_thread().name == "MainThread"

        # The function should not raise RuntimeError for MainThread
        # (it may fail later for other reasons, but not the thread check)