muse-agent-example/tests/skills/访问数据库/test_authorization_snapshot_ddl.py

35 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""参考作品授权快照 DDL 的 append-only 静态门禁。"""
import pathlib
import re
import unittest
PROJECT_ROOT = pathlib.Path(__file__).resolve().parents[3]
DDL_PATH = PROJECT_ROOT / "muse" / "authority" / "db" / "ddl" / "96-example参考作品授权快照.sql"
class AuthorizationSnapshotDdlTest(unittest.TestCase):
def test_authorization_snapshot_is_append_only_and_version_unique(self):
self.assertTrue(DDL_PATH.is_file(), "缺少授权快照 DDL")
ddl = DDL_PATH.read_text(encoding="utf-8")
self.assertIn("CREATE TABLE example_reference_authorization_snapshot", ddl)
self.assertRegex(ddl, r"UNIQUE\s*\(tenant_id,\s*work_id,\s*snapshot_version\)")
self.assertRegex(ddl, r"BEFORE\s+UPDATE\s+OR\s+DELETE")
self.assertIn("source_hash", ddl)
self.assertIn("source_version", ddl)
self.assertRegex(ddl, r"research_only.*public_domain.*licensed.*unauthorized")
self.assertRegex(ddl, r"jsonb_typeof\(allowed_purpose\)\s*=\s*'array'")
self.assertRegex(ddl, r"jsonb_typeof\(forbidden_purpose\)\s*=\s*'array'")
self.assertRegex(
ddl,
r"CHECK\s*\(example_jsonb_text_arrays_disjoint\(allowed_purpose,\s*forbidden_purpose\)\)",
)
self.assertIn("authorization_basis <> 'user_authorization' OR copyright_status = 'research_only'", ddl)
self.assertIn("allowed_purpose = '[\"offline_evaluation\"]'::jsonb", ddl)
self.assertIsNotNone(re.search(r"RAISE\s+EXCEPTION", ddl, re.IGNORECASE))
if __name__ == "__main__":
unittest.main()