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.

test: polish pdf_storage_manager coverage and add changelog fragment (#5356)

Ziyang Guo114317b80e59
diff --git a/changelog.d/5263.bugfix.md b/changelog.d/5263.bugfix.md
new file mode 100644
index 0000000000..700f253cf7
--- /dev/null
+++ b/changelog.d/5263.bugfix.md
@@ -0,0 +1,4 @@
+Fixed a `DocumentBlob` query attribute error in the research library's
+`has_pdf()` check that could raise when probing whether a document's PDF
+was available, so PDF availability now resolves correctly across the
+database and filesystem backends.
diff --git a/tests/research_library/services/test_pdf_storage_manager.py b/tests/research_library/services/test_pdf_storage_manager.py
index 8c1c2d6289..05fc61af19 100644
--- a/tests/research_library/services/test_pdf_storage_manager.py
+++ b/tests/research_library/services/test_pdf_storage_manager.py
@@ -2,6 +2,7 @@
from unittest.mock import MagicMock, patch
+import pytest
from local_deep_research.constants import (
FILE_PATH_METADATA_ONLY,
@@ -280,7 +281,16 @@ class TestPDFStorageManagerHasPdf:
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
+ # Explicitly tie the False result to the traversal guard firing:
+ # patch _get_safe_file_path so we can assert it was consulted with
+ # the traversal string and returned None (the reject signal), rather
+ # than merely inferring rejection from the boolean result.
+ with patch.object(
+ manager, "_get_safe_file_path", return_value=None
+ ) as mock_get_path:
+ assert manager.has_pdf(mock_doc, mock_session) is False
+ mock_get_path.assert_called_once_with("../../etc/passwd")
+
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"
@@ -342,6 +352,45 @@ class TestPDFStorageManagerPdfExists:
document_id="doc-456"
)
+ def test_returns_false_on_db_miss_and_filesystem_miss(self, tmp_path):
+ """Direct negative case: no blob in the DB AND no file on disk must
+ yield False. Pins the both-backends-miss path end-to-end through the
+ classmethod (not just the non-PDF short-circuit)."""
+ mock_doc = MagicMock()
+ mock_doc.id = "doc-789"
+ mock_doc.file_type = "pdf"
+ # Points at a file that does not exist under library_root.
+ mock_doc.file_path = "pdfs/missing.pdf"
+ mock_session = MagicMock()
+
+ # No blob in database.
+ mock_session.query.return_value.filter_by.return_value.first.return_value = None
+
+ result = PDFStorageManager.pdf_exists(tmp_path, mock_doc, mock_session)
+
+ assert result 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-789"
+ )
+
+ def test_propagates_query_failure(self, tmp_path):
+ """A DB error while probing for the blob must NOT be swallowed and
+ misreported as "no PDF": ``has_pdf``/``pdf_exists`` deliberately have
+ no try/except around ``session.query(...)``, so a genuine backend
+ failure surfaces to the caller instead of masquerading as absence.
+ This locks that contract so a future refactor can't quietly turn DB
+ outages into false "PDF missing" results."""
+ mock_doc = MagicMock()
+ mock_doc.id = "doc-err"
+ mock_doc.file_type = "pdf"
+ mock_doc.file_path = None
+ mock_session = MagicMock()
+ mock_session.query.side_effect = RuntimeError("db connection lost")
+
+ with pytest.raises(RuntimeError, match="db connection lost"):
+ PDFStorageManager.pdf_exists(tmp_path, mock_doc, mock_session)
+
class TestPDFStorageManagerDeletePdf:
"""Tests for delete_pdf method."""