astrbot_plugin_imagestudio/tests/test_pg_smoke.py

82 行
2.4 KiB
Python

2026-07-06 04:12:43 +08:00
from __future__ import annotations
import os
import tempfile
import unittest
from pathlib import Path
from scripts.imagestudio_pg_smoke import build_pg_smoke_plan, run_pg_smoke
class PostgresSmokeTests(unittest.TestCase):
def test_pg_smoke_plan_redacts_config_and_reports_connection_shape(self):
with tempfile.TemporaryDirectory() as tmp:
config_path = Path(tmp) / "config.yaml"
config_path.write_text(
"""
database:
backend: postgresql
postgresql:
enabled: true
host: localhost
port: 5432
database: imagestudio_test
user: imagestudio
password: super-secret
""",
encoding="utf-8",
)
plan = build_pg_smoke_plan(config_path)
self.assertEqual(plan["config_path"], "***PATH***")
self.assertEqual(plan["selected_backend"], "postgresql")
self.assertTrue(plan["postgresql_configured"])
self.assertEqual(plan["connection_mode"], "host")
self.assertTrue(plan["password_set"])
self.assertNotIn("super-secret", str(plan))
self.assertNotIn(str(config_path), str(plan))
def test_pg_smoke_dry_run_does_not_connect(self):
with tempfile.TemporaryDirectory() as tmp:
config_path = Path(tmp) / "config.yaml"
config_path.write_text(
"""
database:
backend: sqlite
""",
encoding="utf-8",
)
result = run_pg_smoke(config_path, dry_run=True)
self.assertTrue(result["ok"])
self.assertTrue(result["dry_run"])
self.assertEqual(result["plan"]["selected_backend"], "sqlite")
@unittest.skipUnless(os.environ.get("IMAGESTUDIO_TEST_PG_DSN"), "IMAGESTUDIO_TEST_PG_DSN is not set")
def test_live_pg_smoke_from_env_dsn(self):
with tempfile.TemporaryDirectory() as tmp:
config_path = Path(tmp) / "config.yaml"
config_path.write_text(
f"""
database:
backend: postgresql
postgresql:
enabled: true
dsn: "{os.environ["IMAGESTUDIO_TEST_PG_DSN"]}"
""",
encoding="utf-8",
)
result = run_pg_smoke(config_path)
self.assertTrue(result["ok"])
self.assertFalse(result["dry_run"])
self.assertGreaterEqual(result["migrations_applied"], 1)
self.assertGreaterEqual(result["material_categories"], 1)
if __name__ == "__main__":
unittest.main()