chore: baseline before agreements
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.database.database import Base, DATABASE_PATH
|
||||
from app.main import app
|
||||
from app.models.work_journal import WorkJournalEntry
|
||||
from app.models.project import Project
|
||||
from app.routers import projects
|
||||
from app.services.project_dashboard import build_project_dashboard
|
||||
|
||||
|
||||
def test_project_work_journal_crud_and_dashboard_warnings(tmp_path):
|
||||
db_path = tmp_path / "work-journal-copy.db"
|
||||
shutil.copy2(DATABASE_PATH, db_path)
|
||||
engine = create_engine(f"sqlite:///{Path(db_path).as_posix()}", connect_args={"check_same_thread": False})
|
||||
Base.metadata.create_all(bind=engine)
|
||||
TestingSession = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
|
||||
def override_db():
|
||||
session = TestingSession()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
app.dependency_overrides[projects.get_db] = override_db
|
||||
try:
|
||||
with TestClient(app) as client:
|
||||
dashboard = client.get("/projects/2")
|
||||
assert dashboard.status_code == 200
|
||||
assert "Рабочий журнал" in dashboard.text
|
||||
assert "planWarningsModal" in dashboard.text
|
||||
assert "Фактическое количество" in dashboard.text
|
||||
status_position = dashboard.text.find('aria-live="polite"')
|
||||
kpi_position = dashboard.text.find('id="kpi-heading"')
|
||||
assert 0 <= status_position < kpi_position
|
||||
assert 'class="alert alert-warning py-2 mb-0"' in dashboard.text[:kpi_position]
|
||||
with TestingSession() as session:
|
||||
real_project = session.get(Project, 2)
|
||||
warnings = build_project_dashboard(session, real_project)["over_plan_items"]
|
||||
assert warnings
|
||||
assert all(row["item"].name in dashboard.text for row in warnings)
|
||||
|
||||
with TestingSession() as session:
|
||||
journal_baseline_count = session.query(WorkJournalEntry).filter_by(project_id=2).count()
|
||||
no_warning_project = Project(name="No plan warnings test")
|
||||
session.add(no_warning_project)
|
||||
session.commit()
|
||||
no_warning_project_id = no_warning_project.id
|
||||
no_warning_dashboard = client.get(f"/projects/{no_warning_project_id}")
|
||||
assert no_warning_dashboard.status_code == 200
|
||||
assert 'class="text-success small py-2"' in no_warning_dashboard.text
|
||||
assert 'data-bs-target="#planWarningsModal"' not in no_warning_dashboard.text
|
||||
|
||||
task = client.post("/projects/2/journal", data={
|
||||
"type": "task", "text": "Проверить узел АУПС", "due_date": "2026-10-01",
|
||||
})
|
||||
note = client.post("/projects/2/journal", data={
|
||||
"type": "note", "text": "Уточнить поставку оборудования", "due_date": "",
|
||||
})
|
||||
assert task.status_code == note.status_code == 200
|
||||
dashboard = client.get("/projects/2")
|
||||
assert "Проверить узел АУПС" in dashboard.text
|
||||
assert "01.10.2026" in dashboard.text
|
||||
assert "Уточнить поставку оборудования" in dashboard.text
|
||||
|
||||
with TestingSession() as session:
|
||||
task_entry = session.query(WorkJournalEntry).filter_by(
|
||||
project_id=2, type="task", text="Проверить узел АУПС"
|
||||
).one()
|
||||
note_entry = session.query(WorkJournalEntry).filter_by(
|
||||
project_id=2, type="note", text="Уточнить поставку оборудования"
|
||||
).one()
|
||||
task_id, note_id = task_entry.id, note_entry.id
|
||||
|
||||
toggled = client.post(f"/projects/2/journal/{task_id}/toggle")
|
||||
assert toggled.status_code == 200 and toggled.json()["status"] == "done"
|
||||
assert f"Проверить узел АУПС" not in client.get("/projects/2").text
|
||||
|
||||
updated = client.post(f"/projects/2/journal/{note_id}", data={
|
||||
"type": "note", "text": "Поставка подтверждена", "due_date": "",
|
||||
})
|
||||
assert updated.status_code == 200
|
||||
assert "Поставка подтверждена" in client.get("/projects/2").text
|
||||
|
||||
deleted = client.post(f"/projects/2/journal/{note_id}/delete")
|
||||
assert deleted.status_code == 200
|
||||
assert "Поставка подтверждена" not in client.get("/projects/2").text
|
||||
|
||||
deleted_task = client.post(f"/projects/2/journal/{task_id}/delete")
|
||||
assert deleted_task.status_code == 200
|
||||
with TestingSession() as session:
|
||||
assert session.query(WorkJournalEntry).filter_by(project_id=2).count() == journal_baseline_count
|
||||
finally:
|
||||
app.dependency_overrides.pop(projects.get_db, None)
|
||||
engine.dispose()
|
||||
Reference in New Issue
Block a user