test_print_log.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """Unit tests for print log service and schema."""
  2. from datetime import datetime, timedelta
  3. from unittest.mock import AsyncMock, MagicMock
  4. import pytest
  5. from backend.app.schemas.print_log import PrintLogEntrySchema, PrintLogResponse
  6. from backend.app.services.print_log import write_log_entry
  7. class TestPrintLogEntrySchema:
  8. """Test PrintLogEntrySchema validation."""
  9. def test_minimal_entry(self):
  10. """Schema accepts minimal required fields."""
  11. entry = PrintLogEntrySchema(
  12. id=1,
  13. status="completed",
  14. created_at=datetime(2024, 1, 15, 10, 30, 0),
  15. )
  16. assert entry.id == 1
  17. assert entry.status == "completed"
  18. assert entry.print_name is None
  19. assert entry.printer_name is None
  20. assert entry.duration_seconds is None
  21. def test_full_entry(self):
  22. """Schema accepts all fields."""
  23. started = datetime(2024, 1, 15, 10, 0, 0)
  24. completed = datetime(2024, 1, 15, 12, 30, 0)
  25. entry = PrintLogEntrySchema(
  26. id=42,
  27. print_name="Benchy",
  28. printer_name="X1C-01",
  29. printer_id=3,
  30. status="completed",
  31. started_at=started,
  32. completed_at=completed,
  33. duration_seconds=9000,
  34. filament_type="PLA",
  35. filament_color="#FF5500",
  36. filament_used_grams=15.2,
  37. thumbnail_path="archives/3/20240115_benchy/thumbnail.png",
  38. created_by_username="admin",
  39. created_at=datetime(2024, 1, 15, 12, 30, 0),
  40. )
  41. assert entry.print_name == "Benchy"
  42. assert entry.printer_name == "X1C-01"
  43. assert entry.filament_used_grams == 15.2
  44. assert entry.created_by_username == "admin"
  45. def test_failed_status(self):
  46. """Schema accepts various status values."""
  47. for status in ("completed", "failed", "stopped", "cancelled", "skipped"):
  48. entry = PrintLogEntrySchema(id=1, status=status, created_at=datetime.now())
  49. assert entry.status == status
  50. class TestPrintLogResponse:
  51. """Test PrintLogResponse pagination wrapper."""
  52. def test_empty_response(self):
  53. """Empty response with zero total."""
  54. resp = PrintLogResponse(items=[], total=0)
  55. assert len(resp.items) == 0
  56. assert resp.total == 0
  57. def test_paginated_response(self):
  58. """Response with items and total count > items count."""
  59. items = [PrintLogEntrySchema(id=i, status="completed", created_at=datetime.now()) for i in range(3)]
  60. resp = PrintLogResponse(items=items, total=100)
  61. assert len(resp.items) == 3
  62. assert resp.total == 100
  63. class TestWriteLogEntry:
  64. """Test the write_log_entry service function (logic only, no DB)."""
  65. def test_duration_calculation(self):
  66. """Duration is computed from started_at and completed_at."""
  67. started = datetime(2024, 1, 15, 10, 0, 0)
  68. completed = started + timedelta(hours=2, minutes=30)
  69. # Simulating the duration calculation from write_log_entry
  70. duration = int((completed - started).total_seconds())
  71. assert duration == 9000 # 2.5 hours = 9000 seconds
  72. def test_duration_none_when_missing_times(self):
  73. """Duration is None when started_at or completed_at is missing."""
  74. started = datetime(2024, 1, 15, 10, 0, 0)
  75. completed_at = None
  76. started_at = None
  77. completed = datetime.now()
  78. # No completed_at
  79. duration = None
  80. if started and completed_at:
  81. duration = int((completed_at - started).total_seconds())
  82. assert duration is None
  83. # No started_at
  84. duration = None
  85. if started_at and completed:
  86. duration = int((completed - started_at).total_seconds())
  87. assert duration is None
  88. class TestWriteLogEntryReconciledDuration:
  89. """write_log_entry duration handling for reconciled (synthetic) completions (#2592).
  90. A reconciled abort closes out a stale ``status="printing"`` archive at
  91. reconnect; its real end time is unknown, so ``completed_at - started_at``
  92. would bank the whole disconnect gap as print time. Those entries must log
  93. 0, while genuine prints (including >24h ones) keep their real duration.
  94. """
  95. @staticmethod
  96. async def _write(**kwargs):
  97. db = MagicMock()
  98. db.flush = AsyncMock()
  99. return await write_log_entry(db, **kwargs)
  100. @pytest.mark.asyncio
  101. async def test_reconciled_logs_zero_despite_multiday_gap(self):
  102. started = datetime(2026, 7, 15, 10, 0, 0)
  103. completed = started + timedelta(days=2, hours=4) # the reconnect moment, not the real end
  104. entry = await self._write(status="aborted", started_at=started, completed_at=completed, reconciled=True)
  105. assert entry.duration_seconds == 0
  106. @pytest.mark.asyncio
  107. async def test_reconciled_logs_zero_even_without_timestamps(self):
  108. entry = await self._write(status="aborted", reconciled=True)
  109. assert entry.duration_seconds == 0
  110. @pytest.mark.asyncio
  111. async def test_genuine_long_print_retains_full_duration(self):
  112. """A legitimate >24h print keeps its real duration — no cap, no zeroing."""
  113. started = datetime(2026, 7, 15, 10, 0, 0)
  114. completed = started + timedelta(hours=30)
  115. entry = await self._write(status="completed", started_at=started, completed_at=completed)
  116. assert entry.duration_seconds == 30 * 3600
  117. @pytest.mark.asyncio
  118. async def test_non_reconciled_missing_times_is_none(self):
  119. entry = await self._write(status="completed", started_at=datetime(2026, 7, 15, 10, 0, 0))
  120. assert entry.duration_seconds is None