92 lines
3.4 KiB
Python
92 lines
3.4 KiB
Python
"""P4.0: категория жалобы, source, старые NULL видны в attention."""
|
|
import importlib
|
|
import os
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
if ROOT not in sys.path:
|
|
sys.path.insert(0, ROOT)
|
|
|
|
|
|
@pytest.fixture()
|
|
def env(tmp_path, monkeypatch):
|
|
monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))
|
|
monkeypatch.setenv("SESSION_SECRET", "test-secret")
|
|
monkeypatch.setenv("AI_BACKEND", "claude")
|
|
monkeypatch.chdir(ROOT)
|
|
import auth
|
|
import db
|
|
import main
|
|
import attention
|
|
for m in (db, auth, main, attention):
|
|
importlib.reload(m)
|
|
db.init_db()
|
|
db.create_user("joe@x", auth.hash_password("Passw0rd!ok"))
|
|
db.admin_verify_user("joe@x")
|
|
from fastapi.testclient import TestClient
|
|
client = TestClient(main.app)
|
|
h = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
|
|
return db, client, h, attention
|
|
|
|
|
|
def test_category_and_source_are_stored(env):
|
|
db, client, h, attention = env
|
|
r = client.post("/api/feedback", json={"comment": "не то", "answer": "a",
|
|
"category": "off_topic", "source": "chat"}, headers=h)
|
|
assert r.status_code == 200, r.text
|
|
row = db.get_feedback(1)[0]
|
|
assert row["category"] == "off_topic" and row["source"] == "chat"
|
|
assert db.count_feedback_new() == 1
|
|
keys = {i["key"] for i in attention.collect({}, {"accounts_total_rub": 0})["items"]}
|
|
assert "feedback:new" in keys
|
|
assert client.post("/api/feedback", json={"comment": "x", "category": "nope"},
|
|
headers=h).status_code == 400
|
|
|
|
|
|
def test_old_null_category_still_counts(env):
|
|
db, client, h, attention = env
|
|
db.save_feedback("joe@x", "t1", "старая жалоба", "a")
|
|
row = db.get_feedback(1)[0]
|
|
assert row["category"] is None and row["status"] == "new"
|
|
assert db.count_feedback_new() == 1
|
|
|
|
|
|
def test_praise_does_not_raise_red_alert(env):
|
|
db, client, h, attention = env
|
|
db.save_feedback("joe@x", "t1", "спасибо", "a", category="praise", source="chat")
|
|
assert db.count_feedback_new() == 0
|
|
keys = {i["key"] for i in attention.collect({}, {"accounts_total_rub": 0})["items"]}
|
|
assert "feedback:new" not in keys
|
|
|
|
|
|
def test_old_schema_migrates(tmp_path, monkeypatch):
|
|
import sqlite3
|
|
p = tmp_path / "old.db"
|
|
c = sqlite3.connect(p)
|
|
c.executescript("CREATE TABLE feedback (id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
"timestamp REAL NOT NULL, username TEXT NOT NULL, tab_id TEXT, "
|
|
"comment TEXT, answer TEXT);"
|
|
"INSERT INTO feedback (timestamp,username,comment,answer) "
|
|
"VALUES (1,'a','старая','ans');")
|
|
c.commit(); c.close()
|
|
monkeypatch.setenv("DB_PATH", str(p))
|
|
monkeypatch.setenv("SESSION_SECRET", "s")
|
|
import db
|
|
importlib.reload(db)
|
|
db.init_db()
|
|
row = db.get_feedback(1)[0]
|
|
assert row["category"] is None and row["status"] == "new"
|
|
assert db.count_feedback_new() == 1
|
|
|
|
|
|
def test_status_closes_alert(env):
|
|
db, client, h, attention = env
|
|
db.save_feedback("joe@x", "t1", "x", "a", category="failure", source="chat")
|
|
fid = db.get_feedback(1)[0]["id"]
|
|
assert db.set_feedback_status(fid, "resolved") is True
|
|
assert db.count_feedback_new() == 0
|
|
with pytest.raises(ValueError):
|
|
db.set_feedback_status(fid, "bad")
|