Files
simbarag/tests/unit/test_manage_vectorstore.py
T
ryan b8d8bb8a57
CI / backend (push) Successful in 42s
CI / frontend (push) Failing after 7s
CI / backend (pull_request) Successful in 42s
CI / frontend (pull_request) Failing after 7s
Fetch Paperless before wiping the index in reindex
`reindex` deleted every chunk before contacting Paperless, so an unreachable
or empty Paperless left the store empty (which happened when Paperless was
crash-looping during its v3 upgrade). It now fetches first, aborts without
deleting if that fails or returns nothing, and indexes the fetched documents
instead of fetching twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 08:30:27 -04:00

67 lines
2.0 KiB
Python

import sys
from unittest.mock import AsyncMock, MagicMock
import pytest
from scripts import manage_vectorstore as cli
@pytest.fixture
def calls(monkeypatch):
calls = []
monkeypatch.setattr(cli, "delete_all_documents", lambda: calls.append("delete_all"))
monkeypatch.setattr(
cli,
"delete_documents_by_metadata",
lambda k, v: calls.append(f"delete {k}={v}"),
)
monkeypatch.setattr(
cli,
"fetch_documents_from_paperless_ngx",
AsyncMock(side_effect=lambda: calls.append("fetch") or ["doc"]),
)
monkeypatch.setattr(
cli,
"index_documents",
AsyncMock(side_effect=lambda *docs: calls.append(f"paperless {list(docs)}")),
)
monkeypatch.setattr(
cli,
"index_obsidian_documents",
AsyncMock(side_effect=lambda: calls.append("obsidian")),
)
monkeypatch.setattr(cli, "stats", MagicMock())
return calls
@pytest.mark.parametrize(
"command, expected",
[
# A full reindex must not drop Obsidian notes after wiping everything.
# Fetch before deleting, then index the fetched documents (no refetch).
("reindex", ["fetch", "delete_all", "paperless [['doc']]", "obsidian"]),
("reindex-obsidian", ["delete source=obsidian", "obsidian"]),
],
)
def test_reindex_commands(calls, monkeypatch, command, expected):
monkeypatch.setattr(sys, "argv", ["manage_vectorstore", command])
cli.main()
assert calls == expected
@pytest.mark.parametrize(
"failure",
[
AsyncMock(side_effect=ConnectionError("Name or service not known")),
AsyncMock(return_value=[]),
],
)
def test_reindex_leaves_index_untouched_when_paperless_fails(
calls, monkeypatch, failure
):
monkeypatch.setattr(cli, "fetch_documents_from_paperless_ngx", failure)
monkeypatch.setattr(sys, "argv", ["manage_vectorstore", "reindex"])
with pytest.raises(SystemExit):
cli.main()
assert "delete_all" not in calls