test_github_backup_spool_fields.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """The spool collector's field whitelist for Git backup (#2870).
  2. ``_collect_spools`` builds an explicit dict per spool, so anything missing
  3. from it is silently absent from the backup and therefore lost on restore.
  4. The owner's own bookkeeping — purchasing number (#2870), category and
  5. low-stock override (#729), free-text storage — belongs in the file.
  6. """
  7. import pytest
  8. from backend.app.models.spool import Spool
  9. from backend.app.services.github_backup import GitHubBackupService
  10. @pytest.mark.asyncio
  11. async def test_collects_the_owners_own_bookkeeping_fields(db_session):
  12. db_session.add(
  13. Spool(
  14. material="PLA",
  15. material_number="15",
  16. category="Production",
  17. low_stock_threshold_pct=40,
  18. storage_location="Shelf B",
  19. )
  20. )
  21. await db_session.commit()
  22. files: dict = {}
  23. await GitHubBackupService()._collect_spools(db_session, files)
  24. entry = files["spools/inventory.json"]["spools"][0]
  25. assert entry["material_number"] == "15"
  26. assert entry["category"] == "Production"
  27. assert entry["low_stock_threshold_pct"] == 40
  28. assert entry["storage_location"] == "Shelf B"
  29. @pytest.mark.asyncio
  30. async def test_location_id_is_left_out(db_session):
  31. """The locations table is not backed up, so the ID has nothing to mean."""
  32. db_session.add(Spool(material="PLA"))
  33. await db_session.commit()
  34. files: dict = {}
  35. await GitHubBackupService()._collect_spools(db_session, files)
  36. assert "location_id" not in files["spools/inventory.json"]["spools"][0]
  37. @pytest.mark.asyncio
  38. async def test_collects_the_drying_record(db_session):
  39. """#2863 — written with str(), the form _parse_dt reads back."""
  40. from datetime import datetime
  41. db_session.add(
  42. Spool(material="PLA", last_dried_at=datetime(2026, 10, 6, 12, 30), last_dried_temp=55, last_dried_hours=7.5)
  43. )
  44. await db_session.commit()
  45. files: dict = {}
  46. await GitHubBackupService()._collect_spools(db_session, files)
  47. entry = files["spools/inventory.json"]["spools"][0]
  48. assert entry["last_dried_at"] == "2026-10-06 12:30:00"
  49. assert entry["last_dried_temp"] == 55
  50. assert entry["last_dried_hours"] == 7.5
  51. @pytest.mark.asyncio
  52. async def test_collects_the_total_consumed_reset_point(db_session):
  53. """WU-7: without it, a restore lost every "Reset Total Consumed"."""
  54. db_session.add(Spool(material="PLA", weight_used=500, weight_used_baseline=300))
  55. await db_session.commit()
  56. files: dict = {}
  57. await GitHubBackupService()._collect_spools(db_session, files)
  58. assert files["spools/inventory.json"]["spools"][0]["weight_used_baseline"] == 300
  59. @pytest.mark.asyncio
  60. async def test_collects_when_the_spool_was_last_used(db_session):
  61. """LU-4: without it, every spool read as never used after a restore."""
  62. from datetime import datetime
  63. db_session.add(Spool(material="PLA", last_used=datetime(2026, 10, 18, 9, 15)))
  64. await db_session.commit()
  65. files: dict = {}
  66. await GitHubBackupService()._collect_spools(db_session, files)
  67. assert files["spools/inventory.json"]["spools"][0]["last_used"] == "2026-10-18 09:15:00"