526 lines
18 KiB
Python
526 lines
18 KiB
Python
"""Integration tests for DOC-B5: issue artifact lifecycle.
|
|
|
|
Covers: artifact creation on issue, draft invalidation, issue rollback,
|
|
cancellation preservation, immutability, hash verification.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine, select
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
from app.config import ENV_DOCUMENT_STORAGE_ROOT
|
|
from app.database.database import Base
|
|
from app.exporters.ks2_dto import build_ks2_export_dto
|
|
from app.exporters.ks3_dto import build_ks3_export_dto
|
|
from app.models import ( # noqa: F401 — register mappers
|
|
agreement,
|
|
boq,
|
|
daily_report,
|
|
document_artifact,
|
|
ks2 as ks2_models,
|
|
ks3 as ks3_models,
|
|
payment,
|
|
project,
|
|
work_journal,
|
|
)
|
|
from app.models.boq import BoqDocument, WorkItem
|
|
from app.models.daily_report import DailyReport, DailyReportItem
|
|
from app.models.document_artifact import DocumentArtifact
|
|
from app.models.ks2 import KS2
|
|
from app.models.ks3 import KS3, KS3KS2
|
|
from app.models.project import Project
|
|
from app.services.document_artifact_service import (
|
|
compute_content_hash,
|
|
compute_input_hash,
|
|
invalidate_artifacts,
|
|
persist_artifact,
|
|
remove_artifact_file,
|
|
resolve_artifact,
|
|
resolve_storage_path,
|
|
)
|
|
from app.services.ks2_service import KS2ValidationError, issue_ks2, save_draft as save_ks2_draft
|
|
from app.services.ks3_service import KS3ValidationError, cancel_ks3, issue_ks3, save_draft as save_ks3_draft
|
|
|
|
|
|
@pytest.fixture
|
|
def storage_root(tmp_path, monkeypatch):
|
|
root = tmp_path / "b5-storage"
|
|
monkeypatch.setenv(ENV_DOCUMENT_STORAGE_ROOT, str(root))
|
|
return root
|
|
|
|
|
|
@pytest.fixture
|
|
def db_session(storage_root):
|
|
engine = create_engine(
|
|
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
|
|
)
|
|
Base.metadata.create_all(engine)
|
|
Session = sessionmaker(bind=engine, expire_on_commit=False)
|
|
session = Session()
|
|
try:
|
|
yield session
|
|
finally:
|
|
session.close()
|
|
engine.dispose()
|
|
|
|
|
|
@pytest.fixture
|
|
def project_id(db_session):
|
|
p = Project(name="B5 lifecycle")
|
|
db_session.add(p)
|
|
db_session.commit()
|
|
return p.id
|
|
|
|
|
|
def _seed_work_item(db, project_id):
|
|
doc = BoqDocument(project_id=project_id, name="BOQ")
|
|
db.add(doc)
|
|
db.flush()
|
|
wi = WorkItem(
|
|
project_id=project_id, boq_document_id=doc.id,
|
|
name="Позиция", unit="шт",
|
|
quantity=Decimal("100"), unit_price=Decimal("10"), amount=Decimal("1000"),
|
|
)
|
|
db.add(wi)
|
|
db.commit()
|
|
return wi.id
|
|
|
|
|
|
def _seed_dr(db, project_id, work_item_id, qty=Decimal("50")):
|
|
dr = DailyReport(project_id=project_id, report_date=date(2026, 2, 1))
|
|
db.add(dr)
|
|
db.flush()
|
|
dri = DailyReportItem(daily_report_id=dr.id, work_item_id=work_item_id, completed_quantity=qty)
|
|
db.add(dri)
|
|
db.commit()
|
|
return dr.id
|
|
|
|
|
|
def _make_draft_ks2(db, project_id, work_item_id, number="K2-B5"):
|
|
return save_ks2_draft(
|
|
db, project_id, number, date(2026, 3, 1), None,
|
|
[(work_item_id, Decimal("10"))],
|
|
reporting_period_from=date(2026, 2, 1),
|
|
reporting_period_to=date(2026, 2, 28),
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Artifact creation on issue
|
|
# ---------------------------------------------------------------------------
|
|
def test_issue_ks2_creates_current_xlsx_artifact(storage_root, db_session, project_id):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft = _make_draft_ks2(db_session, project_id, wi)
|
|
ks2 = issue_ks2(db_session, project_id, draft.id)
|
|
assert ks2.status == "issued"
|
|
|
|
art = db_session.execute(
|
|
select(DocumentArtifact).where(
|
|
DocumentArtifact.project_id == project_id,
|
|
DocumentArtifact.document_type == "KS2",
|
|
DocumentArtifact.document_id == ks2.id,
|
|
DocumentArtifact.format == "xlsx",
|
|
DocumentArtifact.is_current.is_(True),
|
|
)
|
|
).scalar_one_or_none()
|
|
assert art is not None
|
|
assert art.template_type == "ks2"
|
|
assert art.template_version == 1
|
|
assert art.is_current is True or art.is_current == 1
|
|
|
|
path = resolve_storage_path(art.storage_key)
|
|
assert path.is_file()
|
|
content = path.read_bytes()
|
|
assert art.content_hash == compute_content_hash(content)
|
|
|
|
dto = build_ks2_export_dto(db_session, ks2)
|
|
assert art.input_hash == compute_input_hash(dto, "ks2", 1)
|
|
|
|
|
|
def test_issue_ks3_creates_current_docx_artifact(storage_root, db_session, project_id):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft_ks2 = _make_draft_ks2(db_session, project_id, wi)
|
|
ks2 = issue_ks2(db_session, project_id, draft_ks2.id)
|
|
|
|
draft_ks3 = save_ks3_draft(
|
|
db_session, project_id, "K3-B5", date(2026, 4, 1), None,
|
|
[ks2.id],
|
|
reporting_period_from=date(2026, 2, 1),
|
|
reporting_period_to=date(2026, 2, 28),
|
|
)
|
|
ks3 = issue_ks3(db_session, project_id, draft_ks3.id)
|
|
assert ks3.status == "issued"
|
|
|
|
art = db_session.execute(
|
|
select(DocumentArtifact).where(
|
|
DocumentArtifact.document_type == "KS3",
|
|
DocumentArtifact.document_id == ks3.id,
|
|
DocumentArtifact.format == "docx",
|
|
DocumentArtifact.is_current.is_(True),
|
|
)
|
|
).scalar_one_or_none()
|
|
assert art is not None
|
|
assert art.template_type == "ks3"
|
|
assert art.template_version == 1
|
|
assert resolve_storage_path(art.storage_key).is_file()
|
|
|
|
dto = build_ks3_export_dto(db_session, ks3)
|
|
assert art.input_hash == compute_input_hash(dto, "ks3", 1)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Draft invalidation
|
|
# ---------------------------------------------------------------------------
|
|
def test_draft_edit_invalidates_existing_artifact(storage_root, db_session, project_id):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft = _make_draft_ks2(db_session, project_id, wi)
|
|
|
|
# Manually create a draft artifact (as if export generated one)
|
|
art = persist_artifact(
|
|
db_session,
|
|
project_id=project_id,
|
|
document_type="KS2",
|
|
document_id=draft.id,
|
|
format="xlsx",
|
|
template_type="ks2",
|
|
template_version=1,
|
|
content=b"draft-bytes",
|
|
dto={"n": 1},
|
|
)
|
|
db_session.commit()
|
|
assert art.is_current in (True, 1)
|
|
art_path = resolve_storage_path(art.storage_key)
|
|
|
|
# Edit the draft → artifact becomes non-current
|
|
save_ks2_draft(
|
|
db_session, project_id, "K2-B5", date(2026, 3, 1), "changed",
|
|
[(wi, Decimal("20"))],
|
|
reporting_period_from=date(2026, 2, 1),
|
|
reporting_period_to=date(2026, 2, 28),
|
|
ks2_id=draft.id,
|
|
)
|
|
db_session.refresh(art)
|
|
assert art.is_current in (False, 0)
|
|
# Physical file remains
|
|
assert art_path.is_file()
|
|
|
|
|
|
def test_draft_edit_rollback_keeps_artifact_current(storage_root, db_session, project_id):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft = _make_draft_ks2(db_session, project_id, wi)
|
|
|
|
art = persist_artifact(
|
|
db_session,
|
|
project_id=project_id,
|
|
document_type="KS2",
|
|
document_id=draft.id,
|
|
format="xlsx",
|
|
template_type="ks2",
|
|
template_version=1,
|
|
content=b"draft-v2",
|
|
dto={"n": 2},
|
|
)
|
|
db_session.commit()
|
|
art_id = art.id
|
|
|
|
# Force a failure inside save_draft after invalidation would run:
|
|
# invalid quantity triggers rollback before commit
|
|
with pytest.raises(KS2ValidationError):
|
|
save_ks2_draft(
|
|
db_session, project_id, "K2-B5", date(2026, 3, 1), "fail",
|
|
[(wi, Decimal("-5"))], # invalid quantity
|
|
reporting_period_from=date(2026, 2, 1),
|
|
reporting_period_to=date(2026, 2, 28),
|
|
ks2_id=draft.id,
|
|
)
|
|
db_session.rollback()
|
|
art = db_session.get(DocumentArtifact, art_id)
|
|
assert art.is_current in (True, 1)
|
|
assert resolve_storage_path(art.storage_key).is_file()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Issue rollback / orphan cleanup
|
|
# ---------------------------------------------------------------------------
|
|
def test_issue_rollback_on_persist_failure(storage_root, db_session, project_id, monkeypatch):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft = _make_draft_ks2(db_session, project_id, wi)
|
|
|
|
def boom(*args, **kwargs):
|
|
raise RuntimeError("forced persist failure")
|
|
|
|
monkeypatch.setattr(
|
|
"app.services.document_artifact_service.persist_artifact", boom
|
|
)
|
|
with pytest.raises(RuntimeError, match="forced persist failure"):
|
|
issue_ks2(db_session, project_id, draft.id)
|
|
|
|
db_session.rollback()
|
|
# Not left issued
|
|
doc = db_session.get(KS2, draft.id)
|
|
assert doc.status == "draft"
|
|
# No committed artifact
|
|
arts = db_session.execute(
|
|
select(DocumentArtifact).where(DocumentArtifact.document_type == "KS2")
|
|
).all()
|
|
assert arts == []
|
|
# No orphan files
|
|
files = list(storage_root.rglob("*")) if storage_root.exists() else []
|
|
assert [f for f in files if f.is_file()] == []
|
|
|
|
|
|
def test_issue_rollback_on_commit_failure(storage_root, db_session, project_id, monkeypatch):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft = _make_draft_ks2(db_session, project_id, wi)
|
|
|
|
original_commit = db_session.commit
|
|
|
|
def failing_commit():
|
|
raise RuntimeError("forced commit failure")
|
|
|
|
monkeypatch.setattr(db_session, "commit", failing_commit)
|
|
with pytest.raises(RuntimeError, match="forced commit failure"):
|
|
issue_ks2(db_session, project_id, draft.id)
|
|
monkeypatch.setattr(db_session, "commit", original_commit)
|
|
|
|
db_session.rollback()
|
|
doc = db_session.get(KS2, draft.id)
|
|
assert doc.status == "draft"
|
|
arts = db_session.execute(
|
|
select(DocumentArtifact).where(DocumentArtifact.document_type == "KS2")
|
|
).all()
|
|
assert arts == []
|
|
files = [f for f in storage_root.rglob("*") if f.is_file()] if storage_root.exists() else []
|
|
assert files == []
|
|
|
|
|
|
def test_issue_ks3_rollback_leaves_no_orphan(storage_root, db_session, project_id, monkeypatch):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft_ks2 = _make_draft_ks2(db_session, project_id, wi)
|
|
ks2 = issue_ks2(db_session, project_id, draft_ks2.id)
|
|
draft_ks3 = save_ks3_draft(
|
|
db_session, project_id, "K3-B5", date(2026, 4, 1), None,
|
|
[ks2.id],
|
|
reporting_period_from=date(2026, 2, 1),
|
|
reporting_period_to=date(2026, 2, 28),
|
|
)
|
|
|
|
original_commit = db_session.commit
|
|
|
|
def failing_commit():
|
|
raise RuntimeError("forced commit failure")
|
|
|
|
monkeypatch.setattr(db_session, "commit", failing_commit)
|
|
with pytest.raises(RuntimeError, match="forced commit failure"):
|
|
issue_ks3(db_session, project_id, draft_ks3.id)
|
|
monkeypatch.setattr(db_session, "commit", original_commit)
|
|
|
|
db_session.rollback()
|
|
doc = db_session.get(KS3, draft_ks3.id)
|
|
assert doc.status == "draft"
|
|
arts = db_session.execute(
|
|
select(DocumentArtifact).where(DocumentArtifact.document_type == "KS3")
|
|
).all()
|
|
assert arts == []
|
|
# Only KS3 artifacts are orphans of this failed attempt; the committed
|
|
# KS-2 artifact file must remain untouched.
|
|
ks3_files = list((storage_root / "projects").rglob("*/*.docx")) if storage_root.exists() else []
|
|
assert [f for f in ks3_files if f.is_file()] == []
|
|
ks2_files = list((storage_root / "projects").rglob("*/*.xlsx")) if storage_root.exists() else []
|
|
assert len([f for f in ks2_files if f.is_file()]) == 1
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Cancellation preserves issued artifact
|
|
# ---------------------------------------------------------------------------
|
|
def test_cancel_ks2_preserves_issued_artifact(storage_root, db_session, project_id):
|
|
from app.services.ks2_service import cancel_ks2
|
|
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft = _make_draft_ks2(db_session, project_id, wi)
|
|
ks2 = issue_ks2(db_session, project_id, draft.id)
|
|
|
|
art = db_session.execute(
|
|
select(DocumentArtifact).where(
|
|
DocumentArtifact.document_type == "KS2",
|
|
DocumentArtifact.document_id == ks2.id,
|
|
DocumentArtifact.is_current.is_(True),
|
|
)
|
|
).scalar_one()
|
|
art_id = art.id
|
|
content_hash = art.content_hash
|
|
input_hash = art.input_hash
|
|
storage_key = art.storage_key
|
|
|
|
cancel_ks2(db_session, project_id, ks2.id)
|
|
assert db_session.get(KS2, ks2.id).status == "cancelled"
|
|
|
|
art = db_session.get(DocumentArtifact, art_id)
|
|
assert art is not None
|
|
assert art.is_current in (True, 1)
|
|
assert art.content_hash == content_hash
|
|
assert art.input_hash == input_hash
|
|
assert art.storage_key == storage_key
|
|
assert resolve_storage_path(storage_key).is_file()
|
|
|
|
|
|
def test_cancel_ks3_preserves_issued_artifact(storage_root, db_session, project_id):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft_ks2 = _make_draft_ks2(db_session, project_id, wi)
|
|
ks2 = issue_ks2(db_session, project_id, draft_ks2.id)
|
|
draft_ks3 = save_ks3_draft(
|
|
db_session, project_id, "K3-B5", date(2026, 4, 1), None,
|
|
[ks2.id],
|
|
reporting_period_from=date(2026, 2, 1),
|
|
reporting_period_to=date(2026, 2, 28),
|
|
)
|
|
ks3 = issue_ks3(db_session, project_id, draft_ks3.id)
|
|
|
|
art = db_session.execute(
|
|
select(DocumentArtifact).where(
|
|
DocumentArtifact.document_type == "KS3",
|
|
DocumentArtifact.document_id == ks3.id,
|
|
DocumentArtifact.is_current.is_(True),
|
|
)
|
|
).scalar_one()
|
|
art_id = art.id
|
|
content_hash = art.content_hash
|
|
input_hash = art.input_hash
|
|
|
|
cancel_ks3(db_session, project_id, ks3.id)
|
|
assert db_session.get(KS3, ks3.id).status == "cancelled"
|
|
|
|
art = db_session.get(DocumentArtifact, art_id)
|
|
assert art.is_current in (True, 1)
|
|
assert art.content_hash == content_hash
|
|
assert art.input_hash == input_hash
|
|
assert resolve_storage_path(art.storage_key).is_file()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Immutability / repeated issue
|
|
# ---------------------------------------------------------------------------
|
|
def test_issued_document_cannot_be_issued_again(storage_root, db_session, project_id):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft = _make_draft_ks2(db_session, project_id, wi)
|
|
ks2 = issue_ks2(db_session, project_id, draft.id)
|
|
|
|
art_count = db_session.execute(
|
|
select(DocumentArtifact).where(DocumentArtifact.document_type == "KS2")
|
|
).all()
|
|
assert len(art_count) == 1
|
|
|
|
with pytest.raises(KS2ValidationError, match="черновик"):
|
|
issue_ks2(db_session, project_id, ks2.id)
|
|
|
|
# No second artifact created
|
|
arts = db_session.execute(
|
|
select(DocumentArtifact).where(DocumentArtifact.document_type == "KS2")
|
|
).all()
|
|
assert len(arts) == 1
|
|
|
|
|
|
def test_issued_document_cannot_be_edited(storage_root, db_session, project_id):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft = _make_draft_ks2(db_session, project_id, wi)
|
|
ks2 = issue_ks2(db_session, project_id, draft.id)
|
|
|
|
with pytest.raises(KS2ValidationError, match="редактировать"):
|
|
save_ks2_draft(
|
|
db_session, project_id, "K2-B5", date(2026, 3, 1), "edit",
|
|
[(wi, Decimal("5"))],
|
|
reporting_period_from=date(2026, 2, 1),
|
|
reporting_period_to=date(2026, 2, 28),
|
|
ks2_id=ks2.id,
|
|
)
|
|
|
|
|
|
def test_ks3_issue_does_not_touch_ks2_artifacts(storage_root, db_session, project_id):
|
|
wi = _seed_work_item(db_session, project_id)
|
|
_seed_dr(db_session, project_id, wi)
|
|
draft_ks2 = _make_draft_ks2(db_session, project_id, wi)
|
|
ks2 = issue_ks2(db_session, project_id, draft_ks2.id)
|
|
|
|
ks2_art = db_session.execute(
|
|
select(DocumentArtifact).where(
|
|
DocumentArtifact.document_type == "KS2",
|
|
DocumentArtifact.is_current.is_(True),
|
|
)
|
|
).scalar_one()
|
|
ks2_art_id = ks2_art.id
|
|
ks2_hash = ks2_art.content_hash
|
|
|
|
draft_ks3 = save_ks3_draft(
|
|
db_session, project_id, "K3-B5", date(2026, 4, 1), None,
|
|
[ks2.id],
|
|
reporting_period_from=date(2026, 2, 1),
|
|
reporting_period_to=date(2026, 2, 28),
|
|
)
|
|
issue_ks3(db_session, project_id, draft_ks3.id)
|
|
|
|
ks2_art = db_session.get(DocumentArtifact, ks2_art_id)
|
|
assert ks2_art.is_current in (True, 1)
|
|
assert ks2_art.content_hash == ks2_hash
|
|
# Only one KS-2 artifact
|
|
assert db_session.execute(
|
|
select(DocumentArtifact).where(DocumentArtifact.document_type == "KS2")
|
|
).all() and len(db_session.execute(
|
|
select(DocumentArtifact).where(DocumentArtifact.document_type == "KS2")
|
|
).all()) == 1
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# remove_artifact_file helper
|
|
# ---------------------------------------------------------------------------
|
|
def test_remove_artifact_file_only_touches_given_artifact(storage_root, db_session, project_id):
|
|
a = persist_artifact(
|
|
db_session,
|
|
project_id=project_id,
|
|
document_type="KS2",
|
|
document_id=1,
|
|
format="xlsx",
|
|
template_type="ks2",
|
|
template_version=1,
|
|
content=b"file-a",
|
|
dto={},
|
|
)
|
|
b = persist_artifact(
|
|
db_session,
|
|
project_id=project_id,
|
|
document_type="KS2",
|
|
document_id=2,
|
|
format="xlsx",
|
|
template_type="ks2",
|
|
template_version=1,
|
|
content=b"file-b",
|
|
dto={},
|
|
)
|
|
db_session.commit()
|
|
a_path = resolve_storage_path(a.storage_key)
|
|
b_path = resolve_storage_path(b.storage_key)
|
|
assert a_path.is_file() and b_path.is_file()
|
|
|
|
assert remove_artifact_file(a) is True
|
|
assert not a_path.exists()
|
|
assert b_path.is_file()
|
|
|
|
# Safe on missing file / None
|
|
assert remove_artifact_file(a) is False
|
|
assert remove_artifact_file(None) is False
|