test_dispatched_print_named_like_calibration.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. """A print Bambuddy dispatched is adopted whatever it is called; the printer's
  2. own calibration runs are still left out of the archive."""
  3. import pytest
  4. from backend.app import main as main_module
  5. from backend.app.main import register_expected_print
  6. from backend.app.models.archive import PrintArchive
  7. from backend.tests.integration import test_external_print_confirmation_1898 as harness
  8. from backend.tests.integration.test_external_print_confirmation_1898 import ( # noqa: F401
  9. _clear_print_state,
  10. _drive_print_start,
  11. )
  12. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  13. NAME = "pa_line_calib_mode"
  14. @pytest.fixture(autouse=True)
  15. def _calibration_name(monkeypatch):
  16. monkeypatch.setattr(harness, "SUBTASK", NAME)
  17. monkeypatch.setattr(main_module.printer_manager, "get_current_print_user", lambda printer_id: None)
  18. main_module._owned_print_archive_ids.clear()
  19. yield
  20. main_module._owned_print_archive_ids.clear()
  21. async def test_a_dispatched_print_named_like_a_calibration_is_adopted(test_engine, db_session, printer_factory):
  22. printer_id = (await printer_factory()).id
  23. archive = PrintArchive(
  24. printer_id=printer_id,
  25. filename=f"{NAME}.gcode.3mf",
  26. file_path=f"archives/test/{NAME}.gcode.3mf",
  27. file_size=1,
  28. print_name=NAME,
  29. status="pending",
  30. )
  31. db_session.add(archive)
  32. await db_session.commit()
  33. archive_id = archive.id
  34. register_expected_print(printer_id, f"{NAME}.gcode.3mf", archive_id)
  35. await _drive_print_start(test_engine, type("P", (), {"id": printer_id})(), download_ok=False)
  36. db_session.expire_all()
  37. adopted = await db_session.get(PrintArchive, archive_id)
  38. assert adopted.status == "printing"
  39. assert archive_id in main_module._owned_print_archive_ids
  40. async def test_the_printers_own_calibration_is_still_skipped(test_engine, db_session, printer_factory):
  41. """Nothing dispatched: a calibration run started on the printer or from the
  42. slicer is not archived, as before."""
  43. printer_id = (await printer_factory()).id
  44. await _drive_print_start(test_engine, type("P", (), {"id": printer_id})(), download_ok=False)
  45. db_session.expire_all()
  46. from sqlalchemy import select
  47. rows = (await db_session.execute(select(PrintArchive).where(PrintArchive.printer_id == printer_id))).scalars().all()
  48. assert rows == []
  49. async def test_after_a_restart_the_printing_queue_job_says_it_is_bambuddys(test_engine, db_session, printer_factory):
  50. """The expected entry is in memory only; the queue row is what survives."""
  51. from sqlalchemy import select
  52. from backend.app.models.print_queue import PrintQueueItem
  53. printer_id = (await printer_factory()).id
  54. archive = PrintArchive(
  55. printer_id=printer_id,
  56. filename=f"{NAME}.gcode.3mf",
  57. file_path="archives/x.3mf",
  58. file_size=1,
  59. status="printing",
  60. billing_run_id="run-9",
  61. )
  62. db_session.add(archive)
  63. await db_session.flush()
  64. db_session.add(PrintQueueItem(printer_id=printer_id, status="printing", position=1, archive_id=archive.id))
  65. await db_session.commit()
  66. await _drive_print_start(
  67. test_engine, type("P", (), {"id": printer_id})(), download_ok=False, state_fields={"progress": 40}
  68. )
  69. db_session.expire_all()
  70. rows = (await db_session.execute(select(PrintArchive).where(PrintArchive.printer_id == printer_id))).scalars().all()
  71. assert [row.status for row in rows] == ["printing"]
  72. assert rows[0].id in main_module._owned_print_archive_ids
  73. async def test_a_queue_job_of_another_file_does_not_count(test_engine, db_session, printer_factory):
  74. """A row left printing by a run whose end was missed names another file."""
  75. from sqlalchemy import select
  76. from backend.app.models.print_queue import PrintQueueItem
  77. printer_id = (await printer_factory()).id
  78. other = PrintArchive(
  79. printer_id=printer_id, filename="Bracket.gcode.3mf", file_path="archives/b.3mf", file_size=1, status="printing"
  80. )
  81. db_session.add(other)
  82. await db_session.flush()
  83. db_session.add(PrintQueueItem(printer_id=printer_id, status="printing", position=1, archive_id=other.id))
  84. await db_session.commit()
  85. other_id = other.id
  86. await _drive_print_start(test_engine, type("P", (), {"id": printer_id})(), download_ok=False)
  87. db_session.expire_all()
  88. rows = (await db_session.execute(select(PrintArchive).where(PrintArchive.printer_id == printer_id))).scalars().all()
  89. assert [row.id for row in rows] == [other_id]
  90. async def test_a_system_partition_file_is_the_printers_whatever_the_queue_says(
  91. test_engine, db_session, printer_factory, monkeypatch
  92. ):
  93. """Nothing a user prints lives under /usr/: Bambuddy's own Calibrate runs it."""
  94. from sqlalchemy import select
  95. from backend.app.models.print_queue import PrintQueueItem
  96. monkeypatch.setattr(harness, "SUBTASK", "auto_cali_for_user_param")
  97. monkeypatch.setattr(harness, "DISPATCH", "/usr/etc/print/O1C2/auto_cali_for_user_param.gcode")
  98. printer_id = (await printer_factory()).id
  99. db_session.add(PrintQueueItem(printer_id=printer_id, status="printing", position=1))
  100. await db_session.commit()
  101. await _drive_print_start(test_engine, type("P", (), {"id": printer_id})(), download_ok=False)
  102. db_session.expire_all()
  103. rows = (await db_session.execute(select(PrintArchive).where(PrintArchive.printer_id == printer_id))).scalars().all()
  104. assert rows == []