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.

fix(tests): address review feedback on private function testing

LearningCircuit0f0cfed80031
diff --git a/src/local_deep_research/config/paths.py b/src/local_deep_research/config/paths.py
index 6b382cbe65..592af0ad12 100644
--- a/src/local_deep_research/config/paths.py
+++ b/src/local_deep_research/config/paths.py
@@ -109,7 +109,7 @@ def get_database_path() -> Path:
# Raise exception instead of returning path
db_path = data_dir / "deprecated_shared.db"
- raise DeprecationWarning(
+ raise RuntimeError(
f"DEPRECATED: Shared database path requested: {db_path}. "
"This function should not be used - use per-user encrypted databases instead"
)
@@ -152,6 +152,6 @@ def get_db_path() -> str:
DEPRECATED: This function should not be used - use per-user encrypted databases instead.
"""
- raise DeprecationWarning(
+ raise RuntimeError(
"DEPRECATED: get_db_path() should not be used - use per-user encrypted databases instead"
)
diff --git a/tests/utilities/test_log_utils.py b/tests/utilities/test_log_utils.py
index 420be9d4ef..4b7f066655 100644
--- a/tests/utilities/test_log_utils.py
+++ b/tests/utilities/test_log_utils.py
@@ -128,65 +128,6 @@ class TestLogForResearch:
assert result == ("uuid", "value1", "value2")
-class TestGetResearchId:
- """Tests for _get_research_id function."""
-
- def test_returns_none_when_not_set(self):
- """Should return None when no research_id is set."""
- from local_deep_research.utilities.log_utils import _get_research_id
-
- with patch(
- "local_deep_research.utilities.log_utils.has_app_context",
- return_value=False,
- ):
- result = _get_research_id()
- assert result is None
-
- def test_gets_from_record_extra(self):
- """Should get research_id from log record extra."""
- from local_deep_research.utilities.log_utils import _get_research_id
-
- record = {"extra": {"research_id": "record-uuid"}}
-
- result = _get_research_id(record)
-
- assert result == "record-uuid"
-
- def test_gets_from_flask_g(self):
- """Should get research_id from Flask g object."""
- from local_deep_research.utilities.log_utils import _get_research_id
-
- mock_g = Mock()
- mock_g.get.return_value = "flask-uuid"
-
- with patch(
- "local_deep_research.utilities.log_utils.has_app_context",
- return_value=True,
- ):
- with patch("local_deep_research.utilities.log_utils.g", mock_g):
- result = _get_research_id()
-
- assert result == "flask-uuid"
-
- def test_record_takes_priority_over_flask(self):
- """Record research_id should take priority over Flask g."""
- from local_deep_research.utilities.log_utils import _get_research_id
-
- record = {"extra": {"research_id": "record-uuid"}}
-
- mock_g = Mock()
- mock_g.get.return_value = "flask-uuid"
-
- with patch(
- "local_deep_research.utilities.log_utils.has_app_context",
- return_value=True,
- ):
- with patch("local_deep_research.utilities.log_utils.g", mock_g):
- result = _get_research_id(record)
-
- assert result == "record-uuid"
-
-
class TestDatabaseSink:
"""Tests for database_sink function."""
@@ -277,6 +218,174 @@ class TestDatabaseSink:
# Should not raise
database_sink(mock_message)
+ def test_writes_to_database_in_main_thread(self):
+ """Should write to database when in main thread with app context."""
+ from local_deep_research.utilities.log_utils import database_sink
+ import local_deep_research.utilities.log_utils as module
+
+ mock_message = Mock()
+ mock_message.record = {
+ "time": datetime.now(),
+ "message": "Test message",
+ "name": "test_module",
+ "function": "test_func",
+ "line": 42,
+ "level": Mock(name="INFO"),
+ "extra": {"username": "testuser", "research_id": "test-uuid"},
+ }
+
+ mock_thread = Mock()
+ mock_thread.name = "MainThread"
+
+ mock_session = MagicMock()
+ mock_cm = MagicMock()
+ mock_cm.__enter__ = Mock(return_value=mock_session)
+ mock_cm.__exit__ = Mock(return_value=None)
+
+ mock_g = Mock()
+ mock_g.get.return_value = None
+
+ with patch.object(module, "has_app_context", return_value=True):
+ with patch.object(module, "g", mock_g):
+ with patch.object(
+ threading, "current_thread", return_value=mock_thread
+ ):
+ with patch(
+ "local_deep_research.database.session_context.get_user_db_session",
+ return_value=mock_cm,
+ ):
+ database_sink(mock_message)
+
+ # Should write to database
+ mock_session.add.assert_called_once()
+ mock_session.commit.assert_called_once()
+
+ def test_handles_database_error_gracefully(self):
+ """Should not raise on database errors when writing."""
+ from local_deep_research.utilities.log_utils import database_sink
+ import local_deep_research.utilities.log_utils as module
+
+ mock_message = Mock()
+ mock_message.record = {
+ "time": datetime.now(),
+ "message": "Test",
+ "name": "mod",
+ "function": "f",
+ "line": 1,
+ "level": Mock(name="INFO"),
+ "extra": {"research_id": "test-uuid"},
+ }
+
+ mock_thread = Mock()
+ mock_thread.name = "MainThread"
+
+ mock_g = Mock()
+ mock_g.get.return_value = None
+
+ with patch.object(module, "has_app_context", return_value=True):
+ with patch.object(module, "g", mock_g):
+ with patch.object(
+ threading, "current_thread", return_value=mock_thread
+ ):
+ with patch(
+ "local_deep_research.database.session_context.get_user_db_session",
+ side_effect=Exception("DB error"),
+ ):
+ # Should not raise
+ database_sink(mock_message)
+
+ def test_extracts_research_id_from_record_extra(self):
+ """Should extract research_id from record extra."""
+ from local_deep_research.utilities.log_utils import database_sink
+ import local_deep_research.utilities.log_utils as module
+
+ mock_message = Mock()
+ mock_message.record = {
+ "time": datetime.now(),
+ "message": "Test",
+ "name": "mod",
+ "function": "f",
+ "line": 1,
+ "level": Mock(name="INFO"),
+ "extra": {"research_id": "record-uuid"},
+ }
+
+ with patch.object(module, "has_app_context", return_value=False):
+ with patch.object(module, "_log_queue") as mock_queue:
+ database_sink(mock_message)
+
+ # Verify the queued log entry contains the research_id
+ call_args = mock_queue.put_nowait.call_args[0][0]
+ assert call_args["research_id"] == "record-uuid"
+
+ def test_extracts_research_id_from_flask_g(self):
+ """Should extract research_id from Flask g when not in record."""
+ from local_deep_research.utilities.log_utils import database_sink
+ import local_deep_research.utilities.log_utils as module
+
+ mock_message = Mock()
+ mock_message.record = {
+ "time": datetime.now(),
+ "message": "Test",
+ "name": "mod",
+ "function": "f",
+ "line": 1,
+ "level": Mock(name="INFO"),
+ "extra": {},
+ }
+
+ mock_g = Mock()
+ mock_g.get.return_value = "flask-uuid"
+
+ with patch.object(module, "has_app_context", return_value=True):
+ with patch.object(module, "g", mock_g):
+ with patch.object(module, "_log_queue") as mock_queue:
+ # Use non-main thread to queue instead of write
+ mock_thread = Mock()
+ mock_thread.name = "WorkerThread"
+ with patch.object(
+ threading, "current_thread", return_value=mock_thread
+ ):
+ database_sink(mock_message)
+
+ # Verify the queued log entry contains the research_id
+ call_args = mock_queue.put_nowait.call_args[0][0]
+ assert call_args["research_id"] == "flask-uuid"
+
+ def test_record_research_id_takes_priority_over_flask(self):
+ """Record research_id should take priority over Flask g."""
+ from local_deep_research.utilities.log_utils import database_sink
+ import local_deep_research.utilities.log_utils as module
+
+ mock_message = Mock()
+ mock_message.record = {
+ "time": datetime.now(),
+ "message": "Test",
+ "name": "mod",
+ "function": "f",
+ "line": 1,
+ "level": Mock(name="INFO"),
+ "extra": {"research_id": "record-uuid"},
+ }
+
+ mock_g = Mock()
+ mock_g.get.return_value = "flask-uuid"
+
+ with patch.object(module, "has_app_context", return_value=True):
+ with patch.object(module, "g", mock_g):
+ with patch.object(module, "_log_queue") as mock_queue:
+ # Use non-main thread to queue instead of write
+ mock_thread = Mock()
+ mock_thread.name = "WorkerThread"
+ with patch.object(
+ threading, "current_thread", return_value=mock_thread
+ ):
+ database_sink(mock_message)
+
+ # Record research_id should win
+ call_args = mock_queue.put_nowait.call_args[0][0]
+ assert call_args["research_id"] == "record-uuid"
+
class TestFrontendProgressSink:
"""Tests for frontend_progress_sink function."""
@@ -344,24 +453,49 @@ class TestFlushLogQueue:
def test_flushes_all_queued_logs(self):
"""Should flush all logs from queue."""
from local_deep_research.utilities.log_utils import flush_log_queue
+ import local_deep_research.utilities.log_utils as module
log_entries = [
- {"message": "Log 1", "research_id": None},
- {"message": "Log 2", "research_id": None},
+ {
+ "timestamp": datetime.now(),
+ "message": "Log 1",
+ "module": "mod",
+ "function": "f",
+ "line_no": 1,
+ "level": "INFO",
+ "research_id": None,
+ "username": None,
+ },
+ {
+ "timestamp": datetime.now(),
+ "message": "Log 2",
+ "module": "mod",
+ "function": "f",
+ "line_no": 2,
+ "level": "INFO",
+ "research_id": None,
+ "username": None,
+ },
]
- with patch(
- "local_deep_research.utilities.log_utils._log_queue"
- ) as mock_queue:
+ mock_session = MagicMock()
+ mock_cm = MagicMock()
+ mock_cm.__enter__ = Mock(return_value=mock_session)
+ mock_cm.__exit__ = Mock(return_value=None)
+
+ with patch.object(module, "_log_queue") as mock_queue:
mock_queue.empty.side_effect = [False, False, True]
mock_queue.get_nowait.side_effect = log_entries + [queue.Empty()]
with patch(
- "local_deep_research.utilities.log_utils._write_log_to_database"
- ) as mock_write:
+ "local_deep_research.database.session_context.get_user_db_session",
+ return_value=mock_cm,
+ ):
flush_log_queue()
- assert mock_write.call_count == 2
+ # Should have written 2 logs
+ assert mock_session.add.call_count == 2
+ assert mock_session.commit.call_count == 2
def test_handles_empty_queue(self):
"""Should handle empty queue gracefully."""
@@ -428,89 +562,3 @@ class TestConfigLogger:
# Should not raise
config_logger("test_app")
-
-
-class TestWriteLogToDatabase:
- """Tests for _write_log_to_database function."""
-
- def test_writes_research_log(self):
- """Should write ResearchLog to database."""
- from local_deep_research.utilities.log_utils import (
- _write_log_to_database,
- )
-
- log_entry = {
- "timestamp": datetime.now(),
- "message": "Test message",
- "module": "test_module",
- "function": "test_func",
- "line_no": 42,
- "level": "INFO",
- "research_id": "test-uuid",
- "username": "testuser",
- }
-
- mock_session = MagicMock()
- mock_cm = MagicMock()
- mock_cm.__enter__ = Mock(return_value=mock_session)
- mock_cm.__exit__ = Mock(return_value=None)
-
- with patch(
- "local_deep_research.database.session_context.get_user_db_session",
- return_value=mock_cm,
- ):
- _write_log_to_database(log_entry)
-
- mock_session.add.assert_called_once()
- mock_session.commit.assert_called_once()
-
- def test_handles_database_error_gracefully(self):
- """Should not raise on database errors."""
- from local_deep_research.utilities.log_utils import (
- _write_log_to_database,
- )
-
- log_entry = {
- "timestamp": datetime.now(),
- "message": "Test",
- "module": "mod",
- "function": "f",
- "line_no": 1,
- "level": "INFO",
- "research_id": None,
- "username": None,
- }
-
- with patch(
- "local_deep_research.database.session_context.get_user_db_session",
- side_effect=Exception("DB error"),
- ):
- # Should not raise
- _write_log_to_database(log_entry)
-
-
-class TestLogQueue:
- """Tests for log queue functionality."""
-
- def test_queue_has_max_size(self):
- """Log queue should have a maximum size."""
- from local_deep_research.utilities.log_utils import _log_queue
-
- assert _log_queue.maxsize == 1000
-
- def test_stop_event_exists(self):
- """Stop event for queue processor should exist."""
- from local_deep_research.utilities.log_utils import _stop_queue
-
- assert isinstance(_stop_queue, threading.Event)
-
-
-class TestLogDir:
- """Tests for log directory configuration."""
-
- def test_log_dir_is_created(self):
- """Log directory should be created."""
- from local_deep_research.utilities.log_utils import _LOG_DIR
-
- # Should be a Path object
- assert hasattr(_LOG_DIR, "mkdir")