Files
work-volume-tracker/_tmp_verify_029.py
T
2026-09-30 16:42:02 +03:00

145 lines
5.9 KiB
Python

"""Browser smoke for task 029 (temporary)."""
from datetime import date
from decimal import Decimal
from playwright.sync_api import sync_playwright
from app.main import app
from app.database.database import SessionLocal
from app.models.project import Project
from app.models.agreement import Agreement, AgreementItem
from app.models.boq import BoqDocument, System, WorkItem
BASE = "http://127.0.0.1:8029"
PID = 2
results = []
def ok(name, cond, detail=""):
results.append((name, bool(cond), detail))
print(("PASS" if cond else "FAIL"), name, detail)
db = SessionLocal()
p = db.query(Project).filter_by(id=PID).first()
doc = db.query(BoqDocument).filter_by(project_id=PID).first()
system = db.query(System).filter_by(project_id=PID).first()
wi = db.query(WorkItem).filter(
WorkItem.project_id == PID,
WorkItem.system_id.isnot(None),
WorkItem.quantity > 0,
).first()
assert p and doc and system and wi
sys_id, wi_id, doc_id = system.id, wi.id, doc.id
print("ctx", PID, doc_id, sys_id, wi_id)
db.close()
def make_draft(number):
import time
number = f"{number}-{int(time.time() * 1000) % 100000}"
db = SessionLocal()
a = Agreement(
project_id=PID, number=number, date=date(2026, 1, 1),
title=f"Smoke {number}", status="draft", boq_document_id=doc_id,
)
db.add(a)
db.commit()
aid = a.id
db.close()
return aid
with sync_playwright() as pw:
b = pw.chromium.launch(headless=True)
page = b.new_page(viewport={"width": 1280, "height": 900})
page.on("dialog", lambda d: d.accept())
# 2-5. Empty draft: button exists, delete works
aid = make_draft("SM-EMPTY")
page.goto(f"{BASE}/projects/{PID}/agreements/{aid}")
page.wait_for_load_state("networkidle")
ok("empty-draft-btn", page.locator("button[data-bs-target='#deleteAgreementModal']").count() == 1)
ok("empty-draft-modal", "deleteAgreementModal" in page.content())
page.locator("button[data-bs-target='#deleteAgreementModal']").click()
page.wait_for_timeout(300)
page.locator("#deleteAgreementModal button.btn-danger").click()
page.wait_for_load_state("networkidle")
ok("empty-deleted", "agreements" in page.url and "deleted=1" in page.url, page.url)
ok("empty-flash", "Черновик ДС удалён" in page.content())
db = SessionLocal()
ok("empty-gone", db.get(Agreement, aid) is None)
db.close()
# 6-8. Draft with existing item: base WorkItem preserved
aid = make_draft("SM-EXIST")
page.goto(f"{BASE}/projects/{PID}/agreements/{aid}")
page.wait_for_load_state("networkidle")
page.fill("#existing-position-search", "")
page.wait_for_timeout(200)
# pick first autocomplete option
page.locator(".autocomplete-position").first.click()
page.locator("form[action$='/items/existing'] input[name=quantity_delta]").fill("3")
page.locator("button", has_text="Добавить позицию").click()
page.wait_for_load_state("networkidle")
ok("existing-added", "Увеличение" in page.content() or "3" in page.content())
page.locator("button[data-bs-target='#deleteAgreementModal']").click()
page.wait_for_timeout(300)
page.locator("#deleteAgreementModal button.btn-danger").click()
page.wait_for_load_state("networkidle")
db = SessionLocal()
ok("existing-agr-gone", db.get(Agreement, aid) is None)
ok("base-wi-kept", db.get(WorkItem, wi_id) is not None)
db.close()
# 9-11. Draft with new_position: orphan WorkItem removed
aid = make_draft("SM-NEW")
page.goto(f"{BASE}/projects/{PID}/agreements/{aid}")
page.wait_for_load_state("networkidle")
page.locator("form[action$='/items/new'] input[name=name]").fill("Сирота 029")
page.locator("form[action$='/items/new'] input[name=quantity_delta]").fill("2")
page.locator("form[action$='/items/new'] input[name=unit_price]").fill("5")
page.select_option("form[action$='/items/new'] select[name=system_id]", str(sys_id))
page.wait_for_timeout(100)
page.select_option("form[action$='/items/new'] select[name=section_id]", str(wi.section_id or 0) if False else page.locator("form[action$='/items/new'] select[name=section_id] option").nth(1).get_attribute("value"))
page.locator("button", has_text="Добавить новую позицию").click()
page.wait_for_load_state("networkidle")
ok("new-added", "Сирота 029" in page.content() or "Новая" in page.content())
db = SessionLocal()
orphan = db.query(WorkItem).filter_by(name="Сирота 029").first()
orphan_id = orphan.id if orphan else None
db.close()
ok("orphan-created", orphan_id is not None)
page.locator("button[data-bs-target='#deleteAgreementModal']").click()
page.wait_for_timeout(300)
page.locator("#deleteAgreementModal button.btn-danger").click()
page.wait_for_load_state("networkidle")
db = SessionLocal()
ok("new-agr-gone", db.get(Agreement, aid) is None)
ok("orphan-wi-gone", orphan_id is None or db.get(WorkItem, orphan_id) is None)
db.close()
# 12-13. Approved cannot be deleted
db = SessionLocal()
a = Agreement(project_id=PID, number="SM-APPR", date=date(2026, 1, 1),
title="Approved smoke", status="approved", boq_document_id=doc_id)
db.add(a)
db.commit()
appr_id = a.id
db.close()
page.goto(f"{BASE}/projects/{PID}/agreements/{appr_id}")
page.wait_for_load_state("networkidle")
ok("approved-no-btn", page.locator("button[data-bs-target='#deleteAgreementModal']").count() == 0)
ok("approved-no-modal", "deleteAgreementModal" not in page.content())
# direct POST blocked
resp = page.request.post(f"{BASE}/projects/{PID}/agreements/{appr_id}/delete")
ok("approved-post-blocked", resp.status == 409, str(resp.status))
page.screenshot(path="output/playwright/029-approved-no-delete.png")
b.close()
print("\nSUMMARY", sum(1 for r in results if r[1]), "/", len(results))
for n, p, d in results:
if not p:
print("FAIL", n, d)