288 lines
18 KiB
Python
288 lines
18 KiB
Python
from datetime import date
|
|
from decimal import Decimal
|
|
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
from app.database.database import Base
|
|
from app.main import app
|
|
from app.models.project import Project
|
|
from app.models import boq
|
|
from app.models.agreement import Agreement, AgreementItem
|
|
from app.models.daily_report import DailyReport, DailyReportItem
|
|
from app.routers import projects as project_router, daily_reports, agreements as agreement_router
|
|
from app.services.effective_plan import build_effective_plan
|
|
|
|
|
|
def test_agreement_ui_draft_then_approval_updates_plan_and_daily_reports():
|
|
from app.models import work_journal as work_journal_models
|
|
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
|
Base.metadata.create_all(engine)
|
|
TestingSession = sessionmaker(bind=engine, expire_on_commit=False)
|
|
|
|
with TestingSession() as db:
|
|
project = Project(name="Agreement UI project")
|
|
db.add(project); db.flush()
|
|
system = boq.System(project_id=project.id, name="System A", sort_order=1)
|
|
db.add(system); db.flush()
|
|
section = boq.Section(system_id=system.id, name="Section A")
|
|
db.add(section); db.flush()
|
|
doc = boq.BoqDocument(project_id=project.id, name="Base BOQ")
|
|
db.add(doc); db.flush()
|
|
base_item = boq.WorkItem(project_id=project.id, boq_document_id=doc.id,
|
|
system_id=system.id, section_id=section.id,
|
|
source_number="1", name="Existing item", unit="pcs",
|
|
quantity=100, unit_price=10, amount=1000)
|
|
db.add(base_item); db.commit()
|
|
project_id, doc_id = project.id, doc.id
|
|
system_id, section_id, base_item_id = system.id, section.id, base_item.id
|
|
|
|
def override_db():
|
|
db = TestingSession()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
for dependency in (project_router.get_db, daily_reports.get_db, agreement_router.get_db):
|
|
app.dependency_overrides[dependency] = override_db
|
|
try:
|
|
with TestClient(app) as client:
|
|
assert client.get(f"/projects/{project_id}/agreements").status_code == 200
|
|
form = client.get(f"/projects/{project_id}/agreements/new")
|
|
assert form.status_code == 200 and f"boq:{doc_id}" in form.text
|
|
created = client.post(f"/projects/{project_id}/agreements", data={
|
|
"number": "A-1", "agreement_date": "2026-09-25", "title": "Scope test",
|
|
"description": "UI workflow", "scope": f"boq:{doc_id}",
|
|
}, follow_redirects=False)
|
|
assert created.status_code == 303
|
|
detail_url = created.headers["location"]
|
|
detail = client.get(detail_url)
|
|
assert detail.status_code == 200 and "approveModal" in detail.text
|
|
assert "data-position-picker" in detail.text
|
|
assert 'name="work_item_id"' in detail.text
|
|
assert f'data-id="{base_item_id}"' in detail.text
|
|
assert "data-search=" in detail.text and "Existing item" in detail.text
|
|
assert "replace(/\\s+/g,' ')" in detail.text and "shown>=20" in detail.text
|
|
agreement_id = int(detail_url.rsplit("/", 1)[1])
|
|
|
|
existing = client.post(f"{detail_url}/items/existing", data={
|
|
"work_item_id": base_item_id, "quantity_delta": "5",
|
|
}, follow_redirects=False)
|
|
assert existing.status_code == 303
|
|
existing_line = db.query(AgreementItem).filter_by(agreement_id=agreement_id).one()
|
|
edited_existing = client.post(f"{detail_url}/items/{existing_line.id}/edit", data={
|
|
"quantity_delta": "6",
|
|
}, follow_redirects=False)
|
|
assert edited_existing.status_code == 303
|
|
edited_meta = client.post(f"{detail_url}/update", data={
|
|
"number": "A-1", "agreement_date": "2026-09-25", "title": "Scope test edited",
|
|
"description": "Edited description", "scope": f"boq:{doc_id}",
|
|
}, follow_redirects=False)
|
|
assert edited_meta.status_code == 303
|
|
with TestingSession() as db:
|
|
draft_plan = build_effective_plan(db, project_id)
|
|
assert next(row for row in draft_plan["items"] if row.item.id == base_item_id).quantity == Decimal("100.0000")
|
|
assert db.query(boq.WorkItem).filter_by(project_id=project_id).count() == 1
|
|
|
|
added_new = client.post(f"{detail_url}/items/new", data={
|
|
"name": "New draft item", "model": "M-1", "manufacturer": "Maker",
|
|
"unit": "pcs", "quantity_delta": "2", "unit_price": "3.5",
|
|
"system_id": system_id, "section_id": section_id,
|
|
}, follow_redirects=False)
|
|
assert added_new.status_code == 303
|
|
with TestingSession() as db:
|
|
line = db.query(AgreementItem).filter_by(agreement_id=agreement_id,
|
|
change_type="new_position").one()
|
|
new_line_id = line.id
|
|
draft_item_id = line.work_item_id
|
|
edited_new = client.post(f"{detail_url}/items/{new_line_id}/edit", data={
|
|
"quantity_delta": "3", "unit_price": "4.5", "name": "New draft item edited",
|
|
"model": "M-2", "manufacturer": "Maker 2", "unit": "pcs",
|
|
"system_id": system_id, "section_id": section_id,
|
|
}, follow_redirects=False)
|
|
assert edited_new.status_code == 303
|
|
with TestingSession() as db:
|
|
assert db.get(boq.WorkItem, draft_item_id) is not None
|
|
plan_ids = {row.item.id for row in build_effective_plan(db, project_id)["items"]}
|
|
assert draft_item_id not in plan_ids
|
|
assert "New draft item" not in client.get(
|
|
f"/projects/{project_id}/daily-reports/new?system_id={system_id}"
|
|
).text
|
|
blocked = client.post(f"/projects/{project_id}/daily-reports", data={
|
|
"report_date": "2026-09-25", "system_id": system_id,
|
|
f"quantity_{draft_item_id}": "1",
|
|
})
|
|
assert blocked.status_code == 400
|
|
|
|
approval = client.post(f"{detail_url}/approve", follow_redirects=False)
|
|
assert approval.status_code == 303
|
|
with TestingSession() as db:
|
|
plan = build_effective_plan(db, project_id)
|
|
plan_rows = {row.item.id: row for row in plan["items"]}
|
|
assert plan_rows[base_item_id].quantity == Decimal("106.0000")
|
|
assert plan_rows[draft_item_id].quantity == Decimal("3.0000")
|
|
assert plan_rows[draft_item_id].amount == Decimal("13.500000000000")
|
|
assert db.query(boq.WorkItem).filter_by(project_id=project_id).count() == 2
|
|
assert db.query(Agreement).filter_by(id=agreement_id).one().status == "approved"
|
|
dr_form = client.get(f"/projects/{project_id}/daily-reports/new?system_id={system_id}")
|
|
assert dr_form.status_code == 200 and "New draft item" in dr_form.text
|
|
saved_fact = client.post(f"/projects/{project_id}/daily-reports", data={
|
|
"report_date": "2026-09-25", "system_id": system_id,
|
|
f"quantity_{draft_item_id}": "1",
|
|
}, follow_redirects=False)
|
|
assert saved_fact.status_code == 303
|
|
assert client.get(f"/projects/{project_id}").status_code == 200
|
|
assert client.get(detail_url).status_code == 200
|
|
readonly = client.post(f"{detail_url}/update", data={
|
|
"number": "A-1", "agreement_date": "2026-09-25", "title": "Changed",
|
|
"description": "", "scope": f"boq:{doc_id}",
|
|
})
|
|
assert readonly.status_code == 409
|
|
project_scope = client.post(f"/projects/{project_id}/agreements", data={
|
|
"number": "A-2", "agreement_date": "2026-09-25", "title": "Project scope",
|
|
"description": "", "scope": "project",
|
|
}, follow_redirects=False)
|
|
assert project_scope.status_code == 303
|
|
with TestingSession() as db:
|
|
project_agreement = db.query(Agreement).filter_by(number="A-2").one()
|
|
assert project_agreement.boq_document_id is None
|
|
scope_detail = client.get(project_scope.headers["location"])
|
|
assert scope_detail.status_code == 200
|
|
assert "\u0412\u0435\u0441\u044c \u043f\u0440\u043e\u0435\u043a\u0442" in scope_detail.text
|
|
finally:
|
|
for dependency in (project_router.get_db, daily_reports.get_db, agreement_router.get_db):
|
|
app.dependency_overrides.pop(dependency, None)
|
|
engine.dispose()
|
|
|
|
|
|
def test_project_wide_agreement_can_create_project_system_section_and_unlinked_work_item():
|
|
from app.models import work_journal as work_journal_models
|
|
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
|
Base.metadata.create_all(engine)
|
|
TestingSession = sessionmaker(bind=engine, expire_on_commit=False)
|
|
with TestingSession() as db:
|
|
project = Project(name="Project-wide agreement")
|
|
db.add(project); db.flush()
|
|
scud_boq = boq.BoqDocument(project_id=project.id, name="BOQ SCUD")
|
|
ops_boq = boq.BoqDocument(project_id=project.id, name="BOQ OPS")
|
|
db.add_all([scud_boq, ops_boq]); db.commit()
|
|
project_id, scud_boq_id, ops_boq_id = project.id, scud_boq.id, ops_boq.id
|
|
|
|
def override_db():
|
|
db = TestingSession()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
for dependency in (project_router.get_db, daily_reports.get_db, agreement_router.get_db):
|
|
app.dependency_overrides[dependency] = override_db
|
|
try:
|
|
with TestClient(app) as client:
|
|
response = client.post(f"/projects/{project_id}/agreements", data={
|
|
"number": "PW-1", "agreement_date": "2026-09-25", "title": "Repair",
|
|
"description": "", "scope": "project",
|
|
}, follow_redirects=False)
|
|
assert response.status_code == 303
|
|
detail_url = response.headers["location"]
|
|
agreement_id = int(detail_url.rsplit("/", 1)[1])
|
|
|
|
created_system = client.post(f"{detail_url}/systems", data={"name": "\u0421\u0430\u043d\u0442\u0435\u0445\u043d\u0438\u043a\u0430"})
|
|
assert created_system.status_code == 200
|
|
system_data = created_system.json()
|
|
created_section = client.post(f"{detail_url}/sections", data={
|
|
"system_id": system_data["id"], "name": "\u0421\u0430\u043d\u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u044b",
|
|
})
|
|
assert created_section.status_code == 200
|
|
section_data = created_section.json()
|
|
assert section_data["system_id"] == system_data["id"]
|
|
|
|
added = client.post(f"{detail_url}/items/new", data={
|
|
"name": "\u0420\u0435\u043c\u043e\u043d\u0442 \u0443\u043d\u0438\u0442\u0430\u0437\u0430", "model": "", "manufacturer": "", "unit": "\u0448\u0442",
|
|
"quantity_delta": "1", "unit_price": "1250.00",
|
|
"system_id": system_data["id"], "section_id": section_data["id"],
|
|
"source_boq_document_id": scud_boq_id,
|
|
}, follow_redirects=False)
|
|
assert added.status_code == 303, added.text
|
|
ops_added = client.post(f"{detail_url}/items/new", data={
|
|
"name": "OPS position", "unit": "pcs", "quantity_delta": "2", "unit_price": "20",
|
|
"system_id": system_data["id"], "section_id": section_data["id"],
|
|
"source_boq_document_id": ops_boq_id,
|
|
}, follow_redirects=False)
|
|
assert ops_added.status_code == 303, ops_added.text
|
|
unclassified = client.post(f"{detail_url}/items/new", data={
|
|
"name": "\u041d\u0435\u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430",
|
|
"unit": "\u0448\u0442", "quantity_delta": "1", "unit_price": "1250",
|
|
"system_id": "", "section_id": "", "source_boq_document_id": "",
|
|
}, follow_redirects=False)
|
|
assert unclassified.status_code == 303, unclassified.text
|
|
with TestingSession() as db:
|
|
agreement = db.query(Agreement).filter_by(id=agreement_id).one()
|
|
new_lines = db.query(AgreementItem).filter_by(
|
|
agreement_id=agreement_id, change_type="new_position"
|
|
).order_by(AgreementItem.id).all()
|
|
assert len(new_lines) == 3
|
|
item = next(db.get(boq.WorkItem, line.work_item_id) for line in new_lines
|
|
if db.get(boq.WorkItem, line.work_item_id).name == "\u0420\u0435\u043c\u043e\u043d\u0442 \u0443\u043d\u0438\u0442\u0430\u0437\u0430")
|
|
unclassified_item = next(db.get(boq.WorkItem, line.work_item_id) for line in new_lines
|
|
if db.get(boq.WorkItem, line.work_item_id).system_id is None)
|
|
assert unclassified_item.boq_document_id is None
|
|
unclassified_id = unclassified_item.id
|
|
ops_id = new_lines[1].work_item_id
|
|
assert new_lines[0].source_boq_document_id == scud_boq_id
|
|
assert new_lines[1].source_boq_document_id == ops_boq_id
|
|
assert new_lines[2].source_boq_document_id is None
|
|
assert all(line.work_item.boq_document_id is None for line in new_lines)
|
|
assert agreement.boq_document_id is None
|
|
assert item.project_id == project_id and item.boq_document_id is None
|
|
assert item.system_id == system_data["id"] and item.section_id == section_data["id"]
|
|
assert db.get(boq.System, system_data["id"]).project_id == project_id
|
|
assert db.get(boq.System, system_data["id"]).name == "\u0421\u0430\u043d\u0442\u0435\u0445\u043d\u0438\u043a\u0430"
|
|
assert db.get(boq.Section, section_data["id"]).system_id == system_data["id"]
|
|
assert db.get(boq.Section, section_data["id"]).name == "\u0421\u0430\u043d\u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u044b"
|
|
assert item.name == "\u0420\u0435\u043c\u043e\u043d\u0442 \u0443\u043d\u0438\u0442\u0430\u0437\u0430"
|
|
item_id = item.id
|
|
draft_ids = {row.item.id for row in build_effective_plan(db, project_id)["items"]}
|
|
assert item_id not in draft_ids and unclassified_id not in draft_ids
|
|
before = client.get(f"/projects/{project_id}/daily-reports/new?system_id={system_data['id']}")
|
|
assert before.status_code == 200 and "\u0420\u0435\u043c\u043e\u043d\u0442 \u0443\u043d\u0438\u0442\u0430\u0437\u0430" not in before.text
|
|
|
|
approved = client.post(f"{detail_url}/approve", follow_redirects=False)
|
|
assert approved.status_code == 303, approved.text
|
|
with TestingSession() as db:
|
|
effective = {row.item.id: row for row in build_effective_plan(db, project_id)["items"]}
|
|
assert effective[item_id].quantity == Decimal("1.0000")
|
|
assert effective[item_id].amount == Decimal("1250.000000000000")
|
|
assert len(effective) == 3
|
|
assert all(work_item_id in effective for work_item_id in (item_id, ops_id, unclassified_id))
|
|
assert effective[unclassified_id].quantity == Decimal("1.0000")
|
|
assert effective[unclassified_id].item.system_id is None
|
|
assert effective[unclassified_id].item.section_id is None
|
|
report_form = client.get(f"/projects/{project_id}/daily-reports/new?system_id=unclassified")
|
|
assert report_form.status_code == 200 and "\u041d\u0435\u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430" in report_form.text
|
|
saved = client.post(f"/projects/{project_id}/daily-reports", data={
|
|
"report_date": "2026-09-25", "system_id": "unclassified",
|
|
f"quantity_{unclassified_id}": "0.5",
|
|
}, follow_redirects=False)
|
|
assert saved.status_code == 303
|
|
view = client.get(saved.headers["location"])
|
|
assert view.status_code == 200 and "\u041d\u0435\u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430" in view.text
|
|
assert client.get(f"/projects/{project_id}/daily-reports").status_code == 200
|
|
dashboard = client.get(f"/projects/{project_id}")
|
|
assert dashboard.status_code == 200
|
|
with TestingSession() as db:
|
|
assert db.query(DailyReportItem).filter_by(work_item_id=unclassified_id).one().completed_quantity == Decimal("0.5000")
|
|
from app.services.project_dashboard import build_project_dashboard
|
|
dashboard_data = build_project_dashboard(db, db.get(Project, project_id))
|
|
assert dashboard_data["performed_cost"] == Decimal("625.00")
|
|
assert db.query(DailyReport).one().system_id is None
|
|
from app.utils.formatters import format_money
|
|
assert format_money(Decimal("625.00"), "RUB") in dashboard.text
|
|
finally:
|
|
for dependency in (project_router.get_db, daily_reports.get_db, agreement_router.get_db):
|
|
app.dependency_overrides.pop(dependency, None)
|
|
engine.dispose()
|