PageIndex/tests/test_sqlite_storage.py
mountain 956147d864 fix: resolve five P1 defects from the SDK review
- AgentRunner.run: offload to a worker-thread event loop when called
  from inside a running loop (Jupyter, FastAPI handlers) — mirrors
  pipeline._run_async; Runner.run_sync raised RuntimeError there.

- SQLiteStorage: create connections with check_same_thread=False so
  close() can actually close connections created by worker threads.
  Each thread still gets its own connection via threading.local; with
  the default True those closes raised ProgrammingError (silently
  swallowed) and leaked every worker connection.

- CloudBackend.query: non-streaming chat completions now use a 300s
  timeout and a single attempt. The default 30s ReadTimeout fired
  before generation finished and the retry loop re-billed the full
  server-side retrieval + generation up to three times. _request gains
  retries/timeout overrides; the exhausted-retry path also no longer
  sleeps before raising.

- MarkdownParser: content before the first heading (abstract/preamble)
  becomes a node instead of being silently dropped and unretrievable;
  a file with no headings at all yields a single document node instead
  of zero nodes (which pushed an empty page list into the pipeline).

- LegacyCloudAPI.is_retrieval_ready: API failures (revoked key, network
  down) now propagate as PageIndexAPIError instead of reading as
  "not ready", which turned polling loops into infinite loops.

Adds regression tests for each fix.

Claude-Session: https://claude.ai/code/session_01Kx5DgKbhK1N8autqXH8SmS
2026-07-07 10:12:51 +08:00

81 lines
3.1 KiB
Python

import pytest
from pageindex.storage.sqlite import SQLiteStorage
@pytest.fixture
def storage(tmp_path):
return SQLiteStorage(str(tmp_path / "test.db"))
def test_create_and_list_collections(storage):
storage.create_collection("papers")
assert "papers" in storage.list_collections()
def test_get_or_create_collection_idempotent(storage):
storage.get_or_create_collection("papers")
storage.get_or_create_collection("papers")
assert storage.list_collections().count("papers") == 1
def test_delete_collection(storage):
storage.create_collection("papers")
storage.delete_collection("papers")
assert "papers" not in storage.list_collections()
def test_save_and_get_document(storage):
storage.create_collection("papers")
doc = {
"doc_name": "test.pdf", "doc_description": "A test",
"file_path": "/tmp/test.pdf", "doc_type": "pdf",
"structure": [{"title": "Intro", "node_id": "0001"}],
}
storage.save_document("papers", "doc-1", doc)
result = storage.get_document("papers", "doc-1")
assert result["doc_name"] == "test.pdf"
assert result["doc_type"] == "pdf"
def test_get_document_structure(storage):
storage.create_collection("papers")
structure = [{"title": "Ch1", "node_id": "0001", "nodes": []}]
storage.save_document("papers", "doc-1", {
"doc_name": "test.pdf", "doc_type": "pdf",
"file_path": "/tmp/test.pdf", "structure": structure,
})
result = storage.get_document_structure("papers", "doc-1")
assert result[0]["title"] == "Ch1"
def test_list_documents(storage):
storage.create_collection("papers")
storage.save_document("papers", "doc-1", {"doc_name": "p1.pdf", "doc_type": "pdf", "file_path": "/tmp/p1.pdf", "structure": []})
storage.save_document("papers", "doc-2", {"doc_name": "p2.pdf", "doc_type": "pdf", "file_path": "/tmp/p2.pdf", "structure": []})
docs = storage.list_documents("papers")
assert len(docs) == 2
def test_delete_document(storage):
storage.create_collection("papers")
storage.save_document("papers", "doc-1", {"doc_name": "test.pdf", "doc_type": "pdf", "file_path": "/tmp/test.pdf", "structure": []})
storage.delete_document("papers", "doc-1")
assert len(storage.list_documents("papers")) == 0
def test_delete_collection_cascades_documents(storage):
storage.create_collection("papers")
storage.save_document("papers", "doc-1", {"doc_name": "test.pdf", "doc_type": "pdf", "file_path": "/tmp/test.pdf", "structure": []})
storage.delete_collection("papers")
assert "papers" not in storage.list_collections()
def test_close_closes_connections_created_in_other_threads(storage):
"""Regression: with check_same_thread=True, close() from another thread
raised ProgrammingError (swallowed) and leaked every worker connection."""
import sqlite3
import threading
conns = {}
def worker():
conns["worker"] = storage._get_conn()
t = threading.Thread(target=worker)
t.start()
t.join()
storage.close() # main thread closes the worker's connection too
with pytest.raises(sqlite3.ProgrammingError):
conns["worker"].execute("SELECT 1")