test_reprint_adoption.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """A print Bambuddy expected starts on a clean archive, or on a new one if the archive is gone."""
  2. from __future__ import annotations
  3. from types import SimpleNamespace
  4. import pytest
  5. from sqlalchemy import func, select
  6. from backend.app.main import register_expected_print
  7. from backend.app.models.archive import PrintArchive
  8. from backend.tests.integration.test_external_print_confirmation_1898 import ( # noqa: F401
  9. SUBTASK,
  10. _clear_print_state,
  11. _drive_print_start,
  12. )
  13. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  14. async def test_a_reprint_does_not_keep_the_last_runs_failure(test_engine, db_session, printer_factory):
  15. printer = await printer_factory()
  16. printer_id = printer.id
  17. archive = PrintArchive(
  18. printer_id=printer_id,
  19. filename=f"{SUBTASK}.gcode.3mf",
  20. file_path=f"archives/test/{SUBTASK}.gcode.3mf",
  21. file_size=1,
  22. print_name=SUBTASK,
  23. status="failed",
  24. failure_reason="cloggedNozzle",
  25. )
  26. db_session.add(archive)
  27. await db_session.flush()
  28. archive_id = archive.id
  29. await db_session.commit()
  30. register_expected_print(printer_id, f"{SUBTASK}.gcode.3mf", archive_id)
  31. await _drive_print_start(test_engine, SimpleNamespace(id=printer_id), download_ok=False)
  32. db_session.expire_all()
  33. row = await db_session.get(PrintArchive, archive_id)
  34. assert (row.status, row.failure_reason, row.completed_at) == ("printing", None, None)
  35. async def test_a_print_whose_archive_was_deleted_is_archived_anew(test_engine, db_session, printer_factory):
  36. printer_id = (await printer_factory()).id
  37. register_expected_print(printer_id, f"{SUBTASK}.gcode.3mf", 999_999)
  38. await _drive_print_start(test_engine, SimpleNamespace(id=printer_id), download_ok=False)
  39. db_session.expire_all()
  40. count = await db_session.scalar(
  41. select(func.count()).select_from(PrintArchive).where(PrintArchive.printer_id == printer_id)
  42. )
  43. assert count == 1
  44. async def test_a_printer_that_does_not_archive_gets_no_archive_for_it(test_engine, db_session, printer_factory):
  45. printer_id = (await printer_factory(auto_archive=False)).id
  46. register_expected_print(printer_id, f"{SUBTASK}.gcode.3mf", 999_999)
  47. await _drive_print_start(test_engine, SimpleNamespace(id=printer_id), download_ok=False)
  48. db_session.expire_all()
  49. count = await db_session.scalar(
  50. select(func.count()).select_from(PrintArchive).where(PrintArchive.printer_id == printer_id)
  51. )
  52. assert count == 0