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(research-library): query DocumentBlob by document_id, not id (#5263)

Se5949168b146
diff --git a/src/local_deep_research/research_library/services/pdf_storage_manager.py b/src/local_deep_research/research_library/services/pdf_storage_manager.py
index 3f653aa57f..f284f50145 100644
--- a/src/local_deep_research/research_library/services/pdf_storage_manager.py
+++ b/src/local_deep_research/research_library/services/pdf_storage_manager.py
@@ -203,7 +203,7 @@ class PDFStorageManager:
from ...database.models.library import DocumentBlob
has_blob = (
- session.query(DocumentBlob.id)
+ session.query(DocumentBlob.document_id)
.filter_by(document_id=document.id)
.first()
is not None
diff --git a/tests/research_library/services/test_pdf_storage_manager.py b/tests/research_library/services/test_pdf_storage_manager.py
index 0b029ab08d..8c1c2d6289 100644
--- a/tests/research_library/services/test_pdf_storage_manager.py
+++ b/tests/research_library/services/test_pdf_storage_manager.py
@@ -6,6 +6,7 @@ from unittest.mock import MagicMock, patch
from local_deep_research.constants import (
FILE_PATH_METADATA_ONLY,
)
+from local_deep_research.database.models.library import DocumentBlob
from local_deep_research.research_library.services.pdf_storage_manager import (
PDFStorageManager,
)
@@ -175,20 +176,82 @@ class TestPDFStorageManagerHasPdf:
assert result is False
- def test_returns_false_for_non_pdf_type(self, tmp_path):
- """Should return False for non-PDF file types."""
+ def test_has_pdf_checks_database_first(self, tmp_path):
+ """Should check database for blob existence."""
manager = PDFStorageManager(tmp_path, "database")
mock_doc = MagicMock()
mock_doc.id = "doc-123"
- mock_doc.file_type = "txt" # Not a PDF
+ mock_doc.file_type = "pdf"
+ mock_doc.file_path = None
mock_session = MagicMock()
- result = manager.has_pdf(mock_doc, mock_session)
+ # Mock session returns a truthy row, so the DB path should succeed
+ # and the filesystem fallback should never be reached.
+ mock_first = mock_session.query.return_value.filter_by.return_value
+ mock_first.first.return_value = MagicMock()
- assert result is False
+ with patch.object(manager, "_get_safe_file_path") as mock_get_path:
+ assert manager.has_pdf(mock_doc, mock_session) is True
+ mock_session.query.assert_called_once_with(DocumentBlob.document_id)
+ mock_session.query.return_value.filter_by.assert_called_once_with(
+ document_id="doc-123"
+ )
+ mock_get_path.assert_not_called()
- def test_has_pdf_checks_database_first(self, tmp_path):
- """Should check database for blob existence."""
+ def test_has_pdf_falls_back_to_filesystem_when_not_in_db(self, tmp_path):
+ """Should fall back to checking filesystem if not found in database."""
+ manager = PDFStorageManager(tmp_path, "database")
+ mock_doc = MagicMock()
+ mock_doc.id = "doc-123"
+ mock_doc.file_type = "pdf"
+ mock_doc.file_path = "pdfs/test.pdf"
+ mock_session = MagicMock()
+
+ mock_first = mock_session.query.return_value.filter_by.return_value
+ mock_first.first.return_value = None
+
+ with patch.object(manager, "_get_safe_file_path") as mock_get_path:
+ mock_file_path = MagicMock()
+ mock_file_path.is_file.return_value = True
+ mock_get_path.return_value = mock_file_path
+
+ assert manager.has_pdf(mock_doc, mock_session) is True
+ mock_session.query.assert_called_once_with(DocumentBlob.document_id)
+ mock_session.query.return_value.filter_by.assert_called_once_with(
+ document_id="doc-123"
+ )
+ mock_get_path.assert_called_once_with("pdfs/test.pdf")
+
+ def test_has_pdf_returns_false_when_not_in_db_and_not_on_filesystem(
+ self, tmp_path
+ ):
+ """Should return False when PDF is in neither database nor filesystem."""
+ manager = PDFStorageManager(tmp_path, "database")
+ mock_doc = MagicMock()
+ mock_doc.id = "doc-123"
+ mock_doc.file_type = "pdf"
+ mock_doc.file_path = "pdfs/missing.pdf"
+ mock_session = MagicMock()
+
+ mock_first = mock_session.query.return_value.filter_by.return_value
+ mock_first.first.return_value = None
+
+ with patch.object(manager, "_get_safe_file_path") as mock_get_path:
+ mock_file_path = MagicMock()
+ mock_file_path.is_file.return_value = False
+ mock_get_path.return_value = mock_file_path
+
+ assert manager.has_pdf(mock_doc, mock_session) is False
+ mock_session.query.assert_called_once_with(DocumentBlob.document_id)
+ mock_session.query.return_value.filter_by.assert_called_once_with(
+ document_id="doc-123"
+ )
+ mock_get_path.assert_called_once_with("pdfs/missing.pdf")
+
+ def test_has_pdf_returns_false_when_no_blob_and_file_path_none(
+ self, tmp_path
+ ):
+ """Should return False when PDF is not in DB and document.file_path is None."""
manager = PDFStorageManager(tmp_path, "database")
mock_doc = MagicMock()
mock_doc.id = "doc-123"
@@ -196,13 +259,32 @@ class TestPDFStorageManagerHasPdf:
mock_doc.file_path = None
mock_session = MagicMock()
- # This test just verifies the method can be called
- # The actual implementation queries DocumentBlob
- try:
- manager.has_pdf(mock_doc, mock_session)
- except AttributeError:
- # Expected if DocumentBlob.id isn't accessible in test context
- pass
+ mock_first = mock_session.query.return_value.filter_by.return_value
+ mock_first.first.return_value = None
+
+ assert manager.has_pdf(mock_doc, mock_session) is False
+ mock_session.query.assert_called_once_with(DocumentBlob.document_id)
+ mock_session.query.return_value.filter_by.assert_called_once_with(
+ document_id="doc-123"
+ )
+
+ def test_has_pdf_returns_false_when_path_traversal_blocked(self, tmp_path):
+ """Should return False when path traversal protection rejects the file path."""
+ manager = PDFStorageManager(tmp_path, "database")
+ mock_doc = MagicMock()
+ mock_doc.id = "doc-123"
+ mock_doc.file_type = "pdf"
+ mock_doc.file_path = "../../etc/passwd"
+ mock_session = MagicMock()
+
+ mock_first = mock_session.query.return_value.filter_by.return_value
+ mock_first.first.return_value = None
+
+ assert manager.has_pdf(mock_doc, mock_session) is False
+ mock_session.query.assert_called_once_with(DocumentBlob.document_id)
+ mock_session.query.return_value.filter_by.assert_called_once_with(
+ document_id="doc-123"
+ )
class TestPDFStorageManagerPdfExists:
@@ -229,16 +311,13 @@ class TestPDFStorageManagerPdfExists:
# Simulate DocumentBlob found in database
mock_session.query.return_value.filter_by.return_value.first.return_value = MagicMock()
- mock_blob_class = MagicMock()
- with patch(
- "local_deep_research.database.models.library.DocumentBlob",
- mock_blob_class,
- ):
- result = PDFStorageManager.pdf_exists(
- tmp_path, mock_doc, mock_session
- )
+ result = PDFStorageManager.pdf_exists(tmp_path, mock_doc, mock_session)
assert result is True
+ mock_session.query.assert_called_once_with(DocumentBlob.document_id)
+ mock_session.query.return_value.filter_by.assert_called_once_with(
+ document_id="doc-123"
+ )
def test_returns_true_when_file_exists(self, tmp_path):
"""Should find PDF on filesystem via classmethod."""
@@ -255,16 +334,13 @@ class TestPDFStorageManagerPdfExists:
# No blob in database
mock_session.query.return_value.filter_by.return_value.first.return_value = None
- mock_blob_class = MagicMock()
- with patch(
- "local_deep_research.database.models.library.DocumentBlob",
- mock_blob_class,
- ):
- result = PDFStorageManager.pdf_exists(
- tmp_path, mock_doc, mock_session
- )
+ result = PDFStorageManager.pdf_exists(tmp_path, mock_doc, mock_session)
assert result is True
+ mock_session.query.assert_called_once_with(DocumentBlob.document_id)
+ mock_session.query.return_value.filter_by.assert_called_once_with(
+ document_id="doc-456"
+ )
class TestPDFStorageManagerDeletePdf: