99 lines
2.8 KiB
Python
99 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""search 公共范式专用范围的离线合同测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from search import search_cards
|
|
|
|
|
|
class _Result:
|
|
def __init__(self, rows):
|
|
self._rows = rows
|
|
|
|
def fetchall(self):
|
|
return self._rows
|
|
|
|
|
|
class _Connection:
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *_args):
|
|
return False
|
|
|
|
def execute(self, sql, args):
|
|
self.calls.append((sql, list(args)))
|
|
if "muse_meta_schema" in sql:
|
|
return _Result([])
|
|
return _Result(
|
|
[
|
|
(
|
|
"draft",
|
|
7,
|
|
{
|
|
"型": "craft",
|
|
"名称": "递进揭示",
|
|
"一句话摘要": "逐层增加信息压力",
|
|
"字段": {"节拍": "三段递进"},
|
|
"目标库": "公共范式库",
|
|
},
|
|
"pending",
|
|
0.91,
|
|
3,
|
|
{},
|
|
None,
|
|
"active",
|
|
)
|
|
]
|
|
)
|
|
|
|
|
|
class SearchCardsTest(unittest.TestCase):
|
|
def test_public_pattern_scope_is_sql_enforced_and_tenant_bound(self):
|
|
conn = _Connection()
|
|
|
|
cards = search_cards(
|
|
"信息揭示",
|
|
scope="public_pattern",
|
|
ttype="craft",
|
|
dsn="postgresql://test/muse-example",
|
|
tenant_id=7,
|
|
connection_factory=lambda dsn: conn if dsn == "postgresql://test/muse-example" else None,
|
|
embedder=lambda _intent: [0.1, 0.2],
|
|
)
|
|
|
|
self.assertEqual(len(conn.calls), 2)
|
|
policy_sql, policy_args = conn.calls[0]
|
|
self.assertIn("muse_meta_schema", policy_sql)
|
|
self.assertEqual(policy_args, [7])
|
|
sql, args = conn.calls[1]
|
|
for fragment in (
|
|
"d.work_id=0",
|
|
"d.draft_payload->>'目标库'='公共范式库'",
|
|
"kb.kb_type='global'",
|
|
"d.source_action_policy",
|
|
"d.draft_payload->>'型' = %s",
|
|
):
|
|
self.assertIn(fragment, sql)
|
|
self.assertEqual(args, ["[0.1, 0.2]", 7, 7, 7, "craft", 5])
|
|
self.assertEqual(cards[0]["retrievalScope"], "public_pattern")
|
|
self.assertTrue(cards[0]["productionRetrievalEligible"])
|
|
|
|
def test_unknown_scope_fails_before_connecting(self):
|
|
with self.assertRaises(ValueError):
|
|
search_cards(
|
|
"冲突",
|
|
scope="unknown",
|
|
connection_factory=lambda _dsn: _Connection(),
|
|
embedder=lambda _intent: [0.1],
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|