97 lines
3.3 KiB
Python
97 lines
3.3 KiB
Python
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.config.database import AgentSessionLocal
|
|
from app.main import app
|
|
from app.model.entities import ScriptTemplate
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DATASET = ROOT / "docs" / "开发文档" / "29-Sprint2首批话术模板数据集.md"
|
|
SCRIPT = ROOT / "scripts" / "seed" / "import_script_templates.py"
|
|
client = TestClient(app)
|
|
|
|
|
|
def token_for(username: str, password: str) -> str:
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={"username": username, "password": password},
|
|
)
|
|
assert response.status_code == 200
|
|
return response.json()["data"]["access_token"]
|
|
|
|
|
|
def count_seed_templates() -> int:
|
|
with AgentSessionLocal() as session:
|
|
return session.query(ScriptTemplate).filter(ScriptTemplate.created_by == "seed:template_test_data").count()
|
|
|
|
|
|
def test_template_dataset_loads_at_least_20_test_templates():
|
|
assert DATASET.exists()
|
|
|
|
from scripts.seed.import_script_templates import load_templates_from_markdown
|
|
|
|
templates = load_templates_from_markdown(DATASET)
|
|
scenes = {template.scene for template in templates}
|
|
statuses = {template.review_status for template in templates}
|
|
|
|
assert len(templates) >= 20
|
|
assert {"loss_comfort", "product_recommend", "dual_record", "market_comment"}.issubset(scenes)
|
|
assert statuses <= {"test_data", "pending"}
|
|
assert all(template.is_approved is False for template in templates)
|
|
assert all(template.is_active is True for template in templates)
|
|
|
|
|
|
def test_template_import_is_idempotent_and_keeps_test_templates_unapproved():
|
|
from scripts.seed.import_compliance_rules import import_rules_from_markdown
|
|
from scripts.seed.import_script_templates import import_templates_from_markdown
|
|
|
|
import_rules_from_markdown()
|
|
first = import_templates_from_markdown(DATASET)
|
|
second = import_templates_from_markdown(DATASET)
|
|
|
|
assert first.total >= 20
|
|
assert second.total == first.total
|
|
assert count_seed_templates() == first.total
|
|
|
|
with AgentSessionLocal() as session:
|
|
rows = session.query(ScriptTemplate).filter(ScriptTemplate.created_by == "seed:template_test_data").all()
|
|
assert rows
|
|
assert all(row.is_approved is False for row in rows)
|
|
assert all(row.approved_by is None for row in rows)
|
|
assert all(row.is_active is True for row in rows)
|
|
|
|
|
|
def test_advisor_search_does_not_return_unapproved_seed_templates():
|
|
from scripts.seed.import_script_templates import import_templates_from_markdown
|
|
|
|
import_templates_from_markdown(DATASET)
|
|
advisor_token = token_for("advisor_test", "advisor_test")
|
|
|
|
response = client.get(
|
|
"/api/v1/templates/search",
|
|
params={"q": "DEV-TOP"},
|
|
headers={"Authorization": f"Bearer {advisor_token}"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["data"]["items"] == []
|
|
|
|
|
|
def test_template_import_script_prints_import_summary():
|
|
from scripts.seed.import_compliance_rules import import_rules_from_markdown
|
|
|
|
import_rules_from_markdown()
|
|
completed = subprocess.run(
|
|
[sys.executable, str(SCRIPT), "--dataset", str(DATASET)],
|
|
check=True,
|
|
capture_output=True,
|
|
text=True,
|
|
cwd=ROOT,
|
|
)
|
|
|
|
assert "Imported script templates:" in completed.stdout
|
|
assert "total=" in completed.stdout
|