Files
estimator/tests/unit/api/test_audit_and_preliminary.py
T
2026-11-09 08:43:30 +03:00

112 lines
6.2 KiB
Python

"""Who did what (the audit trail) and the 'preliminary' marker of estimates built on assumptions."""
import pytest
from tests.fakes import AutoGateway
from tests.unit.agent.conftest import clarify_out, q
from tests.unit.api.test_estimates_api import REQ, A, B, create, make_client, wait
EDIT = {"optimistic": 16, "likely": 32, "pessimistic": 64}
def test_every_version_records_who_made_it(monkeypatch, backend_dsn):
gw = AutoGateway(clarify=[clarify_out([q("crm", "Какая CRM?", blocking=True)]), clarify_out([])])
with make_client(monkeypatch, backend_dsn, gw) as c:
v1 = create(c)
assert (v1["created_by"], v1["estimate_created_by"]) == ("client_a", "client_a")
eid = v1["id"]
wait(c, eid)
answered = c.post(f"/api/estimates/{eid}/answers", json={"answers": [{"question_id": "crm", "answer": "amoCRM"}]}, headers=A).json()
wait(c, eid)
edited = c.patch(f"/api/estimates/{eid}/tasks/t4", json=EDIT, headers=A).json()
switched = c.post(f"/api/estimates/{eid}/engagement", json={"engagement": "time_and_materials"}, headers=A).json()
vat = c.post(f"/api/estimates/{eid}/vat", json={"vat_rate": 0}, headers=A).json()
recalculated = c.post(f"/api/estimates/{eid}/recalculate", headers=A).json()
for v in (answered, edited, switched, vat, recalculated):
assert v["created_by"] == "client_a" and v["estimate_created_by"] == "client_a"
assert [v["version"] for v in (answered, edited, switched, vat, recalculated)] == [2, 3, 4, 5, 6]
def test_the_creator_of_the_estimate_stays_while_later_versions_have_their_own_author(monkeypatch, backend_dsn):
"""Two clients cannot share an estimate through the API, so a second author is simulated at the
service level: the estimate keeps its creator and the new version records the person."""
import asyncio
from estimator.config.sections import DatabaseCfg
from estimator.domain.enums import Region
from estimator.domain.estimates import NewVersion
from estimator.infra.db import SqlEstimateRepo, make_engine, make_session_factory
async def go():
engine = make_engine(DatabaseCfg(dsn=backend_dsn))
try:
repo = SqlEstimateRepo(make_session_factory(engine))
data = NewVersion(requirements="x", region=Region.RU, currency="RUB", created_by="alex")
eid, *_ = await repo.create(client="ui", idempotency_key=None, request_hash="h", data=data)
v2 = await repo.add_version(client="ui", estimate_id=eid, data=data.model_copy(update={"created_by": "bob"}))
first, second = await repo.get("ui", eid, 1), await repo.get("ui", eid, v2)
return first, second, (await repo.list_page("ui", status=None, limit=5, cursor=None)).items[0]
finally:
await engine.dispose()
first, second, item = asyncio.run(go())
assert (first.created_by, first.estimate_created_by) == ("alex", "alex")
assert (second.created_by, second.estimate_created_by) == ("bob", "alex")
assert item.created_by == "alex"
def test_the_list_shows_who_started_each_estimate(monkeypatch, backend_dsn):
with make_client(monkeypatch, backend_dsn, AutoGateway()) as c:
wait(c, create(c)["id"])
items = c.get("/api/estimates", headers=A).json()["items"]
assert items[0]["created_by"] == "client_a"
assert c.get("/api/estimates", headers=B).json()["items"] == []
# ------------------------------------------------------------------ preliminary
def test_a_complete_estimate_is_not_preliminary(monkeypatch, backend_dsn):
with make_client(monkeypatch, backend_dsn, AutoGateway()) as c:
body = wait(c, create(c)["id"])
assert body["result"]["preliminary"] is False and body["result"]["preliminary_reasons"] == []
assert c.get("/api/estimates", headers=A).json()["items"][0]["preliminary"] is False
def test_an_estimate_that_rests_on_assumptions_is_preliminary_and_says_why(monkeypatch, backend_dsn):
gw = AutoGateway(clarify=[clarify_out([q("crm", "Какая CRM?", blocking=True), q("tone", "Какой тон?")])])
with make_client(monkeypatch, backend_dsn, gw) as c:
body = wait(c, create(c, {**REQ, "proceed_with_assumptions": True})["id"])
r = body["result"]
assert body["status"] == "done" and r["preliminary"] is True
assert r["preliminary_reasons"] == ["Не уточнено: Какая CRM?", "Не уточнено: Какой тон?"]
assert c.get("/api/estimates", headers=A).json()["items"][0]["preliminary"] is True
def test_answering_the_questions_makes_the_next_version_final(monkeypatch, backend_dsn):
gw = AutoGateway(clarify=[clarify_out([q("crm", "Какая CRM?", blocking=True)]), clarify_out([])])
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c, {**REQ, "proceed_with_assumptions": True})["id"]
wait(c, eid) # the blocking question was skipped by proceeding on assumptions
again = c.post(f"/api/estimates/{eid}/recalculate", json={"proceed_with_assumptions": False}, headers=A).json()
assert again["version"] == 2
final = wait(c, eid)
assert final["status"] == "done" and final["result"]["preliminary"] is False
def test_changing_how_an_estimate_is_shown_keeps_its_preliminary_status(monkeypatch, backend_dsn):
gw = AutoGateway(clarify=[clarify_out([q("crm", "Какая CRM?", blocking=True)])])
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c, {**REQ, "proceed_with_assumptions": True})["id"]
wait(c, eid)
switched = c.post(f"/api/estimates/{eid}/engagement", json={"engagement": "time_and_materials"}, headers=A).json()
assert switched["result"]["preliminary"] is True
edited = c.patch(f"/api/estimates/{eid}/tasks/t4", json=EDIT, headers=A).json()
assert edited["result"]["preliminary"] is True # a manual edit does not answer the open questions
@pytest.mark.parametrize("flag", [True, False])
def test_the_preliminary_flag_depends_on_open_points_not_on_the_proceed_flag(monkeypatch, backend_dsn, flag):
with make_client(monkeypatch, backend_dsn, AutoGateway()) as c: # no questions at all
body = wait(c, create(c, {**REQ, "proceed_with_assumptions": flag})["id"])
assert body["result"]["preliminary"] is False