106 lines
3.3 KiB
Python
106 lines
3.3 KiB
Python
from datetime import date, datetime
|
|
|
|
from sqlalchemy import BigInteger, create_engine
|
|
from sqlalchemy.ext.compiler import compiles
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.database import Base
|
|
from app.models import (
|
|
AttendancePoint,
|
|
Personnel,
|
|
Product,
|
|
ProductionReport,
|
|
ProductionReportItem,
|
|
ReportStatus,
|
|
SessionStatus,
|
|
WorkSession,
|
|
)
|
|
from app.routers.dashboard import _dashboard_rows
|
|
|
|
|
|
@compiles(BigInteger, "sqlite")
|
|
def _compile_big_integer_for_sqlite(type_, compiler, **kw):
|
|
return "INTEGER"
|
|
|
|
|
|
def _db_session():
|
|
engine = create_engine("sqlite+pysqlite:///:memory:", future=True)
|
|
Base.metadata.create_all(engine)
|
|
SessionLocal = sessionmaker(bind=engine, future=True)
|
|
return SessionLocal()
|
|
|
|
|
|
def test_dashboard_rows_populate_source_report_dates_from_original_reports():
|
|
db = _db_session()
|
|
try:
|
|
point_name = "总厂"
|
|
report_date = date(2026, 7, 25)
|
|
person = Personnel(phone="13800000000", name="张三")
|
|
product = Product(
|
|
attendance_point_name=point_name,
|
|
project_no="P1",
|
|
product_name="23#",
|
|
device_no="",
|
|
process_name="冲压",
|
|
operator_count=1,
|
|
process_unit_price_yuan=2,
|
|
standard_beat=1,
|
|
standard_workload=0,
|
|
)
|
|
db.add_all([
|
|
AttendancePoint(name=point_name),
|
|
person,
|
|
product,
|
|
])
|
|
for index, start_hour in enumerate([8, 10], start=1):
|
|
session = WorkSession(
|
|
id=index,
|
|
attendance_point_name=point_name,
|
|
employee_phone=person.phone,
|
|
start_at=datetime(2026, 7, 25, start_hour, 0),
|
|
end_at=datetime(2026, 7, 25, start_hour + 1, 0),
|
|
status=SessionStatus.submitted,
|
|
)
|
|
report = ProductionReport(
|
|
id=index,
|
|
session_id=session.id,
|
|
attendance_point_name=point_name,
|
|
employee_phone=person.phone,
|
|
report_date=report_date,
|
|
start_at=session.start_at,
|
|
end_at=session.end_at,
|
|
duration_minutes=60,
|
|
break_minutes=0,
|
|
effective_minutes=60,
|
|
status=ReportStatus.approved,
|
|
submitted_at=session.end_at,
|
|
)
|
|
item = ProductionReportItem(
|
|
id=index,
|
|
report_id=report.id,
|
|
attendance_point_name=point_name,
|
|
device_no="23#",
|
|
project_no=product.project_no,
|
|
product_name=product.product_name,
|
|
process_name=product.process_name,
|
|
operator_count=1,
|
|
process_unit_price_yuan=2,
|
|
standard_beat=1,
|
|
standard_workload=0,
|
|
good_qty=10,
|
|
defect_qty=0,
|
|
scrap_qty=0,
|
|
allocated_minutes=60,
|
|
)
|
|
db.add_all([session, report, item])
|
|
db.commit()
|
|
|
|
rows = _dashboard_rows(db, report_date, report_date)
|
|
|
|
assert len(rows) == 1
|
|
assert rows[0].report_count == 2
|
|
assert rows[0].source_report_date == report_date
|
|
assert rows[0].source_report_dates == [report_date]
|
|
finally:
|
|
db.close()
|